Adding MaidBotApi

This commit is contained in:
Zeeshaun
2026-03-11 13:21:47 -05:00
parent f3cb730f3b
commit f004c8205a
122 changed files with 4227 additions and 0 deletions
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
using LCU.Api.Internal.MaidBotApi.Services;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.API.Internal.MaidBotApi.Controllers;
namespace LCU.Api.Internal.MaidBotTests.ControllerTests;
public class LoggingMatrixControllerTest
{
private readonly LoggingMatrixController _loggingMatrixController;
private readonly Mock<ILoggingMatrixService> _mockLoggingMatrixService;
public LoggingMatrixControllerTest()
{
_mockLoggingMatrixService = new Mock<ILoggingMatrixService>();
_loggingMatrixController = new LoggingMatrixController(_mockLoggingMatrixService.Object);
}
[Fact]
public async void GetAllLoggingMatrix_Success_200()
{
// Arrange
_mockLoggingMatrixService.Setup(service => service.GetAsync()).ReturnsAsync(new List<LoggingMatrix>
{
new LoggingMatrix(),
new LoggingMatrix()
});
// Act
var result = await _loggingMatrixController.Get();
// Assert
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.NotNull(okResult.Value);
var extractedValue = okResult.Value as IEnumerable<LoggingMatrix>;
Assert.NotNull(extractedValue);
Assert.Equal(2, extractedValue.Count());
}
[Fact]
public async void GetAllLoggingMatrix_BsaCorrectionServiceThrowsError_500()
{
// Arrange
_mockLoggingMatrixService.Setup(service => service.GetAsync()).ThrowsAsync(new Exception());
// Act
var result = await _loggingMatrixController.Get();
// Assert
Assert.NotNull(result);
var okResult = result as StatusCodeResult;
Assert.NotNull(okResult);
Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode);
}
[Fact]
public async void PostLoggingMatrix_LoggingMatrixReturnsNull_BadRequest400()
{
var loggingMatrix = new LoggingMatrix() { };
_mockLoggingMatrixService.Setup(service => service.SaveAsync(loggingMatrix)).ReturnsAsync(() => null);
// Act
var result = await _loggingMatrixController.Post(loggingMatrix);
// Assert
Assert.NotNull(result);
var okResult = result as ObjectResult;
Assert.NotNull(okResult);
Assert.Equal(StatusCodes.Status400BadRequest, okResult.StatusCode);
}
[Fact]
public async void PostLoggingMatrix_throwsException_500()
{
var loggingMatrix = new LoggingMatrix() { };
_mockLoggingMatrixService.Setup(service => service.SaveAsync(loggingMatrix)).Throws(new Exception("Exception from unit test"));
// Act
var result = await _loggingMatrixController.Post(loggingMatrix);
// Assert
Assert.NotNull(result);
var okResult = result as StatusCodeResult;
Assert.NotNull(okResult);
Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode);
}
[Fact]
public async void PutLoggingMatrix_BadRequestIdEqualZero_400()
{
var loggingMatrix = new LoggingMatrix
{
LoggingMatrixId = 0
};
// Act
var result = await _loggingMatrixController.Put(loggingMatrix);
// Assert
Assert.NotNull(result);
var returnedResult = result as BadRequestObjectResult;
Assert.NotNull(returnedResult);
Assert.Equal(StatusCodes.Status400BadRequest, returnedResult.StatusCode);
}
[Fact]
public async void PutLoggingMatrix_ThrowsException_500()
{
var loggingMatrix = new LoggingMatrix
{
LoggingMatrixId = 1
};
_mockLoggingMatrixService.Setup(service => service.UpdateAsync(loggingMatrix)).Throws(new Exception("Exception from update unit test"));
// Act
var result = await _loggingMatrixController.Put(loggingMatrix);
// Assert
Assert.NotNull(result);
var okResult = result as StatusCodeResult;
Assert.NotNull(okResult);
Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode);
}
}
@@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
using LCU.Api.Internal.MaidBotApi.Services;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.API.Internal.MaidBotApi.Controllers;
namespace LCU.Api.Internal.MaidBotTests.ControllerTests;
public class QueueEventLogControllerTest
{
private readonly QueueEventLogController _queueEventLogController;
private readonly Mock<IQueueEventLogService> _mockQueueEventLogService;
public QueueEventLogControllerTest()
{
_mockQueueEventLogService = new Mock<IQueueEventLogService>();
_queueEventLogController = new QueueEventLogController(_mockQueueEventLogService.Object);
}
[Fact]
public async Task PostLoggingMatrix_throwsException_500()
{
var eventLog = new EventLog() { };
_mockQueueEventLogService.Setup(service => service.QueueEventLog(eventLog)).Throws(new Exception("Exception from unit test"));
// Act
var result = await _queueEventLogController.Post(eventLog);
// Assert
Assert.NotNull(result);
var okResult = result as StatusCodeResult;
Assert.NotNull(okResult);
Assert.Equal(StatusCodes.Status500InternalServerError, okResult.StatusCode);
}
}
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LCU.API.Internal.MaidBotApi\LCU.Api.Internal.MaidBotApi.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="RepositoryTests\LoggingMatrixTestData.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
@@ -0,0 +1,21 @@
using LCU.Api.Internal.MaidBotLib.Models;
using Xunit;
namespace LCU.Api.Internal.MaidBotTests.ModelTest
{
public class DebugEventLogTests
{
[Fact]
public void CreateDebugEventLog_RawLog_LengthToLong_TrimToMax()
{
// Arrange / Act
var testEventLog = new EventLog();
testEventLog.DebugData = "TESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETETESTTESTETSETETSETSETSETSETSETSETSETETESTSETSETSETSETSETESTESTESTSETSETSETSETSTESTETEETE";
var debugEventLog = new DebugEventLog(testEventLog);
// Assert
Assert.NotNull(debugEventLog);
Assert.Equal(DebugEventLog.DebugMaxLength, debugEventLog.RawLog.Length);
}
}
}
@@ -0,0 +1,70 @@
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Api.Internal.MaidBotLib.Persistence;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LCU.Api.Internal.MaidBotTests.RepositoryTests
{
public class DbContextTestSet
{
public IReadOnlyCollection<LoggingMatrix> LoggingMatrixSet { get; set; }
}
public abstract class DbContextFromJsonTest
{
public DbContextTestSet GetTestAccounts(string filePath)
{
var testSet = new DbContextTestSet();
// Get the absolute path to the JSON file
var path = Path.IsPathRooted(filePath)
? filePath
: Path.GetRelativePath(Directory.GetCurrentDirectory(), filePath);
if (!File.Exists(path))
{
throw new ArgumentException($"Could not find file at path: {path}");
}
var fileData = File.ReadAllText(filePath);
var allData = JObject.Parse(fileData);
var runInfo = allData["LoggingMatrixs"];
testSet.LoggingMatrixSet = runInfo?.ToObject<List<LoggingMatrix>>();
return testSet;
}
public AppDbContext GetBlankContext()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new AppDbContext(options);
}
public AppDbContext GetContextWithData(string jsonFileName)
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var context = new AppDbContext(options);
var testData = GetTestAccounts(jsonFileName);
AddTo(context.LoggingMatrixs, testData.LoggingMatrixSet);
context.SaveChanges();
return context;
}
private static void AddTo<T>(DbSet<T> collection, IEnumerable<T> range) where T : class
{
if (range != null) collection.AddRange(range);
}
}
}
@@ -0,0 +1,66 @@
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Api.Internal.MaidBotLib.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace LCU.Api.Internal.MaidBotTests.RepositoryTests
{
public class LoggingMatrixRepositoryTest : DbContextFromJsonTest
{
private readonly LoggingMatrixRepository _loggingMatrixRepository;
private const string JsonTestDataFilePath = "RepositoryTests\\LoggingMatrixTestData.json";
public LoggingMatrixRepositoryTest()
{
var dbContext = GetContextWithData(JsonTestDataFilePath);
_loggingMatrixRepository = new LoggingMatrixRepository(dbContext);
}
//TODO: These UnitTest have been commented out since there is version mismatch and at this time
// we are not going to undergo the work that is required to upgrade. The work to upgrade
// the .net version will need to be done in a separate Story:
// 89988 Maidbot API > Library Modernization
// https://landmarkcu.visualstudio.com/LMCU/_boards/board/t/Development/Stories?workitem=89988.
// This decision was made during this PR:
// https://landmarkcu.visualstudio.com/Platform/_git/LCU.API.Internal.MaidBotApi/pullrequest/17634
//[Fact]
//public void ListAsync_Test()
//{
// var loggingMatrixes = _loggingMatrixRepository.ListAsync().Result;
// Assert.NotNull(loggingMatrixes);
// Assert.Equal(6, loggingMatrixes.Count());
//}
//[Fact]
//public async void FindById_Test()
//{
// var loggingMatrix = await _loggingMatrixRepository.FindByIdAsync(1);
// Assert.NotNull(loggingMatrix);
// Assert.Equal(1, loggingMatrix.LoggingMatrixId);
//}
//[Fact]
//public void RetrieveByApplicationLevelAndGroup_Test()
//{
// var application = "Test App 1";
// var group = "Test Group 1";
// var level = "Test Level 1";
// var loggingMatrixes = _loggingMatrixRepository.RetrieveByApplicationLevelAndGroup(application, level, group ).Result;
// Assert.NotNull(loggingMatrixes);
// Assert.Equal(2, loggingMatrixes.Count());
//}
//[Fact]
//public void RetrieveByApplciationAndLevel_Test()
//{
// var application = "Test App 1";
// var level = "Test Level 1";
// var loggingMatrixes = _loggingMatrixRepository.RetrieveByApplciationAndLevel(application, level).Result;
// Assert.NotNull(loggingMatrixes);
// Assert.Equal(3, loggingMatrixes.Count());
//}
}
}
@@ -0,0 +1,52 @@
{
"LoggingMatrixs": [
{
"LoggingmatrixId": 1,
"Application": "Test App 1",
"Level": "Test Level 1",
"Group": "Test Group 1",
"Email": "[email protected]",
"Disabled": false
},
{
"LoggingmatrixId": 2,
"Application": "Test App 2",
"Level": "Test Level 2",
"Group": "Test Group 2",
"Email": "[email protected]",
"Disabled": false
},
{
"LoggingmatrixId": 3,
"Application": "Test App 3",
"Level": "Test Level 3",
"Group": "Test Group 3",
"Email": "[email protected]",
"Disabled": true
},
{
"LoggingmatrixId": 4,
"Application": "Test App 1",
"Level": "Test Level 1",
"Group": "Test Group 1",
"Email": "[email protected]",
"Disabled": false
},
{
"LoggingmatrixId": 5,
"Application": "Test App 1",
"Level": "Test Level 1",
"Group": "Test Group 1",
"Email": "[email protected]",
"Disabled": true
},
{
"LoggingmatrixId": 6,
"Application": "Test App 1",
"Level": "Test Level 1",
"Group": "Test Group 6",
"Email": "[email protected]",
"Disabled": false
}
]
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- additional settings used during the dotnet test execution step in the azure dev ops pipeline -->
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<CompanyNames>
<Exclude>
<CompanyName>.*microsoft.*</CompanyName>
</Exclude>
</CompanyNames>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>