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 mockUnitOfWork; private Mock mockRegexMappingRepository; private RegexMappingService service; [SetUp] public void Setup() { mockUnitOfWork = new Mock(); mockRegexMappingRepository = new Mock(); service = new RegexMappingService( mockUnitOfWork.Object, mockRegexMappingRepository.Object); } [Test] public async Task Post_Success() { mockRegexMappingRepository.Setup(repository => repository.AddAsync(It.IsAny())) .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()), Times.Once); } [Test] public void Post_InvalidTag() { Assert.ThrowsAsync(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()), Times.Never); } [Test] public void Post_Exception() { mockRegexMappingRepository.Setup(repository => repository.AddAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(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()), Times.Once()); } [Test] public async Task GetByApplicationMappingId_Success() { mockRegexMappingRepository.Setup(repository => repository.FindByApplicationMappingIdAsync(It.IsAny())) .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()), Times.Once); } [Test] public void GetByApplicationMappingId_Exception() { mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await service.GetByApplicationMappingIdAsync(1)); mockRegexMappingRepository.Verify(x => x.FindByApplicationMappingIdAsync(It.IsAny()), Times.Once()); } [Test] public async Task GetByregexMappingId_Success() { mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny())) .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()), Times.Once); } [Test] public void GetByRegexMappingId_Exception() { mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.ThrowsAsync(async () => await service.GetByRegexMappingIdAsync(1)); mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny()), 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())) .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()), Times.Once()); mockUnitOfWork.Verify(x => x.Complete(), Times.Once); } [Test] public void Update_InvalidTag() { Assert.ThrowsAsync(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()), 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())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.Throws(() => service.Update(updatedRegex)); mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny()), Times.Once()); } [Test] public void Delete_Success() { mockRegexMappingRepository.Setup(repository => repository.FindByRegexMappingIdAsync(It.IsAny())) .ReturnsAsync(new RegexMapping() { RegexMappingId = 1, ApplicationMappingId = 1, Tag = "{Regex5}", Regex = "[0-9]" }) .Verifiable(); mockRegexMappingRepository.Setup(repository => repository.Remove(It.IsAny())) .Verifiable(); service.DeleteAsync(1); mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny()), 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())) .ThrowsAsync(new Exception("Test")).Verifiable(); Assert.Throws(() => service.Update(deleteRegex)); mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never); mockRegexMappingRepository.Verify(x => x.FindByRegexMappingIdAsync(It.IsAny()), Times.Once()); } } }