106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
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<ILoggingMatrixRepository> _mockLoggingMatrixRepository;
|
|
|
|
public LoggingMatrixServiceTest()
|
|
{
|
|
var mockUnitOfWork = new Mock<IUnitOfWork>();
|
|
_mockLoggingMatrixRepository = new Mock<ILoggingMatrixRepository>();
|
|
_loggingMatrixService = new LoggingMatrixService(mockUnitOfWork.Object, _mockLoggingMatrixRepository.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAsync_Success()
|
|
{
|
|
// Arrange
|
|
_mockLoggingMatrixRepository.Setup(repo => repo.ListAsync()).ReturnsAsync(new List<LoggingMatrix>()
|
|
{
|
|
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<ServiceException>(async () => await _loggingMatrixService.GetAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAsync_Success()
|
|
{
|
|
// Arrange
|
|
_mockLoggingMatrixRepository.Setup(repo => repo.AddAsync(It.IsAny<LoggingMatrix>())).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<LoggingMatrix>())).ThrowsAsync(new Exception());
|
|
|
|
// Act / Assert
|
|
await Assert.ThrowsAsync<ServiceException>(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 = "[email protected]",
|
|
Disabled = false
|
|
};
|
|
_mockLoggingMatrixRepository.Setup(repo => repo.FindByIdAsync(It.IsAny<int>())).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);
|
|
}
|
|
} |