refactor: standardize MIME handling via Spring MediaType (#4389)

This commit is contained in:
Ludy
2025-09-05 12:08:24 +02:00
committed by GitHub
parent f14955a019
commit 9a39aff19f
82 changed files with 310 additions and 164 deletions

View File

@@ -24,6 +24,7 @@ import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
@@ -56,7 +57,10 @@ class EditTableOfContentsControllerTest {
void setUp() {
mockFile =
new MockMultipartFile(
"file", "test.pdf", "application/pdf", "PDF content".getBytes());
"file",
"test.pdf",
MediaType.APPLICATION_PDF_VALUE,
"PDF content".getBytes());
mockDocument = mock(PDDocument.class);
mockCatalog = mock(PDDocumentCatalog.class);
mockPages = mock(PDPageTree.class);

View File

@@ -23,6 +23,7 @@ import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
@@ -49,13 +50,22 @@ class MergeControllerTest {
void setUp() {
mockFile1 =
new MockMultipartFile(
"file1", "document1.pdf", "application/pdf", "PDF content 1".getBytes());
"file1",
"document1.pdf",
MediaType.APPLICATION_PDF_VALUE,
"PDF content 1".getBytes());
mockFile2 =
new MockMultipartFile(
"file2", "document2.pdf", "application/pdf", "PDF content 2".getBytes());
"file2",
"document2.pdf",
MediaType.APPLICATION_PDF_VALUE,
"PDF content 2".getBytes());
mockFile3 =
new MockMultipartFile(
"file3", "chapter3.pdf", "application/pdf", "PDF content 3".getBytes());
"file3",
"chapter3.pdf",
MediaType.APPLICATION_PDF_VALUE,
"PDF content 3".getBytes());
mockDocument = mock(PDDocument.class);
mockMergedDocument = mock(PDDocument.class);
@@ -202,7 +212,10 @@ class MergeControllerTest {
// Given
MockMultipartFile fileWithoutExtension =
new MockMultipartFile(
"file", "document_no_ext", "application/pdf", "PDF content".getBytes());
"file",
"document_no_ext",
MediaType.APPLICATION_PDF_VALUE,
"PDF content".getBytes());
MultipartFile[] files = {fileWithoutExtension};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);

View File

