refactor: move modules under app/ directory and update file paths (#3938)

# Description of Changes

- **What was changed:**  
- Renamed top-level directories: `stirling-pdf` → `app/core`, `common` →
`app/common`, `proprietary` → `app/proprietary`.
- Updated all path references in `.gitattributes`, GitHub workflows
(`.github/workflows/*`), scripts (`.github/scripts/*`), `.gitignore`,
Dockerfiles, license files, and template settings to reflect the new
structure.
- Added a new CI job `check-generateOpenApiDocs` to generate and upload
OpenAPI documentation.
- Removed redundant `@Autowired` annotations from `TempFileShutdownHook`
and `UnlockPDFFormsController`.
- Minor formatting and comment adjustments in YAML templates and
resource files.

- **Why the change was made:**  
- To introduce a clear `app/` directory hierarchy for core, common, and
proprietary modules, improving organization and maintainability.
- To ensure continuous integration and Docker builds continue to work
seamlessly with the reorganized structure.
- To automate OpenAPI documentation generation as part of the CI
pipeline.

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/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/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] 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/devGuide/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/devGuide/DeveloperGuide.md#6-testing)
for more details.
This commit is contained in:
Ludy
2025-07-14 21:53:11 +02:00
committed by GitHub
parent 38b53d7cc1
commit 299d52c517
1155 changed files with 219 additions and 276 deletions

View File

@@ -0,0 +1,39 @@
package stirling.software.SPDF;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.env.Environment;
import stirling.software.common.model.ApplicationProperties;
@ExtendWith(MockitoExtension.class)
public class SPDFApplicationTest {
@Mock private Environment env;
@Mock private ApplicationProperties applicationProperties;
@InjectMocks private SPDFApplication sPDFApplication;
@BeforeEach
public void setUp() {
SPDFApplication.setServerPortStatic("8080");
}
@Test
public void testSetServerPortStatic() {
SPDFApplication.setServerPortStatic("9090");
assertEquals("9090", SPDFApplication.getStaticPort());
}
@Test
public void testGetStaticPort() {
assertEquals("8080", SPDFApplication.getStaticPort());
}
}

View File

@@ -0,0 +1,382 @@
package stirling.software.SPDF.controller.api;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import stirling.software.SPDF.controller.api.EditTableOfContentsController.BookmarkItem;
import stirling.software.SPDF.model.api.EditTableOfContentsRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
@ExtendWith(MockitoExtension.class)
class EditTableOfContentsControllerTest {
@Mock
private CustomPDFDocumentFactory pdfDocumentFactory;
@Mock
private ObjectMapper objectMapper;
@InjectMocks
private EditTableOfContentsController editTableOfContentsController;
private MockMultipartFile mockFile;
private PDDocument mockDocument;
private PDDocumentCatalog mockCatalog;
private PDPageTree mockPages;
private PDPage mockPage1;
private PDPage mockPage2;
private PDDocumentOutline mockOutline;
private PDOutlineItem mockOutlineItem;
@BeforeEach
void setUp() {
mockFile = new MockMultipartFile("file", "test.pdf", "application/pdf", "PDF content".getBytes());
mockDocument = mock(PDDocument.class);
mockCatalog = mock(PDDocumentCatalog.class);
mockPages = mock(PDPageTree.class);
mockPage1 = mock(PDPage.class);
mockPage2 = mock(PDPage.class);
mockOutline = mock(PDDocumentOutline.class);
mockOutlineItem = mock(PDOutlineItem.class);
}
@Test
void testExtractBookmarks_WithExistingBookmarks_Success() throws Exception {
// Given
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getDocumentOutline()).thenReturn(mockOutline);
when(mockOutline.getFirstChild()).thenReturn(mockOutlineItem);
when(mockOutlineItem.getTitle()).thenReturn("Chapter 1");
when(mockOutlineItem.findDestinationPage(mockDocument)).thenReturn(mockPage1);
when(mockDocument.getPages()).thenReturn(mockPages);
when(mockPages.indexOf(mockPage1)).thenReturn(0);
when(mockOutlineItem.getFirstChild()).thenReturn(null);
when(mockOutlineItem.getNextSibling()).thenReturn(null);
// When
List<Map<String, Object>> result = editTableOfContentsController.extractBookmarks(mockFile);
// Then
assertNotNull(result);
assertEquals(1, result.size());
Map<String, Object> bookmark = result.get(0);
assertEquals("Chapter 1", bookmark.get("title"));
assertEquals(1, bookmark.get("pageNumber")); // 1-based
assertInstanceOf(List.class, bookmark.get("children"));
verify(mockDocument).close();
}
@Test
void testExtractBookmarks_NoOutline_ReturnsEmptyList() throws Exception {
// Given
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getDocumentOutline()).thenReturn(null);
// When
List<Map<String, Object>> result = editTableOfContentsController.extractBookmarks(mockFile);
// Then
assertNotNull(result);
assertTrue(result.isEmpty());
verify(mockDocument).close();
}
@Test
void testExtractBookmarks_WithNestedBookmarks_Success() throws Exception {
// Given
PDOutlineItem childItem = mock(PDOutlineItem.class);
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getDocumentOutline()).thenReturn(mockOutline);
when(mockOutline.getFirstChild()).thenReturn(mockOutlineItem);
// Parent bookmark
when(mockOutlineItem.getTitle()).thenReturn("Chapter 1");
when(mockOutlineItem.findDestinationPage(mockDocument)).thenReturn(mockPage1);
when(mockDocument.getPages()).thenReturn(mockPages);
when(mockPages.indexOf(mockPage1)).thenReturn(0);
when(mockOutlineItem.getFirstChild()).thenReturn(childItem);
when(mockOutlineItem.getNextSibling()).thenReturn(null);
// Child bookmark
when(childItem.getTitle()).thenReturn("Section 1.1");
when(childItem.findDestinationPage(mockDocument)).thenReturn(mockPage2);
when(mockPages.indexOf(mockPage2)).thenReturn(1);
when(childItem.getFirstChild()).thenReturn(null);
when(childItem.getNextSibling()).thenReturn(null);
// When
List<Map<String, Object>> result = editTableOfContentsController.extractBookmarks(mockFile);
// Then
assertNotNull(result);
assertEquals(1, result.size());
Map<String, Object> parentBookmark = result.get(0);
assertEquals("Chapter 1", parentBookmark.get("title"));
assertEquals(1, parentBookmark.get("pageNumber"));
@SuppressWarnings("unchecked")
List<Map<String, Object>> children = (List<Map<String, Object>>) parentBookmark.get("children");
assertEquals(1, children.size());
Map<String, Object> childBookmark = children.get(0);
assertEquals("Section 1.1", childBookmark.get("title"));
assertEquals(2, childBookmark.get("pageNumber"));
verify(mockDocument).close();
}
@Test
void testExtractBookmarks_PageNotFound_UsesPageOne() throws Exception {
// Given
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockCatalog.getDocumentOutline()).thenReturn(mockOutline);
when(mockOutline.getFirstChild()).thenReturn(mockOutlineItem);
when(mockOutlineItem.getTitle()).thenReturn("Chapter 1");
when(mockOutlineItem.findDestinationPage(mockDocument)).thenReturn(null); // Page not found
when(mockOutlineItem.getFirstChild()).thenReturn(null);
when(mockOutlineItem.getNextSibling()).thenReturn(null);
// When
List<Map<String, Object>> result = editTableOfContentsController.extractBookmarks(mockFile);
// Then
assertNotNull(result);
assertEquals(1, result.size());
Map<String, Object> bookmark = result.get(0);
assertEquals("Chapter 1", bookmark.get("title"));
assertEquals(1, bookmark.get("pageNumber")); // Default to page 1
verify(mockDocument).close();
}
@Test
void testEditTableOfContents_Success() throws Exception {
// Given
EditTableOfContentsRequest request = new EditTableOfContentsRequest();
request.setFileInput(mockFile);
request.setBookmarkData("[{\"title\":\"Chapter 1\",\"pageNumber\":1,\"children\":[]}]");
request.setReplaceExisting(true);
List<BookmarkItem> bookmarks = new ArrayList<>();
BookmarkItem bookmark = new BookmarkItem();
bookmark.setTitle("Chapter 1");
bookmark.setPageNumber(1);
bookmark.setChildren(new ArrayList<>());
bookmarks.add(bookmark);
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(objectMapper.readValue(eq(request.getBookmarkData()), any(TypeReference.class))).thenReturn(bookmarks);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockDocument.getNumberOfPages()).thenReturn(5);
when(mockDocument.getPage(0)).thenReturn(mockPage1);
// Mock saving behavior
doAnswer(invocation -> {
ByteArrayOutputStream baos = invocation.getArgument(0);
baos.write("mocked pdf content".getBytes());
return null;
}).when(mockDocument).save(any(ByteArrayOutputStream.class));
// When
ResponseEntity<byte[]> result = editTableOfContentsController.editTableOfContents(request);
// Then
assertNotNull(result);
assertNotNull(result.getBody());
ArgumentCaptor<PDDocumentOutline> outlineCaptor = ArgumentCaptor.forClass(PDDocumentOutline.class);
verify(mockCatalog).setDocumentOutline(outlineCaptor.capture());
PDDocumentOutline capturedOutline = outlineCaptor.getValue();
assertNotNull(capturedOutline);
verify(mockDocument).close();
}
@Test
void testEditTableOfContents_WithNestedBookmarks_Success() throws Exception {
// Given
EditTableOfContentsRequest request = new EditTableOfContentsRequest();
request.setFileInput(mockFile);
String bookmarkJson = "[{\"title\":\"Chapter 1\",\"pageNumber\":1,\"children\":[{\"title\":\"Section 1.1\",\"pageNumber\":2,\"children\":[]}]}]";
request.setBookmarkData(bookmarkJson);
List<BookmarkItem> bookmarks = new ArrayList<>();
BookmarkItem parentBookmark = new BookmarkItem();
parentBookmark.setTitle("Chapter 1");
parentBookmark.setPageNumber(1);
BookmarkItem childBookmark = new BookmarkItem();
childBookmark.setTitle("Section 1.1");
childBookmark.setPageNumber(2);
childBookmark.setChildren(new ArrayList<>());
List<BookmarkItem> children = new ArrayList<>();
children.add(childBookmark);
parentBookmark.setChildren(children);
bookmarks.add(parentBookmark);
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(objectMapper.readValue(eq(bookmarkJson), any(TypeReference.class))).thenReturn(bookmarks);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockDocument.getNumberOfPages()).thenReturn(5);
when(mockDocument.getPage(0)).thenReturn(mockPage1);
when(mockDocument.getPage(1)).thenReturn(mockPage2);
doAnswer(invocation -> {
ByteArrayOutputStream baos = invocation.getArgument(0);
baos.write("mocked pdf content".getBytes());
return null;
}).when(mockDocument).save(any(ByteArrayOutputStream.class));
// When
ResponseEntity<byte[]> result = editTableOfContentsController.editTableOfContents(request);
// Then
assertNotNull(result);
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verify(mockDocument).close();
}
@Test
void testEditTableOfContents_PageNumberBounds_ClampsValues() throws Exception {
// Given
EditTableOfContentsRequest request = new EditTableOfContentsRequest();
request.setFileInput(mockFile);
request.setBookmarkData("[{\"title\":\"Chapter 1\",\"pageNumber\":-5,\"children\":[]},{\"title\":\"Chapter 2\",\"pageNumber\":100,\"children\":[]}]");
List<BookmarkItem> bookmarks = new ArrayList<>();
BookmarkItem bookmark1 = new BookmarkItem();
bookmark1.setTitle("Chapter 1");
bookmark1.setPageNumber(-5); // Negative page number
bookmark1.setChildren(new ArrayList<>());
BookmarkItem bookmark2 = new BookmarkItem();
bookmark2.setTitle("Chapter 2");
bookmark2.setPageNumber(100); // Page number exceeds document pages
bookmark2.setChildren(new ArrayList<>());
bookmarks.add(bookmark1);
bookmarks.add(bookmark2);
when(pdfDocumentFactory.load(mockFile)).thenReturn(mockDocument);
when(objectMapper.readValue(eq(request.getBookmarkData()), any(TypeReference.class))).thenReturn(bookmarks);
when(mockDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockDocument.getNumberOfPages()).thenReturn(5);
when(mockDocument.getPage(0)).thenReturn(mockPage1); // For negative page number
when(mockDocument.getPage(4)).thenReturn(mockPage2); // For page number exceeding bounds
doAnswer(invocation -> {
ByteArrayOutputStream baos = invocation.getArgument(0);
baos.write("mocked pdf content".getBytes());
return null;
}).when(mockDocument).save(any(ByteArrayOutputStream.class));
// When
ResponseEntity<byte[]> result = editTableOfContentsController.editTableOfContents(request);
// Then
assertNotNull(result);
verify(mockDocument).getPage(0); // Clamped to first page
verify(mockDocument).getPage(4); // Clamped to last page
verify(mockDocument).close();
}
@Test
void testCreateOutlineItem_ValidPageNumber_Success() throws Exception {
// Given
BookmarkItem bookmark = new BookmarkItem();
bookmark.setTitle("Test Chapter");
bookmark.setPageNumber(3);
when(mockDocument.getNumberOfPages()).thenReturn(5);
when(mockDocument.getPage(2)).thenReturn(mockPage1); // 0-indexed
// When
Method createOutlineItemMethod = EditTableOfContentsController.class.getDeclaredMethod("createOutlineItem", PDDocument.class, BookmarkItem.class);
createOutlineItemMethod.setAccessible(true);
PDOutlineItem result = (PDOutlineItem) createOutlineItemMethod.invoke(editTableOfContentsController, mockDocument, bookmark);
// Then
assertNotNull(result);
verify(mockDocument).getPage(2);
}
@Test
void testBookmarkItem_GettersAndSetters() {
// Given
BookmarkItem bookmark = new BookmarkItem();
List<BookmarkItem> children = new ArrayList<>();
// When
bookmark.setTitle("Test Title");
bookmark.setPageNumber(5);
bookmark.setChildren(children);
// Then
assertEquals("Test Title", bookmark.getTitle());
assertEquals(5, bookmark.getPageNumber());
assertEquals(children, bookmark.getChildren());
}
@Test
void testEditTableOfContents_IOExceptionDuringLoad_ThrowsException() throws Exception {
// Given
EditTableOfContentsRequest request = new EditTableOfContentsRequest();
request.setFileInput(mockFile);
when(pdfDocumentFactory.load(mockFile)).thenThrow(new RuntimeException("Failed to load PDF"));
// When & Then
assertThrows(RuntimeException.class, () -> editTableOfContentsController.editTableOfContents(request));
}
@Test
void testExtractBookmarks_IOExceptionDuringLoad_ThrowsException() throws Exception {
// Given
when(pdfDocumentFactory.load(mockFile)).thenThrow(new RuntimeException("Failed to load PDF"));
// When & Then
assertThrows(RuntimeException.class, () -> editTableOfContentsController.extractBookmarks(mockFile));
}
}

