using System; using System.Threading.Tasks; 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; using System.Collections.Generic; namespace LCU.API.Internal.DocumentIngestorTests.Controllers; public class ProcessingLogControllerTest { private ProcessingLogController _processingLogController; private Mock _mockProcessingLogService; [SetUp] public void Setup() { _mockProcessingLogService = new Mock(); _processingLogController = new ProcessingLogController(_mockProcessingLogService.Object); } [Test] public async Task Get_OK() { var processingLogs = new List() { new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 } }; _mockProcessingLogService.Setup(service => service.GetAsync()) .ReturnsAsync(processingLogs).Verifiable(); var result = await _processingLogController.Get(); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Get_ServiceException_InternalServerError() { _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task Get_UnexpectedException_InternalServerError() { _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task GetById_OK() { var id = 1; var processingLog = new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 }; _mockProcessingLogService.Setup(service => service.GetByIdAsync(id)) .ReturnsAsync(processingLog).Verifiable(); var result = await _processingLogController.GetById(id); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task GetById_ServiceException_InternalServerError() { var id = 1; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task GetById_UnexpectedException_InternalServerError() { var id = 1; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_OK() { var documentProcessingInfoId = 1; var processingLogs = new List() { new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 } }; _mockProcessingLogService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ReturnsAsync(processingLogs).Verifiable(); var result = await _processingLogController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_ServiceException_InternalServerError() { var documentProcessingInfoId = 1; _mockProcessingLogService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_UnexpectedException_InternalServerError() { var documentProcessingInfoId = 1; _mockProcessingLogService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Post_CreatedAtAction() { var request = new ProcessingLog() { DocumentProcessingInfoId = 1, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Test" }; var requestReturn = new ProcessingLog() { DocumentProcessingInfoId = 1, ProcessingLogId = 1, LogTimeStamp = Convert.ToDateTime("1/1/2022"), LogMessage = "Test", UserName = "User" }; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task Post_ServiceException_InternalServerError() { var request = new ProcessingLog(); _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task Post_UnexpectedException_InternalServerError() { var processingLog = new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 }; _mockProcessingLogService.Setup(service => service.SaveAsync(processingLog)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Post(processingLog); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Put_OK() { var processingLog = new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 }; _mockProcessingLogService.Setup(service => service.Update(processingLog)).ReturnsAsync(processingLog).Verifiable(); var result = await _processingLogController.Put(processingLog); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Put_ServiceException_InternalServerError() { var processingLog = new ProcessingLog() { ProcessingLogId = 1, DocumentProcessingInfoId = 1 }; _mockProcessingLogService.Setup(service => service.Update(processingLog)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Put(processingLog); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Put_UnexpectedException_InternalServerError() { var request = new ProcessingLog(); _mockProcessingLogService.Setup(service => service.Update(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _processingLogController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockProcessingLogService.Verify(); } [Test] public async Task Delete_OK() { var id = 1; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task Delete_ServiceException_InternalServerError() { var id = 1; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } [Test] public async Task Delete_UnexpectedException_InternalServerError() { var id = 1; _mockProcessingLogService.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); _mockProcessingLogService.Verify(); } }