using System; using Microsoft.AspNetCore.Mvc; using Moq; using Xunit; using LCU.Library.Internal.Exceptions; using LCU.Api.Internal.FileOperations.Controllers; using LCU.Api.Internal.FileOperations.Services; using LCU.Library.Internal.FileOperationsModels.Models; using System.Threading.Tasks; using System.Collections.Generic; using LCU.Library.Internal.FileOperationsModels.Models.HtmlToPdfModels; using LCU.Api.Internal.FileOperations.Services.QueueServices; using LCU.Api.Internal.FileOperations.Services.PdfServices; namespace LCU.Api.Internal.FileOperationsTests.Controllers { public class PdfControllerTest { private PdfController _pdfController; private Mock _mockQueueFileOperationsService; private Mock _mockPdfParserService; private Mock _mockBatchPdfParserService; private Mock _mockDirectFileService; private Mock _mockHtmlToPdfService; public PdfControllerTest() { _mockQueueFileOperationsService = new Mock(); _mockPdfParserService = new Mock(); _mockBatchPdfParserService = new Mock(); _mockDirectFileService = new Mock(); _mockHtmlToPdfService = new Mock(); _pdfController = new PdfController(_mockQueueFileOperationsService.Object, _mockPdfParserService.Object, _mockBatchPdfParserService.Object, _mockDirectFileService.Object, _mockHtmlToPdfService.Object); } [Fact] public async Task BatchPdfParser_OK() { var batchPdfParserRequest = new BatchParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", BatchPdfs = new List() { new BatchPdf { PageStart = 1, PageStop = 2 } } }; var requestResponse = new RequestResponse { IsSuccessful = true }; _mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny())) .Returns(requestResponse).Verifiable(); _mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny(), It.IsAny())).Verifiable(); var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest); Assert.NotNull(result); var acceptedResult = result as AcceptedResult; Assert.NotNull(acceptedResult); Assert.Equal(202, acceptedResult!.StatusCode); _mockBatchPdfParserService.Verify(); _mockQueueFileOperationsService.Verify(); } [Fact] public async Task BatchParsePdfRequest_ServiceException_InternalServerError() { var batchPdfParserRequest = new BatchParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", BatchPdfs = new List() { new BatchPdf { PageStart = 1, PageStop = 2 } } }; var requestResponse = new RequestResponse { IsSuccessful = true }; _mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny())) .Returns(requestResponse).Verifiable(); _mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny(), It.IsAny())) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockBatchPdfParserService.Verify(); _mockQueueFileOperationsService.Verify(); } [Fact] public async Task BatchParsePdfRequest_UnexpectedException_InternalServerError() { var batchPdfParserRequest = new BatchParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", BatchPdfs = new List() { new BatchPdf { PageStart = 1, PageStop = 2 } } }; var requestResponse = new RequestResponse { IsSuccessful = true }; _mockBatchPdfParserService.Setup(service => service.IsRequestValid(batchPdfParserRequest, It.IsAny())) .Returns(requestResponse).Verifiable(); _mockQueueFileOperationsService.Setup(service => service.QueueFileOperations(It.IsAny(), It.IsAny())) .Throws(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _pdfController.QueueBatchPdfParser(batchPdfParserRequest); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockBatchPdfParserService.Verify(); _mockQueueFileOperationsService.Verify(); } [Fact] public void ParsePdf_OK() { var parsePdfRequest = new ParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", PageStart = 1, PageStop = 2 }; var newPdfResponse = "test.pdf"; _mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest)).Returns(newPdfResponse).Verifiable(); var result = _pdfController.ParsePdf(parsePdfRequest); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.Equal(200, okResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void ParsePdf_ServiceException_InternalServerError() { var parsePdfRequest = new ParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", PageStart = 1, PageStop = 2 }; _mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = _pdfController.ParsePdf(parsePdfRequest); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void ParsePdf_UnexpectedException_InternalServerError() { var parsePdfRequest = new ParsePdfRequest() { RootPath = "\\test\test", PdfName = "Test.pdf", PageStart = 1, PageStop = 2 }; _mockPdfParserService.Setup(service => service.ParsePdf(parsePdfRequest)) .Throws(new Exception("Thrown From Unit Test")).Verifiable(); var result = _pdfController.ParsePdf(parsePdfRequest); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void GetPdfConent_OK() { var pdfPath = "\\\\Test\\PdfPath\\test.pdf"; var btyes = new byte[23122321]; _mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath)).Returns(btyes).Verifiable(); var result = _pdfController.GetPdfContent(pdfPath); var okResult = result as FileContentResult; Assert.NotNull(okResult); _mockDirectFileService.Verify(); } [Fact] public void GetPdfConent_UnexpectedException_InternalServerError() { var pdfPath = "\\\\Test\\PdfPath\\test.pdf"; _mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath)) .Throws(new Exception("Thrown From Unit Test")).Verifiable(); var result = _pdfController.GetPdfContent(pdfPath); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void GetPdfConent_ArgumentException_InternalServerError() { var pdfPath = "\\\\Test\\PdfPath\\test.pdf"; _mockDirectFileService.Setup(service => service.GetFileBytes(pdfPath)) .Throws(new ArgumentException("Thrown From Unit Test")).Verifiable(); var result = _pdfController.GetPdfContent(pdfPath); Assert.NotNull(result); var statusResult = result as BadRequestObjectResult; Assert.NotNull(statusResult); Assert.Equal(400, statusResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void ConvertHtmlToPdf_OK() { var htmlToPdfRequest = new HtmlToPdfRequest() { HtmlTemplateFilePath = "\\Test", BodyTagValues = new List { new TagValue { Tag = "Test Tag", Value = "Test Value" } } }; var btyes = new byte[23122321]; _mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest)).Returns(btyes).Verifiable(); var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest); var okResult = result as FileContentResult; Assert.NotNull(okResult); _mockDirectFileService.Verify(); } [Fact] public void ConvertHtmlToPdf_ServiceExceptionException_InternalServerError() { var htmlToPdfRequest = new HtmlToPdfRequest() { HtmlTemplateFilePath = "\\Test", BodyTagValues = new List { new TagValue { Tag = "Test Tag", Value = "Test Value" } } }; _mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockPdfParserService.Verify(); } [Fact] public void GetConvertHtmlToPdf_UnexpectedExceptionException_InternalServerError() { var htmlToPdfRequest = new HtmlToPdfRequest() { HtmlTemplateFilePath = "\\Test", BodyTagValues = new List { new TagValue { Tag = "Test Tag", Value = "Test Value" } } }; _mockHtmlToPdfService.Setup(service => service.ConvertHtmlToPdf(htmlToPdfRequest)) .Throws(new Exception("Thrown From Unit Test")).Verifiable(); var result = _pdfController.ConvertHtmlToPdf(htmlToPdfRequest); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.Equal(500, statusResult!.StatusCode); _mockPdfParserService.Verify(); } } }