Skip to content

Commit b4ae6a9

Browse files
committed
Load resources for MigrationIT dynamically
1 parent 0a82503 commit b4ae6a9

File tree

5 files changed

+117
-85
lines changed

5 files changed

+117
-85
lines changed

Kitodo/src/test/java/org/kitodo/production/helper/tasks/MigrationTaskIT.java

+18-61
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,51 @@
1111

1212
package org.kitodo.production.helper.tasks;
1313

14-
import java.io.File;
15-
import java.lang.reflect.Field;
16-
import java.util.Arrays;
14+
import java.util.LinkedList;
1715
import java.util.List;
18-
import java.util.Objects;
1916

20-
import org.apache.commons.io.FileUtils;
2117
import org.junit.AfterClass;
2218
import org.junit.Assert;
2319
import org.junit.BeforeClass;
2420
import org.junit.Test;
2521
import org.kitodo.MockDatabase;
26-
import org.kitodo.config.ConfigCore;
2722
import org.kitodo.data.database.beans.Project;
2823
import org.kitodo.production.services.ServiceManager;
2924
import org.kitodo.production.services.data.ProcessService;
3025
import org.kitodo.production.services.dataformat.MetsService;
26+
import org.kitodo.utils.ProcessTestUtils;
3127

3228
public class MigrationTaskIT {
3329

3430
private static final MetsService metsService = ServiceManager.getMetsService();
3531
private static final ProcessService processService = ServiceManager.getProcessService();
3632
private static Project project;
33+
private static List<Integer> dummyProcessIds = new LinkedList<>();
34+
private static final String METADATA_MIGRATION_SOURCE_FILE = "testMetadataMigrationSourceFile.xml";
35+
private static final String METADATA_MIGRATION_PROCESS_TITLE = "Migration process";
36+
private static int migrationTestProcessId = -1;
37+
private static final int PROJECT_ID = 1;
38+
private static final int MIGRATION_PROJECT_ID = 3;
39+
private static final int TEMPLATE_ID = 1;
40+
private static final int RULESET_ID = 1;
3741

3842
@BeforeClass
3943
public static void prepareDatabase() throws Exception {
4044
MockDatabase.startNode();
4145
MockDatabase.insertProcessesFull();
42-
43-
project = ServiceManager.getProjectService().getById(1);
44-
moveMetaFileAway(2, "meta_MigrationTaskIT.tmp");
45-
createTestMetafile(2);
46+
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
47+
project = ServiceManager.getProjectService().getById(MIGRATION_PROJECT_ID);
48+
migrationTestProcessId = MockDatabase.insertTestProcess(METADATA_MIGRATION_PROCESS_TITLE, MIGRATION_PROJECT_ID,
49+
TEMPLATE_ID, RULESET_ID);
50+
ProcessTestUtils.copyTestMetadataFile(migrationTestProcessId, METADATA_MIGRATION_SOURCE_FILE);
4651
}
4752

4853
@AfterClass
4954
public static void cleanDatabase() throws Exception {
55+
ProcessTestUtils.cleanupDummyProcesses(dummyProcessIds);
56+
ProcessTestUtils.removeTestProcess(migrationTestProcessId);
5057
MockDatabase.stopNode();
5158
MockDatabase.cleanDatabase();
52-
cleanUp();
5359
}
5460

5561
@Test
@@ -61,55 +67,6 @@ public void testMigrationTask() throws Exception {
6167
Assert.assertFalse(migrationTask.isAlive());
6268
Assert.assertEquals(100, migrationTask.getProgress());
6369
Assert.assertNotNull("Process migration failed",
64-
metsService.loadWorkpiece(processService.getMetadataFileUri(processService.getById(2))));
65-
}
66-
67-
private static void moveMetaFileAway(int recordNumber, String tempFileName) throws Exception {
68-
File processHome = new File(ConfigCore.getKitodoDataDirectory(), Integer.toString(recordNumber));
69-
new File(processHome, "meta.xml").renameTo(new File(processHome, tempFileName));
70-
}
71-
72-
private static void createTestMetafile(int recordNumber) throws Exception {
73-
List<String> lines = Arrays.asList("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
74-
"<mets:mets xsi:schemaLocation=\"http://www.loc.gov/mods/v3 "
75-
+ "http://www.loc.gov/standards/mods/mods.xsd http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd "
76-
+ "info:lc/xmlns/premis-v2 http://www.loc.gov/standards/premis/v2/premis-v2-0.xsd http://www.loc.gov/mix/v10 "
77-
+ "http://www.loc.gov/standards/mix/mix10/mix10.xsd\" xmlns:mets=\"http://www.loc.gov/METS/\" "
78-
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
79-
+ "<mets:metsHdr CREATEDATE=\"2018-09-14T07:45:17\">"
80-
+ "<mets:agent OTHERTYPE=\"SOFTWARE\" ROLE=\"CREATOR\" TYPE=\"OTHER\">"
81-
+ "<mets:name>Kitodo - kitodo-ugh-3.0-SNAPSHOT - 18-April-2018 13:20:13</mets:name>"
82-
+ "<mets:note>Kitodo</mets:note></mets:agent></mets:metsHdr>"
83-
+ "<mets:dmdSec ID=\"DMDLOG_0000\"><mets:mdWrap MDTYPE=\"MODS\"><mets:xmlData>"
84-
+ "<mods:mods xmlns:mods=\"http://www.loc.gov/mods/v3\"><mods:extension>"
85-
+ "<goobi:goobi xmlns:goobi=\"http://meta.goobi.org/v1.5.1/\">"
86-
+ "<goobi:metadata name=\"TitleDocMain\">Second process</goobi:metadata>"
87-
+ "<goobi:metadata name=\"TitleDocMainShort\">SecondMetaShort</goobi:metadata>"
88-
+ "<goobi:metadata name=\"TSL_ATS\">Proc</goobi:metadata>"
89-
+ "</goobi:goobi></mods:extension></mods:mods></mets:xmlData></mets:mdWrap></mets:dmdSec>"
90-
+ "<mets:dmdSec ID=\"DMDPHYS_0000\"><mets:mdWrap MDTYPE=\"MODS\"><mets:xmlData>"
91-
+ "<mods:mods xmlns:mods=\"http://www.loc.gov/mods/v3\">"
92-
+ "<mods:extension><goobi:goobi xmlns:goobi=\"http://meta.goobi.org/v1.5.1/\">"
93-
+ "<goobi:metadata name=\"pathimagefiles\">file:/2/images/Sec_Proc_tif</goobi:metadata>"
94-
+ "</goobi:goobi></mods:extension></mods:mods></mets:xmlData></mets:mdWrap></mets:dmdSec>"
95-
+ "<mets:structMap TYPE=\"LOGICAL\">"
96-
+ "<mets:div DMDID=\"DMDLOG_0000\" ID=\"LOG_0000\" TYPE=\"Monograph\"/>" + "</mets:structMap>"
97-
+ "<mets:structMap TYPE=\"PHYSICAL\">"
98-
+ "<mets:div DMDID=\"DMDPHYS_0000\" ID=\"PHYS_0000\" TYPE=\"physSequence\"/>" + "</mets:structMap>"
99-
+ "<mets:structLink>"
100-
+ "<mets:smLink xlink:to=\"PHYS_0000\" xlink:from=\"LOG_0000\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"/>"
101-
+ "</mets:structLink></mets:mets>");
102-
File processHome = new File(ConfigCore.getKitodoDataDirectory(), "2");
103-
FileUtils.writeLines(new File(processHome, "meta.xml"), "UTF-8", lines);
104-
}
105-
106-
private static void cleanUp() throws Exception {
107-
File processHome = new File(ConfigCore.getKitodoDataDirectory(), "2");
108-
File originalMeta = new File(processHome, "meta_MigrationTaskIT.tmp");
109-
if (originalMeta.exists()) {
110-
File metaFile = new File(processHome, "meta.xml");
111-
metaFile.delete();
112-
originalMeta.renameTo(metaFile);
113-
}
70+
metsService.loadWorkpiece(processService.getMetadataFileUri(processService.getById(migrationTestProcessId))));
11471
}
11572
}

