329 lines
10 KiB
C#
329 lines
10 KiB
C#
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 class PCRBServiceTests
|
|
{
|
|
private readonly Mock<ILogger<PCRBService>> _loggerMock;
|
|
private readonly Mock<IDalService> _dalServiceMock;
|
|
private Mock<IMemoryCache> _cacheMock;
|
|
private readonly Mock<IUserService> _userServiceMock;
|
|
private readonly Mock<IApprovalService> _approvalServiceMock;
|
|
private readonly Mock<ISmtpService> _smtpServiceMock;
|
|
private PCRBService _pcrbService;
|
|
|
|
private static IEnumerable<PCRB> PCRBS = new List<PCRB>() {
|
|
new PCRB {
|
|
PlanNumber = 1,
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
InsertTimeStamp = DateTime.Now,
|
|
LastUpdateDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
}
|
|
};
|
|
|
|
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"
|
|
);
|
|
|
|
public PCRBServiceTests()
|
|
{
|
|
_loggerMock = new Mock<ILogger<PCRBService>>();
|
|
_dalServiceMock = new Mock<IDalService>();
|
|
_userServiceMock = new Mock<IUserService>();
|
|
_approvalServiceMock = new Mock<IApprovalService>();
|
|
_smtpServiceMock = new Mock<ISmtpService>();
|
|
_cacheMock = MockMemoryCacheService.GetMemoryCache(FOLLOW_UPS);
|
|
|
|
_pcrbService = new PCRBService(
|
|
_loggerMock.Object,
|
|
_dalServiceMock.Object,
|
|
_cacheMock.Object,
|
|
_userServiceMock.Object,
|
|
_approvalServiceMock.Object,
|
|
_smtpServiceMock.Object,
|
|
appSettings
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateNewPCRB_WithValidParam_ShouldCreatePCRB()
|
|
{
|
|
var pcrb = new PCRB
|
|
{
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
InsertTimeStamp = DateTime.Now,
|
|
LastUpdateDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.CreateNewPCRB(pcrb);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync(It.IsAny<string>(), pcrb), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateNewPCRB_WithNullParam_ShouldThrowException()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateNewPCRB(null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateNewPCRB_WithDatabaseFailure_ShouldThrowException()
|
|
{
|
|
var pcrb = new PCRB
|
|
{
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
InsertTimeStamp = DateTime.Now,
|
|
LastUpdateDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateNewPCRB(pcrb));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateNewPCRB_WithDatabaseException_ShouldThrowException()
|
|
{
|
|
var pcrb = new PCRB
|
|
{
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
InsertTimeStamp = DateTime.Now,
|
|
LastUpdateDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateNewPCRB(pcrb));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdatePCRB_WithValidParam_ShouldUpdatePCRB()
|
|
{
|
|
_cacheMock = MockMemoryCacheService.GetMemoryCache(PCRBS);
|
|
|
|
_pcrbService = new PCRBService(
|
|
_loggerMock.Object,
|
|
_dalServiceMock.Object,
|
|
_cacheMock.Object,
|
|
_userServiceMock.Object,
|
|
_approvalServiceMock.Object,
|
|
_smtpServiceMock.Object,
|
|
appSettings
|
|
);
|
|
|
|
var pcrb = new PCRB
|
|
{
|
|
PlanNumber = 1,
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
LastUpdateDate = DateTime.Now,
|
|
ClosedDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.UpdatePCRB(pcrb);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync(It.IsAny<string>(), pcrb), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdatePCRB_WithNullParam_ShouldThrowException()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdatePCRB(null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdatePCRB_WithDatabaseFailure_ShouldThrowException()
|
|
{
|
|
var pcrb = new PCRB
|
|
{
|
|
PlanNumber = 1,
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
LastUpdateDate = DateTime.Now,
|
|
ClosedDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdatePCRB(pcrb));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdatePCRB_WithDatabaseException_ShouldThrowException()
|
|
{
|
|
var pcrb = new PCRB
|
|
{
|
|
PlanNumber = 1,
|
|
OwnerID = 1,
|
|
Title = "Test Title",
|
|
ChangeLevel = "Level 1",
|
|
ReasonForChange = "Test Reason",
|
|
ChangeDescription = "Test Description",
|
|
IsITAR = false,
|
|
CurrentStep = 1,
|
|
LastUpdateDate = DateTime.Now,
|
|
ClosedDate = DateTime.Now,
|
|
Type = "Type 1"
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), pcrb))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdatePCRB(pcrb));
|
|
}
|
|
|
|
[Fact]
|
|
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
|
|
}
|
|
};
|
|
|
|
_userServiceMock.Setup(s => s.GetUserByUserId(It.IsAny<int>()))
|
|
.ReturnsAsync(new User
|
|
{
|
|
UserID = 1,
|
|
LoginID = "testLogin",
|
|
FirstName = "Test",
|
|
LastName = "User",
|
|
Email = "test@example.com"
|
|
});
|
|
|
|
_smtpServiceMock.Setup(s => s.SendEmail(It.IsAny<IEnumerable<MailAddress>>(),
|
|
It.IsAny<IEnumerable<MailAddress>>(),
|
|
It.IsAny<string>(),
|
|
It.IsAny<string>()))
|
|
.ReturnsAsync(true);
|
|
|
|
_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 NotifyApprover_ShouldThrowException_WhenNotificationIsNull()
|
|
{
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(null));
|
|
}
|
|
|
|
[Fact]
|
|
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
|
|
}
|
|
};
|
|
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NotifyApprover_ShouldThrowException_WhenApprovalIsNull()
|
|
{
|
|
PCRBNotification notification = new PCRBNotification
|
|
{
|
|
Message = "Test Message",
|
|
PCRB = new PCRB { PlanNumber = 1, Title = "Test PCRB" },
|
|
Approval = null
|
|
};
|
|
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
|
|
}
|
|
}
|