using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Moq; using Xunit; using LCU.Api.Internal.MaidBotApi.Exceptions; using LCU.Api.Internal.MaidBotApi.Services; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Api.Internal.MaidBotLib.Repositories; using LCU.Library.Internal.Exceptions; namespace LCU.Api.Internal.MaidBotTests.ServiceTests; public class LoggingMatrixServiceTest { private readonly LoggingMatrixService _loggingMatrixService; private readonly Mock _mockLoggingMatrixRepository; public LoggingMatrixServiceTest() { var mockUnitOfWork = new Mock(); _mockLoggingMatrixRepository = new Mock(); _loggingMatrixService = new LoggingMatrixService(mockUnitOfWork.Object, _mockLoggingMatrixRepository.Object); } [Fact] public async Task GetAsync_Success() { // Arrange _mockLoggingMatrixRepository.Setup(repo => repo.ListAsync()).ReturnsAsync(new List() { new LoggingMatrix(), new LoggingMatrix() }); // Act var result = await _loggingMatrixService.GetAsync(); // Assert Assert.NotNull(result); Assert.Equal(2, result.Count()); } [Fact] public async Task GetAsync_Exception_Throws_ServerError() { // Arrange _mockLoggingMatrixRepository.Setup(repo => repo.ListAsync()).ThrowsAsync(new Exception()); // Act / Assert await Assert.ThrowsAsync(async () => await _loggingMatrixService.GetAsync()); } [Fact] public async Task SaveAsync_Success() { // Arrange _mockLoggingMatrixRepository.Setup(repo => repo.AddAsync(It.IsAny())).ReturnsAsync(new LoggingMatrix()); // Act var result = await _loggingMatrixService.SaveAsync(new LoggingMatrix()); // Assert Assert.NotNull(result); } [Fact] public async Task SaveAsync_Exception_Throws_ServerError() { // Arrange _mockLoggingMatrixRepository.Setup(repo => repo.AddAsync(It.IsAny())).ThrowsAsync(new Exception()); // Act / Assert await Assert.ThrowsAsync(async () => await _loggingMatrixService.SaveAsync(new LoggingMatrix())); } [Fact] public async Task UpdateAsync_Success_Updates_Fields() { // Arrange var testingLoggingMatrix = new LoggingMatrix() { LoggingMatrixId = 1, Application = "Test Application", Level = "Test Level", Group = "Test Group", Email = "TESTEmail@Landmarkcu.com", Disabled = false }; _mockLoggingMatrixRepository.Setup(repo => repo.FindByIdAsync(It.IsAny())).ReturnsAsync(testingLoggingMatrix); // Act var result = await _loggingMatrixService.UpdateAsync(testingLoggingMatrix); // Assert Assert.NotNull(result); Assert.Equal(testingLoggingMatrix.LoggingMatrixId, result.LoggingMatrixId); Assert.Equal(testingLoggingMatrix.Application, result.Application); Assert.Equal(testingLoggingMatrix.Level, result.Level); Assert.Equal(testingLoggingMatrix.Group, result.Group); Assert.Equal(testingLoggingMatrix.Email, result.Email); Assert.Equal(testingLoggingMatrix.Disabled, result.Disabled); } }