PCRB follow up client side logic
This commit is contained in:
55
MesaFabApproval.API.Test/ApprovalServiceTests.cs
Normal file
55
MesaFabApproval.API.Test/ApprovalServiceTests.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Moq;
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace MesaFabApproval.API.Test;
|
||||
|
||||
public class ApprovalServiceTests {
|
||||
private readonly Mock<ILogger<ApprovalService>> _loggerMock;
|
||||
private readonly Mock<IMemoryCache> _cacheMock;
|
||||
private readonly Mock<IDalService> _dalServiceMock;
|
||||
private readonly Mock<IUserService> _userServiceMock;
|
||||
private readonly ApprovalService _approvalService;
|
||||
|
||||
public ApprovalServiceTests() {
|
||||
_loggerMock = new Mock<ILogger<ApprovalService>>();
|
||||
_cacheMock = new Mock<IMemoryCache>();
|
||||
_dalServiceMock = new Mock<IDalService>();
|
||||
_userServiceMock = new Mock<IUserService>();
|
||||
_approvalService = new ApprovalService(_loggerMock.Object, _cacheMock.Object, _dalServiceMock.Object, _userServiceMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteApproval_ValidApprovalID_DeletesApproval() {
|
||||
int approvalID = 1;
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>())).ReturnsAsync(1);
|
||||
|
||||
await _approvalService.DeleteApproval(approvalID);
|
||||
|
||||
_dalServiceMock.Verify(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteApproval_InvalidApprovalID_ThrowsArgumentException() {
|
||||
int approvalID = 0;
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => _approvalService.DeleteApproval(approvalID));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteApproval_DeletionFails_ThrowsException() {
|
||||
int approvalID = 1;
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync(It.IsAny<string>(), It.IsAny<object>())).ReturnsAsync(0);
|
||||
|
||||
await Assert.ThrowsAsync<Exception>(() => _approvalService.DeleteApproval(approvalID));
|
||||
}
|
||||
}
|
18
MesaFabApproval.API.Test/MockMemoryCacheService.cs
Normal file
18
MesaFabApproval.API.Test/MockMemoryCacheService.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
196
MesaFabApproval.API.Test/PCRBFollowUpCommentsTests.cs
Normal file
196
MesaFabApproval.API.Test/PCRBFollowUpCommentsTests.cs
Normal file
@ -0,0 +1,196 @@
|
||||
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));
|
||||
}
|
||||
}
|
198
MesaFabApproval.API.Test/PCRBFollowUpTests.cs
Normal file
198
MesaFabApproval.API.Test/PCRBFollowUpTests.cs
Normal file
@ -0,0 +1,198 @@
|
||||
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 PCRBFollowUpTests {
|
||||
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 PCRBFollowUp FOLLOW_UP = new PCRBFollowUp {
|
||||
ID = 1,
|
||||
PlanNumber = 1,
|
||||
Step = 1,
|
||||
FollowUpDate = DateTime.Now
|
||||
};
|
||||
|
||||
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
|
||||
new PCRBFollowUp { ID = 1, PlanNumber = 1, Step = 1, FollowUpDate = DateTime.Now }
|
||||
};
|
||||
|
||||
public PCRBFollowUpTests() {
|
||||
_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);
|
||||
_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() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
await _pcrbService.CreateFollowUp(FOLLOW_UP);
|
||||
|
||||
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateFollowUp_WithNullParam_ShouldThrowException() {
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUp(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateFollowUp_WithDatabaseFailure_ShouldThrowException() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.ReturnsAsync(0);
|
||||
|
||||
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(FOLLOW_UP));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateFollowUp_WithDatabaseException_ShouldThrowException() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.Throws<Exception>();
|
||||
|
||||
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(FOLLOW_UP));
|
||||
}
|
||||
|
||||
[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() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
await _pcrbService.UpdateFollowUp(FOLLOW_UP);
|
||||
|
||||
_dalServiceMock.Verify(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateFollowUp_WithNullParam_ShouldThrowException() {
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUp(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateFollowUp_WithDatabaseFailure_ShouldThrowException() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.ReturnsAsync(0);
|
||||
|
||||
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(FOLLOW_UP));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateFollowUp_WithDatabaseException_ShouldThrowException() {
|
||||
_dalServiceMock.Setup(d => d.ExecuteAsync<PCRBFollowUp>(It.IsAny<string>(), FOLLOW_UP))
|
||||
.Throws<Exception>();
|
||||
|
||||
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(FOLLOW_UP));
|
||||
}
|
||||
|
||||
[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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user