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 _mockCache; private readonly Mock _mockHttpClientFactory; private readonly Mock _mockSnackbar; private readonly Mock _mockUserService; private readonly Mock _mockPCRB; private readonly Mock _mockApproval; private readonly PCRBService _pcrbService; public PCRBServiceTests() { _mockCache = new Mock(); _mockHttpClientFactory = new Mock(); _mockSnackbar = new Mock(); _mockUserService = new Mock(); _mockPCRB = new Mock(); _mockApproval = new Mock(); _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 handlerMock = new Mock(); handlerMock .Protected() .Setup>( "SendAsync", ItExpr.Is(_ => _.Method == HttpMethod.Post), ItExpr.IsAny() ) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK }) .Verifiable(); HttpClient httpClient = new HttpClient(handlerMock.Object) { BaseAddress = new Uri("https://localhost:5000") }; _mockHttpClientFactory.Setup(_ => _.CreateClient(It.IsAny())).Returns(httpClient); await _pcrbService.NotifyApprover(notification); handlerMock.Protected().Verify( "SendAsync", Times.Once(), ItExpr.Is(req => req.Method == HttpMethod.Post && req.RequestUri == new Uri("https://localhost:5000/pcrb/notify/approver") ), ItExpr.IsAny() ); } [Fact] public async Task NotifyApprover_ShouldThrowException_WhenResponseIsNotSuccess() { PCRBNotification notification = new PCRBNotification { Message = "Test Message", PCRB = _mockPCRB.Object, Approval = _mockApproval.Object }; var handlerMock = new Mock(); handlerMock .Protected() .Setup>( "SendAsync", ItExpr.Is(_ => _.Method == HttpMethod.Post), ItExpr.IsAny() ) .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())).Returns(httpClient); Exception exception = await Assert.ThrowsAsync(() => _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(() => _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(() => _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(() => _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(() => _pcrbService.NotifyApprover(notification)); notification.Message = string.Empty; await Assert.ThrowsAsync(() => _pcrbService.NotifyApprover(notification)); } }