Skip to content

Commit e640821

Browse files
committedMar 6, 2025·
Added integration tests for File Preview job
1 parent 1fdb508 commit e640821

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed
 

‎dspace-api/src/main/java/org/dspace/scripts/filepreview/FilePreview.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public void internalRun() throws Exception {
8888
}
8989

9090
Context context = new Context();
91+
context.turnOffAuthorisationSystem();
9192
if (StringUtils.isNotBlank(specificItemUUID)) {
9293
// Generate the preview only for a specific item
9394
generateItemFilePreviews(context, UUID.fromString(specificItemUUID));
9495
} else {
9596
// Generate the preview for all items
96-
context.turnOffAuthorisationSystem();
9797
Iterator<Item> items = itemService.findAll(context);
9898

9999
int count = 0;

‎dspace-api/src/test/data/dspaceFolder/config/spring/api/scripts.xml

+5
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@
101101
<property name="dspaceRunnableClass" value="org.dspace.app.healthreport.HealthReport"/>
102102
</bean>
103103

104+
<bean id="file-preview" class="org.dspace.scripts.filepreview.FilePreviewConfiguration" primary="true">
105+
<property name="description" value="Generate the file preview"/>
106+
<property name="dspaceRunnableClass" value="org.dspace.scripts.filepreview.FilePreview"/>
107+
</bean>
108+
104109
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.scripts.filepreview;
9+
10+
import static org.hamcrest.CoreMatchers.containsString;
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.empty;
13+
import static org.hamcrest.Matchers.hasItem;
14+
import static org.hamcrest.Matchers.hasSize;
15+
16+
import java.io.InputStream;
17+
import java.sql.SQLException;
18+
import java.util.List;
19+
20+
import org.dspace.AbstractIntegrationTestWithDatabase;
21+
import org.dspace.app.launcher.ScriptLauncher;
22+
import org.dspace.app.scripts.handler.impl.TestDSpaceRunnableHandler;
23+
import org.dspace.authorize.AuthorizeException;
24+
import org.dspace.builder.CollectionBuilder;
25+
import org.dspace.builder.CommunityBuilder;
26+
import org.dspace.builder.WorkspaceItemBuilder;
27+
import org.dspace.content.Bitstream;
28+
import org.dspace.content.BitstreamFormat;
29+
import org.dspace.content.Bundle;
30+
import org.dspace.content.Collection;
31+
import org.dspace.content.Community;
32+
import org.dspace.content.Item;
33+
import org.dspace.content.WorkspaceItem;
34+
import org.dspace.content.factory.ContentServiceFactory;
35+
import org.dspace.content.service.BitstreamFormatService;
36+
import org.dspace.content.service.BitstreamService;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
40+
/**
41+
* Integration test for the FilePreview script
42+
* @author Milan Majchrak (milan.majchrak at dataquest.sk)
43+
*/
44+
public class FilePreviewIT extends AbstractIntegrationTestWithDatabase {
45+
BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
46+
47+
BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
48+
49+
Item item;
50+
51+
@Before
52+
public void setup() throws SQLException, AuthorizeException {
53+
InputStream previewZipIs = getClass().getResourceAsStream("preview-file-test.zip");
54+
55+
context.turnOffAuthorisationSystem();
56+
Community community = CommunityBuilder.createCommunity(context).withName("Com").build();
57+
Collection collection = CollectionBuilder.createCollection(context, community).withName("Col").build();
58+
WorkspaceItem wItem = WorkspaceItemBuilder.createWorkspaceItem(context, collection)
59+
.withFulltext("preview-file-test.zip", "/local/path/preview-file-test.zip", previewZipIs)
60+
.build();
61+
context.restoreAuthSystemState();
62+
63+
// Get the item and its bitstream
64+
item = wItem.getItem();
65+
List<Bundle> bundles = item.getBundles();
66+
List<Bitstream> bitstreams = bundles.get(0).getBitstreams();
67+
Bitstream bitstream = bitstreams.get(0);
68+
69+
// Set the bitstream format to application/zip
70+
BitstreamFormat bitstreamFormat = bitstreamFormatService.findByMIMEType(context, "application/zip");
71+
bitstream.setFormat(context, bitstreamFormat);
72+
bitstreamService.update(context, bitstream);
73+
context.commit();
74+
context.reloadEntity(bitstream);
75+
context.reloadEntity(item);
76+
}
77+
78+
@Test
79+
public void testWhenNoFilesRun() throws Exception {
80+
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
81+
82+
String[] args = new String[] { "file-preview" };
83+
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
84+
85+
checkNoError(testDSpaceRunnableHandler);
86+
}
87+
88+
@Test
89+
public void testForSpecificItem() throws Exception {
90+
// Run the script
91+
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
92+
String[] args = new String[] { "file-preview", "-u", item.getID().toString() };
93+
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
94+
95+
// There should be no errors or warnings
96+
checkNoError(testDSpaceRunnableHandler);
97+
98+
// There should be an info message about generating the file previews for the specified item
99+
List<String> messages = testDSpaceRunnableHandler.getInfoMessages();
100+
assertThat(messages, hasSize(1));
101+
assertThat(messages, hasItem(containsString("Generate the file previews for the specified item with " +
102+
"the given UUID: " + item.getID())));
103+
}
104+
105+
@Test
106+
public void testForAllItem() throws Exception {
107+
// Run the script
108+
TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();
109+
String[] args = new String[] { "file-preview" };
110+
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), testDSpaceRunnableHandler, kernelImpl);
111+
112+
// There should be no errors or warnings
113+
checkNoError(testDSpaceRunnableHandler);
114+
}
115+
116+
private void checkNoError(TestDSpaceRunnableHandler testDSpaceRunnableHandler) {
117+
assertThat(testDSpaceRunnableHandler.getErrorMessages(), empty());
118+
assertThat(testDSpaceRunnableHandler.getWarningMessages(), empty());
119+
}
120+
}
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.