Integration

This commit is contained in:
Carpe-Wang 2025-04-19 14:56:17 -04:00
parent 0ee17334c4
commit 60ecff3051
2 changed files with 48 additions and 14 deletions

View File

@ -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
*
* <p>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.
* <p>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
*
* <p> 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
*
* <p> 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
}
}