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 _mockEventLogService; private readonly Mock _mockLoggingMatrixService; private readonly Mock _notificationsApiService; public LogDistributionServiceTest() { _mockEventLogService = new Mock(); _mockLoggingMatrixService = new Mock(); _notificationsApiService = new Mock(); _logDistributionService = new LogDistributionService(_mockEventLogService.Object, _mockLoggingMatrixService.Object, _notificationsApiService.Object); } [Fact] public async Task HandleLogDistribution_NoEventLogs() { var eventLogs = new List(); _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(), It.IsAny>()), Times.Never); _mockEventLogService.Verify(x => x.DeleteMany(It.IsAny>()), Times.Never); } [Fact] public async Task HandleLogDistribution_EventLogs_NoMatrix() { var eventLogs = new List { 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(); _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(), It.IsAny>()), Times.Never); _mockEventLogService.Verify(x => x.DeleteMany(It.IsAny>()), Times.Never); } [Fact] public async Task HandleLogDistribution_EventLogs_LoggingMatrix_SendEmail() { var eventLogs = new List { 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 { new LoggingMatrix { LoggingMatrixId = 1, Application = "Test App", Level = "Test Level", Group = "Test Group", Email = "TestEmail@Landmarkcu.com", 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(), It.IsAny>()), Times.Once); _mockEventLogService.Verify(x => x.DeleteMany(It.IsAny>()), Times.Once); } }