public async void Then_Do_Not_Update_Email_History_If_Authorization_Failed(
            MatchingConfiguration matchingConfiguration,
            EmailDeliveryStatusPayLoad payLoad,
            EmailHistory emailHistory,
            ExecutionContext context,
            IRepository <EmailHistory> emailHistoryRepository,
            IMessageQueueService messageQueueService,
            IEmailService emailService,
            IOpportunityRepository opportunityRepository,
            IRepository <FunctionLog> functionLogRepository,
            ILogger <EmailDeliveryStatusService> logger
            )
        {
            //Arrange
            matchingConfiguration.EmailDeliveryStatusToken = Guid.NewGuid();
            var serializedPayLoad   = JsonConvert.SerializeObject(payLoad);
            var notificationService = new EmailDeliveryStatusService(matchingConfiguration, emailService,
                                                                     opportunityRepository, messageQueueService, logger);

            emailService.UpdateEmailStatus(Arg.Any <EmailDeliveryStatusPayLoad>()).Returns(-1);

            var query = new Dictionary <string, StringValues>();

            query.TryAdd("content-type", "application/json");

            emailHistoryRepository.GetFirstOrDefaultAsync(Arg.Any <Expression <Func <EmailHistory, bool> > >())
            .Returns(emailHistory);

            //Act
            var emailDeliveryStatusFunctions = new Functions.EmailDeliveryStatus(matchingConfiguration, notificationService, functionLogRepository);
            var result = await emailDeliveryStatusFunctions.EmailDeliveryStatusHandlerAsync(
                HttpRequestSetup(query, serializedPayLoad),
                context, logger) as BadRequestObjectResult;

            //Assert
            result.Should().NotBeNull();
            result?.StatusCode.Should().Be(400);
            result?.Should().BeOfType <BadRequestObjectResult>();

            await emailHistoryRepository.DidNotReceive().UpdateWithSpecifiedColumnsOnlyAsync(Arg.Any <EmailHistory>(),
                                                                                             Arg.Any <Expression <Func <EmailHistory, object> >[]>());

            await emailHistoryRepository.DidNotReceive().UpdateWithSpecifiedColumnsOnlyAsync(
                Arg.Is <EmailHistory>(eh =>
                                      eh.Status == "delivered" && eh.ModifiedBy == "System"),
                Arg.Any <Expression <Func <EmailHistory, object> >[]>());

            await messageQueueService.DidNotReceive().PushEmailDeliveryStatusMessageAsync(Arg.Any <SendEmailDeliveryStatus>());
        }
        public async void Then_Do_Not_Update_Email_History_If_No_Record_Found(
            MatchingConfiguration matchingConfiguration,
            EmailDeliveryStatusPayLoad payLoad,
            ExecutionContext context,
            IRepository <EmailHistory> emailHistoryRepository,
            IMessageQueueService messageQueueService,
            IEmailService emailService,
            IOpportunityRepository opportunityRepository,
            IRepository <FunctionLog> functionLogRepository,
            ILogger <EmailDeliveryStatusService> logger
            )
        {
            //Arrange
            var serializedPayLoad   = JsonConvert.SerializeObject(payLoad);
            var notificationService = new EmailDeliveryStatusService(matchingConfiguration, emailService,
                                                                     opportunityRepository, messageQueueService, logger);

            emailService.UpdateEmailStatus(Arg.Any <EmailDeliveryStatusPayLoad>()).Returns(-1);

            var query = new Dictionary <string, StringValues>();

            query.TryAdd("content-type", "application/json");

            emailHistoryRepository.GetFirstOrDefaultAsync(Arg.Any <Expression <Func <EmailHistory, bool> > >())
            .Returns(Task.FromResult <EmailHistory>(null));

            //Act
            var emailDeliveryStatusFunctions = new Functions.EmailDeliveryStatus(matchingConfiguration, notificationService, functionLogRepository);
            var result = await emailDeliveryStatusFunctions.EmailDeliveryStatusHandlerAsync(
                HttpRequestSetup(query, serializedPayLoad),
                context, logger) as OkObjectResult;

            //Arrange
            result.Should().NotBeNull();
            result?.StatusCode.Should().Be(200);
            result?.Value.Should().Be("-1 records updated.");

            await emailHistoryRepository.DidNotReceive().UpdateWithSpecifiedColumnsOnlyAsync(Arg.Any <EmailHistory>(),
                                                                                             Arg.Any <Expression <Func <EmailHistory, object> >[]>());

            await messageQueueService.DidNotReceive().PushEmailDeliveryStatusMessageAsync(Arg.Any <SendEmailDeliveryStatus>());
        }
        public async void Then_Update_Email_History_With_Failed_Status_And_Push_To_Email_Delivery_Status_Queue(
            string status,
            MatchingConfiguration matchingConfiguration,
            EmailDeliveryStatusPayLoad payLoad,
            EmailHistory emailHistory,
            ExecutionContext context,
            IRepository <EmailHistory> emailHistoryRepository,
            IMessageQueueService messageQueueService,
            IEmailService emailService,
            IOpportunityRepository opportunityRepository,
            IRepository <FunctionLog> functionLogRepository,
            ILogger <EmailDeliveryStatusService> logger
            )
        {
            //Arrange
            payLoad.Status = status;
            var serializedPayLoad   = JsonConvert.SerializeObject(payLoad);
            var notificationService = new EmailDeliveryStatusService(matchingConfiguration, emailService,
                                                                     opportunityRepository, messageQueueService, logger);

            emailService.UpdateEmailStatus(Arg.Any <EmailDeliveryStatusPayLoad>()).Returns(1);

            var query = new Dictionary <string, StringValues>();

            query.TryAdd("content-type", "application/json");

            emailHistoryRepository.GetFirstOrDefaultAsync(Arg.Any <Expression <Func <EmailHistory, bool> > >())
            .Returns(emailHistory);

            //Act
            var emailDeliveryStatusFunctions = new Functions.EmailDeliveryStatus(matchingConfiguration, notificationService, functionLogRepository);
            var result = await emailDeliveryStatusFunctions.EmailDeliveryStatusHandlerAsync(
                HttpRequestSetup(query, serializedPayLoad),
                context, logger) as OkObjectResult;

            //Assert
            result.Should().NotBeNull();
            result?.StatusCode.Should().Be(200);
            result?.Value.Should().Be("1 records updated.");

            await messageQueueService.Received(1).PushEmailDeliveryStatusMessageAsync(Arg.Is <SendEmailDeliveryStatus>(email => email.NotificationId == payLoad.Id));
        }
        public async void Then_Update_Email_History_With_Status_And_Do_Not_Push_To_Email_Delivery_Status_Queue(
            MatchingConfiguration matchingConfiguration,
            EmailDeliveryStatusPayLoad payLoad,
            ExecutionContext context,
            IMessageQueueService messageQueueService,
            IRepository <FunctionLog> functionLogRepository,
            IEmailService emailService,
            IOpportunityRepository opportunityRepository,
            ILogger <EmailDeliveryStatusService> logger
            )
        {
            //Arrange
            var serializedPayLoad   = JsonConvert.SerializeObject(payLoad);
            var notificationService = new EmailDeliveryStatusService(matchingConfiguration, emailService,
                                                                     opportunityRepository, messageQueueService, logger);

            emailService.UpdateEmailStatus(Arg.Any <EmailDeliveryStatusPayLoad>()).Returns(1);

            var query = new Dictionary <string, StringValues>();

            query.TryAdd("content-type", "application/json");

            var httpRequest = HttpRequestSetup(query, serializedPayLoad);

            //Act
            var emailDeliveryStatusFunctions = new Functions.EmailDeliveryStatus(matchingConfiguration, notificationService, functionLogRepository);
            var result = await emailDeliveryStatusFunctions.EmailDeliveryStatusHandlerAsync(httpRequest, context, logger) as OkObjectResult;

            //Assert
            httpRequest.Headers.TryGetValue("Authorization", out var token);

            token.Should().Equal($"Bearer {matchingConfiguration.EmailDeliveryStatusToken}");
            result.Should().NotBeNull();
            result?.StatusCode.Should().Be(200);
            result?.Value.Should().Be("1 records updated.");

            await messageQueueService.DidNotReceive().PushEmailDeliveryStatusMessageAsync(Arg.Any <SendEmailDeliveryStatus>());
        }
        public When_SendEmailDeliveryStatusNotification_Function_Queue_Trigger_Fires()
        {
            var matchingConfiguration = new MatchingConfiguration
            {
                SendEmailEnabled = true
            };

            _emailDeliveryStatusService = Substitute.For <IEmailDeliveryStatusService>();

            _functionLogRepository = Substitute.For <IRepository <FunctionLog> >();

            var data = new SendEmailDeliveryStatus
            {
                NotificationId = _notificationId
            };

            var emailDeliveryStatusFunctions = new Functions.EmailDeliveryStatus(matchingConfiguration, _emailDeliveryStatusService, _functionLogRepository);

            emailDeliveryStatusFunctions.SendEmailDeliveryStatusNotification(
                data,
                new ExecutionContext(),
                new NullLogger <Functions.ReferralEmails>()
                ).GetAwaiter().GetResult();
        }