diff --git a/build.gradle b/build.gradle index fdb231107..6e0f07741 100644 --- a/build.gradle +++ b/build.gradle @@ -466,6 +466,8 @@ dependencies { implementation 'org.snakeyaml:snakeyaml-engine:2.9' testImplementation "org.springframework.boot:spring-boot-starter-test:$springBootVersion" + testImplementation 'org.mockito:mockito-inline:5.2.0' + // Batik implementation "org.apache.xmlgraphics:batik-all:1.18" diff --git a/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java b/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java new file mode 100644 index 000000000..18963a4bd --- /dev/null +++ b/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java @@ -0,0 +1,71 @@ +package stirling.software.SPDF.utils; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "spring.security.enabled=false", + "security.enableLogin=false", + "security.csrfDisabled=true", + "system.enableUrlToPDF=false", + "system.enableAlphaFunctionality=false", + "system.disableSanitize=false" + }) +@AutoConfigureMockMvc(addFilters = false) // Skip security filters +public class ConvertMarkdownToPdfIntegrationTest { + + @Autowired private MockMvc mockMvc; + + /** + * Integration test for converting Markdown to PDF. + * + *
Note: This test requires weasyprint to be installed in the system. It will automatically + * skip in environments where weasyprint is not available, so it's designed to be safe to run in + * CI environments. + */ + @Test + public void convertValidMarkdownToPdf_shouldReturnPdfBytes() throws Exception { + // Skip test automatically if weasyprint is missing + try { + ProcessBuilder pb = new ProcessBuilder("which", "weasyprint"); + Process process = pb.start(); + int exitCode = process.waitFor(); + org.junit.jupiter.api.Assumptions.assumeTrue( + exitCode == 0, + "Skipping test: weasyprint is not installed in this environment"); + } catch (Exception e) { + org.junit.jupiter.api.Assumptions.assumeTrue( + false, "Skipping test: weasyprint availability check failed"); + return; + } + + // Load sample Markdown file from resources + ClassPathResource markdownResource = new ClassPathResource("Markdown.md"); + MockMultipartFile mockFile = + new MockMultipartFile( + "fileInput", + "Markdown.md", + "text/markdown", + markdownResource.getInputStream()); + + // Test the conversion endpoint + mockMvc.perform( + multipart("/api/v1/convert/markdown/pdf") + .file(mockFile) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isOk()) + .andExpect( + header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)); + } +} diff --git a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java index b8cddd580..a85f32a33 100644 --- a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java +++ b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java @@ -32,15 +32,19 @@ public class ConvertPDFToMarkdownIntegrationTest { // Load sample PDF file from resources ClassPathResource pdfResource = new ClassPathResource("sample/sample.pdf"); MockMultipartFile mockFile = - new MockMultipartFile( - "fileInput", "sample.pdf", "application/pdf", pdfResource.getInputStream()); + new MockMultipartFile( + "fileInput", "sample.pdf", "application/pdf", pdfResource.getInputStream()); mockMvc.perform( - multipart("/api/v1/convert/pdf/markdown") - .file(mockFile) - .contentType(MediaType.MULTIPART_FORM_DATA)) - .andExpect(status().isOk()) - .andExpect( - header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)); + multipart("/api/v1/convert/pdf/markdown") + .file(mockFile) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isOk()) + .andExpect( + header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)); } + + // The Markdown to PDF integration test is omitted because it requires weasyprint, which + // is unlikely to be available in the test environment. The unit test in + // ConvertMarkdownToPdfTest.java already tests the controller logic thoroughly with mocks. }