Stirling-PDF/src/test/java/stirling/software/SPDF/service/PdfMetadataServiceBasicTest.java
Anthony Stirling 21832729d2
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 <175728472+Copilot@users.noreply.github.com>
Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
2025-05-19 14:12:06 +01:00

110 lines
4.6 KiB
Java

package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
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 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;
class PdfMetadataServiceBasicTest {
private ApplicationProperties applicationProperties;
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
applicationProperties = mock(ApplicationProperties.class);
Premium premium = mock(Premium.class);
ProFeatures proFeatures = mock(ProFeatures.class);
CustomMetadata customMetadata = mock(CustomMetadata.class);
userService = mock(UserServiceInterface.class);
when(applicationProperties.getPremium()).thenReturn(premium);
when(premium.getProFeatures()).thenReturn(proFeatures);
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 test document
PDDocument testDocument = mock(PDDocument.class);
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
// Set up expected metadata values
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();
// Configure mock returns
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() {
// Create test document
PDDocument testDocument = mock(PDDocument.class);
PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
when(testDocument.getDocumentInformation()).thenReturn(testInfo);
// Act
pdfMetadataService.setDefaultMetadata(testDocument);
// Verify basic calls
verify(testInfo, times(1)).setModificationDate(any(Calendar.class));
verify(testInfo, times(1)).setProducer(STIRLING_PDF_LABEL);
}
}