using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moq; using Xunit; using LCU.Api.Internal.MaidBotApi.Services; using LCU.Api.Internal.MaidBotLib.Models; using LCU.API.Internal.MaidBotApi.Controllers; namespace LCU.Api.Internal.MaidBotTests.ControllerTests; public class LoggingMatrixControllerTest { private readonly LoggingMatrixController _loggingMatrixController; private readonly Mock _mockLoggingMatrixService; public LoggingMatrixControllerTest() { _mockLoggingMatrixService = new Mock(); _loggingMatrixController = new LoggingMatrixController(_mockLoggingMatrixService.Object); } [Fact] public async void GetAllLoggingMatrix_Success_200() { // Arrange _mockLoggingMatrixService.Setup(service => service.GetAsync()).ReturnsAsync(new List { new LoggingMatrix(), new LoggingMatrix() }); // Act var result = await _loggingMatrixController.Get(); // Assert Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.NotNull(okResult.Value); var extractedValue = okResult.Value as IEnumerable; Assert.NotNull(extractedValue); Assert.Equal(2, extractedValue.Count()); } [Fact] public async void GetAllLoggingMatrix_BsaCorrectionServiceThrowsError_500() { // Arrange _mockLoggingMatrixService.Setup(service => service.GetAsync()).ThrowsAsync(new Exception()); // Act var result = await _loggingMatrixController.Get(); // Assert Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode); } [Fact] public async void PostLoggingMatrix_LoggingMatrixReturnsNull_BadRequest400() { var loggingMatrix = new LoggingMatrix() { }; _mockLoggingMatrixService.Setup(service => service.SaveAsync(loggingMatrix)).ReturnsAsync(() => null); // Act var result = await _loggingMatrixController.Post(loggingMatrix); // Assert Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.Equal(StatusCodes.Status400BadRequest, okResult.StatusCode); } [Fact] public async void PostLoggingMatrix_throwsException_500() { var loggingMatrix = new LoggingMatrix() { }; _mockLoggingMatrixService.Setup(service => service.SaveAsync(loggingMatrix)).Throws(new Exception("Exception from unit test")); // Act var result = await _loggingMatrixController.Post(loggingMatrix); // Assert Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode); } [Fact] public async void PutLoggingMatrix_BadRequestIdEqualZero_400() { var loggingMatrix = new LoggingMatrix { LoggingMatrixId = 0 }; // Act var result = await _loggingMatrixController.Put(loggingMatrix); // Assert Assert.NotNull(result); var returnedResult = result as BadRequestObjectResult; Assert.NotNull(returnedResult); Assert.Equal(StatusCodes.Status400BadRequest, returnedResult.StatusCode); } [Fact] public async void PutLoggingMatrix_ThrowsException_500() { var loggingMatrix = new LoggingMatrix { LoggingMatrixId = 1 }; _mockLoggingMatrixService.Setup(service => service.UpdateAsync(loggingMatrix)).Throws(new Exception("Exception from update unit test")); // Act var result = await _loggingMatrixController.Put(loggingMatrix); // Assert Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode); } }