Kitodo/src/test/java/org/kitodo/selenium/CalendarST.java

+3-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
3131
import org.kitodo.data.exceptions.DataException;
3232
import org.kitodo.production.services.ServiceManager;
33-
import org.kitodo.production.services.data.ProcessService;
3433
import org.kitodo.selenium.testframework.BaseTestSelenium;
3534
import org.kitodo.selenium.testframework.Pages;
3635
import org.kitodo.selenium.testframework.pages.CalendarPage;
@@ -55,8 +54,8 @@ public static void setup() throws Exception {
5554
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
5655
newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId);
5756
ProcessTestUtils.copyTestMetadataFile(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE);
58-
Process newsPaperProcess = ServiceManager.getProcessService().getById(newspaperTestProcessId);
5957
// re-save process to index meta.xml content including process base type!
58+
Process newsPaperProcess = ServiceManager.getProcessService().getById(newspaperTestProcessId);
6059
ServiceManager.getProcessService().save(newsPaperProcess);
6160
}
6261

@@ -74,7 +73,8 @@ public void logout() throws Exception {
7473

7574
@AfterClass
7675
public static void cleanup() throws CustomResponseException, DAOException, DataException, IOException {
77-
cleanupNewspaperProcess();
76+
ProcessTestUtils.cleanupDummyProcesses(dummyProcessIds);
77+
ProcessTestUtils.removeTestProcess(newspaperTestProcessId);
7878
}
7979

8080
@Test
@@ -91,21 +91,4 @@ public void createProcessFromCalendar() throws Exception {
9191
assertEquals("Metadata for morning issue is incorrect", Arrays.asList("Signatur", "Process title"), morningIssueMetadata);
9292
assertEquals("Metadata for evening issue is incorrect", List.of("Signatur"), eveningIssueMetadata);
9393
}
94-
95-
private static void deleteProcessHierarchy(Process process) throws DAOException, DataException, IOException {
96-
for (Process childProcess : process.getChildren()) {
97-
deleteProcessHierarchy(childProcess);
98-
}
99-
ProcessService.deleteProcess(process.getId());
100-
}
101-
102-
private static void cleanupNewspaperProcess() throws CustomResponseException, DataException, DAOException, IOException {
103-
for (int processId : dummyProcessIds) {
104-
ServiceManager.getProcessService().removeFromDatabase(processId);
105-
ServiceManager.getProcessService().removeFromIndex(processId, false);
106-
}
107-
if (newspaperTestProcessId > 0) {
108-
deleteProcessHierarchy(ServiceManager.getProcessService().getById(newspaperTestProcessId));
109-
}
110-
}
11194
}

