Files
mesa-fab-approval/MesaFabApproval.Client.Test/PCRBFollowUpCommentTests.cs
2025-06-04 10:26:48 -07:00

321 lines
13 KiB
C#

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));
}
}