Skip to content

Commit f36b852

Browse files
committed
Refactor ProcessServiceIT to load test files dynamically
1 parent 1121736 commit f36b852

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java

+34-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Collections;
2929
import java.util.HashSet;
3030
import java.util.List;
31+
import java.util.Map;
32+
import java.util.OptionalInt;
3133

3234
import org.apache.commons.lang3.SystemUtils;
3335
import org.elasticsearch.index.query.Operator;
@@ -64,6 +66,7 @@
6466
import org.kitodo.production.services.ServiceManager;
6567
import org.kitodo.production.services.dataformat.MetsService;
6668
import org.kitodo.production.services.file.FileService;
69+
import org.kitodo.utils.ProcessTestUtils;
6770

6871
/**
6972
* Tests for ProcessService class.
@@ -72,10 +75,12 @@ public class ProcessServiceIT {
7275

7376
private static final FileService fileService = new FileService();
7477
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";
7680
private static final String firstProcess = "First process";
7781
private static final String processNotFound = "Process was not found in index!";
7882
private static final File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
83+
private static Map<String, Integer> testProcessIds;
7984

8085
@BeforeClass
8186
public static void prepareDatabase() throws Exception {
@@ -84,7 +89,8 @@ public static void prepareDatabase() throws Exception {
8489
}
8590
MockDatabase.startNode();
8691
MockDatabase.insertProcessesFull();
87-
MockDatabase.insertProcessesForHierarchyTests();
92+
testProcessIds = MockDatabase.insertProcessesForHierarchyTests();
93+
ProcessTestUtils.copyHierarchyTestFiles(testProcessIds);
8894
MockDatabase.setUpAwaitility();
8995
fileService.createDirectory(URI.create(""), "1");
9096
User userOne = ServiceManager.getUserService().getById(1);
@@ -97,6 +103,7 @@ public static void prepareDatabase() throws Exception {
97103

98104
@AfterClass
99105
public static void cleanDatabase() throws Exception {
106+
ProcessTestUtils.removeTestProcess(testProcessIds.get(MockDatabase.HIERARCHY_PARENT));
100107
MockDatabase.stopNode();
101108
MockDatabase.cleanDatabase();
102109
fileService.delete(URI.create("1"));
@@ -183,7 +190,7 @@ public void shouldRemoveProcess() throws Exception {
183190
Process process = new Process();
184191
process.setTitle("To Remove");
185192
processService.save(process);
186-
Process foundProcess = processService.getById(8);
193+
Process foundProcess = processService.getById(process.getId());
187194
assertEquals("Additional process was not inserted in database!", "To Remove", foundProcess.getTitle());
188195

189196
processService.remove(foundProcess);
@@ -214,7 +221,7 @@ public void shouldFindByTitle() throws DataException {
214221

215222
@Test
216223
public void shouldFindByMetadata() throws DataException {
217-
assertEquals(processNotFound, 4,
224+
assertEquals(processNotFound, 3,
218225
processService.findByMetadata(Collections.singletonMap("TSL_ATS", "Proc")).size());
219226
}
220227

@@ -225,8 +232,11 @@ public void shouldNotFindByMetadata() throws DataException {
225232
}
226233

227234
@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);
229238
assertEquals(processNotFound, 1, processService.findByAnything("SecondMetaShort").size());
239+
ProcessTestUtils.removeTestProcess(testProcessId);
230240
}
231241

232242
@Test
@@ -256,9 +266,11 @@ public void shouldNotFindByAnything() throws DataException {
256266
}
257267

258268
@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);
261272
assertEquals(processNotFound, 1, processService.findByAnything("August").size());
273+
ProcessTestUtils.removeTestProcess(testProcessId);
262274
}
263275

264276
@Test
@@ -480,10 +492,13 @@ public void shouldGetProgressLocked() throws Exception {
480492

481493
@Test
482494
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);
484498
URI directory = fileService.getMetadataFilePath(process);
485-
boolean condition = directory.getRawPath().contains("2/meta.xml");
499+
boolean condition = directory.getRawPath().contains(testProcessId + "/meta.xml");
486500
assertTrue("Metadata file path doesn't match to given file path!", condition);
501+
ProcessTestUtils.removeTestProcess(testProcessId);
487502
}
488503

489504
@Test
@@ -599,7 +614,7 @@ public void shouldUpdateChildrenFromLogicalStructure() throws Exception {
599614

600615
for (Process child : process.getChildren()) {
601616
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()));
603618
assertEquals("Child should have parent as parent", process, child.getParent());
604619
}
605620
assertNull("Process to remove should have no parent", processService.getById(6).getParent());
@@ -613,8 +628,10 @@ public void testFindAllIDs() throws DataException {
613628

614629
allIDs = ServiceManager.getProcessService().findAllIDs(0L, 5);
615630
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();
618635
int minId = allIDs.stream().mapToInt(Integer::intValue).min().getAsInt();
619636
Assert.assertTrue("Ids should all be smaller than 8", maxId < 8);
620637
Assert.assertTrue("Ids should all be larger than 0", minId > 0);
@@ -624,11 +641,14 @@ public void testFindAllIDs() throws DataException {
624641
}
625642

626643
@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);
629648
URI metadataFilePath = ServiceManager.getFileService().getMetadataFilePath(process);
630649
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metadataFilePath);
631650
long logicalMetadata = MetsService.countLogicalMetadata(workpiece);
632651
Assert.assertEquals("Wrong amount of metadata found!", 4, logicalMetadata);
652+
ProcessTestUtils.removeTestProcess(testProcessId);
633653
}
634654
}

0 commit comments

Comments
 (0)