@@ -17,6 +17,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
@@ -34,7 +35,8 @@ public class RotationControllerTest {
public void testRotatePDF() throws IOException {
// Create a mock file
MockMultipartFile mockFile =
new MockMultipartFile("file", "test.pdf", "application/pdf", new byte[] {1, 2, 3});
new MockMultipartFile(
"file", "test.pdf", MediaType.APPLICATION_PDF_VALUE, new byte[] {1, 2, 3});
RotatePDFRequest request = new RotatePDFRequest();
request.setFileInput(mockFile);
request.setAngle(90);
@@ -62,7 +64,8 @@ public class RotationControllerTest {
public void testRotatePDFInvalidAngle() throws IOException {
// Create a mock file
MockMultipartFile mockFile =
new MockMultipartFile("file", "test.pdf", "application/pdf", new byte[] {1, 2, 3});
new MockMultipartFile(
"file", "test.pdf", MediaType.APPLICATION_PDF_VALUE, new byte[] {1, 2, 3});
RotatePDFRequest request = new RotatePDFRequest();
request.setFileInput(mockFile);
request.setAngle(45); // Invalid angle

View File

@@ -16,6 +16,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;
@@ -45,13 +46,22 @@ class AttachmentControllerTest {
void setUp() {
pdfFile =
new MockMultipartFile(
"fileInput", "test.pdf", "application/pdf", "PDF content".getBytes());
"fileInput",
"test.pdf",
MediaType.APPLICATION_PDF_VALUE,
"PDF content".getBytes());
attachment1 =
new MockMultipartFile(
"attachment1", "file1.txt", "text/plain", "File 1 content".getBytes());
"attachment1",
"file1.txt",
MediaType.TEXT_PLAIN_VALUE,
"File 1 content".getBytes());
attachment2 =
new MockMultipartFile(
"attachment2", "file2.jpg", "image/jpeg", "Image content".getBytes());
"attachment2",
"file2.jpg",
MediaType.IMAGE_JPEG_VALUE,
"Image content".getBytes());
request = new AddAttachmentRequest();
mockDocument = mock(PDDocument.class);
modifiedMockDocument = mock(PDDocument.class);

View File

@@ -18,6 +18,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
@@ -117,7 +118,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithPfx() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile pfxFile =
new MockMultipartFile("p12File", "test-cert.pfx", "application/x-pkcs12", pfxBytes);
@@ -142,7 +144,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithPkcs12() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile p12File =
new MockMultipartFile("p12File", "test-cert.p12", "application/x-pkcs12", p12Bytes);
@@ -167,7 +170,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithJks() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile jksFile =
new MockMultipartFile(
"jksFile", "test-cert.jks", "application/octet-stream", jksBytes);
@@ -193,7 +197,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithPem() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile keyFile =
new MockMultipartFile(
"privateKeyFile", "test-key.pem", "application/x-pem-file", pemKeyBytes);
@@ -223,7 +228,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithCrt() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile keyFile =
new MockMultipartFile(
"privateKeyFile", "test-key.key", "application/x-pem-file", keyBytes);
@@ -253,7 +259,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithCer() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile keyFile =
new MockMultipartFile(
"privateKeyFile", "test-key.key", "application/x-pem-file", keyBytes);
@@ -283,7 +290,8 @@ class CertSignControllerTest {
@Test
void testSignPdfWithDer() throws Exception {
MockMultipartFile pdfFile =
new MockMultipartFile("fileInput", "test.pdf", "application/pdf", pdfBytes);
new MockMultipartFile(
"fileInput", "test.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
MockMultipartFile keyFile =
new MockMultipartFile(
"privateKeyFile", "test-key.key", "application/x-pem-file", keyBytes);

View File

@@ -42,6 +42,7 @@ import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
@@ -130,7 +131,10 @@ class RedactControllerTest {
void setUp() throws IOException {
mockPdfFile =
new MockMultipartFile(
"fileInput", "test.pdf", "application/pdf", createSimplePdfContent());
"fileInput",
"test.pdf",
MediaType.APPLICATION_PDF_VALUE,
createSimplePdfContent());
// Mock PDF document and related objects
mockDocument = mock(PDDocument.class);
@@ -632,7 +636,7 @@ class RedactControllerTest {
new MockMultipartFile(
"fileInput",
"malformed.pdf",
"application/pdf",
MediaType.APPLICATION_PDF_VALUE,
"Not a real PDF content".getBytes());
RedactPdfRequest request = new RedactPdfRequest();

View File

@@ -11,6 +11,7 @@ import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
class AttachmentServiceTest {
@@ -54,13 +55,13 @@ class AttachmentServiceTest {
when(attachment1.getInputStream())
.thenReturn(new ByteArrayInputStream("PDF content".getBytes()));
when(attachment1.getSize()).thenReturn(15L);
when(attachment1.getContentType()).thenReturn("application/pdf");
when(attachment1.getContentType()).thenReturn(MediaType.APPLICATION_PDF_VALUE);
when(attachment2.getOriginalFilename()).thenReturn("image.jpg");
when(attachment2.getInputStream())
.thenReturn(new ByteArrayInputStream("Image content".getBytes()));
when(attachment2.getSize()).thenReturn(20L);
when(attachment2.getContentType()).thenReturn("image/jpeg");
when(attachment2.getContentType()).thenReturn(MediaType.IMAGE_JPEG_VALUE);
PDDocument result = attachmentService.addAttachment(document, attachments);

View File

@@ -1,6 +1,7 @@
package stirling.software.common.controller;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import java.util.Map;
@@ -11,6 +12,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpSession;
@@ -126,7 +128,7 @@ class JobControllerTest {
String jobId = "test-job-id";
String fileId = "file-id";
String originalFileName = "test.pdf";
String contentType = "application/pdf";
String contentType = MediaType.APPLICATION_PDF_VALUE;
byte[] fileContent = "Test file content".getBytes();
JobResult mockResult = new JobResult();
@@ -206,7 +208,7 @@ class JobControllerTest {
String jobId = "test-job-id";
String fileId = "file-id";
String originalFileName = "test.pdf";
String contentType = "application/pdf";
String contentType = MediaType.APPLICATION_PDF_VALUE;
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);