using LCU.API.Internal.DocumentIngestor.Configurations; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.Library.Internal.ApiResponse.Models; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.Exceptions; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class MemberNotificationCompletionServiceTest { private MemberNotificationCompletionService _memberNotificationCompletionService; private Mock _mockMemberNotificationInfoService; private Mock _mockMemberPreferencesApiService; private Mock _mockProcessingLogService; private ApiConfiguration _apiConfiguration; [SetUp] public void Setup() { _mockMemberNotificationInfoService = new Mock(); _mockMemberPreferencesApiService = new Mock(); _mockProcessingLogService = new Mock(); _apiConfiguration = new ApiConfiguration() { MaxMemberNotificationAttempts = 3, TimeZone = "America/Chicago" }; _memberNotificationCompletionService = new MemberNotificationCompletionService(_mockMemberNotificationInfoService.Object, _mockMemberPreferencesApiService.Object, _mockProcessingLogService.Object, _apiConfiguration); } [Test] public void HandleMemberNotificationCompletion_ArgumentNullException_ResponseNull() { Response response = null; Assert.ThrowsAsync(async () => await _memberNotificationCompletionService.HandleMemberNotificationCompletion(response)); } [Test] public async Task CompleteAndUpdateStatuses_Success() { var memberNotificationInfoIds = new List { 1, 2, 3, 4, 5 }; var success = true; var logs = new List(); var memberNotificationInfos = new List() { new() { MemberNotificationInfoId = memberNotificationInfoIds[0], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com" }, new() { MemberNotificationInfoId = memberNotificationInfoIds[1], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com" }, new() { MemberNotificationInfoId = memberNotificationInfoIds[2], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com" }, new() { MemberNotificationInfoId = memberNotificationInfoIds[3], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com" }, new() { MemberNotificationInfoId = memberNotificationInfoIds[4], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com" }, }; // Get MemberNotificationInfos _mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(memberNotificationInfoIds)).ReturnsAsync(memberNotificationInfos); // Update _mockMemberNotificationInfoService.SetupSequence(x => x.Update(It.IsAny())).ReturnsAsync(memberNotificationInfos[0]) .ReturnsAsync(memberNotificationInfos[1]) .ReturnsAsync(memberNotificationInfos[2]) .ReturnsAsync(memberNotificationInfos[3]) .ReturnsAsync(memberNotificationInfos[4]); await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs); var completed = memberNotificationInfos?.Where(x => x.NotificationStatusType == NotificationStatusTypes.Complete).Count(); Assert.AreEqual(completed, 5); _mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), success), Times.Exactly(2)); _mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(5)); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task CompleteAndUpdateStatuses_Failure() { var memberNotificationInfoIds = new List { 1, 2, 3, 4, 5 }; var success = false; var logs = new List() { "Failure 1", "Failure 2" }; var memberNotificationInfos = new List() { new() { MemberNotificationInfoId = memberNotificationInfoIds[0], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[1], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 3 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[2], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[3], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[4], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 1 }, }; // Get MemberNotificationInfos _mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(memberNotificationInfoIds)).ReturnsAsync(memberNotificationInfos); // Update _mockMemberNotificationInfoService.SetupSequence(x => x.Update(It.IsAny())).ReturnsAsync(memberNotificationInfos[0]) .ReturnsAsync(memberNotificationInfos[1]) .ReturnsAsync(memberNotificationInfos[2]) .ReturnsAsync(memberNotificationInfos[3]) .ReturnsAsync(memberNotificationInfos[4]); await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs); var undeliverable = memberNotificationInfos?.Where(x => x.NotificationStatusType == NotificationStatusTypes.Undeliverable).Count(); var failure = memberNotificationInfos?.Where(x => x.NotificationStatusType == NotificationStatusTypes.Failure).Count(); Assert.AreEqual(undeliverable, 1); Assert.AreEqual(failure, 4); _mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), success), Times.Once); _mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(5)); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Exactly(10)); } [Test] public void CompleteAndUpdateStatuses_Exception() { var memberNotificationInfoIds = new List { 1, 2, 3, 4, 5 }; var success = false; var logs = new List(); _mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny>())).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs)); ; _mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny()), Times.Never); _mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), success), Times.Never); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public async Task CompleteAndUpdateStatuses_Exception_EmailNotFound() { var memberNotificationInfoIds = new List { 1, 2, 3, 4, 5 }; var success = false; var logs = new List(); var memberNotificationInfos = new List() { new() { MemberNotificationInfoId = memberNotificationInfoIds[0], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[1], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 3 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[2], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[3], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[4], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 1 }, }; _mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny>())).ReturnsAsync(memberNotificationInfos); _mockMemberPreferencesApiService.Setup(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(new Exception("StatusCode:NotFound")); await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs); _mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(memberNotificationInfos.Count)); _mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } [Test] public void CompleteAndUpdateStatuses_Exception_EmailOtherError() { var memberNotificationInfoIds = new List { 1, 2, 3, 4, 5 }; var success = false; var logs = new List(); var memberNotificationInfos = new List() { new() { MemberNotificationInfoId = memberNotificationInfoIds[0], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[1], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 3 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[2], NotificationStatusType = NotificationStatusTypes.Pending, AgreeUserId = "111", EmailAddress = "test@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[3], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 2 }, new() { MemberNotificationInfoId = memberNotificationInfoIds[4], NotificationStatusType = NotificationStatusTypes.Failure, AgreeUserId = "444", EmailAddress = "test2@test.com", Attempts = 1 }, }; var pendingCount = memberNotificationInfos.Count(x => x.NotificationStatusType == NotificationStatusTypes.Pending); _mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny>())).ReturnsAsync(memberNotificationInfos); _mockMemberPreferencesApiService.Setup(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(new Exception("Unexpected Error")); Assert.ThrowsAsync(async () => await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs)); _mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny()), Times.Exactly(pendingCount)); _mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny()), Times.Never); } }