PCRB follow up client side logic

This commit is contained in:
Chase Tucker
2025-03-19 10:01:35 -07:00
parent 4871668a90
commit cc4781b990
45 changed files with 3082 additions and 1008 deletions

View File

@ -0,0 +1,95 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MesaFabApproval.Client.Services;
using MesaFabApproval.Shared.Models;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using Xunit;
namespace MesaFabApproval.Client.Test;
public class ApprovalServiceTests {
private readonly Mock<IMemoryCache> _cacheMock;
private readonly Mock<IHttpClientFactory> _httpClientFactoryMock;
private readonly ApprovalService _approvalService;
public ApprovalServiceTests() {
_cacheMock = new Mock<IMemoryCache>();
_httpClientFactoryMock = new Mock<IHttpClientFactory>();
_approvalService = new ApprovalService(_cacheMock.Object, _httpClientFactoryMock.Object);
}
[Fact]
public async Task DeleteApproval_ValidApprovalID_DeletesApproval() {
int approvalID = 1;
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
});
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _approvalService.DeleteApproval(approvalID);
handlerMock.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>()
);
}
[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;
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.BadRequest,
ReasonPhrase = "Bad Request"
});
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _approvalService.DeleteApproval(approvalID));
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.Extensions.Caching.Memory;
using Moq;
namespace MesaFabApproval.Client.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;
}
}

View File

@ -0,0 +1,320 @@
using System.Net;
using System.Text.Json;
using MesaFabApproval.Client.Services;
using MesaFabApproval.Shared.Models;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using MudBlazor;
namespace MesaFabApproval.Client.Test;
public class PCRBFollowUpCommentTests {
private readonly Mock<IMemoryCache> _mockCache;
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
private readonly Mock<ISnackbar> _mockSnackbar;
private readonly Mock<IUserService> _mockUserService;
private readonly PCRBService _pcrbService;
private static PCRBFollowUpComment FOLLOW_UP_COMMENT = new PCRBFollowUpComment {
ID = 1,
PlanNumber = 123,
FollowUpID = 1,
Comment = "Test Comment",
UserID = 1
};
private static HttpResponseMessage GET_RESPONSE = new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonSerializer.Serialize(new List<PCRBFollowUpComment> { FOLLOW_UP_COMMENT }))
};
private static IEnumerable<PCRBFollowUpComment> FOLLOW_UPS_COMMENTS = new List<PCRBFollowUpComment>() { FOLLOW_UP_COMMENT };
private static HttpResponseMessage SUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.OK);
private static HttpResponseMessage UNSUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.InternalServerError);
public PCRBFollowUpCommentTests() {
_mockCache = MockMemoryCacheService.GetMemoryCache(FOLLOW_UPS_COMMENTS);
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
_mockSnackbar = new Mock<ISnackbar>();
_mockUserService = new Mock<IUserService>();
_pcrbService = new PCRBService(
_mockCache.Object,
_mockHttpClientFactory.Object,
_mockSnackbar.Object,
_mockUserService.Object);
}
[Fact]
public async Task CreateFollowUpComment_WithValidParams_ShouldCallHttpPost_AndRefreshCache() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.CreateFollowUpComment(FOLLOW_UP_COMMENT);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Post &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComment")),
ItExpr.IsAny<CancellationToken>());
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComments?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task CreateFollowUpComment_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUpComment(FOLLOW_UP_COMMENT));
}
[Fact]
public async Task CreateFollowUpComment_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUpComment(null));
}
[Fact]
public async Task GetFollowUpCommentsByPlanNumber_WithBypassCache_ShouldCallHttpGetAndReturnFollowUps() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
IEnumerable<PCRBFollowUpComment> comments = await _pcrbService.GetFollowUpCommentsByPlanNumber(123, true);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComments?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
Assert.Single(comments);
}
[Fact]
public async Task GetFollowUpCommentsByPlanNumber_WithoutBypassCache_ShouldReturnFollowUpsFromCache() {
IEnumerable<PCRBFollowUpComment> comments = await _pcrbService.GetFollowUpCommentsByPlanNumber(1, false);
Assert.Single(comments);
}
[Fact]
public async Task GetFollowUpCommentsByPlanNumber_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.GetFollowUpCommentsByPlanNumber(1, true));
}
[Fact]
public async Task UpdateFollowUpComment_WithValidParams_ShouldCallHttpPut() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.UpdateFollowUpComment(FOLLOW_UP_COMMENT);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Put &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComment")),
ItExpr.IsAny<CancellationToken>());
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComments?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task UpdateFollowUpComment_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUpComment(null));
}
[Fact]
public async Task UpdateFollowUpComment_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUpComment(FOLLOW_UP_COMMENT));
}
[Fact]
public async Task DeleteFollowUpComment_WithValidParams_ShouldCallHttpDelete() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.DeleteFollowUpComment(1);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Delete &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUpComment?id=1")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task DeleteFollowUpComment_WithBadId_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.DeleteFollowUpComment(0));
}
[Fact]
public async Task DeleteFollowUpComment_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUpComment(1));
}
}

