Files
monorepo/LCU.API.Internal.MaidBotApi/LCU.Api.Internal.MaidBotTests/ServiceTests/EventLogServiceTest.cs
T
2026-03-11 13:21:47 -05:00

170 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Xunit;
using LCU.Api.Internal.MaidBotApi.Services;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Api.Internal.MaidBotLib.Repositories;
namespace LCU.Api.Internal.MaidBotTests.ServiceTests;
public class EventLogServiceTest
{
private EventLogService _eventLogService;
private readonly Mock<IEventLogRepository> _mockEventLogRepository;
private readonly Mock<IDebugEventLogService> _mockDebugEventLogService;
private readonly Mock<ILoggingMatrixService> _mockLoggingMatrixService;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
public EventLogServiceTest()
{
_mockEventLogRepository = new Mock<IEventLogRepository>();
_mockDebugEventLogService = new Mock<IDebugEventLogService>();
_mockLoggingMatrixService = new Mock<ILoggingMatrixService>();
_mockUnitOfWork = new Mock<IUnitOfWork>();
_eventLogService = new EventLogService(_mockEventLogRepository.Object,
_mockDebugEventLogService.Object,
_mockLoggingMatrixService.Object,
_mockUnitOfWork.Object);
}
[Fact]
public async Task HandleEventLog_AllMethods_Called_Once()
{
// Arrange
var testEventLog = new EventLog
{
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = "Test Debug Data"
};
var testLoggingMatrix = new List<LoggingMatrix>
{
new LoggingMatrix
{
LoggingMatrixId = 1,
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
Email = "[email protected]",
Disabled = false
}
};
_mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(testLoggingMatrix);
var eventLogResult = new EventLog
{
EventLogId = 1,
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = "Test Debug Data"
};
_mockEventLogRepository.Setup(x => x.AddAsync(It.IsAny<EventLog>())).ReturnsAsync(eventLogResult);
_mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny<EventLog>()));
//Act
await _eventLogService.HandleIncomingEventLog(testEventLog);
//Assert
_mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny<EventLog>()), Times.Once);
_mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny<EventLog>()), Times.Once);
}
[Fact]
public async Task HandleEventLog_NoMatrixFound_EventLogNotSaved()
{
// Arrange
var testEventLog = new EventLog()
{
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = null
};
var testLoggingMatrix = new List<LoggingMatrix>();
_mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>())).ReturnsAsync(testLoggingMatrix);
var eventLogResult = new EventLog
{
EventLogId = 1,
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = null
};
_mockEventLogRepository.Setup(x => x.AddAsync(It.IsAny<EventLog>())).ReturnsAsync(eventLogResult);
_mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny<EventLog>()));
//Act
await _eventLogService.HandleIncomingEventLog(testEventLog);
//Assert
_mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny<EventLog>()), Times.Never);
_mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny<EventLog>()), Times.Once);
}
[Fact]
public async Task HandleEventLog_NoMatrixFound_EventLogNotSaved_DebugSaved()
{
// Arrange
var testEventLog = new EventLog()
{
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = "Test Debug Data"
};
var testLoggingMatrix = new List<LoggingMatrix>();
_mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>())).ReturnsAsync(testLoggingMatrix);
var eventLogResult = new EventLog
{
EventLogId = 1,
Application = "Test App",
Level = "Test Level",
Group = "Test Group",
LogTimeStamp = Convert.ToDateTime("1/1/2020"),
Message = "Test Message",
Environment = "Test",
DebugData = "Test Debug Data"
};
_mockEventLogRepository.Setup(x => x.AddAsync(It.IsAny<EventLog>())).ReturnsAsync(eventLogResult);
_mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny<EventLog>()));
//Act
await _eventLogService.HandleIncomingEventLog(testEventLog);
//Assert
_mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
_mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny<EventLog>()), Times.Never);
_mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny<EventLog>()), Times.Once);
}
}