1313import static org .hamcrest .Matchers .hasItem ;
1414import static org .hamcrest .Matchers .hasSize ;
1515import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertNotNull ;
17+ import static org .junit .Assert .assertTrue ;
1618
1719import java .io .InputStream ;
1820import java .sql .SQLException ;
3638import org .dspace .content .factory .ContentServiceFactory ;
3739import org .dspace .content .service .BitstreamFormatService ;
3840import org .dspace .content .service .BitstreamService ;
41+ import org .dspace .content .service .PreviewContentService ;
3942import org .dspace .eperson .EPerson ;
43+ import org .dspace .services .ConfigurationService ;
44+ import org .dspace .services .factory .DSpaceServicesFactory ;
45+ import org .dspace .storage .bitstore .SyncBitstreamStorageServiceImpl ;
4046import org .junit .Before ;
4147import org .junit .Test ;
4248
4551 * @author Milan Majchrak (milan.majchrak at dataquest.sk)
4652 */
4753public class FilePreviewIT extends AbstractIntegrationTestWithDatabase {
48- BitstreamService bitstreamService = ContentServiceFactory . getInstance (). getBitstreamService () ;
54+ private static final int SYNC_STORE_NUMBER = SyncBitstreamStorageServiceImpl . SYNCHRONIZED_STORES_NUMBER ;
4955
56+ BitstreamService bitstreamService = ContentServiceFactory .getInstance ().getBitstreamService ();
5057 BitstreamFormatService bitstreamFormatService = ContentServiceFactory .getInstance ().getBitstreamFormatService ();
58+ PreviewContentService previewContentService = ContentServiceFactory .getInstance ().getPreviewContentService ();
59+ ConfigurationService configurationService = DSpaceServicesFactory .getInstance ().getConfigurationService ();
5160
61+ Collection collection ;
5262 Item item ;
5363 EPerson eperson ;
5464 String PASSWORD = "test" ;
@@ -61,7 +71,7 @@ public void setup() throws SQLException, AuthorizeException {
6171 eperson = EPersonBuilder .createEPerson (context )
6272 .withEmail ("test@test.edu" ).withPassword (PASSWORD ).build ();
6373 Community community = CommunityBuilder .createCommunity (context ).withName ("Com" ).build ();
64- Collection collection = CollectionBuilder .createCollection (context , community ).withName ("Col" ).build ();
74+ collection = CollectionBuilder .createCollection (context , community ).withName ("Col" ).build ();
6575 WorkspaceItem wItem = WorkspaceItemBuilder .createWorkspaceItem (context , collection )
6676 .withFulltext ("preview-file-test.zip" , "/local/path/preview-file-test.zip" , previewZipIs )
6777 .build ();
@@ -116,22 +126,45 @@ public void testWhenNoFilesRun() throws Exception {
116126 @ Test
117127 public void testForSpecificItem () throws Exception {
118128 // Run the script
119- TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler ();
120- String [] args = new String [] { "file-preview" , "-u" , item .getID ().toString (),
121- "-e" , eperson .getEmail (), "-p" , PASSWORD };
122- int run = ScriptLauncher .handleScript (args , ScriptLauncher .getConfig (kernelImpl ),
123- testDSpaceRunnableHandler , kernelImpl );
124- assertEquals (0 , run );
125- // There should be no errors or warnings
126- checkNoError (testDSpaceRunnableHandler );
129+ runScriptForItemWithBitstreams (item );
130+ }
127131
128- // There should be an info message about generating the file previews for the specified item
129- List <String > messages = testDSpaceRunnableHandler .getInfoMessages ();
130- assertThat (messages , hasSize (2 ));
131- assertThat (messages , hasItem (containsString ("Generate the file previews for the specified item with " +
132- "the given UUID: " + item .getID ())));
133- assertThat (messages ,
134- hasItem (containsString ("Authentication by user: " + eperson .getEmail ())));
132+ @ Test
133+ public void testPreviewWithSyncStorage () throws Exception {
134+ configurationService .setProperty ("sync.storage.service.enabled" , true );
135+
136+ context .turnOffAuthorisationSystem ();
137+
138+ WorkspaceItem wItem2 ;
139+ try (InputStream tgzFile = getClass ().getResourceAsStream ("logos.tgz" )) {
140+ wItem2 = WorkspaceItemBuilder .createWorkspaceItem (context , collection )
141+ .withBitstream ("logos.tgz" , "/local/path/logos.tgz" , tgzFile , SYNC_STORE_NUMBER )
142+ .build ();
143+ }
144+
145+ context .restoreAuthSystemState ();
146+
147+ // Get the item and its bitstream
148+ Item item2 = wItem2 .getItem ();
149+ List <Bundle > bundles = item2 .getBundles ();
150+ Bitstream bitstream2 = bundles .get (0 ).getBitstreams ().get (0 );
151+
152+ // Set the bitstream format to application/zip
153+ BitstreamFormat bitstreamFormat = bitstreamFormatService .findByMIMEType (context , "application/x-gtar" );
154+ bitstream2 .setFormat (context , bitstreamFormat );
155+ bitstreamService .update (context , bitstream2 );
156+ context .commit ();
157+ context .reloadEntity (bitstream2 );
158+ context .reloadEntity (item2 );
159+
160+ runScriptForItemWithBitstreams (item2 );
161+
162+ Bitstream b2 = bitstreamService .findAll (context ).stream ()
163+ .filter (b -> b .getStoreNumber () == SYNC_STORE_NUMBER )
164+ .findFirst ().orElse (null );
165+
166+ assertNotNull (b2 );
167+ assertTrue ("Expects preview content created and stored." , previewContentService .hasPreview (context , b2 ));
135168 }
136169
137170 @ Test
@@ -150,4 +183,24 @@ private void checkNoError(TestDSpaceRunnableHandler testDSpaceRunnableHandler) {
150183 assertThat (testDSpaceRunnableHandler .getErrorMessages (), empty ());
151184 assertThat (testDSpaceRunnableHandler .getWarningMessages (), empty ());
152185 }
186+
187+ private void runScriptForItemWithBitstreams (Item item ) throws Exception {
188+ // Run the script
189+ TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler ();
190+ String [] args = new String [] { "file-preview" , "-u" , item .getID ().toString (),
191+ "-e" , eperson .getEmail (), "-p" , PASSWORD };
192+ int run = ScriptLauncher .handleScript (args , ScriptLauncher .getConfig (kernelImpl ),
193+ testDSpaceRunnableHandler , kernelImpl );
194+ assertEquals (0 , run );
195+ // There should be no errors or warnings
196+ checkNoError (testDSpaceRunnableHandler );
197+
198+ // There should be an info message about generating the file previews for the specified item
199+ List <String > messages = testDSpaceRunnableHandler .getInfoMessages ();
200+ assertThat (messages , hasSize (2 ));
201+ assertThat (messages , hasItem (containsString ("Generate the file previews for the specified item with " +
202+ "the given UUID: " + item .getID ())));
203+ assertThat (messages ,
204+ hasItem (containsString ("Authentication by user: " + eperson .getEmail ())));
205+ }
153206}
0 commit comments