Files
monorepo/LCU.API.Internal.DocumentIngestor/LCU.API.Internal.DocumentIngestorTests/Services/IngestionOperationServices/MemberNotificationCompletionServiceTest.cs
T
2026-03-25 14:50:13 -05:00

340 lines
13 KiB
C#

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<IMemberNotificationInfoService> _mockMemberNotificationInfoService;
private Mock<IMemberPreferencesApiService> _mockMemberPreferencesApiService;
private Mock<IProcessingLogService> _mockProcessingLogService;
private ApiConfiguration _apiConfiguration;
[SetUp]
public void Setup()
{
_mockMemberNotificationInfoService = new Mock<IMemberNotificationInfoService>();
_mockMemberPreferencesApiService = new Mock<IMemberPreferencesApiService>();
_mockProcessingLogService = new Mock<IProcessingLogService>();
_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<ArgumentNullException>(async () => await _memberNotificationCompletionService.HandleMemberNotificationCompletion(response));
}
[Test]
public async Task CompleteAndUpdateStatuses_Success()
{
var memberNotificationInfoIds = new List<int> { 1, 2, 3, 4, 5 };
var success = true;
var logs = new List<string>();
var memberNotificationInfos = new List<MemberNotificationInfo>()
{
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[0],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]"
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[1],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]"
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[2],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]"
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[3],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]"
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[4],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]"
},
};
// Get MemberNotificationInfos
_mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(memberNotificationInfoIds)).ReturnsAsync(memberNotificationInfos);
// Update
_mockMemberNotificationInfoService.SetupSequence(x => x.Update(It.IsAny<MemberNotificationInfo>())).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<List<int>>()), Times.Once);
_mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny<string>(),
It.IsAny<string>(),
success), Times.Exactly(2));
_mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny<MemberNotificationInfo>()), Times.Exactly(5));
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Never);
}
[Test]
public async Task CompleteAndUpdateStatuses_Failure()
{
var memberNotificationInfoIds = new List<int> { 1, 2, 3, 4, 5 };
var success = false;
var logs = new List<string>() { "Failure 1", "Failure 2" };
var memberNotificationInfos = new List<MemberNotificationInfo>()
{
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[0],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[1],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 3
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[2],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[3],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[4],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 1
},
};
// Get MemberNotificationInfos
_mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(memberNotificationInfoIds)).ReturnsAsync(memberNotificationInfos);
// Update
_mockMemberNotificationInfoService.SetupSequence(x => x.Update(It.IsAny<MemberNotificationInfo>())).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<List<int>>()), Times.Once);
_mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny<string>(),
It.IsAny<string>(),
success), Times.Once);
_mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny<MemberNotificationInfo>()), Times.Exactly(5));
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Exactly(10));
}
[Test]
public void CompleteAndUpdateStatuses_Exception()
{
var memberNotificationInfoIds = new List<int> { 1, 2, 3, 4, 5 };
var success = false;
var logs = new List<string>();
_mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny<List<int>>())).Throws(new Exception("Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs)); ;
_mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny<MemberNotificationInfo>()), Times.Never);
_mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny<List<int>>()), Times.Once);
_mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny<string>(),
It.IsAny<string>(),
success), Times.Never);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Never);
}
[Test]
public async Task CompleteAndUpdateStatuses_Exception_EmailNotFound()
{
var memberNotificationInfoIds = new List<int> { 1, 2, 3, 4, 5 };
var success = false;
var logs = new List<string>();
var memberNotificationInfos = new List<MemberNotificationInfo>()
{
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[0],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[1],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 3
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[2],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[3],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[4],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 1
},
};
_mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny<List<int>>())).ReturnsAsync(memberNotificationInfos);
_mockMemberPreferencesApiService.Setup(x => x.EmailStatusSend(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Throws(new Exception("StatusCode:NotFound"));
await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs);
_mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny<List<int>>()), Times.Once);
_mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny<MemberNotificationInfo>()), Times.Exactly(memberNotificationInfos.Count));
_mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<bool>()), Times.Once);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Never);
}
[Test]
public void CompleteAndUpdateStatuses_Exception_EmailOtherError()
{
var memberNotificationInfoIds = new List<int> { 1, 2, 3, 4, 5 };
var success = false;
var logs = new List<string>();
var memberNotificationInfos = new List<MemberNotificationInfo>()
{
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[0],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[1],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 3
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[2],
NotificationStatusType = NotificationStatusTypes.Pending,
AgreeUserId = "111",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[3],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 2
},
new()
{
MemberNotificationInfoId = memberNotificationInfoIds[4],
NotificationStatusType = NotificationStatusTypes.Failure,
AgreeUserId = "444",
EmailAddress = "[email protected]",
Attempts = 1
},
};
var pendingCount = memberNotificationInfos.Count(x => x.NotificationStatusType == NotificationStatusTypes.Pending);
_mockMemberNotificationInfoService.Setup(x => x.GetListByIdAsync(It.IsAny<List<int>>())).ReturnsAsync(memberNotificationInfos);
_mockMemberPreferencesApiService.Setup(x => x.EmailStatusSend(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Throws(new Exception("Unexpected Error"));
Assert.ThrowsAsync<ServiceException>(async () => await _memberNotificationCompletionService.CompleteAndUpdateStatuses(memberNotificationInfoIds, success, logs));
_mockMemberNotificationInfoService.Verify(x => x.GetListByIdAsync(It.IsAny<List<int>>()), Times.Once);
_mockMemberNotificationInfoService.Verify(x => x.Update(It.IsAny<MemberNotificationInfo>()), Times.Exactly(pendingCount));
_mockMemberPreferencesApiService.Verify(x => x.EmailStatusSend(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<bool>()), Times.Once);
_mockProcessingLogService.Verify(x => x.SaveAsync(It.IsAny<ProcessingLog>()), Times.Never);
}
}