281 lines
10 KiB
C#
281 lines
10 KiB
C#
using LCU.Api.Internal.ArchiverApi.Services;
|
|
using LCU.Library.Internal.ArchiverApiLib.Repositories;
|
|
using LCU.Library.Internal.ArchiverApiModel.Entities;
|
|
using LCU.Library.Internal.Exceptions;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LCU.Api.Internal.ArchiverApiTest.Services
|
|
{
|
|
public class RegexMappingServiceTest
|
|
{
|
|
private Mock<IUnitOfWork> mockUnitOfWork;
|
|
private Mock<IRegexMappingRepository> mockRegexMappingRepository;
|
|
|
|
private RegexMappingService service;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
mockUnitOfWork = new Mock<IUnitOfWork>();
|
|
mockRegexMappingRepository = new Mock<IRegexMappingRepository>();
|
|
|
|
service = new RegexMappingService(
|
|
mockUnitOfWork.Object, mockRegexMappingRepository.Object);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_Success()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.AddAsync(It.IsAny<RegexMapping>()))
|
|
.ReturnsAsync(new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
})
|
|
.Verifiable();
|
|
|
|
var regexMappingResponse = await service.SaveAsync(new RegexMapping()
|
|
{
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
});
|
|
|
|
Assert.NotNull(regexMappingResponse);
|
|
|
|
Assert.NotZero(regexMappingResponse.RegexMappingId);
|
|
Assert.AreEqual(1, regexMappingResponse.ApplicationMappingId);
|
|
Assert.AreEqual("{Regex5}", regexMappingResponse.Tag);
|
|
Assert.AreEqual("[0-9]", regexMappingResponse.Regex);
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
|
|
mockRegexMappingRepository.Verify(x => x.AddAsync(It.IsAny<RegexMapping>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void Post_InvalidTag()
|
|
{
|
|
Assert.ThrowsAsync<ServiceException>(async () =>
|
|
await service.SaveAsync(new RegexMapping()
|
|
{
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex1}",
|
|
Regex = "[0-9]"
|
|
}));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.AddAsync(It.IsAny<RegexMapping>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void Post_Exception()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.AddAsync(It.IsAny<RegexMapping>()))
|
|
.ThrowsAsync(new Exception("Test")).Verifiable();
|
|
|
|
Assert.ThrowsAsync<ServiceException>(async () =>
|
|
await service.SaveAsync(new RegexMapping()
|
|
{
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
}));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.AddAsync(It.IsAny<RegexMapping>()), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetByApplicationMappingId_Success()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByApplicationMappingIdAsync(It.IsAny<int>()))
|
|
.ReturnsAsync(new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
});
|
|
|
|
var regexMappingResponse = await service.GetByApplicationMappingIdAsync(1);
|
|
|
|
Assert.NotNull(regexMappingResponse);
|
|
|
|
Assert.NotZero(regexMappingResponse.RegexMappingId);
|
|
Assert.AreEqual(1, regexMappingResponse.ApplicationMappingId);
|
|
Assert.AreEqual("{Regex5}", regexMappingResponse.Tag);
|
|
Assert.AreEqual("[0-9]", regexMappingResponse.Regex);
|
|
|
|
mockRegexMappingRepository.Verify(x => x.FindByApplicationMappingIdAsync(It.IsAny<int>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetByApplicationMappingId_Exception()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Test")).Verifiable();
|
|
|
|
Assert.ThrowsAsync<ServiceException>(async () =>
|
|
await service.GetByApplicationMappingIdAsync(1));
|
|
|
|
mockRegexMappingRepository.Verify(x => x.FindByApplicationMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetByregexMappingId_Success()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ReturnsAsync(new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
});
|
|
|
|
var regexMappingResponse = await service.GetByRegexMappingIdAsync(1);
|
|
|
|
Assert.NotNull(regexMappingResponse);
|
|
|
|
Assert.NotZero(regexMappingResponse.RegexMappingId);
|
|
Assert.AreEqual(1, regexMappingResponse.ApplicationMappingId);
|
|
Assert.AreEqual("{Regex5}", regexMappingResponse.Tag);
|
|
Assert.AreEqual("[0-9]", regexMappingResponse.Regex);
|
|
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetByRegexMappingId_Exception()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Test")).Verifiable();
|
|
|
|
Assert.ThrowsAsync<ServiceException>(async () =>
|
|
await service.GetByRegexMappingIdAsync(1));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void Update_Success()
|
|
{
|
|
var originalRegex = new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
};
|
|
|
|
var updatedRegex = new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 2,
|
|
Tag = "{Regex50}",
|
|
Regex = "[0-9]*"
|
|
};
|
|
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ReturnsAsync((RegexMapping)originalRegex).Verifiable();
|
|
|
|
var regexMappingResponse = service.Update(updatedRegex);
|
|
|
|
Assert.NotNull(regexMappingResponse);
|
|
|
|
Assert.AreEqual(regexMappingResponse.RegexMappingId, originalRegex.RegexMappingId);
|
|
Assert.AreEqual(regexMappingResponse.ApplicationMappingId, updatedRegex.ApplicationMappingId);
|
|
Assert.AreEqual(regexMappingResponse.Tag, updatedRegex.Tag);
|
|
Assert.AreEqual(regexMappingResponse.Regex, updatedRegex.Regex);
|
|
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
mockUnitOfWork.Verify(x => x.Complete(), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void Update_InvalidTag()
|
|
{
|
|
Assert.ThrowsAsync<ServiceException>(async () =>
|
|
await service.SaveAsync(new RegexMapping()
|
|
{
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex1}",
|
|
Regex = "[0-9]"
|
|
}));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.AddAsync(It.IsAny<RegexMapping>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void Update_Exception()
|
|
{
|
|
var updatedRegex = new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 2,
|
|
Tag = "{Regex50}",
|
|
Regex = "[0-9]*"
|
|
};
|
|
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Test")).Verifiable();
|
|
|
|
Assert.Throws<ServiceException>(() =>
|
|
service.Update(updatedRegex));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void Delete_Success()
|
|
{
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ReturnsAsync(new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 1,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]"
|
|
})
|
|
.Verifiable();
|
|
|
|
mockRegexMappingRepository.Setup(repository => repository.Remove(It.IsAny<RegexMapping>()))
|
|
.Verifiable();
|
|
|
|
service.DeleteAsync(1);
|
|
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
mockUnitOfWork.Verify(x => x.Complete(), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void Delete_Exception()
|
|
{
|
|
var deleteRegex = new RegexMapping()
|
|
{
|
|
RegexMappingId = 1,
|
|
ApplicationMappingId = 2,
|
|
Tag = "{Regex5}",
|
|
Regex = "[0-9]*"
|
|
};
|
|
|
|
mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("Test")).Verifiable();
|
|
|
|
Assert.Throws<ServiceException>(() =>
|
|
service.Update(deleteRegex));
|
|
|
|
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
|
|
mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny<int>()), Times.Once());
|
|
}
|
|
}
|
|
}
|