PCRB follow up client side services
This commit is contained in:
32
MesaFabApproval.API.Test/MesaFabApproval.API.Test.csproj
Normal file
32
MesaFabApproval.API.Test/MesaFabApproval.API.Test.csproj
Normal file
@ -0,0 +1,32 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="xunit" Version="2.5.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MesaFabApproval.Shared\MesaFabApproval.Shared.csproj" />
|
||||
<ProjectReference Include="..\MesaFabApproval.API\MesaFabApproval.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
63
MesaFabApproval.API.Test/MonInUtilsTests.cs
Normal file
63
MesaFabApproval.API.Test/MonInUtilsTests.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using Moq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MesaFabApproval.API.Utilities;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
namespace MesaFabApproval.API.Test;
|
||||
|
||||
public class MonInUtilsTests {
|
||||
private readonly Mock<IMonInWorkerClient> _mockMonInClient;
|
||||
private readonly Mock<ILogger<MonInUtils>> _mockLogger;
|
||||
private readonly MonInUtils _monInUtils;
|
||||
|
||||
public MonInUtilsTests() {
|
||||
_mockMonInClient = new Mock<IMonInWorkerClient>();
|
||||
_mockMonInClient
|
||||
.Setup(client => client.PostAverage(It.IsAny<string>(), It.IsAny<double>()))
|
||||
.Verifiable();
|
||||
|
||||
_mockLogger = new Mock<ILogger<MonInUtils>>();
|
||||
|
||||
_monInUtils = new MonInUtils(_mockMonInClient.Object, _mockLogger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PostMetrics_ShouldPostAverageAndStatusOk_WhenNoErrors() {
|
||||
string metricName = "TestMetric";
|
||||
double latency = 100;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
|
||||
_monInUtils.PostMetrics(metricName, latency, isArgumentError, isInternalError);
|
||||
|
||||
_mockMonInClient.Verify(client => client.PostAverage(metricName + "Latency", latency), Times.Once);
|
||||
_mockMonInClient.Verify(client => client.PostStatus(metricName, StatusValue.Ok), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PostMetrics_ShouldPostAverageAndStatusOk_WhenArgumentError() {
|
||||
string metricName = "TestMetric";
|
||||
double latency = 100;
|
||||
bool isArgumentError = true;
|
||||
bool isInternalError = false;
|
||||
|
||||
_monInUtils.PostMetrics(metricName, latency, isArgumentError, isInternalError);
|
||||
|
||||
_mockMonInClient.Verify(client => client.PostAverage(metricName + "Latency", latency), Times.Once);
|
||||
_mockMonInClient.Verify(client => client.PostStatus(metricName, StatusValue.Ok), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PostMetrics_ShouldPostAverageAndStatusCritical_WhenInternalError() {
|
||||
string metricName = "TestMetric";
|
||||
double latency = 100;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = true;
|
||||
|
||||
_monInUtils.PostMetrics(metricName, latency, isArgumentError, isInternalError);
|
||||
|
||||
_mockMonInClient.Verify(client => client.PostAverage(metricName + "Latency", latency), Times.Once);
|
||||
_mockMonInClient.Verify(client => client.PostStatus(metricName, StatusValue.Critical), Times.Once);
|
||||
}
|
||||
}
|
243
MesaFabApproval.API.Test/PCRBServiceTests.cs
Normal file
243
MesaFabApproval.API.Test/PCRBServiceTests.cs
Normal file
@ -0,0 +1,243 @@
|
||||
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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user