mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-15 11:00:47 +02:00
feat(cbz-to-pdf,pdf-to-cbz): Converter for CBZ format to and from PDF (#4472)
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
package stirling.software.SPDF.controller.api.converters;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
|
||||
import stirling.software.common.util.CbzUtils;
|
||||
|
||||
class CbzUtilsTest {
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_ValidCbzFile() {
|
||||
MockMultipartFile cbzFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.cbz", "application/zip", "test content".getBytes());
|
||||
|
||||
assertTrue(CbzUtils.isCbzFile(cbzFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_ValidZipFile() {
|
||||
MockMultipartFile zipFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.zip", "application/zip", "test content".getBytes());
|
||||
|
||||
assertTrue(CbzUtils.isCbzFile(zipFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_InvalidFile() {
|
||||
MockMultipartFile textFile =
|
||||
new MockMultipartFile("file", "test.txt", "text/plain", "test content".getBytes());
|
||||
|
||||
assertFalse(CbzUtils.isCbzFile(textFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_NoFilename() {
|
||||
MockMultipartFile noNameFile =
|
||||
new MockMultipartFile("file", null, "application/zip", "test content".getBytes());
|
||||
|
||||
assertFalse(CbzUtils.isCbzFile(noNameFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_PdfFile() {
|
||||
MockMultipartFile pdfFile =
|
||||
new MockMultipartFile(
|
||||
"file", "document.pdf", "application/pdf", "pdf content".getBytes());
|
||||
|
||||
assertFalse(CbzUtils.isCbzFile(pdfFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_JpegFile() {
|
||||
MockMultipartFile jpegFile =
|
||||
new MockMultipartFile("file", "image.jpg", "image/jpeg", "jpeg content".getBytes());
|
||||
|
||||
assertFalse(CbzUtils.isCbzFile(jpegFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_RarFile() {
|
||||
MockMultipartFile rarFile =
|
||||
new MockMultipartFile(
|
||||
"file",
|
||||
"archive.rar",
|
||||
"application/x-rar-compressed",
|
||||
"rar content".getBytes());
|
||||
|
||||
assertFalse(CbzUtils.isCbzFile(rarFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIsCbzFile_MixedCaseExtension() {
|
||||
MockMultipartFile cbzFile =
|
||||
new MockMultipartFile(
|
||||
"file", "test.CBZ", "application/zip", "test content".getBytes());
|
||||
|
||||
assertTrue(CbzUtils.isCbzFile(cbzFile));
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package stirling.software.SPDF.controller.api.converters;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.util.PdfToCbzUtils;
|
||||
|
||||
public class PdfToCbzUtilsTest {
|
||||
|
||||
@Mock private CustomPDFDocumentFactory pdfDocumentFactory;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPdfFile() {
|
||||
MockMultipartFile pdfFile =
|
||||
new MockMultipartFile("test", "test.pdf", "application/pdf", new byte[10]);
|
||||
MockMultipartFile nonPdfFile =
|
||||
new MockMultipartFile("test", "test.txt", "text/plain", new byte[10]);
|
||||
MockMultipartFile noNameFile =
|
||||
new MockMultipartFile("test", null, "application/pdf", new byte[10]);
|
||||
|
||||
Assertions.assertTrue(PdfToCbzUtils.isPdfFile(pdfFile));
|
||||
Assertions.assertFalse(PdfToCbzUtils.isPdfFile(nonPdfFile));
|
||||
Assertions.assertFalse(PdfToCbzUtils.isPdfFile(noNameFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertPdfToCbz_NullFile() {
|
||||
IllegalArgumentException exception =
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
PdfToCbzUtils.convertPdfToCbz(null, 300, pdfDocumentFactory);
|
||||
});
|
||||
Assertions.assertEquals("File cannot be null or empty", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertPdfToCbz_EmptyFile() {
|
||||
MockMultipartFile emptyFile =
|
||||
new MockMultipartFile("test", "test.pdf", "application/pdf", new byte[0]);
|
||||
|
||||
IllegalArgumentException exception =
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
PdfToCbzUtils.convertPdfToCbz(emptyFile, 300, pdfDocumentFactory);
|
||||
});
|
||||
Assertions.assertEquals("File cannot be null or empty", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertPdfToCbz_NonPdfFile() {
|
||||
MockMultipartFile nonPdfFile =
|
||||
new MockMultipartFile("test", "test.txt", "text/plain", new byte[10]);
|
||||
|
||||
IllegalArgumentException exception =
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
PdfToCbzUtils.convertPdfToCbz(nonPdfFile, 300, pdfDocumentFactory);
|
||||
});
|
||||
Assertions.assertEquals("File must be a PDF", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertPdfToCbz_ValidPdf() throws IOException {
|
||||
// Create a simple mock PDF
|
||||
MockMultipartFile pdfFile =
|
||||
new MockMultipartFile("test", "test.pdf", "application/pdf", new byte[100]);
|
||||
|
||||
// Mock the PDF document
|
||||
PDDocument mockDocument = Mockito.mock(PDDocument.class);
|
||||
Mockito.when(mockDocument.getNumberOfPages()).thenReturn(1);
|
||||
Mockito.when(pdfDocumentFactory.load(pdfFile)).thenReturn(mockDocument);
|
||||
|
||||
// structure
|
||||
Assertions.assertThrows(
|
||||
Exception.class,
|
||||
() -> {
|
||||
PdfToCbzUtils.convertPdfToCbz(pdfFile, 300, pdfDocumentFactory);
|
||||
});
|
||||
|
||||
// Verify that load was called
|
||||
Mockito.verify(pdfDocumentFactory).load(pdfFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user