197 lines
7.5 KiB
C#
197 lines
7.5 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 class PCRBFollowUpCommentsTests {
|
|
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 readonly AppSettings _appSettings;
|
|
|
|
private static PCRBFollowUpComment FOLLOW_UP_COMMENT = new PCRBFollowUpComment {
|
|
PlanNumber = 1,
|
|
FollowUpID = 1,
|
|
Comment = "Comment",
|
|
UserID = 1
|
|
};
|
|
|
|
private static IEnumerable<PCRBFollowUpComment> FOLLOW_UP_COMMENTS = new List<PCRBFollowUpComment>() { FOLLOW_UP_COMMENT };
|
|
|
|
public PCRBFollowUpCommentsTests() {
|
|
_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_UP_COMMENTS);
|
|
_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 CreateFollowUpComment_WithValidParam_ShouldCreateFollowUp() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.CreateFollowUpComment(FOLLOW_UP_COMMENT);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUpComment_WithNullParam_ShouldThrowException() {
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUpComment(null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUpComment_WithDatabaseFailure_ShouldThrowException() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUpComment(FOLLOW_UP_COMMENT));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateFollowUpComment_WithDatabaseException_ShouldThrowException() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUpComment(FOLLOW_UP_COMMENT));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFollowUpCommentsByPlanNumber_WithCacheBypass_ShouldReturnFollowUps() {
|
|
int planNumber = 1;
|
|
|
|
_dalServiceMock.Setup(d => d.QueryAsync<PCRBFollowUpComment>(It.IsAny<string>(), It.IsAny<object>()))
|
|
.ReturnsAsync(FOLLOW_UP_COMMENTS);
|
|
|
|
IEnumerable<PCRBFollowUpComment> result = await _pcrbService.GetFollowUpCommentsByPlanNumber(planNumber, true);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result);
|
|
Assert.Equal(FOLLOW_UP_COMMENTS, result);
|
|
_dalServiceMock.Verify(d => d.QueryAsync<PCRBFollowUpComment>(It.IsAny<string>(), It.IsAny<object>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFollowUpCommentsByPlanNumber_WithCacheBypass_AndDatabaseException_ShouldThrowException() {
|
|
int planNumber = 1;
|
|
|
|
_dalServiceMock.Setup(d => d.QueryAsync<PCRBFollowUpComment>(It.IsAny<string>(), It.IsAny<object>()))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.GetFollowUpCommentsByPlanNumber(planNumber, true));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetFollowUpCommentsByPlanNumber_WithoutCacheBypass_ShouldReturnFollowUps() {
|
|
int planNumber = 1;
|
|
|
|
IEnumerable<PCRBFollowUpComment> result = await _pcrbService.GetFollowUpCommentsByPlanNumber(planNumber, false);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result);
|
|
Assert.Equal(FOLLOW_UP_COMMENTS, result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFollowUpComment_WithValidParam_ShouldUpdateFollowUp() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.UpdateFollowUpComment(FOLLOW_UP_COMMENT);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFollowUpComment_WithNullParam_ShouldThrowException() {
|
|
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUpComment(null));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFollowUpComment_WithDatabaseFailure_ShouldThrowException() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUpComment(FOLLOW_UP_COMMENT));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateFollowUpComment_WithDatabaseException_ShouldThrowException() {
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUpComment>(It.IsAny<string>(), FOLLOW_UP_COMMENT))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUpComment(FOLLOW_UP_COMMENT));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFollowUpComment_WithValidId_ShouldDeleteFollowUp() {
|
|
int commentId = 1;
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
|
|
.ReturnsAsync(1);
|
|
|
|
await _pcrbService.DeleteFollowUpComment(commentId);
|
|
|
|
_dalServiceMock.Verify(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFollowUpComment_WithInvalidId_ShouldThrowException() {
|
|
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.DeleteFollowUpComment(0));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFollowUpComment_WithDatabaseFailure_ShouldThrowException() {
|
|
int commentId = 1;
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
|
|
.ReturnsAsync(0);
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUpComment(commentId));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteFollowUpComment_WithDatabaseException_ShouldThrowException() {
|
|
int commentId = 1;
|
|
|
|
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()))
|
|
.Throws<Exception>();
|
|
|
|
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUpComment(commentId));
|
|
}
|
|
}
|