Adding MaidBotApi
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
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 DebugEventLogServiceTest
|
||||
{
|
||||
private readonly DebugEventLogService _debugEventLogService;
|
||||
private readonly Mock<IDebugEventLogRepository> _mockDebugEventLogRepository;
|
||||
|
||||
public DebugEventLogServiceTest()
|
||||
{
|
||||
var mockUnitOfWork = new Mock<IUnitOfWork>();
|
||||
_mockDebugEventLogRepository = new Mock<IDebugEventLogRepository>();
|
||||
_debugEventLogService = new DebugEventLogService(mockUnitOfWork.Object, _mockDebugEventLogRepository.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveAsync_Success()
|
||||
{
|
||||
// Arrange
|
||||
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ReturnsAsync(new DebugEventLog());
|
||||
|
||||
// Act
|
||||
var result = await _debugEventLogService.SaveAsync(new DebugEventLog());
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveAsync_Exception_Throws_ServerError()
|
||||
{
|
||||
// Arrange
|
||||
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ThrowsAsync(new Exception());
|
||||
|
||||
// Act / Assert
|
||||
await Assert.ThrowsAsync<ServiceException>(async () =>
|
||||
await _debugEventLogService.SaveAsync(new DebugEventLog()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DebugEventLog_Verify_AddAsyncCalled_Once()
|
||||
{
|
||||
// Arrange
|
||||
var testEventLog = new EventLog()
|
||||
{
|
||||
DebugData = "Test Debug Data"
|
||||
};
|
||||
var testDebugEventLog = new DebugEventLog()
|
||||
{
|
||||
RawLog = "Test Debug Data"
|
||||
};
|
||||
|
||||
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(testDebugEventLog)).ReturnsAsync(testDebugEventLog);
|
||||
|
||||
//Act
|
||||
await _debugEventLogService.DebugEventLog(testEventLog);
|
||||
|
||||
//Assert
|
||||
_mockDebugEventLogRepository.Verify(x => x.AddAsync(It.IsAny<DebugEventLog>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DebugEventLog_Verify_AddAsyncCalled_Never()
|
||||
{
|
||||
// Arrange
|
||||
var testEventLog = new EventLog() { };
|
||||
_mockDebugEventLogRepository.Setup(repo => repo.AddAsync(It.IsAny<DebugEventLog>())).ReturnsAsync(new DebugEventLog());
|
||||
var testDebugEventLog = new DebugEventLog();
|
||||
|
||||
//Act
|
||||
await _debugEventLogService.DebugEventLog(testEventLog);
|
||||
|
||||
//Assert
|
||||
_mockDebugEventLogRepository.Verify(x => x.AddAsync(testDebugEventLog), Times.Never);
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user