From 60ecff30519428b0a83613c8b297f4f04b5296a9 Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Sat, 19 Apr 2025 14:56:17 -0400 Subject: [PATCH] Integration --- .../ConvertMarkdownToPdfIntegrationTest.java | 48 ++++++++++++++++--- .../ConvertPDFToMarkdownIntegrationTest.java | 14 +++--- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java b/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java index 18963a4bd..3631df488 100644 --- a/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java +++ b/src/test/java/stirling/software/SPDF/utils/ConvertMarkdownToPdfIntegrationTest.java @@ -3,6 +3,7 @@ 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 net.bytebuddy.implementation.bytecode.Throw; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -12,6 +13,8 @@ import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; +import java.io.IOException; + @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { @@ -22,17 +25,16 @@ import org.springframework.test.web.servlet.MockMvc; "system.enableAlphaFunctionality=false", "system.disableSanitize=false" }) -@AutoConfigureMockMvc(addFilters = false) // Skip security filters +@AutoConfigureMockMvc(addFilters = false) // Skip security filters for integration test public class ConvertMarkdownToPdfIntegrationTest { @Autowired private MockMvc mockMvc; /** - * Integration test for converting Markdown to PDF. + * Test case: Valid Markdown file input * - *

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. + *

This test verifies that a proper Markdown file is converted successfully to PDF. If + * weasyprint is missing, this test will be skipped automatically. */ @Test public void convertValidMarkdownToPdf_shouldReturnPdfBytes() throws Exception { @@ -50,7 +52,7 @@ public class ConvertMarkdownToPdfIntegrationTest { return; } - // Load sample Markdown file from resources + // Load sample Markdown file from test resources ClassPathResource markdownResource = new ClassPathResource("Markdown.md"); MockMultipartFile mockFile = new MockMultipartFile( @@ -59,7 +61,6 @@ public class ConvertMarkdownToPdfIntegrationTest { "text/markdown", markdownResource.getInputStream()); - // Test the conversion endpoint mockMvc.perform( multipart("/api/v1/convert/markdown/pdf") .file(mockFile) @@ -68,4 +69,37 @@ public class ConvertMarkdownToPdfIntegrationTest { .andExpect( header().string("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)); } + + /** + * Test case: Empty Markdown file + * + *

❌ This test will fail unless the source code explicitly checks for empty input. Source + * code should handle fileInput.isEmpty() and return HTTP 400. + */ + @Test + public void convertEmptyMarkdownFile_shouldReturnError() throws Exception { + MockMultipartFile emptyFile = + new MockMultipartFile("fileInput", "empty.md", "text/markdown", new byte[0]); + + mockMvc.perform( + multipart("/api/v1/convert/markdown/pdf") + .file(emptyFile) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest());// but there is throws IO Exception + + } + + /** + * Test case: Missing fileInput field + * + *

❌ This test will fail with NullPointerException unless the controller checks fileInput != + * null. Controller should return HTTP 400 Bad Request for missing file input. + */ + @Test + public void missingFileInput_shouldReturnError() throws Exception { + mockMvc.perform( + multipart("/api/v1/convert/markdown/pdf") + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest());// but there is throws IllegalArgument Exception + } } diff --git a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java index fac70a8af..baac0001f 100644 --- a/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java +++ b/src/test/java/stirling/software/SPDF/utils/ConvertPDFToMarkdownIntegrationTest.java @@ -77,13 +77,13 @@ public class ConvertPDFToMarkdownIntegrationTest { *

✅ Expected behavior: The controller should check if `fileInput == null` and return HTTP * 400 Bad Request with a meaningful error message like "Missing file input". */ - @Test - public void missingFileInput_shouldReturnError() throws Exception { - mockMvc.perform( - multipart("/api/v1/convert/pdf/markdown") - .contentType(MediaType.MULTIPART_FORM_DATA)) - .andExpect(status().isBadRequest()); - } + @Test + public void missingFileInput_shouldReturnError() throws Exception { + mockMvc.perform( + multipart("/api/v1/convert/pdf/markdown") + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isBadRequest()); + } /** MIME 类型错误应失败 */ @Test