Kitodo/src/test/java/org/kitodo/selenium/MetadataST.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ public static void cleanup() throws DAOException, CustomResponseException, DataE
258258
for (int processId : processHierarchyTestProcessIds) {
259259
ProcessService.deleteProcess(processId);
260260
}
261-
for (int dummyProcessId : dummyProcessIds) {
262-
ServiceManager.getProcessService().removeFromDatabase(dummyProcessId);
263-
ServiceManager.getProcessService().removeFromIndex(dummyProcessId, false);
264-
}
261+
ProcessTestUtils.cleanupDummyProcesses(dummyProcessIds);
265262
ProcessService.deleteProcess(mediaReferencesProcessId);
266263
ProcessService.deleteProcess(metadataLockProcessId);
267264
ProcessService.deleteProcess(renamingMediaProcessId);

Kitodo/src/test/java/org/kitodo/utils/ProcessTestUtils.java

+37
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import org.kitodo.config.ConfigCore;
3131
import org.kitodo.data.database.beans.Process;
3232
import org.kitodo.data.database.exceptions.DAOException;
33+
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
3334
import org.kitodo.data.exceptions.DataException;
3435
import org.kitodo.production.forms.createprocess.ProcessTextMetadata;
3536
import org.kitodo.production.services.ServiceManager;
37+
import org.kitodo.production.services.data.ProcessService;
3638
import org.primefaces.model.DefaultTreeNode;
3739