View File

@@ -0,0 +1,279 @@
package stirling.software.SPDF.controller.api;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import stirling.software.common.service.CustomPDFDocumentFactory;
@ExtendWith(MockitoExtension.class)
class MergeControllerTest {
@Mock
private CustomPDFDocumentFactory pdfDocumentFactory;
@InjectMocks
private MergeController mergeController;
private MockMultipartFile mockFile1;
private MockMultipartFile mockFile2;
private MockMultipartFile mockFile3;
private PDDocument mockDocument;
private PDDocument mockMergedDocument;
private PDDocumentCatalog mockCatalog;
private PDPageTree mockPages;
private PDPage mockPage1;
private PDPage mockPage2;
@BeforeEach
void setUp() {
mockFile1 = new MockMultipartFile("file1", "document1.pdf", "application/pdf", "PDF content 1".getBytes());
mockFile2 = new MockMultipartFile("file2", "document2.pdf", "application/pdf", "PDF content 2".getBytes());
mockFile3 = new MockMultipartFile("file3", "chapter3.pdf", "application/pdf", "PDF content 3".getBytes());
mockDocument = mock(PDDocument.class);
mockMergedDocument = mock(PDDocument.class);
mockCatalog = mock(PDDocumentCatalog.class);
mockPages = mock(PDPageTree.class);
mockPage1 = mock(PDPage.class);
mockPage2 = mock(PDPage.class);
}
@Test
void testAddTableOfContents_WithMultipleFiles_Success() throws Exception {
// Given
MultipartFile[] files = {mockFile1, mockFile2, mockFile3};
// Mock the merged document setup
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockMergedDocument.getNumberOfPages()).thenReturn(6);
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
when(mockMergedDocument.getPage(2)).thenReturn(mockPage2);
when(mockMergedDocument.getPage(4)).thenReturn(mockPage1);
// Mock individual document loading for page count
PDDocument doc1 = mock(PDDocument.class);
PDDocument doc2 = mock(PDDocument.class);
PDDocument doc3 = mock(PDDocument.class);
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
when(pdfDocumentFactory.load(mockFile2)).thenReturn(doc2);
when(pdfDocumentFactory.load(mockFile3)).thenReturn(doc3);
when(doc1.getNumberOfPages()).thenReturn(2);
when(doc2.getNumberOfPages()).thenReturn(2);
when(doc3.getNumberOfPages()).thenReturn(2);
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files);
// Then
ArgumentCaptor<PDDocumentOutline> outlineCaptor = ArgumentCaptor.forClass(PDDocumentOutline.class);
verify(mockCatalog).setDocumentOutline(outlineCaptor.capture());
PDDocumentOutline capturedOutline = outlineCaptor.getValue();
assertNotNull(capturedOutline);
// Verify that documents were loaded for page count
verify(pdfDocumentFactory).load(mockFile1);
verify(pdfDocumentFactory).load(mockFile2);
verify(pdfDocumentFactory).load(mockFile3);
// Verify document closing
verify(doc1).close();
verify(doc2).close();
verify(doc3).close();
}
@Test
void testAddTableOfContents_WithSingleFile_Success() throws Exception {
// Given
MultipartFile[] files = {mockFile1};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockMergedDocument.getNumberOfPages()).thenReturn(3);
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
PDDocument doc1 = mock(PDDocument.class);
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
when(doc1.getNumberOfPages()).thenReturn(3);
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files);
// Then
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verify(pdfDocumentFactory).load(mockFile1);
verify(doc1).close();
}
@Test
void testAddTableOfContents_WithEmptyArray_Success() throws Exception {
// Given
MultipartFile[] files = {};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files);
// Then
verify(mockMergedDocument).getDocumentCatalog();
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verifyNoInteractions(pdfDocumentFactory);
}
@Test
void testAddTableOfContents_WithIOException_HandlesGracefully() throws Exception {
// Given
MultipartFile[] files = {mockFile1, mockFile2};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockMergedDocument.getNumberOfPages()).thenReturn(4);
when(mockMergedDocument.getPage(anyInt())).thenReturn(mockPage1); // Use anyInt() to avoid stubbing conflicts
// First document loads successfully
PDDocument doc1 = mock(PDDocument.class);
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
when(doc1.getNumberOfPages()).thenReturn(2);
// Second document throws IOException
when(pdfDocumentFactory.load(mockFile2)).thenThrow(new IOException("Failed to load document"));
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
// Should not throw exception
assertDoesNotThrow(() ->
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
);
// Then
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verify(pdfDocumentFactory).load(mockFile1);
verify(pdfDocumentFactory).load(mockFile2);
verify(doc1).close();
}
@Test
void testAddTableOfContents_FilenameWithoutExtension_UsesFullName() throws Exception {
// Given
MockMultipartFile fileWithoutExtension = new MockMultipartFile("file", "document_no_ext", "application/pdf", "PDF content".getBytes());
MultipartFile[] files = {fileWithoutExtension};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockMergedDocument.getNumberOfPages()).thenReturn(1);
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
PDDocument doc = mock(PDDocument.class);
when(pdfDocumentFactory.load(fileWithoutExtension)).thenReturn(doc);
when(doc.getNumberOfPages()).thenReturn(1);
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files);
// Then
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verify(doc).close();
}
@Test
void testAddTableOfContents_PageIndexExceedsDocumentPages_HandlesGracefully() throws Exception {
// Given
MultipartFile[] files = {mockFile1};
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
when(mockMergedDocument.getNumberOfPages()).thenReturn(0); // No pages in merged document
PDDocument doc1 = mock(PDDocument.class);
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
when(doc1.getNumberOfPages()).thenReturn(3);
// When
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
addTableOfContentsMethod.setAccessible(true);
// Should not throw exception
assertDoesNotThrow(() ->
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
);
// Then
verify(mockCatalog).setDocumentOutline(any(PDDocumentOutline.class));
verify(mockMergedDocument, never()).getPage(anyInt());
verify(doc1).close();
}
@Test
void testMergeDocuments_Success() throws IOException {
// Given
PDDocument doc1 = mock(PDDocument.class);
PDDocument doc2 = mock(PDDocument.class);
List<PDDocument> documents = Arrays.asList(doc1, doc2);
PDPageTree pages1 = mock(PDPageTree.class);
PDPageTree pages2 = mock(PDPageTree.class);
PDPage page1 = mock(PDPage.class);
PDPage page2 = mock(PDPage.class);
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
when(doc1.getPages()).thenReturn(pages1);
when(doc2.getPages()).thenReturn(pages2);
when(pages1.iterator()).thenReturn(Arrays.asList(page1).iterator());
when(pages2.iterator()).thenReturn(Arrays.asList(page2).iterator());
// When
PDDocument result = mergeController.mergeDocuments(documents);
// Then
assertNotNull(result);
assertEquals(mockMergedDocument, result);
verify(mockMergedDocument).addPage(page1);
verify(mockMergedDocument).addPage(page2);
}
@Test
void testMergeDocuments_EmptyList_ReturnsEmptyDocument() throws IOException {
// Given
List<PDDocument> documents = Arrays.asList();
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
// When
PDDocument result = mergeController.mergeDocuments(documents);
// Then
assertNotNull(result);
assertEquals(mockMergedDocument, result);
verify(mockMergedDocument, never()).addPage(any(PDPage.class));
}
}

