public void IssueContactReminders_ProcessesAllReturnedRecords() { // Arrange var service = CreateContractReminderService(); var result = GetContracts(); var emptyResult = new ContractReminders(); SetupContractReminderServiceGetContracts(result, emptyResult); SetupLoggerInformation(); Mock.Get(_contractNotificationService) .Setup(p => p.QueueContractEmailReminderMessage(It.IsIn <Contract>(result.Contracts))) .Returns(Task.CompletedTask) .Verifiable(); Mock.Get(_contractNotificationService) .Setup(p => p.NotifyContractReminderSent(It.IsIn <Contract>(result.Contracts))) .Returns(Task.CompletedTask) .Verifiable(); Mock.Get(_auditService) .Setup(p => p.TrySendAuditAsync(It.IsAny <Audit.Api.Client.Models.Audit>())) .Returns(Task.CompletedTask) .Verifiable(); // Act Func <Task> act = async() => await service.IssueContractReminders(); // Assert act.Should().NotThrow(); VerifyAll(result, 2, 2, 2); }
private ContractReminders CreateContractReminders() { ContractReminders rtn = new ContractReminders() { Contracts = new List <Contract>() { new Contract() { ContractNumber = "One123", ContractVersion = 234, Id = 345, Ukprn = 456 }, new Contract() { ContractNumber = "Two123", ContractVersion = 234, Id = 345, Ukprn = 456 } } }; return(rtn); }
public void IssueContactReminders_OnAllProcessingFailure_RaisesException() { // Arrange var service = CreateContractReminderService(); var result = GetContracts(); var emptyResult = new ContractReminders(); SetupContractReminderServiceGetContracts(result, emptyResult); SetupLoggerInformation(); SetupLoggerError <InvalidOperationException>(); Mock.Get(_loggerAdapter) .Setup(p => p.LogCritical(It.IsAny <string>(), It.IsAny <object[]>())) .Verifiable(); Mock.Get(_contractNotificationService) .Setup(p => p.QueueContractEmailReminderMessage(It.IsIn <Contract>(result.Contracts))) .Throws <InvalidOperationException>() .Verifiable(); Mock.Get(_auditService) .Setup(p => p.TrySendAuditAsync(It.IsAny <Audit.Api.Client.Models.Audit>())) .Returns(Task.CompletedTask) .Verifiable(); // Act Func <Task> act = async() => await service.IssueContractReminders(); // Assert act.Should().Throw <Exception>(); Mock.Get(_contractNotificationService).Verify(); Mock.Get(_loggerAdapter).Verify(); }
private void SetupContractReminderServiceGetContracts(ContractReminders firstResult, ContractReminders secondResult) { Mock.Get(_contractNotificationService) .SetupSequence(p => p.GetOverdueContracts()) .Returns(Task.FromResult(firstResult)) .Returns(Task.FromResult(secondResult)) .Throws <InvalidOperationException>(); }
private void VerifyAll(ContractReminders result, int callsToGetOverdue, int callsToEmailReminder, int callsToReminderSent) { var contracts = result?.Contracts; Mock.Get(_contractNotificationService) .Verify(p => p.GetOverdueContracts(), Times.Exactly(callsToGetOverdue)); Mock.Get(_contractNotificationService) .Verify(p => p.QueueContractEmailReminderMessage(It.IsIn <Contract>(contracts)), Times.Exactly(callsToEmailReminder)); Mock.Get(_contractNotificationService) .Verify(p => p.NotifyContractReminderSent(It.IsIn <Contract>(contracts)), Times.Exactly(callsToReminderSent)); Mock.Get(_loggerAdapter).VerifyAll(); }
public async Task GetContractRemindersAsync_MockHttp() { // Arrange int reminderInterval = 14; int pageNumber = 1; int pageSize = 2; ContractSortOptions sort = ContractSortOptions.LastUpdatedAt; SortDirection order = SortDirection.Asc; Mock.Get(_contractsDataLogger) .Setup(p => p.LogInformation(It.IsAny <string>(), It.IsAny <object[]>())); var contractList = new List <ContractReminderItem>() { new ContractReminderItem() { Id = 1, ContractVersion = 1, ContractNumber = "Test1" }, new ContractReminderItem() { Id = 2, ContractVersion = 1, ContractNumber = "Test2" } }; var expectedContractReminders = new ContractReminders { Contracts = contractList }; expectedContractReminders.Paging = new Paging() { CurrentPage = pageNumber, PageSize = pageSize, TotalCount = 2, TotalPages = 1, HasNextPage = false, HasPreviousPage = false, NextPageUrl = string.Empty, PreviousPageUrl = string.Empty }; string jsonString = JsonSerializer.Serialize(expectedContractReminders); _mockHttpMessageHandler.Expect(TestBaseAddress + $"/api/contractReminders?reminderInterval={reminderInterval}&page={pageNumber}&count={pageSize}&sort={sort}&order={order}").Respond("application/json", jsonString); ContractsDataService contractsDataService = CreateContractsDataService(); //Act var result = await contractsDataService.GetContractRemindersAsync((uint)reminderInterval, (uint)pageNumber, (uint)pageSize, sort, order); // Assert result.Should().BeEquivalentTo(expectedContractReminders); _mockHttpMessageHandler.VerifyNoOutstandingExpectation(); VerifyAllMocks(); }
public void IssueContactReminders_HandlesNullResult() { // Arrange var service = CreateContractReminderService(); ContractReminders result = null; SetupContractReminderServiceGetContracts(result, null); SetupLoggerInformation(); Mock.Get(_auditService) .Setup(p => p.TrySendAuditAsync(It.IsAny <Audit.Api.Client.Models.Audit>())) .Returns(Task.CompletedTask) .Verifiable(); // Act Func <Task> act = async() => await service.IssueContractReminders(); // Assert act.Should().NotThrow(); VerifyAll(result, 1, 0, 0); }
public void IssueContactReminders_OnOneRecordProcessingFailure_LogsErrors_And_Continues() { // Arrange var service = new ContractReminderProcessingService(_contractNotificationService, _loggerAdapter, _auditService); var result = GetContracts(); var emptyResult = new ContractReminders(); SetupContractReminderServiceGetContracts(result, emptyResult); SetupLoggerInformation(); SetupLoggerError <InvalidOperationException>(); Mock.Get(_contractNotificationService) .SetupSequence(p => p.QueueContractEmailReminderMessage(It.IsIn <Contract>(result.Contracts))) .Returns(Task.CompletedTask) .Throws <InvalidOperationException>() .Returns(Task.CompletedTask); Mock.Get(_contractNotificationService) .Setup(p => p.NotifyContractReminderSent(It.IsIn <Contract>(result.Contracts))) .Returns(Task.CompletedTask) .Verifiable(); Mock.Get(_auditService) .Setup(p => p.TrySendAuditAsync(It.IsAny <Audit.Api.Client.Models.Audit>())) .Returns(Task.CompletedTask) .Verifiable(); // Act Func <Task> act = async() => await service.IssueContractReminders(); // Assert act.Should().NotThrow(); Mock.Get(_contractNotificationService).Verify(); Mock.Get(_loggerAdapter).Verify(); }
/// <inheritdoc/> public async Task IssueContractReminders() { await _auditService.TrySendAuditAsync( new Audit.Api.Client.Models.Audit { Severity = 0, Action = Audit.Api.Client.Enumerations.ActionType.ContractEmailReminderQueued, Ukprn = null, Message = $"Contract Reminder function has been triggered.", User = Audit_User_System }); ContractReminders reminders = null; int remindersSent = 0; do { reminders = await _contractNotificationService.GetOverdueContracts(); if (reminders?.Contracts?.Count > 0) { _logger.LogInformation($"Processing a list of contracts with {reminders.Contracts.Count} records."); IList <Task> tasks = new List <Task>(); foreach (var contract in reminders.Contracts) { var reminderProcess = Task.Run(async() => { try { _logger.LogInformation($"Starting processing contract with id {contract.Id}."); await _contractNotificationService.QueueContractEmailReminderMessage(contract); await _contractNotificationService.NotifyContractReminderSent(contract); _logger.LogInformation($"Completed processing for contract with id {contract.Id}."); } catch (Exception e) { _logger.LogError(e, $"Contract with id [{contract.Id}] failed to process successfully."); throw; } }); tasks.Add(reminderProcess); } try { await Task.WhenAll(tasks); } catch (Exception e) { _logger.LogError(e, "Error processign one or more records."); } if (tasks.All(p => !p.IsCompletedSuccessfully)) { _logger.LogCritical("All processes failed to complete successfully. Aborting..."); _logger.LogInformation($"Contract reminders processes aborted. Sent {remindersSent} reminders prior to abort."); throw new ContractProcessingException("All processes failed to complete successfully.", tasks.First(p => !p.IsCompletedSuccessfully).Exception); } else { remindersSent += tasks.Count(p => p.IsCompletedSuccessfully); } } }while (reminders?.Contracts?.Count > 0); { await _auditService.TrySendAuditAsync( new Audit.Api.Client.Models.Audit { Severity = 0, Action = Audit.Api.Client.Enumerations.ActionType.ContractEmailReminderQueued, Ukprn = null, Message = $"Contract Reminder function has completed. {remindersSent} contract email reminder(s) processed.", User = Audit_User_System }); _logger.LogInformation($"Contract reminders process completed. Sent {remindersSent} reminders."); } }