Create ConvertPDFToMarkdownTest.java

This commit is contained in:
Ludy87 2025-08-10 00:07:57 +02:00
parent 8eefdbd9e0
commit 63db6699d5
No known key found for this signature in database
GPG Key ID: 92696155E0220F94

View File

@ -0,0 +1,107 @@
package stirling.software.SPDF.model.api.converters;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartFile;
import stirling.software.common.util.PDFToFile;
class ConvertPDFToMarkdownTest {
private MockMvc mockMvc() {
return MockMvcBuilders.standaloneSetup(new ConvertPDFToMarkdown())
.setControllerAdvice(new GlobalErrorHandler())
.build();
}
@RestControllerAdvice
static class GlobalErrorHandler {
@ExceptionHandler(Exception.class)
ResponseEntity<byte[]> handle(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new byte[0]);
}
}
@Test
void pdf_to_markdown_returns_markdown_bytes() throws Exception {
byte[] md = "# heading\n\ncontent\n".getBytes(StandardCharsets.UTF_8);
try (MockedConstruction<PDFToFile> construction =
Mockito.mockConstruction(
PDFToFile.class,
(mock, ctx) -> {
when(mock.processPdfToMarkdown(any(MultipartFile.class)))
.thenAnswer(
inv ->
ResponseEntity.ok()
.header("Content-Type", "text/markdown")
.body(md));
})) {
MockMvc mvc = mockMvc();
MockMultipartFile file =
new MockMultipartFile(
"fileInput", // muss zum Feldnamen in PDFFile passen
"input.pdf",
"application/pdf",
new byte[] {1, 2, 3});
mvc.perform(multipart("/api/v1/convert/pdf/markdown").file(file))
.andExpect(status().isOk())
.andExpect(header().string("Content-Type", "text/markdown"))
.andExpect(content().bytes(md));
// Verifizieren, dass genau eine Instanz erzeugt wurde
assert construction.constructed().size() == 1;
// und dass das hochgeladene File an processPdfToMarkdown() durchgereicht wurde
PDFToFile created = construction.constructed().get(0);
ArgumentCaptor<MultipartFile> captor = ArgumentCaptor.forClass(MultipartFile.class);
verify(created, times(1)).processPdfToMarkdown(captor.capture());
MultipartFile passed = captor.getValue();
// minimal plausi
org.junit.jupiter.api.Assertions.assertEquals(
"input.pdf", passed.getOriginalFilename());
org.junit.jupiter.api.Assertions.assertEquals(
"application/pdf", passed.getContentType());
}
}
@Test
void pdf_to_markdown_when_service_throws_returns_500() throws Exception {
try (MockedConstruction<PDFToFile> ignored =
Mockito.mockConstruction(
PDFToFile.class,
(mock, ctx) -> {
when(mock.processPdfToMarkdown(any(MultipartFile.class)))
.thenThrow(new RuntimeException("boom"));
})) {
MockMvc mvc = mockMvc();
MockMultipartFile file =
new MockMultipartFile(
"fileInput", "x.pdf", "application/pdf", new byte[] {0x01});
mvc.perform(multipart("/api/v1/convert/pdf/markdown").file(file))
.andExpect(status().isInternalServerError());
}
}
}