View File

@@ -0,0 +1,96 @@
package stirling.software.SPDF.controller.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import stirling.software.common.service.CustomPDFDocumentFactory;
class RearrangePagesPDFControllerTest {
@Mock private CustomPDFDocumentFactory mockPdfDocumentFactory;
private RearrangePagesPDFController sut;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
sut = new RearrangePagesPDFController(mockPdfDocumentFactory);
}
/** Tests the behavior of the oddEvenMerge method when there are no pages in the document. */
@Test
void oddEvenMerge_noPages() {
int totalNumberOfPages = 0;
List<Integer> newPageOrder = sut.oddEvenMerge(totalNumberOfPages);
assertNotNull(newPageOrder, "Returning null instead of page order list");
assertEquals(List.of(), newPageOrder, "Page order doesn't match");
}
/**
* Tests the behavior of the oddEvenMerge method when there are odd total pages in the document.
*/
@Test
void oddEvenMerge_oddTotalPageNumber() {
int totalNumberOfPages = 5;
List<Integer> newPageOrder = sut.oddEvenMerge(totalNumberOfPages);
assertNotNull(newPageOrder, "Returning null instead of page order list");
assertEquals(Arrays.asList(0, 3, 1, 4, 2), newPageOrder, "Page order doesn't match");
}
/**
* Tests the behavior of the oddEvenMerge method when there are even total pages in the
* document.
*/
@Test
void oddEvenMerge_evenTotalPageNumber() {
int totalNumberOfPages = 6;
List<Integer> newPageOrder = sut.oddEvenMerge(totalNumberOfPages);
assertNotNull(newPageOrder, "Returning null instead of page order list");
assertEquals(Arrays.asList(0, 3, 1, 4, 2, 5), newPageOrder, "Page order doesn't match");
}
/**
* Tests the behavior of the oddEvenMerge method with multiple test cases of multiple pages.
*
* @param totalNumberOfPages The total number of pages in the document.
* @param expectedPageOrder The expected order of the pages after rearranging.
*/
@ParameterizedTest
@CsvSource({
"1, '0'",
"2, '0,1'",
"3, '0,2,1'",
"4, '0,2,1,3'",
"5, '0,3,1,4,2'",
"6, '0,3,1,4,2,5'",
"10, '0,5,1,6,2,7,3,8,4,9'",
"50, '0,25,1,26,2,27,3,28,4,29,5,30,6,31,7,32,8,33,9,34,10,35,"
+ "11,36,12,37,13,38,14,39,15,40,16,41,17,42,18,43,19,44,20,45,21,46,"
+ "22,47,23,48,24,49'"
})
void oddEvenMerge_multi_test(int totalNumberOfPages, String expectedPageOrder) {
List<Integer> newPageOrder = sut.oddEvenMerge(totalNumberOfPages);
assertNotNull(newPageOrder, "Returning null instead of page order list");
assertEquals(
Arrays.stream(expectedPageOrder.split(",")).map(Integer::parseInt).toList(),
newPageOrder,
"Page order doesn't match");
}
}

View File

@@ -0,0 +1,77 @@
package stirling.software.SPDF.controller.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.junit.jupiter.api.Test;
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.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import stirling.software.SPDF.model.api.general.RotatePDFRequest;
import stirling.software.common.service.CustomPDFDocumentFactory;
@ExtendWith(MockitoExtension.class)
public class RotationControllerTest {
@Mock private CustomPDFDocumentFactory pdfDocumentFactory;
@InjectMocks private RotationController rotationController;
@Test
public void testRotatePDF() throws IOException {
// Create a mock file
MockMultipartFile mockFile =
new MockMultipartFile("file", "test.pdf", "application/pdf", new byte[] {1, 2, 3});
RotatePDFRequest request = new RotatePDFRequest();
request.setFileInput(mockFile);
request.setAngle(90);
PDDocument mockDocument = mock(PDDocument.class);
PDPageTree mockPages = mock(PDPageTree.class);
PDPage mockPage = mock(PDPage.class);
when(pdfDocumentFactory.load(request)).thenReturn(mockDocument);
when(mockDocument.getPages()).thenReturn(mockPages);
when(mockPages.iterator())
.thenReturn(java.util.Collections.singletonList(mockPage).iterator());
when(mockPage.getRotation()).thenReturn(0);
// Act
ResponseEntity<byte[]> response = rotationController.rotatePDF(request);
// Assert
verify(mockPage).setRotation(90);
assertNotNull(response);
assertEquals(200, response.getStatusCode().value());
}
@Test
public void testRotatePDFInvalidAngle() throws IOException {
// Create a mock file
MockMultipartFile mockFile =
new MockMultipartFile("file", "test.pdf", "application/pdf", new byte[] {1, 2, 3});
RotatePDFRequest request = new RotatePDFRequest();
request.setFileInput(mockFile);
request.setAngle(45); // Invalid angle
// Act & Assert: Controller direkt aufrufen und Exception erwarten
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> rotationController.rotatePDF(request));
assertEquals("Angle must be a multiple of 90", exception.getMessage());
}
}

