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; namespace LCU.API.Internal.DocumentIngestorTests.Controllers; public class IngestorRunInfoControllerTest { private IngestorRunInfoController _ingestorRunInfoController; private Mock _mockIngestorRunInfoService; [SetUp] public void Setup() { _mockIngestorRunInfoService = new Mock(); _ingestorRunInfoController = new IngestorRunInfoController(_mockIngestorRunInfoService.Object); } [Test] public async Task GetByIngestorRunInfoId_OK() { var ingestorRunInfoId = new Guid(); var ingestorRunInfo = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.GetByIdAsync(ingestorRunInfoId)) .ReturnsAsync(ingestorRunInfo).Verifiable(); var result = await _ingestorRunInfoController.GetByIngestorRunInfoId(ingestorRunInfoId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task GetByIngestorRunInfoId_ServiceException_InternalServerError() { var ingestorRunInfoId = new Guid(); _mockIngestorRunInfoService.Setup(service => service.GetByIdAsync(ingestorRunInfoId)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.GetByIngestorRunInfoId(ingestorRunInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task GetByIngestorRunInfoId_UnexpectedException_InternalServerError() { var ingestorRunInfoId = new Guid(); _mockIngestorRunInfoService.Setup(service => service.GetByIdAsync(ingestorRunInfoId)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.GetByIngestorRunInfoId(ingestorRunInfoId); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Post_OK() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.SaveAsync(request)).Verifiable(); var result = await _ingestorRunInfoController.Post(request); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Post_ServiceException_InternalServerError() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.SaveAsync(request)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Post(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Post_UnexpectedException_InternalServerError() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.SaveAsync(request)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Post(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Put_OK() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.Update(request)).ReturnsAsync(request).Verifiable(); var result = await _ingestorRunInfoController.Put(request); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Put_ServiceException_InternalServerError() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.Update(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Put_UnexpectedException_InternalServerError() { var request = new IngestorRunInfo(); _mockIngestorRunInfoService.Setup(service => service.Update(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Delete_OK() { var ingestorRunInfoId = new Guid(); _mockIngestorRunInfoService.Setup(service => service.Delete(ingestorRunInfoId)).Verifiable(); var result = await _ingestorRunInfoController.Delete(ingestorRunInfoId); Assert.NotNull(result); var okResult = result as OkResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Delete_ServiceException_InternalServerError() { var ingestorRunInfoId = new Guid(); _mockIngestorRunInfoService.Setup(service => service.Delete(ingestorRunInfoId)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Delete(ingestorRunInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } [Test] public async Task Delete_UnexpectedException_InternalServerError() { var ingestorRunInfoId = new Guid(); _mockIngestorRunInfoService.Setup(service => service.Delete(ingestorRunInfoId)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _ingestorRunInfoController.Delete(ingestorRunInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockIngestorRunInfoService.Verify(); } }