2025-03-12 17:34:41 -07:00

244 lines
8.3 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 readonly Mock<IMemoryCache> _cacheMock;
private readonly Mock<IUserService> _userServiceMock;
private readonly Mock<IApprovalService> _approvalServiceMock;
private readonly Mock<ISmtpService> _smtpServiceMock;
private readonly PCRBService _pcrbService;
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
new PCRBFollowUp { ID = 1, PlanNumber = 1, Step = 1, FollowUpDate = DateTime.Now }
};
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);
var appSettings = new AppSettings(
Company: "Infineon",
DbConnectionString: "connectionString",
JwtAudience: "audience",
JwtIssuer: "issuer",
JwtKey: "key",
MrbAttachmentPath: "mrbAttachmentPath",
PcrbAttachmentPath: "pcrbAttachmentPath",
ShouldSendEmail: false,
SiteBaseUrl: "siteBaseUrl",
WorkingDirectoryName: "workingDirectoryName"
);
_pcrbService = new PCRBService(
_loggerMock.Object,
_dalServiceMock.Object,
_cacheMock.Object,
_userServiceMock.Object,
_approvalServiceMock.Object,
_smtpServiceMock.Object,
appSettings
);
}
[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));
}
}