View File

@@ -0,0 +1,71 @@
package stirling.software.SPDF.controller.api.converters;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import stirling.software.SPDF.model.api.converters.UrlToPdfRequest;
import stirling.software.common.configuration.RuntimePathConfig;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.service.CustomPDFDocumentFactory;
public class ConvertWebsiteToPdfTest {
@Mock private CustomPDFDocumentFactory mockPdfDocumentFactory;
@Mock private RuntimePathConfig runtimePathConfig;
private ApplicationProperties applicationProperties;
private ConvertWebsiteToPDF convertWebsiteToPDF;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
applicationProperties = new ApplicationProperties();
applicationProperties.getSystem().setEnableUrlToPDF(true);
convertWebsiteToPDF =
new ConvertWebsiteToPDF(
mockPdfDocumentFactory, runtimePathConfig, applicationProperties);
}
@Test
public void test_exemption_is_thrown_when_invalid_url_format_provided() {
String invalid_format_Url = "invalid-url";
UrlToPdfRequest request = new UrlToPdfRequest();
request.setUrlInput(invalid_format_Url);
// Act
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> {
convertWebsiteToPDF.urlToPdf(request);
});
// Assert
assertEquals("Invalid URL format: provided format is invalid", thrown.getMessage());
}
@Test
public void test_exemption_is_thrown_when_url_is_not_reachable() {
String unreachable_Url = "https://www.googleeeexyz.com";
// Arrange
UrlToPdfRequest request = new UrlToPdfRequest();
request.setUrlInput(unreachable_Url);
// Act
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> {
convertWebsiteToPDF.urlToPdf(request);
});
// Assert
assertEquals("URL is not reachable, please provide a valid URL", thrown.getMessage());
}
}

View File

@@ -0,0 +1,133 @@
package stirling.software.SPDF.controller.api.misc;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import org.mockito.MockedStatic;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import stirling.software.SPDF.model.api.misc.AddAttachmentRequest;
import stirling.software.SPDF.service.AttachmentServiceInterface;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.WebResponseUtils;
@ExtendWith(MockitoExtension.class)
class AttachmentControllerTest {
@Mock
private CustomPDFDocumentFactory pdfDocumentFactory;
@Mock
private AttachmentServiceInterface pdfAttachmentService;
@InjectMocks
private AttachmentController attachmentController;
private MockMultipartFile pdfFile;
private MockMultipartFile attachment1;
private MockMultipartFile attachment2;
private AddAttachmentRequest request;
private PDDocument mockDocument;
private PDDocument modifiedMockDocument;
@BeforeEach
void setUp() {
pdfFile = new MockMultipartFile("fileInput", "test.pdf", "application/pdf", "PDF content".getBytes());
attachment1 = new MockMultipartFile("attachment1", "file1.txt", "text/plain", "File 1 content".getBytes());
attachment2 = new MockMultipartFile("attachment2", "file2.jpg", "image/jpeg", "Image content".getBytes());
request = new AddAttachmentRequest();
mockDocument = mock(PDDocument.class);
modifiedMockDocument = mock(PDDocument.class);
}
@Test
void addAttachments_Success() throws IOException {
List<MultipartFile> attachments = List.of(attachment1, attachment2);
request.setAttachments(attachments);
request.setFileInput(pdfFile);
ResponseEntity<byte[]> expectedResponse = ResponseEntity.ok("modified PDF content".getBytes());
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenReturn(modifiedMockDocument);
try (MockedStatic<WebResponseUtils> mockedWebResponseUtils = mockStatic(WebResponseUtils.class)) {
mockedWebResponseUtils.when(() -> WebResponseUtils.pdfDocToWebResponse(eq(modifiedMockDocument), eq("test_with_attachments.pdf")))
.thenReturn(expectedResponse);
ResponseEntity<byte[]> response = attachmentController.addAttachments(request);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
verify(pdfDocumentFactory).load(pdfFile, false);
verify(pdfAttachmentService).addAttachment(mockDocument, attachments);
}
}
@Test
void addAttachments_SingleAttachment() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
request.setAttachments(attachments);
request.setFileInput(pdfFile);
ResponseEntity<byte[]> expectedResponse = ResponseEntity.ok("modified PDF content".getBytes());
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenReturn(modifiedMockDocument);
try (MockedStatic<WebResponseUtils> mockedWebResponseUtils = mockStatic(WebResponseUtils.class)) {
mockedWebResponseUtils.when(() -> WebResponseUtils.pdfDocToWebResponse(eq(modifiedMockDocument), eq("test_with_attachments.pdf")))
.thenReturn(expectedResponse);
ResponseEntity<byte[]> response = attachmentController.addAttachments(request);
assertNotNull(response);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
verify(pdfDocumentFactory).load(pdfFile, false);
verify(pdfAttachmentService).addAttachment(mockDocument, attachments);
}
}
@Test
void addAttachments_IOExceptionFromPDFLoad() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
request.setAttachments(attachments);
request.setFileInput(pdfFile);
IOException ioException = new IOException("Failed to load PDF");
when(pdfDocumentFactory.load(pdfFile, false)).thenThrow(ioException);
assertThrows(IOException.class, () -> attachmentController.addAttachments(request));
verify(pdfDocumentFactory).load(pdfFile, false);
verifyNoInteractions(pdfAttachmentService);
}
@Test
void addAttachments_IOExceptionFromAttachmentService() throws IOException {
List<MultipartFile> attachments = List.of(attachment1);
request.setAttachments(attachments);
request.setFileInput(pdfFile);
IOException ioException = new IOException("Failed to add attachment");
when(pdfDocumentFactory.load(pdfFile, false)).thenReturn(mockDocument);
when(pdfAttachmentService.addAttachment(mockDocument, attachments)).thenThrow(ioException);
assertThrows(IOException.class, () -> attachmentController.addAttachments(request));
verify(pdfAttachmentService).addAttachment(mockDocument, attachments);
}
}

View File

@@ -0,0 +1,78 @@
package stirling.software.SPDF.controller.api.pipeline;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.List;
import java.util.Map;
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 org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import jakarta.servlet.ServletContext;
import stirling.software.common.service.UserServiceInterface;
import stirling.software.SPDF.model.PipelineConfig;
import stirling.software.SPDF.model.PipelineOperation;
import stirling.software.SPDF.model.PipelineResult;
import stirling.software.SPDF.service.ApiDocService;
@ExtendWith(MockitoExtension.class)
class PipelineProcessorTest {
@Mock ApiDocService apiDocService;
@Mock UserServiceInterface userService;
@Mock ServletContext servletContext;
PipelineProcessor pipelineProcessor;
@BeforeEach
void setUp() {
pipelineProcessor = spy(new PipelineProcessor(apiDocService, userService, servletContext));
}
@Test
void runPipelineWithFilterSetsFlag() throws Exception {
PipelineOperation op = new PipelineOperation();
op.setOperation("filter-page-count");
op.setParameters(Map.of());
PipelineConfig config = new PipelineConfig();
config.setOperations(List.of(op));
Resource file = new ByteArrayResource("data".getBytes()) {
@Override
public String getFilename() {
return "test.pdf";
}
};
List<Resource> files = List.of(file);
when(apiDocService.isMultiInput("filter-page-count")).thenReturn(false);
when(apiDocService.getExtensionTypes(false, "filter-page-count"))
.thenReturn(List.of("pdf"));
doReturn(new ResponseEntity<>(new byte[0], HttpStatus.OK))
.when(pipelineProcessor)
.sendWebRequest(anyString(), any());
PipelineResult result = pipelineProcessor.runPipelineAgainstFiles(files, config);
assertTrue(
result.isFiltersApplied(),
"Filter flag should be true when operation filters file");
assertFalse(result.isHasErrors(), "No errors should occur");
assertTrue(result.getOutputFiles().isEmpty(), "Filtered file list should be empty");
}
}

View File

