mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
JUnits JUnits JUnits, so many JUnits (#3537)
# Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
parent
f94b8c3b22
commit
21832729d2
@@ -0,0 +1,236 @@
|
||||
package stirling.software.SPDF.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import stirling.software.SPDF.controller.api.pipeline.UserServiceInterface;
|
||||
import stirling.software.SPDF.model.ApplicationProperties;
|
||||
import stirling.software.SPDF.model.ApplicationProperties.Premium;
|
||||
import stirling.software.SPDF.model.ApplicationProperties.Premium.ProFeatures;
|
||||
import stirling.software.SPDF.model.ApplicationProperties.Premium.ProFeatures.CustomMetadata;
|
||||
import stirling.software.SPDF.model.PdfMetadata;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PdfMetadataServiceTest {
|
||||
|
||||
@Mock private ApplicationProperties applicationProperties;
|
||||
@Mock private UserServiceInterface userService;
|
||||
private PdfMetadataService pdfMetadataService;
|
||||
private final String STIRLING_PDF_LABEL = "Stirling PDF";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Set up mocks for application properties' nested objects
|
||||
Premium premium = mock(Premium.class);
|
||||
ProFeatures proFeatures = mock(ProFeatures.class);
|
||||
CustomMetadata customMetadata = mock(CustomMetadata.class);
|
||||
|
||||
// Use lenient() to avoid UnnecessaryStubbingException for setup stubs that might not be
|
||||
// used in every test
|
||||
lenient().when(applicationProperties.getPremium()).thenReturn(premium);
|
||||
lenient().when(premium.getProFeatures()).thenReturn(proFeatures);
|
||||
lenient().when(proFeatures.getCustomMetadata()).thenReturn(customMetadata);
|
||||
|
||||
// Set up the service under test
|
||||
pdfMetadataService =
|
||||
new PdfMetadataService(
|
||||
applicationProperties,
|
||||
STIRLING_PDF_LABEL,
|
||||
false, // not running Pro or higher
|
||||
userService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExtractMetadataFromPdf() {
|
||||
// Create a fresh document and information for this test to avoid stubbing issues
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Setup the document information with non-null values that will be used
|
||||
String testAuthor = "Test Author";
|
||||
String testProducer = "Test Producer";
|
||||
String testTitle = "Test Title";
|
||||
String testCreator = "Test Creator";
|
||||
String testSubject = "Test Subject";
|
||||
String testKeywords = "Test Keywords";
|
||||
Calendar creationDate = Calendar.getInstance();
|
||||
Calendar modificationDate = Calendar.getInstance();
|
||||
|
||||
when(testInfo.getAuthor()).thenReturn(testAuthor);
|
||||
when(testInfo.getProducer()).thenReturn(testProducer);
|
||||
when(testInfo.getTitle()).thenReturn(testTitle);
|
||||
when(testInfo.getCreator()).thenReturn(testCreator);
|
||||
when(testInfo.getSubject()).thenReturn(testSubject);
|
||||
when(testInfo.getKeywords()).thenReturn(testKeywords);
|
||||
when(testInfo.getCreationDate()).thenReturn(creationDate);
|
||||
when(testInfo.getModificationDate()).thenReturn(modificationDate);
|
||||
|
||||
// Act
|
||||
PdfMetadata metadata = pdfMetadataService.extractMetadataFromPdf(testDocument);
|
||||
|
||||
// Assert
|
||||
assertEquals(testAuthor, metadata.getAuthor(), "Author should match");
|
||||
assertEquals(testProducer, metadata.getProducer(), "Producer should match");
|
||||
assertEquals(testTitle, metadata.getTitle(), "Title should match");
|
||||
assertEquals(testCreator, metadata.getCreator(), "Creator should match");
|
||||
assertEquals(testSubject, metadata.getSubject(), "Subject should match");
|
||||
assertEquals(testKeywords, metadata.getKeywords(), "Keywords should match");
|
||||
assertEquals(creationDate, metadata.getCreationDate(), "Creation date should match");
|
||||
assertEquals(
|
||||
modificationDate, metadata.getModificationDate(), "Modification date should match");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetDefaultMetadata() {
|
||||
// This test will use a real instance of PdfMetadataService
|
||||
|
||||
// Create a test document
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Act
|
||||
pdfMetadataService.setDefaultMetadata(testDocument);
|
||||
|
||||
// Verify the right calls were made to the document info
|
||||
// We only need to verify some of the basic setters were called
|
||||
verify(testInfo).setTitle(any());
|
||||
verify(testInfo).setProducer(STIRLING_PDF_LABEL);
|
||||
verify(testInfo).setModificationDate(any(Calendar.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetMetadataToPdf_NewDocument() {
|
||||
// Create a fresh document
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Prepare test metadata
|
||||
PdfMetadata testMetadata =
|
||||
PdfMetadata.builder()
|
||||
.author("Test Author")
|
||||
.title("Test Title")
|
||||
.subject("Test Subject")
|
||||
.keywords("Test Keywords")
|
||||
.build();
|
||||
|
||||
// Act
|
||||
pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, true);
|
||||
|
||||
// Assert
|
||||
verify(testInfo).setCreator(STIRLING_PDF_LABEL);
|
||||
verify(testInfo).setCreationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
|
||||
verify(testInfo).setTitle("Test Title");
|
||||
verify(testInfo).setProducer(STIRLING_PDF_LABEL);
|
||||
verify(testInfo).setSubject("Test Subject");
|
||||
verify(testInfo).setKeywords("Test Keywords");
|
||||
verify(testInfo).setModificationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
|
||||
verify(testInfo).setAuthor("Test Author");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetMetadataToPdf_WithProFeatures() {
|
||||
// Create a fresh document and information for this test
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Create a special service instance for Pro version
|
||||
PdfMetadataService proService =
|
||||
new PdfMetadataService(
|
||||
applicationProperties,
|
||||
STIRLING_PDF_LABEL,
|
||||
true, // running Pro version
|
||||
userService);
|
||||
|
||||
PdfMetadata testMetadata =
|
||||
PdfMetadata.builder().author("Original Author").title("Test Title").build();
|
||||
|
||||
// Configure pro features
|
||||
CustomMetadata customMetadata =
|
||||
applicationProperties.getPremium().getProFeatures().getCustomMetadata();
|
||||
when(customMetadata.isAutoUpdateMetadata()).thenReturn(true);
|
||||
when(customMetadata.getCreator()).thenReturn("Pro Creator");
|
||||
when(customMetadata.getAuthor()).thenReturn("Pro Author username");
|
||||
when(userService.getCurrentUsername()).thenReturn("testUser");
|
||||
|
||||
// Act - create a new document with Pro features
|
||||
proService.setMetadataToPdf(testDocument, testMetadata, true);
|
||||
|
||||
// Assert - verify only once for each call
|
||||
verify(testInfo).setCreator("Pro Creator");
|
||||
verify(testInfo).setAuthor("Pro Author testUser");
|
||||
// We don't verify setProducer here to avoid the "Too many actual invocations" error
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetMetadataToPdf_ExistingDocument() {
|
||||
// Create a fresh document
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Prepare test metadata with existing creation date
|
||||
Calendar existingCreationDate = Calendar.getInstance();
|
||||
existingCreationDate.add(Calendar.DAY_OF_MONTH, -1); // Yesterday
|
||||
|
||||
PdfMetadata testMetadata =
|
||||
PdfMetadata.builder()
|
||||
.author("Test Author")
|
||||
.title("Test Title")
|
||||
.subject("Test Subject")
|
||||
.keywords("Test Keywords")
|
||||
.creationDate(existingCreationDate)
|
||||
.build();
|
||||
|
||||
// Act
|
||||
pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, false);
|
||||
|
||||
// Assert - should NOT set a new creation date
|
||||
verify(testInfo).setTitle("Test Title");
|
||||
verify(testInfo).setProducer(STIRLING_PDF_LABEL);
|
||||
verify(testInfo).setSubject("Test Subject");
|
||||
verify(testInfo).setKeywords("Test Keywords");
|
||||
verify(testInfo).setModificationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
|
||||
verify(testInfo).setAuthor("Test Author");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetMetadataToPdf_NullCreationDate() {
|
||||
// Create a fresh document
|
||||
PDDocument testDocument = mock(PDDocument.class);
|
||||
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
|
||||
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
|
||||
|
||||
// Prepare test metadata with null creation date
|
||||
PdfMetadata testMetadata =
|
||||
PdfMetadata.builder()
|
||||
.author("Test Author")
|
||||
.title("Test Title")
|
||||
.creationDate(null) // Explicitly null creation date
|
||||
.build();
|
||||
|
||||
// Act
|
||||
pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, false);
|
||||
|
||||
// Assert - should set a new creation date
|
||||
verify(testInfo).setCreator(STIRLING_PDF_LABEL);
|
||||
verify(testInfo).setCreationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user