3840
public class ProcessTestUtils {
@@ -154,4 +156,39 @@ private static boolean processDirExists(int processId) {
154156
URI uri = Paths.get(ConfigCore.getKitodoDataDirectory(), String.valueOf(processId)).toUri();
155157
return ServiceManager.getFileService().isDirectory(uri);
156158
}
159+
160+
/**
161+
* Delete processes with given IDs from test database and test index.
162+
* @param dummyProcessIds list of IDs from processes to remove from database and index
163+
* @throws DAOException when removing process from database fails
164+
* @throws CustomResponseException when removing process from index fails
165+
* @throws DataException when removing process from index fails
166+
*/
167+
public static void cleanupDummyProcesses(List<Integer> dummyProcessIds) throws DAOException,
168+
CustomResponseException, DataException {
169+
for (int processId : dummyProcessIds) {
170+
ServiceManager.getProcessService().removeFromDatabase(processId);
171+
ServiceManager.getProcessService().removeFromIndex(processId, false);
172+
}
173+
}
174+
175+
/**
176+
* Delete test process with given ID including potential parent processes.
177+
* @param testProcessId ID of process to delete
178+
* @throws DAOException when retrieving process from database for deletion fails
179+
* @throws DataException when deleting process fails
180+
* @throws IOException when deleting process resources from the file system fails
181+
*/
182+
public static void removeTestProcess(int testProcessId) throws DAOException, DataException, IOException {
183+
if (testProcessId > 0) {
184+
deleteProcessHierarchy(ServiceManager.getProcessService().getById(testProcessId));
185+
}
186+
}
187+
188+
private static void deleteProcessHierarchy(Process process) throws DAOException, DataException, IOException {
189+
for (Process childProcess : process.getChildren()) {
190+
deleteProcessHierarchy(childProcess);
191+
}
192+
ProcessService.deleteProcess(process.getId());
193+
}
157194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
4+
*
5+
* This file is part of the Kitodo project.
6+
*
7+
* It is licensed under GNU General Public License version 3 or later.
8+
*
9+
* For the full copyright and license information, please read the
10+
* GPL3-License.txt file that was distributed with this source code.
11+
-->
12+
<mets:mets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xmlns:mets="http://www.loc.gov/METS/"
14+
xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/mods.xsd http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd info:lc/xmlns/premis-v2 http://www.loc.gov/standards/premis/v2/premis-v2-0.xsd http://www.loc.gov/mix/v10 http://www.loc.gov/standards/mix/mix10/mix10.xsd">
15+
<mets:metsHdr CREATEDATE="2018-09-14T07:45:17">
16+
<mets:agent OTHERTYPE="SOFTWARE" ROLE="CREATOR" TYPE="OTHER">
17+
<mets:name>Kitodo - kitodo-ugh-3.0-SNAPSHOT - 18-April-2018 13:20:13</mets:name>
18+
<mets:note>Kitodo</mets:note>
19+
</mets:agent>
20+
</mets:metsHdr>
21+
<mets:dmdSec ID="DMDLOG_0000">
22+
<mets:mdWrap MDTYPE="MODS">
23+
<mets:xmlData>
24+
<mods:mods xmlns:mods="http://www.loc.gov/mods/v3">
25+
<mods:extension>
26+
<goobi:goobi xmlns:goobi="http://meta.goobi.org/v1.5.1/">
27+
<goobi:metadata name="TitleDocMain">Second process</goobi:metadata>
28+
<goobi:metadata name="TitleDocMainShort">SecondMetaShort</goobi:metadata>
29+
<goobi:metadata name="TSL_ATS">Proc</goobi:metadata>
30+
</goobi:goobi>
31+
</mods:extension>
32+
</mods:mods>
33+
</mets:xmlData>
34+
</mets:mdWrap>
35+
</mets:dmdSec>
36+
<mets:dmdSec ID="DMDPHYS_0000">
37+
<mets:mdWrap MDTYPE="MODS">
38+
<mets:xmlData>
39+
<mods:mods xmlns:mods="http://www.loc.gov/mods/v3">
40+
<mods:extension>
41+
<goobi:goobi xmlns:goobi="http://meta.goobi.org/v1.5.1/">
42+
<goobi:metadata name="pathimagefiles">file:/2/images/Sec_Proc_tif</goobi:metadata>
43+
</goobi:goobi>
44+
</mods:extension>
45+
</mods:mods>
46+
</mets:xmlData>
47+
</mets:mdWrap>
48+
</mets:dmdSec>
49+
<mets:structMap TYPE="LOGICAL">
50+
<mets:div DMDID="DMDLOG_0000" ID="LOG_0000" TYPE="Monograph"/>
51+
</mets:structMap>
52+
<mets:structMap TYPE="PHYSICAL">
53+
<mets:div DMDID="DMDPHYS_0000" ID="PHYS_0000" TYPE="physSequence"/>
54+
</mets:structMap>
55+
<mets:structLink>
56+
<mets:smLink xlink:to="PHYS_0000" xlink:from="LOG_0000" xmlns:xlink="http://www.w3.org/1999/xlink"/>
57+
</mets:structLink>
58+
</mets:mets>

0 commit comments

Comments
 (0)