-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for URL reachability in ConvertWebsiteToPdfTest
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<byte[]> pdfBytes = assertDoesNotThrow(() -> { | ||
return convertWebsiteToPDF.urlToPdf(request); | ||
}); | ||
// Assert | ||
assertNotNull(pdfBytes.getBody()); | ||
} | ||
|
||
} |