130 lines
5.1 KiB
C#
130 lines
5.1 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;
|
|
namespace LCU.Api.Internal.MaidBotTests.ServiceTests;
|
|
|
|
public class LogDistributionServiceTest
|
|
{
|
|
private LogDistributionService _logDistributionService;
|
|
private readonly Mock<IEventLogService> _mockEventLogService;
|
|
private readonly Mock<ILoggingMatrixService> _mockLoggingMatrixService;
|
|
private readonly Mock<INotificationsApiService> _notificationsApiService;
|
|
|
|
public LogDistributionServiceTest()
|
|
{
|
|
_mockEventLogService = new Mock<IEventLogService>();
|
|
_mockLoggingMatrixService = new Mock<ILoggingMatrixService>();
|
|
_notificationsApiService = new Mock<INotificationsApiService>();
|
|
|
|
_logDistributionService = new LogDistributionService(_mockEventLogService.Object,
|
|
_mockLoggingMatrixService.Object,
|
|
_notificationsApiService.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task HandleLogDistribution_NoEventLogs()
|
|
{
|
|
var eventLogs = new List<EventLog>();
|
|
_mockEventLogService.Setup(x => x.GetAsync()).ReturnsAsync(eventLogs);
|
|
|
|
await _logDistributionService.HandleLogDistribution();
|
|
|
|
_mockEventLogService.Verify(x => x.GetAsync(), Times.Once);
|
|
_mockLoggingMatrixService.Verify(x => x.GetActiveAsync(), Times.Never);
|
|
_notificationsApiService.Verify(x => x.SendNotificationAsync(It.IsAny<LoggingMatrix>(), It.IsAny<List<EventLog>>()), Times.Never);
|
|
_mockEventLogService.Verify(x => x.DeleteMany(It.IsAny<List<EventLog>>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task HandleLogDistribution_EventLogs_NoMatrix()
|
|
{
|
|
var eventLogs = new List<EventLog>
|
|
{
|
|
new EventLog
|
|
{
|
|
Application = "Test App",
|
|
Level = "Test Level",
|
|
Group = "Test Group",
|
|
LogTimeStamp = Convert.ToDateTime("1/1/2022"),
|
|
Message = "Test Message 1",
|
|
Environment = "Test",
|
|
DebugData = "Test Debug Data"
|
|
},
|
|
new EventLog
|
|
{
|
|
Application = "Test App",
|
|
Level = "Test Level",
|
|
Group = "Test Group",
|
|
LogTimeStamp = Convert.ToDateTime("1/1/2022"),
|
|
Message = "Test Message 2",
|
|
Environment = "Test",
|
|
DebugData = "Test Debug Data"
|
|
}
|
|
};
|
|
_mockEventLogService.Setup(x => x.GetAsync()).ReturnsAsync(eventLogs);
|
|
|
|
var loggingMatrixs = new List<LoggingMatrix>();
|
|
_mockLoggingMatrixService.Setup(x => x.GetActiveAsync()).ReturnsAsync(loggingMatrixs);
|
|
|
|
await _logDistributionService.HandleLogDistribution();
|
|
|
|
_mockEventLogService.Verify(x => x.GetAsync(), Times.Once);
|
|
_mockLoggingMatrixService.Verify(x => x.GetActiveAsync(), Times.Once);
|
|
_notificationsApiService.Verify(x => x.SendNotificationAsync(It.IsAny<LoggingMatrix>(), It.IsAny<List<EventLog>>()), Times.Never);
|
|
_mockEventLogService.Verify(x => x.DeleteMany(It.IsAny<List<EventLog>>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task HandleLogDistribution_EventLogs_LoggingMatrix_SendEmail()
|
|
{
|
|
var eventLogs = new List<EventLog>
|
|
{
|
|
new EventLog
|
|
{
|
|
Application = "Test App",
|
|
Level = "Test Level",
|
|
Group = "Test Group",
|
|
LogTimeStamp = Convert.ToDateTime("1/1/2022"),
|
|
Message = "Test Message 1",
|
|
Environment = "Test",
|
|
DebugData = "Test Debug Data"
|
|
},
|
|
new EventLog
|
|
{
|
|
Application = "Test App",
|
|
Level = "Test Level",
|
|
Group = "Test Group",
|
|
LogTimeStamp = Convert.ToDateTime("1/1/2022"),
|
|
Message = "Test Message 2",
|
|
Environment = "Test",
|
|
DebugData = "Test Debug Data"
|
|
}
|
|
};
|
|
_mockEventLogService.Setup(x => x.GetAsync()).ReturnsAsync(eventLogs);
|
|
|
|
var loggingMatrixs = 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.GetActiveAsync()).ReturnsAsync(loggingMatrixs);
|
|
|
|
await _logDistributionService.HandleLogDistribution();
|
|
|
|
_mockEventLogService.Verify(x => x.GetAsync(), Times.Once);
|
|
_mockLoggingMatrixService.Verify(x => x.GetActiveAsync(), Times.Once);
|
|
_notificationsApiService.Verify(x => x.SendNotificationAsync(It.IsAny<LoggingMatrix>(), It.IsAny<List<EventLog>>()), Times.Once);
|
|
_mockEventLogService.Verify(x => x.DeleteMany(It.IsAny<List<EventLog>>()), Times.Once);
|
|
}
|
|
} |