28
28
import java .util .Collections ;
29
29
import java .util .HashSet ;
30
30
import java .util .List ;
31
+ import java .util .Map ;
32
+ import java .util .OptionalInt ;
31
33
32
34
import org .apache .commons .lang3 .SystemUtils ;
33
35
import org .elasticsearch .index .query .Operator ;
64
66
import org .kitodo .production .services .ServiceManager ;
65
67
import org .kitodo .production .services .dataformat .MetsService ;
66
68
import org .kitodo .production .services .file .FileService ;
69
+ import org .kitodo .utils .ProcessTestUtils ;
67
70
68
71
/**
69
72
* Tests for ProcessService class.
@@ -72,10 +75,12 @@ public class ProcessServiceIT {
72
75
73
76
private static final FileService fileService = new FileService ();
74
77
private static final ProcessService processService = ServiceManager .getProcessService ();
75
-
78
+ private static final String TEST_PROCESS_TITLE = "Test process" ;
79
+ private static final String TEST_METADATA_FILE = "testMetadataFileServiceTest.xml" ;
76
80
private static final String firstProcess = "First process" ;
77
81
private static final String processNotFound = "Process was not found in index!" ;
78
82
private static final File script = new File (ConfigCore .getParameter (ParameterCore .SCRIPT_CREATE_DIR_META ));
83
+ private static Map <String , Integer > testProcessIds ;
79
84
80
85
@ BeforeClass
81
86
public static void prepareDatabase () throws Exception {
@@ -84,7 +89,8 @@ public static void prepareDatabase() throws Exception {
84
89
}
85
90
MockDatabase .startNode ();
86
91
MockDatabase .insertProcessesFull ();
87
- MockDatabase .insertProcessesForHierarchyTests ();
92
+ testProcessIds = MockDatabase .insertProcessesForHierarchyTests ();
93
+ ProcessTestUtils .copyHierarchyTestFiles (testProcessIds );
88
94
MockDatabase .setUpAwaitility ();
89
95
fileService .createDirectory (URI .create ("" ), "1" );
90
96
User userOne = ServiceManager .getUserService ().getById (1 );
@@ -97,6 +103,7 @@ public static void prepareDatabase() throws Exception {
97
103
98
104
@ AfterClass
99
105
public static void cleanDatabase () throws Exception {
106
+ ProcessTestUtils .removeTestProcess (testProcessIds .get (MockDatabase .HIERARCHY_PARENT ));
100
107
MockDatabase .stopNode ();
101
108
MockDatabase .cleanDatabase ();
102
109
fileService .delete (URI .create ("1" ));
@@ -183,7 +190,7 @@ public void shouldRemoveProcess() throws Exception {
183
190
Process process = new Process ();
184
191
process .setTitle ("To Remove" );
185
192
processService .save (process );
186
- Process foundProcess = processService .getById (8 );
193
+ Process foundProcess = processService .getById (process . getId () );
187
194
assertEquals ("Additional process was not inserted in database!" , "To Remove" , foundProcess .getTitle ());
188
195
189
196
processService .remove (foundProcess );
@@ -214,7 +221,7 @@ public void shouldFindByTitle() throws DataException {
214
221
215
222
@ Test
216
223
public void shouldFindByMetadata () throws DataException {
217
- assertEquals (processNotFound , 4 ,
224
+ assertEquals (processNotFound , 3 ,
218
225
processService .findByMetadata (Collections .singletonMap ("TSL_ATS" , "Proc" )).size ());
219
226
}
220
227
@@ -225,8 +232,11 @@ public void shouldNotFindByMetadata() throws DataException {
225
232
}
226
233
227
234
@ Test
228
- public void shouldFindByMetadataContent () throws DataException {
235
+ public void shouldFindByMetadataContent () throws DataException , DAOException , IOException {
236
+ int testProcessId = MockDatabase .insertTestProcess (TEST_PROCESS_TITLE , 1 , 1 , 1 );
237
+ ProcessTestUtils .copyTestMetadataFile (testProcessId , TEST_METADATA_FILE );
229
238
assertEquals (processNotFound , 1 , processService .findByAnything ("SecondMetaShort" ).size ());
239
+ ProcessTestUtils .removeTestProcess (testProcessId );
230
240
}
231
241
232
242
@ Test
@@ -256,9 +266,11 @@ public void shouldNotFindByAnything() throws DataException {
256
266
}
257
267
258
268
@ Test
259
- public void shouldFindByMetadataGroupContent () throws DataException {
260
- processService .findByTitle ("Second Process" );
269
+ public void shouldFindByMetadataGroupContent () throws DataException , DAOException , IOException {
270
+ int testProcessId = MockDatabase .insertTestProcess ("Test process" , 1 , 1 , 1 );
271
+ ProcessTestUtils .copyTestMetadataFile (testProcessId , TEST_METADATA_FILE );
261
272
assertEquals (processNotFound , 1 , processService .findByAnything ("August" ).size ());
273
+ ProcessTestUtils .removeTestProcess (testProcessId );
262
274
}
263
275
264
276
@ Test
@@ -480,10 +492,13 @@ public void shouldGetProgressLocked() throws Exception {
480
492
481
493
@ Test
482
494
public void shouldGetMetadataFilePath () throws Exception {
483
- Process process = processService .getById (2 );
495
+ int testProcessId = MockDatabase .insertTestProcess (TEST_PROCESS_TITLE , 1 , 1 , 1 );
496
+ ProcessTestUtils .copyTestMetadataFile (testProcessId , TEST_METADATA_FILE );
497
+ Process process = processService .getById (testProcessId );
484
498
URI directory = fileService .getMetadataFilePath (process );
485
- boolean condition = directory .getRawPath ().contains ("2 /meta.xml" );
499
+ boolean condition = directory .getRawPath ().contains (testProcessId + " /meta.xml" );
486
500
assertTrue ("Metadata file path doesn't match to given file path!" , condition );
501
+ ProcessTestUtils .removeTestProcess (testProcessId );
487
502
}
488
503
489
504
@ Test
@@ -599,7 +614,7 @@ public void shouldUpdateChildrenFromLogicalStructure() throws Exception {
599
614
600
615
for (Process child : process .getChildren ()) {
601
616
assertTrue ("Process should have child to keep and child to add as only children" ,
602
- Arrays .asList ("HierarchChildToKeep " , "HierarchChildToAdd " ).contains (child .getTitle ()));
617
+ Arrays .asList ("HierarchyChildToKeep " , "HierarchyChildToAdd " ).contains (child .getTitle ()));
603
618
assertEquals ("Child should have parent as parent" , process , child .getParent ());
604
619
}
605
620
assertNull ("Process to remove should have no parent" , processService .getById (6 ).getParent ());
@@ -613,8 +628,10 @@ public void testFindAllIDs() throws DataException {
613
628
614
629
allIDs = ServiceManager .getProcessService ().findAllIDs (0L , 5 );
615
630
Assert .assertEquals ("Wrong amount of id's in index" , 5 , allIDs .size ());
616
- Assert .assertEquals ("Duplicate ids in index" , allIDs .size (), new HashSet <Integer >(allIDs ).size ());
617
- int maxId = allIDs .stream ().mapToInt (Integer ::intValue ).max ().getAsInt ();
631
+ Assert .assertEquals ("Duplicate ids in index" , allIDs .size (), new HashSet <>(allIDs ).size ());
632
+ OptionalInt maxStream = allIDs .stream ().mapToInt (Integer ::intValue ).max ();
633
+ assertTrue ("Unable to find largest ID in stream of all process IDs!" , maxStream .isPresent ());
634
+ int maxId = maxStream .getAsInt ();
618
635
int minId = allIDs .stream ().mapToInt (Integer ::intValue ).min ().getAsInt ();
619
636
Assert .assertTrue ("Ids should all be smaller than 8" , maxId < 8 );
620
637
Assert .assertTrue ("Ids should all be larger than 0" , minId > 0 );
@@ -624,11 +641,14 @@ public void testFindAllIDs() throws DataException {
624
641
}
625
642
626
643
@ Test
627
- public void testCountMetadata () throws DAOException , IOException {
628
- Process process = ServiceManager .getProcessService ().getById (2 );
644
+ public void testCountMetadata () throws DAOException , IOException , DataException {
645
+ int testProcessId = MockDatabase .insertTestProcess (TEST_PROCESS_TITLE , 1 , 1 , 1 );
646
+ ProcessTestUtils .copyTestMetadataFile (testProcessId , TEST_METADATA_FILE );
647
+ Process process = ServiceManager .getProcessService ().getById (testProcessId );
629
648
URI metadataFilePath = ServiceManager .getFileService ().getMetadataFilePath (process );
630
649
Workpiece workpiece = ServiceManager .getMetsService ().loadWorkpiece (metadataFilePath );
631
650
long logicalMetadata = MetsService .countLogicalMetadata (workpiece );
632
651
Assert .assertEquals ("Wrong amount of metadata found!" , 4 , logicalMetadata );
652
+ ProcessTestUtils .removeTestProcess (testProcessId );
633
653
}
634
654
}
0 commit comments