@@ -0,0 +1,79 @@
package stirling.software.SPDF.controller.web;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import stirling.software.common.model.ApplicationProperties;
class UploadLimitServiceTest {
private UploadLimitService uploadLimitService;
private ApplicationProperties applicationProperties;
private ApplicationProperties.System systemProps;
@BeforeEach
void setUp() {
applicationProperties = mock(ApplicationProperties.class);
systemProps = mock(ApplicationProperties.System.class);
when(applicationProperties.getSystem()).thenReturn(systemProps);
uploadLimitService = new UploadLimitService();
// inject mock
try {
var field = UploadLimitService.class.getDeclaredField("applicationProperties");
field.setAccessible(true);
field.set(uploadLimitService, applicationProperties);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@ParameterizedTest(name = "getUploadLimit case #{index}: input={0}, expected={1}")
@MethodSource("uploadLimitParams")
void shouldComputeUploadLimitCorrectly(String input, long expected) {
when(systemProps.getFileUploadLimit()).thenReturn(input);
long result = uploadLimitService.getUploadLimit();
assertEquals(expected, result);
}
static Stream<Arguments> uploadLimitParams() {
return Stream.of(
// empty or null input yields 0
Arguments.of(null, 0L),
Arguments.of("", 0L),
// invalid formats
Arguments.of("1234MB", 0L),
Arguments.of("5TB", 0L),
// valid formats
Arguments.of("10KB", 10 * 1024L),
Arguments.of("2MB", 2 * 1024 * 1024L),
Arguments.of("1GB", 1L * 1024 * 1024 * 1024),
Arguments.of("5mb", 5 * 1024 * 1024L),
Arguments.of("0MB", 0L));
}
@ParameterizedTest(name = "getReadableUploadLimit case #{index}: rawValue={0}, expected={1}")
@MethodSource("readableLimitParams")
void shouldReturnReadableFormat(String rawValue, String expected) {
when(systemProps.getFileUploadLimit()).thenReturn(rawValue);
String result = uploadLimitService.getReadableUploadLimit();
assertEquals(expected, result);
}
static Stream<Arguments> readableLimitParams() {
return Stream.of(
Arguments.of(null, "0 B"),
Arguments.of("", "0 B"),
Arguments.of("1KB", "1.0 KB"),
Arguments.of("2MB", "2.0 MB"));
}
}

View File

@@ -0,0 +1,105 @@
package stirling.software.SPDF.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
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.web.multipart.MultipartFile;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AttachmentServiceTest {
private AttachmentService attachmentService;
@BeforeEach
void setUp() {
attachmentService = new AttachmentService();
}
@Test
void addAttachmentToPDF() throws IOException {
try (var document = new PDDocument()) {
document.setDocumentId(100L);
var attachments = List.of(mock(MultipartFile.class));
when(attachments.get(0).getOriginalFilename()).thenReturn("test.txt");
when(attachments.get(0).getInputStream()).thenReturn(
new ByteArrayInputStream("Test content".getBytes()));
when(attachments.get(0).getSize()).thenReturn(12L);
when(attachments.get(0).getContentType()).thenReturn("text/plain");
PDDocument result = attachmentService.addAttachment(document, attachments);
assertNotNull(result);
assertEquals(document.getDocumentId(), result.getDocumentId());
assertNotNull(result.getDocumentCatalog().getNames());
}
}
@Test
void addAttachmentToPDF_MultipleAttachments() throws IOException {
try (var document = new PDDocument()) {
document.setDocumentId(100L);
var attachment1 = mock(MultipartFile.class);
var attachment2 = mock(MultipartFile.class);
var attachments = List.of(attachment1, attachment2);
when(attachment1.getOriginalFilename()).thenReturn("document.pdf");
when(attachment1.getInputStream()).thenReturn(
new ByteArrayInputStream("PDF content".getBytes()));
when(attachment1.getSize()).thenReturn(15L);
when(attachment1.getContentType()).thenReturn("application/pdf");
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");
PDDocument result = attachmentService.addAttachment(document, attachments);
assertNotNull(result);
assertNotNull(result.getDocumentCatalog().getNames());
}
}
@Test
void addAttachmentToPDF_WithBlankContentType() throws IOException {
try (var document = new PDDocument()) {
document.setDocumentId(100L);
var attachments = List.of(mock(MultipartFile.class));
when(attachments.get(0).getOriginalFilename()).thenReturn("image.jpg");
when(attachments.get(0).getInputStream()).thenReturn(
new ByteArrayInputStream("Image content".getBytes()));
when(attachments.get(0).getSize()).thenReturn(25L);
when(attachments.get(0).getContentType()).thenReturn("");
PDDocument result = attachmentService.addAttachment(document, attachments);
assertNotNull(result);
assertNotNull(result.getDocumentCatalog().getNames());
}
}
@Test
void addAttachmentToPDF_AttachmentInputStreamThrowsIOException() throws IOException {
try (var document = new PDDocument()) {
var attachments = List.of(mock(MultipartFile.class));
var ioException = new IOException("Failed to read attachment stream");
when(attachments.get(0).getOriginalFilename()).thenReturn("test.txt");
when(attachments.get(0).getInputStream()).thenThrow(ioException);
when(attachments.get(0).getSize()).thenReturn(10L);
PDDocument result = attachmentService.addAttachment(document, attachments);
assertNotNull(result);
assertNotNull(result.getDocumentCatalog().getNames());
}
}
}

View File

@@ -0,0 +1,146 @@
package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.security.PublicKey;
import java.security.cert.CertificateExpiredException;
import java.security.cert.X509Certificate;
import javax.security.auth.x500.X500Principal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
/** Tests for the CertificateValidationService using mocked certificates. */
class CertificateValidationServiceTest {
private CertificateValidationService validationService;
private X509Certificate validCertificate;
private X509Certificate expiredCertificate;
@BeforeEach
void setUp() throws Exception {
validationService = new CertificateValidationService();
// Create mock certificates
validCertificate = mock(X509Certificate.class);
expiredCertificate = mock(X509Certificate.class);
// Set up behaviors for valid certificate
doNothing().when(validCertificate).checkValidity(); // No exception means valid
// Set up behaviors for expired certificate
doThrow(new CertificateExpiredException("Certificate expired"))
.when(expiredCertificate)
.checkValidity();
}
@Test
void testIsRevoked_ValidCertificate() {
// When certificate is valid (not expired)
boolean result = validationService.isRevoked(validCertificate);
// Then it should not be considered revoked
assertFalse(result, "Valid certificate should not be considered revoked");
}
@Test
void testIsRevoked_ExpiredCertificate() {
// When certificate is expired
boolean result = validationService.isRevoked(expiredCertificate);
// Then it should be considered revoked
assertTrue(result, "Expired certificate should be considered revoked");
}
@Test
void testValidateTrustWithCustomCert_Match() {
// Create certificates with matching issuer and subject
X509Certificate issuingCert = mock(X509Certificate.class);
X509Certificate signedCert = mock(X509Certificate.class);
// Create X500Principal objects for issuer and subject
X500Principal issuerPrincipal = new X500Principal("CN=Test Issuer");
// Mock the issuer of the signed certificate to match the subject of the issuing certificate
when(signedCert.getIssuerX500Principal()).thenReturn(issuerPrincipal);
when(issuingCert.getSubjectX500Principal()).thenReturn(issuerPrincipal);
// When validating trust with custom cert
boolean result = validationService.validateTrustWithCustomCert(signedCert, issuingCert);
// Then validation should succeed
assertTrue(result, "Certificate with matching issuer and subject should validate");
}
@Test
void testValidateTrustWithCustomCert_NoMatch() {
// Create certificates with non-matching issuer and subject
X509Certificate issuingCert = mock(X509Certificate.class);
X509Certificate signedCert = mock(X509Certificate.class);
// Create X500Principal objects for issuer and subject
X500Principal issuerPrincipal = new X500Principal("CN=Test Issuer");
X500Principal differentPrincipal = new X500Principal("CN=Different Name");
// Mock the issuer of the signed certificate to NOT match the subject of the issuing
// certificate
when(signedCert.getIssuerX500Principal()).thenReturn(issuerPrincipal);
when(issuingCert.getSubjectX500Principal()).thenReturn(differentPrincipal);
// When validating trust with custom cert
boolean result = validationService.validateTrustWithCustomCert(signedCert, issuingCert);
// Then validation should fail
assertFalse(result, "Certificate with non-matching issuer and subject should not validate");
}
@Test
void testValidateCertificateChainWithCustomCert_Success() throws Exception {
// Setup mock certificates
X509Certificate signedCert = mock(X509Certificate.class);
X509Certificate signingCert = mock(X509Certificate.class);
PublicKey publicKey = mock(PublicKey.class);
when(signingCert.getPublicKey()).thenReturn(publicKey);
// When verifying the certificate with the signing cert's public key, don't throw exception
doNothing().when(signedCert).verify(Mockito.any());
// When validating certificate chain with custom cert
boolean result =
validationService.validateCertificateChainWithCustomCert(signedCert, signingCert);
// Then validation should succeed
assertTrue(result, "Certificate chain with proper signing should validate");
}
@Test
void testValidateCertificateChainWithCustomCert_Failure() throws Exception {
// Setup mock certificates
X509Certificate signedCert = mock(X509Certificate.class);
X509Certificate signingCert = mock(X509Certificate.class);
PublicKey publicKey = mock(PublicKey.class);
when(signingCert.getPublicKey()).thenReturn(publicKey);
// When verifying the certificate with the signing cert's public key, throw exception
// Need to use a specific exception that verify() can throw
doThrow(new java.security.SignatureException("Verification failed"))
.when(signedCert)
.verify(Mockito.any());
// When validating certificate chain with custom cert
boolean result =
validationService.validateCertificateChainWithCustomCert(signedCert, signingCert);
// Then validation should fail
assertFalse(result, "Certificate chain with failed signing should not validate");
}
}

View File

@@ -0,0 +1,131 @@
package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.Resource;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Ui;
class LanguageServiceBasicTest {
private LanguageService languageService;
private ApplicationProperties applicationProperties;
@BeforeEach
void setUp() {
// Mock application properties
applicationProperties = mock(ApplicationProperties.class);
Ui ui = mock(Ui.class);
when(applicationProperties.getUi()).thenReturn(ui);
// Create language service with test implementation
languageService = new LanguageServiceForTest(applicationProperties);
}
@Test
void testGetSupportedLanguages_BasicFunctionality() throws IOException {
// Set up mocked resources
Resource enResource = createMockResource("messages_en_US.properties");
Resource frResource = createMockResource("messages_fr_FR.properties");
Resource[] mockResources = new Resource[] {enResource, frResource};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Basic assertions
assertTrue(supportedLanguages.contains("en_US"), "en_US should be included");
assertTrue(supportedLanguages.contains("fr_FR"), "fr_FR should be included");
}
@Test
void testGetSupportedLanguages_FilteringInvalidFiles() throws IOException {
// Set up mocked resources with invalid files
Resource[] mockResources =
new Resource[] {
createMockResource("messages_en_US.properties"), // Valid
createMockResource("invalid_file.properties"), // Invalid
createMockResource(null) // Null filename
};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify filtering
assertTrue(supportedLanguages.contains("en_US"), "Valid language should be included");
assertFalse(
supportedLanguages.contains("invalid_file"),
"Invalid filename should be filtered out");
}
@Test
void testGetSupportedLanguages_WithRestrictions() throws IOException {
// Set up test resources
Resource[] mockResources =
new Resource[] {
createMockResource("messages_en_US.properties"),
createMockResource("messages_fr_FR.properties"),
createMockResource("messages_de_DE.properties"),
createMockResource("messages_en_GB.properties")
};
// Configure the test service
((LanguageServiceForTest) languageService).setMockResources(mockResources);
// Allow only specific languages (en_GB is always included)
when(applicationProperties.getUi().getLanguages())
.thenReturn(Arrays.asList("en_US", "fr_FR"));
// Execute the method
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify filtering by restrictions
assertTrue(supportedLanguages.contains("en_US"), "Allowed language should be included");
assertTrue(supportedLanguages.contains("fr_FR"), "Allowed language should be included");
assertTrue(supportedLanguages.contains("en_GB"), "en_GB should always be included");
assertFalse(supportedLanguages.contains("de_DE"), "Restricted language should be excluded");
}
// Helper methods
private Resource createMockResource(String filename) {
Resource mockResource = mock(Resource.class);
when(mockResource.getFilename()).thenReturn(filename);
return mockResource;
}
// Test subclass
private static class LanguageServiceForTest extends LanguageService {
private Resource[] mockResources;
public LanguageServiceForTest(ApplicationProperties applicationProperties) {
super(applicationProperties);
}
public void setMockResources(Resource[] mockResources) {
this.mockResources = mockResources;
}
@Override
protected Resource[] getResourcesFromPattern(String pattern) throws IOException {
return mockResources;
}
}
}

View File

@@ -0,0 +1,173 @@
package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Ui;
class LanguageServiceTest {
private LanguageService languageService;
private ApplicationProperties applicationProperties;
private PathMatchingResourcePatternResolver mockedResolver;
@BeforeEach
void setUp() throws Exception {
// Mock ApplicationProperties
applicationProperties = mock(ApplicationProperties.class);
Ui ui = mock(Ui.class);
when(applicationProperties.getUi()).thenReturn(ui);
// Create LanguageService with our custom constructor that allows injection of resolver
languageService = new LanguageServiceForTest(applicationProperties);
}
@Test
void testGetSupportedLanguages_NoRestrictions() throws IOException {
// Setup
Set<String> expectedLanguages =
new HashSet<>(Arrays.asList("en_US", "fr_FR", "de_DE", "en_GB"));
// Mock the resource resolver response
Resource[] mockResources = createMockResources(expectedLanguages);
((LanguageServiceForTest) languageService).setMockResources(mockResources);
// No language restrictions in properties
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Test
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify
assertEquals(
expectedLanguages,
supportedLanguages,
"Should return all languages when no restrictions");
}
@Test
void testGetSupportedLanguages_WithRestrictions() throws IOException {
// Setup
Set<String> expectedLanguages =
new HashSet<>(Arrays.asList("en_US", "fr_FR", "de_DE", "en_GB"));
Set<String> allowedLanguages = new HashSet<>(Arrays.asList("en_US", "fr_FR", "en_GB"));
// Mock the resource resolver response
Resource[] mockResources = createMockResources(expectedLanguages);
((LanguageServiceForTest) languageService).setMockResources(mockResources);
// Set language restrictions in properties
when(applicationProperties.getUi().getLanguages())
.thenReturn(Arrays.asList("en_US", "fr_FR")); // en_GB is always allowed
// Test
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify
assertEquals(
allowedLanguages,
supportedLanguages,
"Should return only allowed languages, plus en_GB which is always allowed");
assertTrue(supportedLanguages.contains("en_GB"), "en_GB should always be included");
}
@Test
void testGetSupportedLanguages_ExceptionHandling() throws IOException {
// Setup - make resolver throw an exception
((LanguageServiceForTest) languageService).setShouldThrowException(true);
// Test
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify
assertTrue(supportedLanguages.isEmpty(), "Should return empty set on exception");
}
@Test
void testGetSupportedLanguages_FilteringNonMatchingFiles() throws IOException {
// Setup with some valid and some invalid filenames
Resource[] mixedResources =
new Resource[] {
createMockResource("messages_en_US.properties"),
createMockResource(
"messages_en_GB.properties"), // Explicitly add en_GB resource
createMockResource("messages_fr_FR.properties"),
createMockResource("not_a_messages_file.properties"),
createMockResource("messages_.properties"), // Invalid format
createMockResource(null) // Null filename
};
((LanguageServiceForTest) languageService).setMockResources(mixedResources);
when(applicationProperties.getUi().getLanguages()).thenReturn(Collections.emptyList());
// Test
Set<String> supportedLanguages = languageService.getSupportedLanguages();
// Verify the valid languages are present
assertTrue(supportedLanguages.contains("en_US"), "en_US should be included");
assertTrue(supportedLanguages.contains("fr_FR"), "fr_FR should be included");
// Add en_GB which is always included
assertTrue(supportedLanguages.contains("en_GB"), "en_GB should always be included");
// Verify no invalid formats are included
assertFalse(
supportedLanguages.contains("not_a_messages_file"),
"Invalid format should be excluded");
// Skip the empty string check as it depends on implementation details of extracting
// language codes
}
// Helper methods to create mock resources
private Resource[] createMockResources(Set<String> languages) {
return languages.stream()
.map(lang -> createMockResource("messages_" + lang + ".properties"))
.toArray(Resource[]::new);
}
private Resource createMockResource(String filename) {
Resource mockResource = mock(Resource.class);
when(mockResource.getFilename()).thenReturn(filename);
return mockResource;
}
// Test subclass that allows us to control the resource resolver
private static class LanguageServiceForTest extends LanguageService {
private Resource[] mockResources;
private boolean shouldThrowException = false;
public LanguageServiceForTest(ApplicationProperties applicationProperties) {
super(applicationProperties);
}
public void setMockResources(Resource[] mockResources) {
this.mockResources = mockResources;
}
public void setShouldThrowException(boolean shouldThrowException) {
this.shouldThrowException = shouldThrowException;
}
@Override
protected Resource[] getResourcesFromPattern(String pattern) throws IOException {
if (shouldThrowException) {
throw new IOException("Test exception");
}
return mockResources;
}
}
}

View File

@@ -0,0 +1,154 @@
package stirling.software.SPDF.service;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
class PdfImageRemovalServiceTest {
private PdfImageRemovalService service;
@BeforeEach
void setUp() {
service = new PdfImageRemovalService();
}
@Test
void testRemoveImagesFromPdf_WithImages() throws IOException {
// Mock PDF document and its components
PDDocument document = mock(PDDocument.class);
PDPage page = mock(PDPage.class);
PDResources resources = mock(PDResources.class);
PDPageTree pageTree = mock(PDPageTree.class);
// Configure page tree to iterate over our single page
when(document.getPages()).thenReturn(pageTree);
Iterator<PDPage> pageIterator = Arrays.asList(page).iterator();
when(pageTree.iterator()).thenReturn(pageIterator);
// Set up page resources
when(page.getResources()).thenReturn(resources);
// Set up image XObjects
COSName img1 = COSName.getPDFName("Im1");
COSName img2 = COSName.getPDFName("Im2");
COSName nonImg = COSName.getPDFName("NonImg");
List<COSName> xObjectNames = Arrays.asList(img1, img2, nonImg);
when(resources.getXObjectNames()).thenReturn(xObjectNames);
// Configure which are image XObjects
when(resources.isImageXObject(img1)).thenReturn(true);
when(resources.isImageXObject(img2)).thenReturn(true);
when(resources.isImageXObject(nonImg)).thenReturn(false);
// Execute the method
PDDocument result = service.removeImagesFromPdf(document);
// Verify that images were removed
verify(resources, times(1)).put(eq(img1), Mockito.<PDXObject>isNull());
verify(resources, times(1)).put(eq(img2), Mockito.<PDXObject>isNull());
verify(resources, never()).put(eq(nonImg), Mockito.<PDXObject>isNull());
}
@Test
void testRemoveImagesFromPdf_NoImages() throws IOException {
// Mock PDF document and its components
PDDocument document = mock(PDDocument.class);
PDPage page = mock(PDPage.class);
PDResources resources = mock(PDResources.class);
PDPageTree pageTree = mock(PDPageTree.class);
// Configure page tree to iterate over our single page
when(document.getPages()).thenReturn(pageTree);
Iterator<PDPage> pageIterator = Arrays.asList(page).iterator();
when(pageTree.iterator()).thenReturn(pageIterator);
// Set up page resources
when(page.getResources()).thenReturn(resources);
// Create empty list of XObject names
List<COSName> emptyList = new ArrayList<>();
when(resources.getXObjectNames()).thenReturn(emptyList);
// Execute the method
PDDocument result = service.removeImagesFromPdf(document);
// Verify that no modifications were made
verify(resources, never()).put(any(COSName.class), any(PDXObject.class));
}
@Test
void testRemoveImagesFromPdf_MultiplePages() throws IOException {
// Mock PDF document and its components
PDDocument document = mock(PDDocument.class);
PDPage page1 = mock(PDPage.class);
PDPage page2 = mock(PDPage.class);
PDResources resources1 = mock(PDResources.class);
PDResources resources2 = mock(PDResources.class);
PDPageTree pageTree = mock(PDPageTree.class);
// Configure page tree to iterate over our two pages
when(document.getPages()).thenReturn(pageTree);
Iterator<PDPage> pageIterator = Arrays.asList(page1, page2).iterator();
when(pageTree.iterator()).thenReturn(pageIterator);
// Set up page resources
when(page1.getResources()).thenReturn(resources1);
when(page2.getResources()).thenReturn(resources2);
// Set up image XObjects for page 1
COSName img1 = COSName.getPDFName("Im1");
when(resources1.getXObjectNames()).thenReturn(Arrays.asList(img1));
when(resources1.isImageXObject(img1)).thenReturn(true);
// Set up image XObjects for page 2
COSName img2 = COSName.getPDFName("Im2");
when(resources2.getXObjectNames()).thenReturn(Arrays.asList(img2));
when(resources2.isImageXObject(img2)).thenReturn(true);
// Execute the method
PDDocument result = service.removeImagesFromPdf(document);
// Verify that images were removed from both pages
verify(resources1, times(1)).put(eq(img1), Mockito.<PDXObject>isNull());
verify(resources2, times(1)).put(eq(img2), Mockito.<PDXObject>isNull());
}
// Helper method for matching COSName in verification
private static COSName eq(final COSName value) {
return Mockito.argThat(
new org.mockito.ArgumentMatcher<COSName>() {
@Override
public boolean matches(COSName argument) {
if (argument == null && value == null) return true;
if (argument == null || value == null) return false;
return argument.getName().equals(value.getName());
}
@Override
public String toString() {
return "eq(" + (value != null ? value.getName() : "null") + ")";
}
});
}
}

View File

@@ -0,0 +1,110 @@
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.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Premium;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures.CustomMetadata;
import stirling.software.common.model.PdfMetadata;
import stirling.software.common.service.PdfMetadataService;
import stirling.software.common.service.UserServiceInterface;
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);
}
}

