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 _mockEventLogRepository; private readonly Mock _mockDebugEventLogService; private readonly Mock _mockLoggingMatrixService; private readonly Mock _mockUnitOfWork; public EventLogServiceTest() { _mockEventLogRepository = new Mock(); _mockDebugEventLogService = new Mock(); _mockLoggingMatrixService = new Mock(); _mockUnitOfWork = new Mock(); _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 { new LoggingMatrix { LoggingMatrixId = 1, Application = "Test App", Level = "Test Level", Group = "Test Group", Email = "TestEmail@Landmarkcu.com", Disabled = false } }; _mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny())).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())).ReturnsAsync(eventLogResult); _mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny())); //Act await _eventLogService.HandleIncomingEventLog(testEventLog); //Assert _mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); _mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny()), 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(); _mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny())).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())).ReturnsAsync(eventLogResult); _mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny())); //Act await _eventLogService.HandleIncomingEventLog(testEventLog); //Assert _mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Never); _mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny()), 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(); _mockLoggingMatrixService.Setup(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny())).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())).ReturnsAsync(eventLogResult); _mockDebugEventLogService.Setup(x => x.DebugEventLog(It.IsAny())); //Act await _eventLogService.HandleIncomingEventLog(testEventLog); //Assert _mockLoggingMatrixService.Verify(x => x.GetByApplicationLevelAndGroupAsync(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockEventLogRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Never); _mockDebugEventLogService.Verify(x => x.DebugEventLog(It.IsAny()), Times.Once); } }