mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-15 19:10:47 +02:00
refactor: standardize MIME handling via Spring MediaType (#4389)
This commit is contained in:
+2
-1
@@ -11,6 +11,7 @@ import java.nio.file.Path;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
@@ -23,7 +24,7 @@ class ApplicationPropertiesSaml2HttpTest {
|
||||
server.enqueue(
|
||||
new MockResponse()
|
||||
.setResponseCode(200)
|
||||
.addHeader("Content-Type", "application/xml")
|
||||
.addHeader("Content-Type", MediaType.APPLICATION_XML_VALUE)
|
||||
.setBody("<EntityDescriptor/>"));
|
||||
server.start();
|
||||
|
||||
|
||||
+5
-3
@@ -1,12 +1,12 @@
|
||||
package stirling.software.common.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static stirling.software.common.service.SpyPDFDocumentFactory.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.pdfbox.Loader;
|
||||
@@ -18,9 +18,11 @@ import org.junit.jupiter.api.parallel.Execution;
|
||||
import org.junit.jupiter.api.parallel.ExecutionMode;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
|
||||
import stirling.software.common.model.api.PDFFile;
|
||||
import stirling.software.common.service.SpyPDFDocumentFactory.StrategyType;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -73,7 +75,7 @@ class CustomPDFDocumentFactoryTest {
|
||||
void testStrategy_MultipartFile(int sizeMB, StrategyType expected) throws IOException {
|
||||
byte[] inflated = inflatePdf(basePdfBytes, sizeMB);
|
||||
MockMultipartFile multipart =
|
||||
new MockMultipartFile("file", "doc.pdf", "application/pdf", inflated);
|
||||
new MockMultipartFile("file", "doc.pdf", MediaType.APPLICATION_PDF_VALUE, inflated);
|
||||
try (PDDocument doc = factory.load(multipart)) {
|
||||
Assertions.assertEquals(expected, factory.lastStrategyUsed);
|
||||
}
|
||||
@@ -84,7 +86,7 @@ class CustomPDFDocumentFactoryTest {
|
||||
void testStrategy_PDFFile(int sizeMB, StrategyType expected) throws IOException {
|
||||
byte[] inflated = inflatePdf(basePdfBytes, sizeMB);
|
||||
MockMultipartFile multipart =
|
||||
new MockMultipartFile("file", "doc.pdf", "application/pdf", inflated);
|
||||
new MockMultipartFile("file", "doc.pdf", MediaType.APPLICATION_PDF_VALUE, inflated);
|
||||
PDFFile pdfFile = new PDFFile();
|
||||
pdfFile.setFileInput(multipart);
|
||||
try (PDDocument doc = factory.load(pdfFile)) {
|
||||
|
||||
@@ -2,6 +2,8 @@ package stirling.software.common.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.AdditionalAnswers.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -15,6 +17,7 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -36,7 +39,7 @@ class FileStorageTest {
|
||||
// Create a mock MultipartFile
|
||||
mockFile = mock(MultipartFile.class);
|
||||
when(mockFile.getOriginalFilename()).thenReturn("test.pdf");
|
||||
when(mockFile.getContentType()).thenReturn("application/pdf");
|
||||
when(mockFile.getContentType()).thenReturn(MediaType.APPLICATION_PDF_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import stirling.software.common.model.job.JobResult;
|
||||
@@ -77,7 +78,7 @@ class TaskManagerTest {
|
||||
taskManager.createTask(jobId);
|
||||
String fileId = "file-id";
|
||||
String originalFileName = "test.pdf";
|
||||
String contentType = "application/pdf";
|
||||
String contentType = MediaType.APPLICATION_PDF_VALUE;
|
||||
long fileSize = 1024L;
|
||||
|
||||
// Mock the fileStorage.getFileSize() call
|
||||
@@ -185,7 +186,8 @@ class TaskManagerTest {
|
||||
// 2. Create completed successful job with file
|
||||
String successFileJobId = "success-file-job";
|
||||
taskManager.createTask(successFileJobId);
|
||||
taskManager.setFileResult(successFileJobId, "file-id", "test.pdf", "application/pdf");
|
||||
taskManager.setFileResult(
|
||||
successFileJobId, "file-id", "test.pdf", MediaType.APPLICATION_PDF_VALUE);
|
||||
|
||||
// 3. Create completed successful job without file
|
||||
String successJobId = "success-job";
|
||||
@@ -235,7 +237,7 @@ class TaskManagerTest {
|
||||
ResultFile.builder()
|
||||
.fileId("file-id")
|
||||
.fileName("test.pdf")
|
||||
.contentType("application/pdf")
|
||||
.contentType(MediaType.APPLICATION_PDF_VALUE)
|
||||
.fileSize(1024L)
|
||||
.build();
|
||||
ReflectionTestUtils.setField(oldJob, "resultFiles", java.util.List.of(resultFile));
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -57,7 +58,10 @@ class PDFToFileTest {
|
||||
// Prepare
|
||||
MultipartFile nonPdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.txt", "text/plain", "This is not a PDF".getBytes());
|
||||
"file",
|
||||
"test.txt",
|
||||
MediaType.TEXT_PLAIN_VALUE,
|
||||
"This is not a PDF".getBytes());
|
||||
|
||||
// Execute
|
||||
ResponseEntity<byte[]> response = pdfToFile.processPdfToMarkdown(nonPdfFile);
|
||||
@@ -71,7 +75,10 @@ class PDFToFileTest {
|
||||
// Prepare
|
||||
MultipartFile nonPdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.txt", "text/plain", "This is not a PDF".getBytes());
|
||||
"file",
|
||||
"test.txt",
|
||||
MediaType.TEXT_PLAIN_VALUE,
|
||||
"This is not a PDF".getBytes());
|
||||
|
||||
// Execute
|
||||
ResponseEntity<byte[]> response = pdfToFile.processPdfToHtml(nonPdfFile);
|
||||
@@ -86,7 +93,10 @@ class PDFToFileTest {
|
||||
// Prepare
|
||||
MultipartFile nonPdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.txt", "text/plain", "This is not a PDF".getBytes());
|
||||
"file",
|
||||
"test.txt",
|
||||
MediaType.TEXT_PLAIN_VALUE,
|
||||
"This is not a PDF".getBytes());
|
||||
|
||||
// Execute
|
||||
ResponseEntity<byte[]> response =
|
||||
@@ -102,7 +112,10 @@ class PDFToFileTest {
|
||||
// Prepare
|
||||
MultipartFile pdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", "application/pdf", "Fake PDF content".getBytes());
|
||||
"file",
|
||||
"test.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Execute with invalid format
|
||||
ResponseEntity<byte[]> response =
|
||||
@@ -120,7 +133,10 @@ class PDFToFileTest {
|
||||
// Create a mock PDF file
|
||||
MultipartFile pdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", "application/pdf", "Fake PDF content".getBytes());
|
||||
"file",
|
||||
"test.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Create a mock HTML output file
|
||||
Path htmlOutputFile = tempDir.resolve("test.html");
|
||||
@@ -168,7 +184,7 @@ class PDFToFileTest {
|
||||
new MockMultipartFile(
|
||||
"file",
|
||||
"multipage.pdf",
|
||||
"application/pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
@@ -245,7 +261,10 @@ class PDFToFileTest {
|
||||
// Create a mock PDF file
|
||||
MultipartFile pdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", "application/pdf", "Fake PDF content".getBytes());
|
||||
"file",
|
||||
"test.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
mockedStaticProcessExecutor
|
||||
@@ -324,7 +343,7 @@ class PDFToFileTest {
|
||||
new MockMultipartFile(
|
||||
"file",
|
||||
"document.pdf",
|
||||
"application/pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
@@ -386,7 +405,7 @@ class PDFToFileTest {
|
||||
new MockMultipartFile(
|
||||
"file",
|
||||
"document.pdf",
|
||||
"application/pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
@@ -472,7 +491,7 @@ class PDFToFileTest {
|
||||
new MockMultipartFile(
|
||||
"file",
|
||||
"document.pdf",
|
||||
"application/pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
@@ -531,7 +550,10 @@ class PDFToFileTest {
|
||||
// Create a mock PDF file with no filename
|
||||
MultipartFile pdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "", "application/pdf", "Fake PDF content".getBytes());
|
||||
"file",
|
||||
"",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"Fake PDF content".getBytes());
|
||||
|
||||
// Setup ProcessExecutor mock
|
||||
mockedStaticProcessExecutor
|
||||
|
||||
@@ -48,7 +48,8 @@ public class WebResponseUtilsTest {
|
||||
try {
|
||||
byte[] fileContent = "Sample file content".getBytes();
|
||||
MockMultipartFile file =
|
||||
new MockMultipartFile("file", "sample.txt", "text/plain", fileContent);
|
||||
new MockMultipartFile(
|
||||
"file", "sample.txt", MediaType.TEXT_PLAIN_VALUE, fileContent);
|
||||
|
||||
ResponseEntity<byte[]> responseEntity =
|
||||
WebResponseUtils.multiPartFileToWebResponse(file);
|
||||
|
||||
+5
-1
@@ -8,6 +8,7 @@ import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -24,7 +25,10 @@ class CustomColorReplaceStrategyTest {
|
||||
// Create a mock file
|
||||
mockFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", "application/pdf", "test pdf content".getBytes());
|
||||
"file",
|
||||
"test.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"test pdf content".getBytes());
|
||||
|
||||
// Initialize strategy with custom colors
|
||||
strategy =
|
||||
|
||||
+4
-1
@@ -24,6 +24,7 @@ import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -38,7 +39,9 @@ class InvertFullColorStrategyTest {
|
||||
void setUp() throws Exception {
|
||||
// Create a simple PDF document for testing
|
||||
byte[] pdfBytes = createSimplePdfWithRectangle();
|
||||
mockPdfFile = new MockMultipartFile("file", "test.pdf", "application/pdf", pdfBytes);
|
||||
mockPdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
// Create the strategy instance
|
||||
strategy = new InvertFullColorStrategy(mockPdfFile, ReplaceAndInvert.FULL_INVERSION);
|
||||
|
||||
+14
-4
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -35,7 +36,10 @@ class ReplaceAndInvertColorStrategyTest {
|
||||
// Arrange
|
||||
MultipartFile mockFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.pdf", "application/pdf", "test content".getBytes());
|
||||
"file",
|
||||
"test.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"test content".getBytes());
|
||||
ReplaceAndInvert replaceAndInvert = ReplaceAndInvert.CUSTOM_COLOR;
|
||||
|
||||
// Act
|
||||
@@ -56,7 +60,7 @@ class ReplaceAndInvertColorStrategyTest {
|
||||
// Arrange
|
||||
byte[] content = "test pdf content".getBytes();
|
||||
MultipartFile mockFile =
|
||||
new MockMultipartFile("file", "test.pdf", "application/pdf", content);
|
||||
new MockMultipartFile("file", "test.pdf", MediaType.APPLICATION_PDF_VALUE, content);
|
||||
ReplaceAndInvert replaceAndInvert = ReplaceAndInvert.CUSTOM_COLOR;
|
||||
|
||||
ReplaceAndInvertColorStrategy strategy =
|
||||
@@ -74,10 +78,16 @@ class ReplaceAndInvertColorStrategyTest {
|
||||
// Arrange
|
||||
MultipartFile mockFile1 =
|
||||
new MockMultipartFile(
|
||||
"file1", "test1.pdf", "application/pdf", "content1".getBytes());
|
||||
"file1",
|
||||
"test1.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"content1".getBytes());
|
||||
MultipartFile mockFile2 =
|
||||
new MockMultipartFile(
|
||||
"file2", "test2.pdf", "application/pdf", "content2".getBytes());
|
||||
"file2",
|
||||
"test2.pdf",
|
||||
MediaType.APPLICATION_PDF_VALUE,
|
||||
"content2".getBytes());
|
||||
|
||||
// Act
|
||||
ReplaceAndInvertColorStrategy strategy =
|
||||
|
||||
Reference in New Issue
Block a user