using System; using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; using LCU.Library.Internal.Exceptions; using LCU.API.Internal.DocumentIngestor.Controllers; using LCU.API.Internal.DocumentIngestor.Services; using LCU.Library.Internal.DocumentIngestorModels.Entities; namespace LCU.API.Internal.DocumentIngestorTests.Controllers; public class PdfPartitionerLogControllerTest { private PdfPartitionerLogController _processingLogController; private Mock _mockPdfPartitionerLogService; [SetUp] public void Setup() { _mockPdfPartitionerLogService = new Mock(); _processingLogController = new PdfPartitionerLogController(_mockPdfPartitionerLogService.Object); } [Test] public async Task Get_OK() { var pdfPartitionerLogId = 1; var pdfPartitionerRunInfoId = Guid.NewGuid(); var PdfPartitionerLogs = new List { new PdfPartitionerLog { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId } }; _mockPdfPartitionerLogService.Setup(service => service.GetAsync()) .ReturnsAsync(PdfPartitionerLogs).Verifiable(); var result = await _processingLogController.Get(); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Get_ServiceException_InternalServerError() { _mockPdfPartitionerLogService.Setup(service => service.GetAsync()) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Get(); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Get_UnexpectedException_InternalServerError() { _mockPdfPartitionerLogService.Setup(service => service.GetAsync()) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Get(); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetById_OK() { var id = 1; var pdfPartitionerLog = new PdfPartitionerLog { PdfPartitionerLogId = id, PdfPartitionerRunInfoId = Guid.NewGuid() }; _mockPdfPartitionerLogService.Setup(service => service.GetByIdAsync(id)) .ReturnsAsync(pdfPartitionerLog).Verifiable(); var result = await _processingLogController.GetById(id); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetById_ServiceException_InternalServerError() { var id = 1; _mockPdfPartitionerLogService.Setup(service => service.GetByIdAsync(id)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetById(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetById_UnexpectedException_InternalServerError() { var id = 1; _mockPdfPartitionerLogService.Setup(service => service.GetByIdAsync(id)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetById(id); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetByPdfPartitionerRunInfoIdAsync_OK() { var pdfPartitionerLogId = 1; var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLogs = new List { new PdfPartitionerLog { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId } }; _mockPdfPartitionerLogService.Setup(service => service.GetByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)) .ReturnsAsync(pdfPartitionerLogs).Verifiable(); var result = await _processingLogController.GetByPdfPartitionerRunInfoId(pdfPartitionerRunInfoId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetByPdfPartitionerRunInfoId_ServiceException_InternalServerError() { var pdfPartitionerRunInfoId = Guid.NewGuid(); _mockPdfPartitionerLogService.Setup(service => service.GetByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetByPdfPartitionerRunInfoId(pdfPartitionerRunInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task GetByPdfPartitionerRunInfoId_UnexpectedException_InternalServerError() { var pdfPartitionerRunInfoId = Guid.NewGuid(); _mockPdfPartitionerLogService.Setup(service => service.GetByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetByPdfPartitionerRunInfoId(pdfPartitionerRunInfoId); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Post_CreatedAtAction() { var pdfPartitionerLogId = 1; var pdfPartitionerRunInfoId = Guid.NewGuid(); var request = new PdfPartitionerLog { PdfPartitionerLogId = 0, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; var requestReturn = new PdfPartitionerLog { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockPdfPartitionerLogService.Setup(service => service.SaveAsync(request)).ReturnsAsync(requestReturn).Verifiable(); var result = await _processingLogController.Post(request); Assert.NotNull(result); var createdAtActionResult = result as CreatedAtActionResult; Assert.NotNull(createdAtActionResult); Assert.AreEqual(201, createdAtActionResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Post_ServiceException_InternalServerError() { var request = new PdfPartitionerLog(); _mockPdfPartitionerLogService.Setup(service => service.SaveAsync(request)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Post(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Post_UnexpectedException_InternalServerError() { var pdfPartitionerLogId = 1; var pdfPartitionerRunInfoId = Guid.NewGuid(); var pdfPartitionerLog = new PdfPartitionerLog { PdfPartitionerLogId = pdfPartitionerLogId, PdfPartitionerRunInfoId = pdfPartitionerRunInfoId }; _mockPdfPartitionerLogService.Setup(service => service.SaveAsync(pdfPartitionerLog)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Post(pdfPartitionerLog); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Delete_OK() { var id = 1; _mockPdfPartitionerLogService.Setup(service => service.Delete(id)).Verifiable(); var result = await _processingLogController.Delete(id); Assert.NotNull(result); var okResult = result as OkResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Delete_ServiceException_InternalServerError() { var id = 1; _mockPdfPartitionerLogService.Setup(service => service.Delete(id)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Delete(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } [Test] public async Task Delete_UnexpectedException_InternalServerError() { var id = 1; _mockPdfPartitionerLogService.Setup(service => service.Delete(id)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Delete(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockPdfPartitionerLogService.Verify(); } }