View File

@ -0,0 +1,321 @@
using System.Net;
using System.Text.Json;
using MesaFabApproval.Client.Services;
using MesaFabApproval.Shared.Models;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using MudBlazor;
namespace MesaFabApproval.Client.Test;
public partial class PCRBFollowUpTests {
private readonly Mock<IMemoryCache> _mockCache;
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
private readonly Mock<ISnackbar> _mockSnackbar;
private readonly Mock<IUserService> _mockUserService;
private readonly PCRBService _pcrbService;
private static PCRBFollowUp FOLLOW_UP = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now
};
private static HttpResponseMessage GET_RESPONSE = new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonSerializer.Serialize(new List<PCRBFollowUp> { FOLLOW_UP }))
};
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
new PCRBFollowUp { ID = 1, PlanNumber = 1, Step = 1, FollowUpDate = DateTime.Now }
};
private static HttpResponseMessage SUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.OK);
private static HttpResponseMessage UNSUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.InternalServerError);
public PCRBFollowUpTests() {
_mockCache = MockMemoryCacheService.GetMemoryCache(FOLLOW_UPS);
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
_mockSnackbar = new Mock<ISnackbar>();
_mockUserService = new Mock<IUserService>();
_pcrbService = new PCRBService(
_mockCache.Object,
_mockHttpClientFactory.Object,
_mockSnackbar.Object,
_mockUserService.Object);
}
[Fact]
public async Task CreateFollowUp_WithValidParams_ShouldCallHttpPost_AndRefreshCache() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.CreateFollowUp(FOLLOW_UP);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Post &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp")),
ItExpr.IsAny<CancellationToken>());
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUps?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task CreateFollowUp_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(FOLLOW_UP));
}
[Fact]
public async Task CreateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUp(null));
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithBypassCache_ShouldCallHttpGetAndReturnFollowUps() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
IEnumerable<PCRBFollowUp> followUps = await _pcrbService.GetFollowUpsByPlanNumber(123, true);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUps?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
Assert.Single(followUps);
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithoutBypassCache_ShouldReturnFollowUpsFromCache() {
IEnumerable<PCRBFollowUp> followUps = await _pcrbService.GetFollowUpsByPlanNumber(1, false);
Assert.Single(followUps);
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.GetFollowUpsByPlanNumber(1, true));
}
[Fact]
public async Task UpdateFollowUp_WithValidParams_ShouldCallHttpPut() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(GET_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.UpdateFollowUp(FOLLOW_UP);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Put &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp")),
ItExpr.IsAny<CancellationToken>());
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUps?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task UpdateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUp(null));
}
[Fact]
public async Task UpdateFollowUp_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(FOLLOW_UP));
}
[Fact]
public async Task DeleteFollowUp_WithValidParams_ShouldCallHttpDelete() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.DeleteFollowUp(1);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Delete &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp?id=1")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task DeleteFollowUp_WithBadId_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.DeleteFollowUp(0));
}
[Fact]
public async Task DeleteFollowUp_WithBadResponse_ShouldThrowException() {
Mock<HttpMessageHandler> mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUp(1));
}
}

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Text.Json;
using System.Net.Http;
using System.Threading.Tasks;
using MesaFabApproval.Client.Services;
using MesaFabApproval.Shared.Models;
@ -11,6 +12,8 @@ using Moq.Protected;
using MudBlazor;
using Xunit;
namespace MesaFabApproval.Client.Test;
public class PCRBServiceTests {
@ -18,330 +21,136 @@ public class PCRBServiceTests {
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
private readonly Mock<ISnackbar> _mockSnackbar;
private readonly Mock<IUserService> _mockUserService;
private readonly Mock<PCRB> _mockPCRB;
private readonly Mock<Approval> _mockApproval;
private readonly PCRBService _pcrbService;
private static IEnumerable<PCRBFollowUp> FOLLOW_UPS = new List<PCRBFollowUp>() {
new PCRBFollowUp { ID = 1, PlanNumber = 1, Step = 1, FollowUpDate = DateTime.Now }
};
private static HttpResponseMessage SUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.OK);
private static HttpResponseMessage UNSUCCESSFUL_RESPONSE = new HttpResponseMessage(HttpStatusCode.InternalServerError);
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 PCRBServiceTests() {
_mockCache = MockMemoryCacheService.GetMemoryCache(FOLLOW_UPS);
_mockCache = new Mock<IMemoryCache>();
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
_mockSnackbar = new Mock<ISnackbar>();
_mockUserService = new Mock<IUserService>();
_mockPCRB = new Mock<PCRB>();
_mockApproval = new Mock<Approval>();
_pcrbService = new PCRBService(
_mockCache.Object,
_mockHttpClientFactory.Object,
_mockSnackbar.Object,
_mockUserService.Object);
_mockUserService.Object
);
}
[Fact]
public async Task CreateFollowUp_WithValidParams_ShouldCallHttpPost_AndRefreshCache() {
PCRBFollowUp followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now,
Comments = "Test"
public async Task NotifyApprover_ShouldSendNotification() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
HttpResponseMessage getResponse = new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonSerializer.Serialize(new List<PCRBFollowUp> { followUp }))
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.OK
})
.Verifiable();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(getResponse)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.CreateFollowUp(followUp);
await _pcrbService.NotifyApprover(notification);
mockHttpMessageHandler.Protected().Verify(
handlerMock.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Post &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp")),
ItExpr.IsAny<CancellationToken>());
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUps?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post &&
req.RequestUri == new Uri("https://localhost:5000/pcrb/notify/approver")
),
ItExpr.IsAny<CancellationToken>()
);
}
[Fact]
public async Task CreateFollowUp_WithBadResponse_ShouldThrowException() {
PCRBFollowUp followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now,
Comments = "Test"
public async Task NotifyApprover_ShouldThrowException_WhenResponseIsNotSuccess() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.BadRequest,
ReasonPhrase = "Bad Request"
});
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.CreateFollowUp(followUp));
}
[Fact]
public async Task CreateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.CreateFollowUp(null));
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithBypassCache_ShouldCallHttpGetAndReturnFollowUps() {
PCRBFollowUp followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now,
Comments = "Test"
};
HttpResponseMessage getResponse = new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonSerializer.Serialize(new List<PCRBFollowUp> { followUp }))
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(getResponse)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
IEnumerable<PCRBFollowUp> followUps = await _pcrbService.GetFollowUpsByPlanNumber(123, true);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Get &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUps?planNumber=123&bypassCache=True")),
ItExpr.IsAny<CancellationToken>());
Assert.Single(followUps);
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithoutBypassCache_ShouldReturnFollowUpsFromCache() {
IEnumerable<PCRBFollowUp> followUps = await _pcrbService.GetFollowUpsByPlanNumber(1, false);
Assert.Single(followUps);
}
[Fact]
public async Task GetFollowUpsByPlanNumber_WithBadResponse_ShouldThrowException() {
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Get),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.GetFollowUpsByPlanNumber(1, true));
Exception exception = await Assert.ThrowsAsync<Exception>(() => _pcrbService.NotifyApprover(notification));
Assert.Equal("Unable to notify PCRB approver, because Bad Request", exception.Message);
}
[Fact]
public async Task UpdateFollowUp_WithValidParams_ShouldCallHttpPut() {
PCRBFollowUp followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now,
Comments = "Test"
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.UpdateFollowUp(followUp);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Put &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp")),
ItExpr.IsAny<CancellationToken>());
public async Task NotifyApprover_ShouldThrowException_WhenNotificationIsNull() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(null));
}
[Fact]
public async Task UpdateFollowUp_WithNullParam_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.UpdateFollowUp(null));
public async Task NotifyApprover_ShouldThrowException_WhenPCRBIsNull() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = null,
Approval = _mockApproval.Object
};
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
}
[Fact]
public async Task UpdateFollowUp_WithBadResponse_ShouldThrowException() {
PCRBFollowUp followUp = new PCRBFollowUp {
ID = 1,
PlanNumber = 123,
Step = 1,
FollowUpDate = DateTime.Now,
Comments = "Test"
public async Task NotifyApprover_ShouldThrowException_WhenApprovalIsNull() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = null
};
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Put),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<Exception>(() => _pcrbService.UpdateFollowUp(followUp));
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
}
[Fact]
public async Task DeleteFollowUp_WithValidParams_ShouldCallHttpDelete() {
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(SUCCESSFUL_RESPONSE)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.DeleteFollowUp(1);
mockHttpMessageHandler.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.Is<HttpRequestMessage>(
req =>
req.Method == HttpMethod.Delete &&
req.RequestUri != null &&
req.RequestUri.AbsoluteUri.Equals("https://localhost:5000/pcrb/followUp?id=1")),
ItExpr.IsAny<CancellationToken>());
}
[Fact]
public async Task DeleteFollowUp_WithBadId_ShouldThrowException() {
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.DeleteFollowUp(0));
}
[Fact]
public async Task DeleteFollowUp_WithBadResponse_ShouldThrowException() {
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Delete),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(UNSUCCESSFUL_RESPONSE)
.Verifiable();
var httpClient = new HttpClient(mockHttpMessageHandler.Object) {
BaseAddress = new Uri("https://localhost:5000")
public async Task NotifyApprover_ShouldThrowException_WhenMessageIsNullOrEmpty() {
PCRBNotification notification = new PCRBNotification {
Message = null,
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.NotifyApprover(notification));
await Assert.ThrowsAsync<Exception>(() => _pcrbService.DeleteFollowUp(1));
notification.Message = string.Empty;
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.NotifyApprover(notification));
}
}