415 lines
14 KiB
C#
415 lines
14 KiB
C#
using MesaFabApproval.API.Services;
|
|
using MesaFabApproval.Models;
|
|
using MesaFabApproval.Shared.Models;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Moq;
|
|
|
|
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 {
|
|
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 CreateFollowUp_WithValidParam_ShouldCreateFollowUp() {
|
|
var followUp = new PCRBFollowUp {
|
|
PlanNumber = 1,
|
|
Step = 1,
|
|
FollowUpDate = DateTime.Now
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.CreateFollowUp(followUp);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUp_WithNullParam_ShouldThrowException() {
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUp(null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUp_WithDatabaseFailure_ShouldThrowException() {
|
|
var followUp = new PCRBFollowUp {
|
|
PlanNumber = 1,
|
|
Step = 1,
|
|
FollowUpDate = DateTime.Now
|
|
};
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), followUp))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(followUp));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUp_WithDatabaseException_ShouldThrowException() {
|
|
var followUp = new PCRBFollowUp {
|
|
PlanNumber = 1,
|
|
Step = 1,
|
|
FollowUpDate = DateTime.Now
|
|
};
|
|
|
|
_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));
|
|
}
|
|
}
|