using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestorLib.Repositories; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.Exceptions; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace LCU.API.Internal.DocumentIngestorTests.Services; public class OsgPreprocessorInfoServiceTest { private OsgPreprocessorInfoService _osgPreprocessorInfoService; private Mock _unitOfWork; private Mock _mockOsgPreprocessorInfoRepository; [SetUp] public void Setup() { _unitOfWork = new Mock(); _mockOsgPreprocessorInfoRepository = new Mock(); _osgPreprocessorInfoService = new OsgPreprocessorInfoService(_unitOfWork.Object, _mockOsgPreprocessorInfoRepository.Object); } [Test] public async Task GetAsync_OsgPreprocessorInfos_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindAsync()).ReturnsAsync(osgPreprocessorInfos); var result = await _osgPreprocessorInfoService.GetAsync(); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public void GetAsync_Exception() { _mockOsgPreprocessorInfoRepository.Setup(x => x.FindAsync()).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.GetAsync()); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindAsync(), Times.Once); } [Test] public async Task GetByIdAsync_OsgPreprocessorInfo_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfo); var result = await _osgPreprocessorInfoService.GetByIdAsync(osgPreprocessorInfoId); Assert.IsNotNull(result); Assert.AreEqual(osgPreprocessorInfoId, result.OsgPreprocessorInfoId); Assert.AreEqual(documentType, result.DocumentType); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public void GetByIdAsync_Exception() { var id = 1; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.GetByIdAsync(id)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); } [Test] public async Task GetByDocumentTypeAsync_OsgPreprocessorInfos_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var entityName = "John Smith"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, EntityName = entityName, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByDocumentType(documentType)).ReturnsAsync(osgPreprocessorInfos); var result = await _osgPreprocessorInfoService.GetByDocumentTypeAsync(documentType); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByDocumentType(It.IsAny()), Times.Once); } [Test] public void GetByDocumentTypeAsync_Exception() { var documentType = "Statement"; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByDocumentType(documentType)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.GetByDocumentTypeAsync(documentType)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByDocumentType(It.IsAny()), Times.Once); } [Test] public async Task GetByMemberNumberAsync_OsgPreprocessorInfos_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var memberNumber = "123"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, MemberNumber = memberNumber, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByMemberNumber(memberNumber)).ReturnsAsync(osgPreprocessorInfos); var result = await _osgPreprocessorInfoService.GetByMemberNumberAsync(memberNumber); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByMemberNumber(It.IsAny()), Times.Once); } [Test] public void GetByMemberNumberAsync_Exception() { var memberNumber = "123"; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByMemberNumber(memberNumber)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.GetByMemberNumberAsync(memberNumber)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByMemberNumber(It.IsAny()), Times.Once); } [Test] public async Task SaveAsync_OsgPreprocessorInfo_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.AddAsync(osgPreprocessorInfo)).ReturnsAsync(osgPreprocessorInfo); var result = await _osgPreprocessorInfoService.SaveAsync(osgPreprocessorInfo); Assert.IsNotNull(result); Assert.AreEqual(osgPreprocessorInfoId, result.OsgPreprocessorInfoId); Assert.AreEqual(documentType, result.DocumentType); _mockOsgPreprocessorInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public void SaveAsync_Exception() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.AddAsync(osgPreprocessorInfo)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.SaveAsync(osgPreprocessorInfo)); _mockOsgPreprocessorInfoRepository.Verify(x => x.AddAsync(It.IsAny()), Times.Once); } [Test] public async Task Update_OsgPreprocessorInfo_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfo); _mockOsgPreprocessorInfoRepository.Setup(x => x.Update(osgPreprocessorInfo)); var result = await _osgPreprocessorInfoService.Update(osgPreprocessorInfo); Assert.IsNotNull(result); Assert.AreEqual(documentType, result.DocumentType); Assert.AreEqual(osgPreprocessorInfoId, result.OsgPreprocessorInfoId); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public void Update_FindByIdAsync_ThrowsException() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.Update(osgPreprocessorInfo)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Never); } [Test] public void Update_Update_ThrowsException() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfo); _mockOsgPreprocessorInfoRepository.Setup(x => x.Update(osgPreprocessorInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.Update(osgPreprocessorInfo)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Update(It.IsAny()), Times.Once); } [Test] public async Task UpdateRange_OsgPreprocessorInfo_Returned() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var entityName = "John Smith"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, EntityName = entityName, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfos[0]); _mockOsgPreprocessorInfoRepository.Setup(x => x.UpdateRange(osgPreprocessorInfos)); var result = await _osgPreprocessorInfoService.UpdateRange(osgPreprocessorInfos); Assert.IsNotNull(result); Assert.AreEqual(documentType, result.FirstOrDefault()?.DocumentType); Assert.AreEqual(osgPreprocessorInfoId, result.FirstOrDefault()?.OsgPreprocessorInfoId); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.UpdateRange(It.IsAny>()), Times.Once); } [Test] public void UpdateRange_FindByIdAsync_ThrowsException() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var entityName = "John Smith"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, EntityName = entityName, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.UpdateRange(osgPreprocessorInfos)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.UpdateRange(It.IsAny>()), Times.Never); } [Test] public void UpdateRange_Update_ThrowsException() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var entityName = "John Smith"; var osgPreprocessorInfos = new List() { new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, EntityName = entityName, CreatedDate = Convert.ToDateTime("1/1/2022") } }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfos[0]); _mockOsgPreprocessorInfoRepository.Setup(x => x.UpdateRange(osgPreprocessorInfos)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.UpdateRange(osgPreprocessorInfos)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.UpdateRange(It.IsAny>()), Times.Once); } [Test] public async Task Delete_Success() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfo); _mockOsgPreprocessorInfoRepository.Setup(x => x.Remove(osgPreprocessorInfo)); await _osgPreprocessorInfoService.Delete(osgPreprocessorInfoId); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } [Test] public void Delete_FindByIdAsync_ThrowsException() { var id = 1; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(id)).ThrowsAsync(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.Delete(id)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Never); } [Test] public void Delete_Remove_ThrowsException() { var osgPreprocessorInfoId = 10; var documentType = "Statement"; var osgPreprocessorInfo = new OsgPreprocessorInfo() { OsgPreprocessorInfoId = osgPreprocessorInfoId, DocumentType = documentType, CreatedDate = Convert.ToDateTime("1/1/2022") }; _mockOsgPreprocessorInfoRepository.Setup(x => x.FindByIdAsync(osgPreprocessorInfoId)).ReturnsAsync(osgPreprocessorInfo); _mockOsgPreprocessorInfoRepository.Setup(x => x.Remove(osgPreprocessorInfo)).Throws(new Exception("Error")); Assert.ThrowsAsync(async () => await _osgPreprocessorInfoService.Delete(osgPreprocessorInfoId)); _mockOsgPreprocessorInfoRepository.Verify(x => x.FindByIdAsync(It.IsAny()), Times.Once); _mockOsgPreprocessorInfoRepository.Verify(x => x.Remove(It.IsAny()), Times.Once); } }