Back to Repositories

Validating Website-to-PDF Conversion Logic in Stirling-PDF

This test suite validates the website-to-PDF conversion functionality in the Stirling-PDF application, focusing on URL validation and error handling. The tests ensure proper handling of invalid URL formats and unreachable websites when attempting PDF conversions.

Test Coverage Overview

The test suite provides coverage for URL validation scenarios in the website-to-PDF conversion process. Key functionality tested includes:

  • Invalid URL format validation
  • Unreachable URL handling
  • Exception message verification
The tests focus on edge cases in URL processing and integration with the CustomPDDocumentFactory component.

Implementation Analysis

The testing approach utilizes JUnit 5 with Mockito for dependency isolation. The implementation follows AAA (Arrange-Act-Assert) pattern with clear test method naming conventions. Mockito annotations are used for efficient mock creation and management, while JUnit’s assertThrows validates exception handling.

Technical Details

Testing tools and configuration include:

  • JUnit Jupiter for test execution
  • Mockito framework for mocking
  • BeforeEach setup for test initialization
  • CustomPDDocumentFactory dependency injection
  • UrlToPdfRequest model for test data

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming indicating test scenarios
  • Proper test isolation using mocks
  • Consistent setup and teardown management
  • Focused test cases with single assertions
  • Appropriate exception handling verification

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/controller/api/converters/ConvertWebsiteToPdfTest.java

            
package stirling.software.SPDF.controller.api.converters;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import stirling.software.SPDF.model.api.converters.UrlToPdfRequest;
import stirling.software.SPDF.service.CustomPDDocumentFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ConvertWebsiteToPdfTest {


    @Mock
    private CustomPDDocumentFactory mockPdfDocumentFactory;

    private ConvertWebsiteToPDF convertWebsiteToPDF;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        convertWebsiteToPDF = new ConvertWebsiteToPDF(mockPdfDocumentFactory);
    }

    @Test
    public void test_exemption_is_thrown_when_invalid_url_format_provided() {

        String invalid_format_Url = "invalid-url";

        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.googleeeexyz.com";
        // Arrange
        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());
    }
}