From 6196e0cce114ce2ba0f0767d4450e906c1d2117c Mon Sep 17 00:00:00 2001 From: diallo Date: Thu, 8 Aug 2024 09:58:56 +0000 Subject: [PATCH] Add tests for URL reachability in ConvertWebsiteToPdfTest --- .../converters/ConvertWebsiteToPdfTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java diff --git a/src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java b/src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java new file mode 100644 index 00000000000..75d56a59b6e --- /dev/null +++ b/src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java @@ -0,0 +1,70 @@ +package stirling.software.SPDF.controller.api.converters; + +import org.junit.jupiter.api.Test; +import org.springframework.http.ResponseEntity; + +import stirling.software.SPDF.model.api.converters.UrlToPdfRequest; +import static org.junit.jupiter.api.Assertions.*; + +public class ConvertWebsiteToPdfTest { + @Test + public void test_exemption_is_thrown_when_invalid_url_format_provided() { + + String invalid_format_Url = "invalid-url"; + // Arrange + ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF(); + UrlToPdfRequest request = new UrlToPdfRequest(); + request.setUrlInput(invalid_format_Url); + // Act + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + convertWebsiteToPDF.urlToPdf(request); + }); + // Assert + assertEquals("Invalid URL format provided.", thrown.getMessage()); + } + + @Test + public void test_exemption_is_thrown_when_url_is_not_reachable() { + + String unreachable_Url = "https://www.googleeee.com"; + // Arrange + ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF(); + UrlToPdfRequest request = new UrlToPdfRequest(); + request.setUrlInput(unreachable_Url); + // Act + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + convertWebsiteToPDF.urlToPdf(request); + }); + // Assert + assertEquals("URL is not reachable, please provide a valid URL.", thrown.getMessage()); + } + + @Test + public void test_no_exemption_is_thrown_when_valid_url_format_provided() { + + String valid_format_Url = "https://www.google.com"; + // Arrange + ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF(); + UrlToPdfRequest request = new UrlToPdfRequest(); + request.setUrlInput(valid_format_Url); + // Act + assertDoesNotThrow(() -> { + convertWebsiteToPDF.urlToPdf(request); + }); + } + + @Test void test_pdf_bytes_are_returned_when_valid_url_provided() { + String valid_format_Url = "https://www.google.com"; + // Arrange + ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF(); + UrlToPdfRequest request = new UrlToPdfRequest(); + request.setUrlInput(valid_format_Url); + // Act + ResponseEntity pdfBytes = assertDoesNotThrow(() -> { + return convertWebsiteToPDF.urlToPdf(request); + }); + // Assert + assertNotNull(pdfBytes.getBody()); + } + +}