View File

@@ -0,0 +1,237 @@
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.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Premium;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures.CustomMetadata;
import stirling.software.common.model.PdfMetadata;
import stirling.software.common.service.PdfMetadataService;
import stirling.software.common.service.UserServiceInterface;
@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));
}
}

View File

@@ -0,0 +1,293 @@
package stirling.software.SPDF.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.MockedStatic;
import stirling.software.common.configuration.InstallationPathConfig;
import stirling.software.SPDF.model.SignatureFile;
class SignatureServiceTest {
@TempDir Path tempDir;
private SignatureService signatureService;
private Path personalSignatureFolder;
private Path sharedSignatureFolder;
private final String ALL_USERS_FOLDER = "ALL_USERS";
private final String TEST_USER = "testUser";
@BeforeEach
void setUp() throws IOException {
// Set up our test directory structure
personalSignatureFolder = tempDir.resolve(TEST_USER);
sharedSignatureFolder = tempDir.resolve(ALL_USERS_FOLDER);
Files.createDirectories(personalSignatureFolder);
Files.createDirectories(sharedSignatureFolder);
// Create test signature files
Files.write(
personalSignatureFolder.resolve("personal.png"),
"personal signature content".getBytes());
Files.write(
sharedSignatureFolder.resolve("shared.jpg"), "shared signature content".getBytes());
// Use try-with-resources for mockStatic
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Initialize the service with our temp directory
signatureService = new SignatureService();
}
}
@Test
void testHasAccessToFile_PersonalFileExists() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
boolean hasAccess = signatureService.hasAccessToFile(TEST_USER, "personal.png");
// Verify
assertTrue(hasAccess, "User should have access to their personal file");
}
}
@Test
void testHasAccessToFile_SharedFileExists() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
boolean hasAccess = signatureService.hasAccessToFile(TEST_USER, "shared.jpg");
// Verify
assertTrue(hasAccess, "User should have access to shared files");
}
}
@Test
void testHasAccessToFile_FileDoesNotExist() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
boolean hasAccess = signatureService.hasAccessToFile(TEST_USER, "nonexistent.png");
// Verify
assertFalse(hasAccess, "User should not have access to non-existent files");
}
}
@Test
void testHasAccessToFile_InvalidFileName() {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test and verify
assertThrows(
IllegalArgumentException.class,
() -> signatureService.hasAccessToFile(TEST_USER, "../invalid.png"),
"Should throw exception for file names with directory traversal");
assertThrows(
IllegalArgumentException.class,
() -> signatureService.hasAccessToFile(TEST_USER, "invalid/file.png"),
"Should throw exception for file names with paths");
}
}
@Test
void testGetAvailableSignatures() {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
List<SignatureFile> signatures = signatureService.getAvailableSignatures(TEST_USER);
// Verify
assertEquals(2, signatures.size(), "Should return both personal and shared signatures");
// Check that we have one of each type
boolean hasPersonal =
signatures.stream()
.anyMatch(
sig ->
"personal.png".equals(sig.getFileName())
&& "Personal".equals(sig.getCategory()));
boolean hasShared =
signatures.stream()
.anyMatch(
sig ->
"shared.jpg".equals(sig.getFileName())
&& "Shared".equals(sig.getCategory()));
assertTrue(hasPersonal, "Should include personal signature");
assertTrue(hasShared, "Should include shared signature");
}
}
@Test
void testGetSignatureBytes_PersonalFile() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
byte[] bytes = signatureService.getSignatureBytes(TEST_USER, "personal.png");
// Verify
assertEquals(
"personal signature content",
new String(bytes),
"Should return the correct content for personal file");
}
}
@Test
void testGetSignatureBytes_SharedFile() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
byte[] bytes = signatureService.getSignatureBytes(TEST_USER, "shared.jpg");
// Verify
assertEquals(
"shared signature content",
new String(bytes),
"Should return the correct content for shared file");
}
}
@Test
void testGetSignatureBytes_FileNotFound() {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test and verify
assertThrows(
FileNotFoundException.class,
() -> signatureService.getSignatureBytes(TEST_USER, "nonexistent.png"),
"Should throw exception for non-existent files");
}
}
@Test
void testGetSignatureBytes_InvalidFileName() {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test and verify
assertThrows(
IllegalArgumentException.class,
() -> signatureService.getSignatureBytes(TEST_USER, "../invalid.png"),
"Should throw exception for file names with directory traversal");
}
}
@Test
void testGetAvailableSignatures_EmptyUsername() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
List<SignatureFile> signatures = signatureService.getAvailableSignatures("");
// Verify - should only have shared signatures
assertEquals(
1,
signatures.size(),
"Should return only shared signatures for empty username");
assertEquals(
"shared.jpg",
signatures.get(0).getFileName(),
"Should have the shared signature");
assertEquals(
"Shared", signatures.get(0).getCategory(), "Should be categorized as shared");
}
}
@Test
void testGetAvailableSignatures_NonExistentUser() throws IOException {
// Mock static method for each test
try (MockedStatic<InstallationPathConfig> mockedConfig =
mockStatic(InstallationPathConfig.class)) {
mockedConfig
.when(InstallationPathConfig::getSignaturesPath)
.thenReturn(tempDir.toString());
// Test
List<SignatureFile> signatures =
signatureService.getAvailableSignatures("nonExistentUser");
// Verify - should only have shared signatures
assertEquals(
1,
signatures.size(),
"Should return only shared signatures for non-existent user");
assertEquals(
"shared.jpg",
signatures.get(0).getFileName(),
"Should have the shared signature");
assertEquals(
"Shared", signatures.get(0).getCategory(), "Should be categorized as shared");
}
}
}

