<commit_before>﻿using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using SFA.DAS.EmployerFinance.Data;
using SFA.DAS.EmployerFinance.Events.ProcessDeclaration;
using SFA.DAS.EmployerFinance.Messages.Events;
using SFA.DAS.NLog.Logger;
using SFA.DAS.NServiceBus;

namespace SFA.DAS.EmployerFinance.UnitTests.Events.ProcessDeclarationTests
{
    [ExcludeFromCodeCoverage]
    public class WhenIProcessDeclarations
    {
        private ProcessDeclarationsEventHandler _processDeclarationsEvent;
        private Mock<IDasLevyRepository> _dasLevyRepository;
        private Mock<ILog> _logger;
        private Mock<IEventPublisher> _eventPublisher;

        [SetUp]
        public void Arrange()
        {
            _eventPublisher = new Mock<IEventPublisher>();
            _dasLevyRepository = new Mock<IDasLevyRepository>();
            _logger = new Mock<ILog>();

            _processDeclarationsEvent = new ProcessDeclarationsEventHandler(
                _dasLevyRepository.Object,
                _logger.Object,
                _eventPublisher.Object);
        }

        [Test]
        public async Task ThenTheProcessDeclarationsRepositoryCallIsMade()
        {
            //Arrange
            const long accountId = 2L;
            const string empRef = "123/ABC456";

            //Act
            await _processDeclarationsEvent.Handle(new ProcessDeclarationsEvent
            {
                AccountId = accountId,
                EmpRef = empRef
            });

            //Assert
            _dasLevyRepository.Verify(x => x.ProcessDeclarations(accountId, empRef), Times.Once);
        }

        [Test]
        public async Task ThenTheLoggerIsCalledWithInfoLevel()
        {
            //Act
            await _processDeclarationsEvent.Handle(new ProcessDeclarationsEvent());

            //Assert
            _logger.Verify(x=>x.Info("Process Declarations Called"));
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsPublishedForGreaterThanZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = 10.0m;

            _dasLevyRepository
                .Setup(
                    m => m.ProcessDeclarations(
                        accountId,
                        It.IsAny<string>()))
                .Returns(
                    Task.FromResult(levyAmount));

            _processDeclarationsEvent
                .Handle(
                    new ProcessDeclarationsEvent
                    {
                        AccountId = accountId
                    });

            _eventPublisher
                .Verify(
                    m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Once);
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsNotPublishedForZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = decimal.Zero;

            _dasLevyRepository
                .Setup(
                    m => m.ProcessDeclarations(
                        accountId,
                        It.IsAny<string>()))
                .Returns(
                    Task.FromResult(levyAmount));

            _processDeclarationsEvent
                .Handle(
                    new ProcessDeclarationsEvent
                    {
                        AccountId = accountId
                    });

            _eventPublisher
                .Verify(
                    m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Never);
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsNotPublishedForLessThanZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = decimal.MinusOne;

            _dasLevyRepository
                .Setup(
                    m => m.ProcessDeclarations(
                        accountId,
                        It.IsAny<string>()))
                .Returns(
                    Task.FromResult(levyAmount));

            _processDeclarationsEvent
                .Handle(
                    new ProcessDeclarationsEvent
                    {
                        AccountId = accountId
                    });

            _eventPublisher
                .Verify(
                    m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Never);
        }
    }
}<commit_msg>AML-3726: re-factor to be more DRY.<commit_after>﻿using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using SFA.DAS.EmployerFinance.Data;
using SFA.DAS.EmployerFinance.Events.ProcessDeclaration;
using SFA.DAS.EmployerFinance.Messages.Events;
using SFA.DAS.NLog.Logger;
using SFA.DAS.NServiceBus;

namespace SFA.DAS.EmployerFinance.UnitTests.Events.ProcessDeclarationTests
{
    [ExcludeFromCodeCoverage]
    public class WhenIProcessDeclarations
    {
        private ProcessDeclarationsEventHandler _processDeclarationsEvent;
        private Mock<IDasLevyRepository> _dasLevyRepository;
        private Mock<ILog> _logger;
        private Mock<IEventPublisher> _eventPublisher;

        [SetUp]
        public void Arrange()
        {
            _eventPublisher = new Mock<IEventPublisher>();
            _dasLevyRepository = new Mock<IDasLevyRepository>();
            _logger = new Mock<ILog>();

            _processDeclarationsEvent = new ProcessDeclarationsEventHandler(
                _dasLevyRepository.Object,
                _logger.Object,
                _eventPublisher.Object);
        }

        [Test]
        public async Task ThenTheProcessDeclarationsRepositoryCallIsMade()
        {
            //Arrange
            const long accountId = 2L;
            const string empRef = "123/ABC456";

            //Act
            await _processDeclarationsEvent.Handle(new ProcessDeclarationsEvent
            {
                AccountId = accountId,
                EmpRef = empRef
            });

            //Assert
            _dasLevyRepository.Verify(x => x.ProcessDeclarations(accountId, empRef), Times.Once);
        }

        [Test]
        public async Task ThenTheLoggerIsCalledWithInfoLevel()
        {
            //Act
            await _processDeclarationsEvent.Handle(new ProcessDeclarationsEvent());

            //Assert
            _logger.Verify(x=>x.Info("Process Declarations Called"));
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsPublishedForGreaterThanZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = 10.0m;

            await
                SetupRepositoryAndHandleMessage(
                    accountId,
                    levyAmount);

                _eventPublisher
                .Verify(
                    m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Once);
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsNotPublishedForZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = decimal.Zero;

            await
            SetupRepositoryAndHandleMessage(
                accountId,
                levyAmount);

            _eventPublisher
                .Verify(
                    m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Never);
        }

        [Test]
        public async Task ThenLevyAddedToAccountEventIsNotPublishedForLessThanZeroLevyAmount()
        {
            long accountId = 900034;
            decimal levyAmount = decimal.MinusOne;

            await
                SetupRepositoryAndHandleMessage(
                    accountId,
                    levyAmount);

            _eventPublisher
                .Verify(
                       m =>
                        m.Publish(
                            It.Is<LevyAddedToAccount>(
                                arg => arg.AccountId == accountId
                                       && arg.Amount == levyAmount)),
                    Times.Never);
        }

        private async Task SetupRepositoryAndHandleMessage(long accountId, decimal levyAmount)
        {
            _dasLevyRepository
                .Setup(
                    m => m.ProcessDeclarations(
                        accountId,
                        It.IsAny<string>()))
                .Returns(
                    Task.FromResult(levyAmount));

            await
            _processDeclarationsEvent
                .Handle(
                    new ProcessDeclarationsEvent
                    {
                        AccountId = accountId
                    });
        }
    }
}
{'fraction_non_alphanumeric': 0.0688976377952756, 'fraction_numerical': 0.006216328222130129, 'mean_word_length': 1.2622451370986643, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410258', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2254, 'n_words': 403}