PCRB follow up client side logic

This commit is contained in:
Chase Tucker
2025-03-19 10:01:35 -07:00
parent 4871668a90
commit cc4781b990
45 changed files with 3082 additions and 1008 deletions

View File

@ -1,28 +1,15 @@
using MesaFabApproval.API.Services;
using MesaFabApproval.API.Services;
using MesaFabApproval.Models;
using MesaFabApproval.Shared.Models;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Moq;
using System.Net.Mail;
namespace MesaFabApproval.API.Test;
public static class MockMemoryCacheService {
public static Mock<IMemoryCache> GetMemoryCache(object expectedValue) {
Mock<IMemoryCache> mockMemoryCache = new Mock<IMemoryCache>();
mockMemoryCache
.Setup(x => x.TryGetValue(It.IsAny<object>(), out expectedValue))
.Returns(true);
mockMemoryCache
.Setup(x => x.CreateEntry(It.IsAny<object>()))
.Returns(Mock.Of<ICacheEntry>());
return mockMemoryCache;
}
}
public class PCRBServiceTests {
public class PCRBServiceTests
{
private readonly Mock<ILogger<PCRBService>> _loggerMock;
private readonly Mock<IDalService> _dalServiceMock;
private Mock<IMemoryCache> _cacheMock;
@ -47,24 +34,25 @@ public class PCRBServiceTests {
}
};
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
new PCRBFollowUp { ID = 1, PlanNumber = 1, Step = 1, FollowUpDate = DateTime.Now }
};
private static AppSettings appSettings = new AppSettings(
Company: "Infineon",
DbConnectionString: "connectionString",
JwtAudience: "audience",
JwtIssuer: "issuer",
JwtKey: "key",
MrbAttachmentPath: "mrbAttachmentPath",
PcrbAttachmentPath: "pcrbAttachmentPath",
ShouldSendEmail: false,
SiteBaseUrl: "siteBaseUrl",
WorkingDirectoryName: "workingDirectoryName"
);
Company: "Infineon",
DbConnectionString: "connectionString",
JwtAudience: "audience",
JwtIssuer: "issuer",
JwtKey: "key",
MrbAttachmentPath: "mrbAttachmentPath",
PcrbAttachmentPath: "pcrbAttachmentPath",
ShouldSendEmail: false,
SiteBaseUrl: "siteBaseUrl",
WorkingDirectoryName: "workingDirectoryName"
);
public PCRBServiceTests() {
public PCRBServiceTests()
{
_loggerMock = new Mock<ILogger<PCRBService>>();
_dalServiceMock = new Mock<IDalService>();
_userServiceMock = new Mock<IUserService>();
@ -84,8 +72,10 @@ public class PCRBServiceTests {
}
[Fact]
public async Task CreateNewPCRB_WithValidParam_ShouldCreatePCRB() {
var pcrb = new PCRB {
public async Task CreateNewPCRB_WithValidParam_ShouldCreatePCRB()
{
var pcrb = new PCRB
{
OwnerID = 1,
Title = "Test Title",
ChangeLevel = "Level 1",
@ -107,13 +97,16 @@ public class PCRBServiceTests {
}
[Fact]
public async Task CreateNewPCRB_WithNullParam_ShouldThrowException() {
public async Task CreateNewPCRB_WithNullParam_ShouldThrowException()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateNewPCRB(null));
}
[Fact]
public async Task CreateNewPCRB_WithDatabaseFailure_ShouldThrowException() {
var pcrb = new PCRB {
public async Task CreateNewPCRB_WithDatabaseFailure_ShouldThrowException()
{
var pcrb = new PCRB
{
OwnerID = 1,
Title = "Test Title",
ChangeLevel = "Level 1",
@ -133,8 +126,10 @@ public class PCRBServiceTests {
}
[Fact]
public async Task CreateNewPCRB_WithDatabaseException_ShouldThrowException() {
var pcrb = new PCRB {
public async Task CreateNewPCRB_WithDatabaseException_ShouldThrowException()
{
var pcrb = new PCRB
{
OwnerID = 1,
Title = "Test Title",
ChangeLevel = "Level 1",
@ -154,7 +149,8 @@ public class PCRBServiceTests {
}
[Fact]
public async Task UpdatePCRB_WithValidParam_ShouldUpdatePCRB() {
public async Task UpdatePCRB_WithValidParam_ShouldUpdatePCRB()
{
_cacheMock = MockMemoryCacheService.GetMemoryCache(PCRBS);
_pcrbService = new PCRBService(
@ -167,7 +163,8 @@ public class PCRBServiceTests {
appSettings
);
var pcrb = new PCRB {
var pcrb = new PCRB
{
PlanNumber = 1,
OwnerID = 1,
Title = "Test Title",
@ -190,13 +187,16 @@ public class PCRBServiceTests {
}
[Fact]
public async Task UpdatePCRB_WithNullParam_ShouldThrowException() {
public async Task UpdatePCRB_WithNullParam_ShouldThrowException()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdatePCRB(null));
}
[Fact]
public async Task UpdatePCRB_WithDatabaseFailure_ShouldThrowException() {
var pcrb = new PCRB {
public async Task UpdatePCRB_WithDatabaseFailure_ShouldThrowException()
{
var pcrb = new PCRB
{
PlanNumber = 1,
OwnerID = 1,
Title = "Test Title",
@ -217,8 +217,10 @@ public class PCRBServiceTests {
}
[Fact]
public async Task UpdatePCRB_WithDatabaseException_ShouldThrowException() {
var pcrb = new PCRB {
public async Task UpdatePCRB_WithDatabaseException_ShouldThrowException()
{
var pcrb = new PCRB
{
PlanNumber = 1,
OwnerID = 1,
Title = "Test Title",
@ -239,176 +241,88 @@ public class PCRBServiceTests {
}
[Fact]
public async Task CreateFollowUp_WithValidParam_ShouldCreateFollowUp() {
var followUp = new PCRBFollowUp {
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
public async Task NotifyApprover_ShouldSendNotification()
{
PCRBNotification notification = new PCRBNotification
{
Message = "Test Message",
PCRB = new PCRB { PlanNumber = 1, Title = "Test PCRB" },
Approval = new Approval
{
UserID = 1,
IssueID = 1,
RoleName = "Role",
SubRole = "SubRole",
SubRoleID = 1,
AssignedDate = DateTime.Now
}
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.ReturnsAsync(1);
_userServiceMock.Setup(s => s.GetUserByUserId(It.IsAny<int>()))
.ReturnsAsync(new User
{
UserID = 1,
LoginID = "testLogin",
FirstName = "Test",
LastName = "User",
Email = "test@example.com"
});
await _pcrbService.CreateFollowUp(followUp);
_smtpServiceMock.Setup(s => s.SendEmail(It.IsAny<IEnumerable<MailAddress>>(),
It.IsAny<IEnumerable<MailAddress>>(),
It.IsAny<string>(),
It.IsAny<string>()))
.ReturnsAsync(true);
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp), Times.Once);
_approvalServiceMock.Setup(s => s.UpdateApproval(It.IsAny<Approval>()))
.Returns(Task.CompletedTask);
await _pcrbService.NotifyApprover(notification);
_smtpServiceMock.Verify(s => s.SendEmail(It.IsAny<IEnumerable<MailAddress>>(),
It.IsAny<IEnumerable<MailAddress>>(),
It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
_approvalServiceMock.Verify(s => s.UpdateApproval(It.IsAny<Approval>()), Times.Once);
}
[Fact]
public async Task CreateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUp(null));
public async Task NotifyApprover_ShouldThrowException_WhenNotificationIsNull()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(null));
}
[Fact]
public async Task CreateFollowUp_WithDatabaseFailure_ShouldThrowException() {
var followUp = new PCRBFollowUp {
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
public async Task NotifyApprover_ShouldThrowException_WhenPCRBIsNull()
{
PCRBNotification notification = new PCRBNotification
{
Message = "Test Message",
PCRB = null,
Approval = new Approval
{
UserID = 1,
IssueID = 1,
RoleName = "Role",
SubRole = "SubRole",
SubRoleID = 1,
AssignedDate = DateTime.Now
}
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.ReturnsAsync(0);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(followUp));
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
}
[Fact]
public async Task CreateFollowUp_WithDatabaseException_ShouldThrowException() {
var followUp = new PCRBFollowUp {
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
public async Task NotifyApprover_ShouldThrowException_WhenApprovalIsNull()
{
PCRBNotification notification = new PCRBNotification
{
Message = "Test Message",
PCRB = new PCRB { PlanNumber = 1, Title = "Test PCRB" },
Approval = null
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.Throws<Exception>();
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(followUp));
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithCacheBypass_ShouldReturnFollowUps() {
int planNumber = 1;
_dalServiceMock.Setup(d => d.QueryAsync<PCRBFollowUp>(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(FOLLOW_UPS);
IEnumerable<PCRBFollowUp> result = await _pcrbService.GetFollowUpsByPlanNumber(planNumber, true);
Assert.NotNull(result);
Assert.Single(result);
Assert.Equal(FOLLOW_UPS, result);
_dalServiceMock.Verify(d => d.QueryAsync<PCRBFollowUp>(It.IsAny<string>(), It.IsAny<object>()), Times.Once);
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithCacheBypass_AndDatabaseException_ShouldThrowException() {
int planNumber = 1;
_dalServiceMock.Setup(d => d.QueryAsync<PCRBFollowUp>(It.IsAny<string>(), It.IsAny<object>()))
.Throws<Exception>();
await Assert.ThrowsAsync<Exception>(() => _pcrbService.GetFollowUpsByPlanNumber(planNumber, true));
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithoutCacheBypass_ShouldReturnFollowUps() {
int planNumber = 1;
IEnumerable<PCRBFollowUp> result = await _pcrbService.GetFollowUpsByPlanNumber(planNumber, false);
Assert.NotNull(result);
Assert.Single(result);
Assert.Equal(FOLLOW_UPS, result);
}
[Fact]
public async Task UpdateFollowUp_WithValidParam_ShouldUpdateFollowUp() {
var followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.ReturnsAsync(1);
await _pcrbService.UpdateFollowUp(followUp);
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp), Times.Once);
}
[Fact]
public async Task UpdateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUp(null));
}
[Fact]
public async Task UpdateFollowUp_WithDatabaseFailure_ShouldThrowException() {
var followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.ReturnsAsync(0);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(followUp));
}
[Fact]
public async Task UpdateFollowUp_WithDatabaseException_ShouldThrowException() {
var followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 1,
Step = 1,
FollowUpDate = DateTime.Now
};
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
.Throws<Exception>();
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(followUp));
}
[Fact]
public async Task DeleteFollowUp_WithValidId_ShouldDeleteFollowUp() {
int followUpId = 1;
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(1);
await _pcrbService.DeleteFollowUp(followUpId);
_dalServiceMock.Verify(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()), Times.Once);
}
[Fact]
public async Task DeleteFollowUp_WithInvalidId_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.DeleteFollowUp(0));
}
[Fact]
public async Task DeleteFollowUp_WithDatabaseFailure_ShouldThrowException() {
int followUpId = 1;
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(0);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUp(followUpId));
}
[Fact]
public async Task DeleteFollowUp_WithDatabaseException_ShouldThrowException() {
int followUpId = 1;
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
.Throws<Exception>();
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUp(followUpId));
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
}
}