View File

@@ -0,0 +1,407 @@
package stirling.software.common.controller;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import stirling.software.common.model.job.JobResult;
import stirling.software.common.model.job.JobStats;
import stirling.software.common.model.job.ResultFile;
import stirling.software.common.service.FileStorage;
import stirling.software.common.service.JobQueue;
import stirling.software.common.service.TaskManager;
class JobControllerTest {
@Mock
private TaskManager taskManager;
@Mock
private FileStorage fileStorage;
@Mock
private JobQueue jobQueue;
@Mock
private HttpServletRequest request;
private MockHttpSession session;
@InjectMocks
private JobController controller;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
// Setup mock session for tests
session = new MockHttpSession();
when(request.getSession()).thenReturn(session);
}
@Test
void testGetJobStatus_ExistingJob() {
// Arrange
String jobId = "test-job-id";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
// Act
ResponseEntity<?> response = controller.getJobStatus(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(mockResult, response.getBody());
}
@Test
void testGetJobStatus_ExistingJobInQueue() {
// Arrange
String jobId = "test-job-id";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.setComplete(false);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
when(jobQueue.isJobQueued(jobId)).thenReturn(true);
when(jobQueue.getJobPosition(jobId)).thenReturn(3);
// Act
ResponseEntity<?> response = controller.getJobStatus(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
assertEquals(mockResult, responseBody.get("jobResult"));
@SuppressWarnings("unchecked")
Map<String, Object> queueInfo = (Map<String, Object>) responseBody.get("queueInfo");
assertTrue((Boolean) queueInfo.get("inQueue"));
assertEquals(3, queueInfo.get("position"));
}
@Test
void testGetJobStatus_NonExistentJob() {
// Arrange
String jobId = "non-existent-job";
when(taskManager.getJobResult(jobId)).thenReturn(null);
// Act
ResponseEntity<?> response = controller.getJobStatus(jobId);
// Assert
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void testGetJobResult_CompletedSuccessfulWithObject() {
// Arrange
String jobId = "test-job-id";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.setComplete(true);
String resultObject = "Test result";
mockResult.completeWithResult(resultObject);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(resultObject, response.getBody());
}
@Test
void testGetJobResult_CompletedSuccessfulWithFile() throws Exception {
// Arrange
String jobId = "test-job-id";
String fileId = "file-id";
String originalFileName = "test.pdf";
String contentType = "application/pdf";
byte[] fileContent = "Test file content".getBytes();
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.completeWithSingleFile(fileId, originalFileName, contentType, fileContent.length);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
when(fileStorage.retrieveBytes(fileId)).thenReturn(fileContent);
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(contentType, response.getHeaders().getFirst("Content-Type"));
assertTrue(response.getHeaders().getFirst("Content-Disposition").contains(originalFileName));
assertEquals(fileContent, response.getBody());
}
@Test
void testGetJobResult_CompletedWithError() {
// Arrange
String jobId = "test-job-id";
String errorMessage = "Test error";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.failWithError(errorMessage);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
assertTrue(response.getBody().toString().contains(errorMessage));
}
@Test
void testGetJobResult_IncompleteJob() {
// Arrange
String jobId = "test-job-id";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.setComplete(false);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
assertTrue(response.getBody().toString().contains("not complete"));
}
@Test
void testGetJobResult_NonExistentJob() {
// Arrange
String jobId = "non-existent-job";
when(taskManager.getJobResult(jobId)).thenReturn(null);
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void testGetJobResult_ErrorRetrievingFile() throws Exception {
// Arrange
String jobId = "test-job-id";
String fileId = "file-id";
String originalFileName = "test.pdf";
String contentType = "application/pdf";
JobResult mockResult = new JobResult();
mockResult.setJobId(jobId);
mockResult.completeWithSingleFile(fileId, originalFileName, contentType, 1024L);
when(taskManager.getJobResult(jobId)).thenReturn(mockResult);
when(fileStorage.retrieveBytes(fileId)).thenThrow(new RuntimeException("File not found"));
// Act
ResponseEntity<?> response = controller.getJobResult(jobId);
// Assert
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertTrue(response.getBody().toString().contains("Error retrieving file"));
}
/*
* @Test void testGetJobStats() { // Arrange JobStats mockStats =
* JobStats.builder() .totalJobs(10) .activeJobs(3) .completedJobs(7) .build();
*
* when(taskManager.getJobStats()).thenReturn(mockStats);
*
* // Act ResponseEntity<?> response = controller.getJobStats();
*
* // Assert assertEquals(HttpStatus.OK, response.getStatusCode());
* assertEquals(mockStats, response.getBody()); }
*
* @Test void testCleanupOldJobs() { // Arrange when(taskManager.getJobStats())
* .thenReturn(JobStats.builder().totalJobs(10).build())
* .thenReturn(JobStats.builder().totalJobs(7).build());
*
* // Act ResponseEntity<?> response = controller.cleanupOldJobs();
*
* // Assert assertEquals(HttpStatus.OK, response.getStatusCode());
*
* @SuppressWarnings("unchecked") Map<String, Object> responseBody =
* (Map<String, Object>) response.getBody(); assertEquals("Cleanup complete",
* responseBody.get("message")); assertEquals(3,
* responseBody.get("removedJobs")); assertEquals(7,
* responseBody.get("remainingJobs"));
*
* verify(taskManager).cleanupOldJobs(); }
*
* @Test void testGetQueueStats() { // Arrange Map<String, Object>
* mockQueueStats = Map.of( "queuedJobs", 5, "queueCapacity", 10,
* "resourceStatus", "OK" );
*
* when(jobQueue.getQueueStats()).thenReturn(mockQueueStats);
*
* // Act ResponseEntity<?> response = controller.getQueueStats();
*
* // Assert assertEquals(HttpStatus.OK, response.getStatusCode());
* assertEquals(mockQueueStats, response.getBody());
* verify(jobQueue).getQueueStats(); }
*/
@Test
void testCancelJob_InQueue() {
// Arrange
String jobId = "job-in-queue";
// Setup user session with job authorization
java.util.Set<String> userJobIds = new java.util.HashSet<>();
userJobIds.add(jobId);
session.setAttribute("userJobIds", userJobIds);
when(jobQueue.isJobQueued(jobId)).thenReturn(true);
when(jobQueue.getJobPosition(jobId)).thenReturn(2);
when(jobQueue.cancelJob(jobId)).thenReturn(true);
// Act
ResponseEntity<?> response = controller.cancelJob(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
assertEquals("Job cancelled successfully", responseBody.get("message"));
assertTrue((Boolean) responseBody.get("wasQueued"));
assertEquals(2, responseBody.get("queuePosition"));
verify(jobQueue).cancelJob(jobId);
verify(taskManager, never()).setError(anyString(), anyString());
}
@Test
void testCancelJob_Running() {
// Arrange
String jobId = "job-running";
JobResult jobResult = new JobResult();
jobResult.setJobId(jobId);
jobResult.setComplete(false);
// Setup user session with job authorization
java.util.Set<String> userJobIds = new java.util.HashSet<>();
userJobIds.add(jobId);
session.setAttribute("userJobIds", userJobIds);
when(jobQueue.isJobQueued(jobId)).thenReturn(false);
when(taskManager.getJobResult(jobId)).thenReturn(jobResult);
// Act
ResponseEntity<?> response = controller.cancelJob(jobId);
// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
assertEquals("Job cancelled successfully", responseBody.get("message"));
assertFalse((Boolean) responseBody.get("wasQueued"));
assertEquals("n/a", responseBody.get("queuePosition"));
verify(jobQueue, never()).cancelJob(jobId);
verify(taskManager).setError(jobId, "Job was cancelled by user");
}
@Test
void testCancelJob_NotFound() {
// Arrange
String jobId = "non-existent-job";
// Setup user session with job authorization
java.util.Set<String> userJobIds = new java.util.HashSet<>();
userJobIds.add(jobId);
session.setAttribute("userJobIds", userJobIds);
when(jobQueue.isJobQueued(jobId)).thenReturn(false);
when(taskManager.getJobResult(jobId)).thenReturn(null);
// Act
ResponseEntity<?> response = controller.cancelJob(jobId);
// Assert
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void testCancelJob_AlreadyComplete() {
// Arrange
String jobId = "completed-job";
JobResult jobResult = new JobResult();
jobResult.setJobId(jobId);
jobResult.setComplete(true);
// Setup user session with job authorization
java.util.Set<String> userJobIds = new java.util.HashSet<>();
userJobIds.add(jobId);
session.setAttribute("userJobIds", userJobIds);
when(jobQueue.isJobQueued(jobId)).thenReturn(false);
when(taskManager.getJobResult(jobId)).thenReturn(jobResult);
// Act
ResponseEntity<?> response = controller.cancelJob(jobId);
// Assert
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
assertEquals("Cannot cancel job that is already complete", responseBody.get("message"));
}
@Test
void testCancelJob_Unauthorized() {
// Arrange
String jobId = "unauthorized-job";
// Setup user session with other job IDs but not this one
java.util.Set<String> userJobIds = new java.util.HashSet<>();
userJobIds.add("other-job-1");
userJobIds.add("other-job-2");
session.setAttribute("userJobIds", userJobIds);
// Act
ResponseEntity<?> response = controller.cancelJob(jobId);
// Assert
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = (Map<String, Object>) response.getBody();
assertEquals("You are not authorized to cancel this job", responseBody.get("message"));
// Verify no cancellation attempts were made
verify(jobQueue, never()).isJobQueued(anyString());
verify(jobQueue, never()).cancelJob(anyString());
verify(taskManager, never()).getJobResult(anyString());
verify(taskManager, never()).setError(anyString(), anyString());
}
}