算完成了第二个

This commit is contained in:
Carpe-Wang 2025-04-19 14:45:37 -04:00
parent 6a40a8be39
commit 8b860a9149
3 changed files with 85 additions and 8 deletions

View File

@ -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"

View File

@ -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.
*
* <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.
*/
@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));
}
}

View File

@ -43,4 +43,8 @@ public class ConvertPDFToMarkdownIntegrationTest {
.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.
}