<commit_before>﻿using System.IO;
using System.Threading.Tasks;
using MediatR;
using Moq;
using NLog;
using NUnit.Framework;
using SFA.DAS.EmployerApprenticeshipsService.Application.Messages;
using SFA.DAS.EmployerApprenticeshipsService.Application.Queries.GetEmployerAccount;
using SFA.DAS.EmployerApprenticeshipsService.Application.Queries.GetLevyDeclaration;
using SFA.DAS.LevyDeclarationProvider.Worker.Providers;
using SFA.DAS.Messaging;
using SFA.DAS.Messaging.FileSystem;

namespace SFA.DAS.LevyDeclarationProvider.Worker.UnitTests.Providers.LevyDeclarationTests
{
    public class WhenHandlingTheTask
    {
        private const int ExpecetedEmpref = 123456;
        private LevyDeclaration _levyDeclaration;
        private Mock<IPollingMessageReceiver> _pollingMessageReceiver;
        private Mock<IMediator> _mediator;
        private Mock<IMessagePublisher> _messagePublisher;
        private Mock<ILogger> _logger;

        [SetUp]
        public void Arrange()
        {
            var stubDataFile = new FileInfo(@"C:\SomeFile.txt");

            _pollingMessageReceiver = new Mock<IPollingMessageReceiver>();
            _pollingMessageReceiver.Setup(x => x.ReceiveAsAsync<EmployerRefreshLevyQueueMessage>()).
                ReturnsAsync(new FileSystemMessage<EmployerRefreshLevyQueueMessage>(stubDataFile, stubDataFile, 
                new EmployerRefreshLevyQueueMessage { AccountId = ExpecetedEmpref }));
            _messagePublisher = new Mock<IMessagePublisher>();

            _mediator = new Mock<IMediator>();
            _mediator.Setup(x => x.SendAsync(new GetEmployerAccountQuery { AccountId = ExpecetedEmpref})).ReturnsAsync(new GetEmployerAccountResponse());

            _logger = new Mock<ILogger>();

            _levyDeclaration = new LevyDeclaration(_pollingMessageReceiver.Object, _mediator.Object, _logger.Object);
        }

        [Test]
        public async Task ThenTheMessageIsReceivedFromTheQueue()
        {
            //Act
            await _levyDeclaration.Handle();

            //Assert
            _pollingMessageReceiver.Verify(x=>x.ReceiveAsAsync<EmployerRefreshLevyQueueMessage>(),Times.Once);
        }

        [Test]
        public async Task ThenTheDeclarationDataIsReceivedForTheQueueMessageId()
        {
            //Act   
            await _levyDeclaration.Handle();

            //Assert
            _mediator.Verify(x=>x.SendAsync(It.Is<GetEmployerAccountQuery>(c=>c.AccountId.Equals(ExpecetedEmpref))), Times.Once());

        }

        [Test]
        public async Task ThenTheRebuildDeclarationCommandIsCalledIfThereIsData()
        {
            //Act
            await _levyDeclaration.Handle();

            //Assert

        }

        [Test]
        public async Task ThenIfATooManyRequestsExceptionIsThrownItIsHandled()
        {
            //Act<commit_msg>Fixed broken test, the wrong query was being used as a result of some prior refactoring.<commit_after>﻿using System.IO;
using System.Threading.Tasks;
using MediatR;
using Moq;
using NLog;
using NUnit.Framework;
using SFA.DAS.EmployerApprenticeshipsService.Application.Messages;
using SFA.DAS.EmployerApprenticeshipsService.Application.Queries.GetEmployerAccount;
using SFA.DAS.EmployerApprenticeshipsService.Application.Queries.GetLevyDeclaration;
using SFA.DAS.LevyDeclarationProvider.Worker.Providers;
using SFA.DAS.LevyDeclarationProvider.Worker.Queries.GetAccount;
using SFA.DAS.Messaging;
using SFA.DAS.Messaging.FileSystem;

namespace SFA.DAS.LevyDeclarationProvider.Worker.UnitTests.Providers.LevyDeclarationTests
{
    public class WhenHandlingTheTask
    {
        private const int ExpecetedEmpref = 123456;
        private LevyDeclaration _levyDeclaration;
        private Mock<IPollingMessageReceiver> _pollingMessageReceiver;
        private Mock<IMediator> _mediator;
        private Mock<IMessagePublisher> _messagePublisher;
        private Mock<ILogger> _logger;

        [SetUp]
        public void Arrange()
        {
            var stubDataFile = new FileInfo(@"C:\SomeFile.txt");

            _pollingMessageReceiver = new Mock<IPollingMessageReceiver>();
            _pollingMessageReceiver.Setup(x => x.ReceiveAsAsync<EmployerRefreshLevyQueueMessage>()).
                ReturnsAsync(new FileSystemMessage<EmployerRefreshLevyQueueMessage>(stubDataFile, stubDataFile, 
                new EmployerRefreshLevyQueueMessage { AccountId = ExpecetedEmpref }));
            _messagePublisher = new Mock<IMessagePublisher>();

            _mediator = new Mock<IMediator>();
            _mediator.Setup(x => x.SendAsync(new GetEmployerAccountQuery { AccountId = ExpecetedEmpref})).ReturnsAsync(new GetEmployerAccountResponse());

            _logger = new Mock<ILogger>();

            _levyDeclaration = new LevyDeclaration(_pollingMessageReceiver.Object, _mediator.Object, _logger.Object);
        }

        [Test]
        public async Task ThenTheMessageIsReceivedFromTheQueue()
        {
            //Act
            await _levyDeclaration.Handle();

            //Assert
            _pollingMessageReceiver.Verify(x=>x.ReceiveAsAsync<EmployerRefreshLevyQueueMessage>(),Times.Once);
        }

        [Test]
        public async Task ThenTheDeclarationDataIsReceivedForTheQueueMessageId()
        {
            //Act   
            await _levyDeclaration.Handle();

            //Assert
            _mediator.Verify(x=>x.SendAsync(It.Is<GetAccountRequest>(c=>c.AccountId.Equals(ExpecetedEmpref))), Times.Once());

        }

        [Test]
        public async Task ThenTheRebuildDeclarationCommandIsCalledIfThereIsData()
        {
            //Act
            await _levyDeclaration.Handle();

            //Assert

        }

        [Test]
        public async Task ThenIfATooManyRequestsExceptionIsThrownItIsHandled()
        {
            //Act
{'fraction_non_alphanumeric': 0.08433734939759036, 'fraction_numerical': 0.002065404475043029, 'mean_word_length': 2.98013698630137, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989032', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1579, 'n_words': 266}