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

157 lines
5.3 KiB
C#

using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using MesaFabApproval.Client.Services;
using MesaFabApproval.Shared.Models;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using MudBlazor;
using Xunit;
namespace MesaFabApproval.Client.Test;
public class PCRBServiceTests {
private readonly Mock<IMemoryCache> _mockCache;
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;
public PCRBServiceTests() {
_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
);
}
[Fact]
public async Task NotifyApprover_ShouldSendNotification() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
Mock<HttpMessageHandler> handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.OK
})
.Verifiable();
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
await _pcrbService.NotifyApprover(notification);
handlerMock.Protected().Verify(
"SendAsync",
Times.Once(),
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 NotifyApprover_ShouldThrowException_WhenResponseIsNotSuccess() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(_ => _.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.BadRequest,
ReasonPhrase = "Bad Request"
});
HttpClient httpClient = new HttpClient(handlerMock.Object) {
BaseAddress = new Uri("https://localhost:5000")
};
_mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(httpClient);
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 NotifyApprover_ShouldThrowException_WhenNotificationIsNull() {
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(null));
}
[Fact]
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 NotifyApprover_ShouldThrowException_WhenApprovalIsNull() {
PCRBNotification notification = new PCRBNotification {
Message = "Test Message",
PCRB = _mockPCRB.Object,
Approval = null
};
await Assert.ThrowsAsync<ArgumentNullException>(() => _pcrbService.NotifyApprover(notification));
}
[Fact]
public async Task NotifyApprover_ShouldThrowException_WhenMessageIsNullOrEmpty() {
PCRBNotification notification = new PCRBNotification {
Message = null,
PCRB = _mockPCRB.Object,
Approval = _mockApproval.Object
};
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.NotifyApprover(notification));
notification.Message = string.Empty;
await Assert.ThrowsAsync<ArgumentException>(() => _pcrbService.NotifyApprover(notification));
}
}