Skip to content

Commit 0a82503

Browse files
committed
Refactor test framework to load test resources for newspaper process generator integration test dynamically
1 parent 457cc74 commit 0a82503

File tree

12 files changed

+197
-107
lines changed

12 files changed

+197
-107
lines changed

Kitodo/src/test/java/org/kitodo/MockDatabase.java

+46-19
Original file line numberDiff line numberDiff line change
@@ -724,23 +724,24 @@ public static void removeProcessesForHierarchyTests() throws DataException {
724724
ServiceManager.getProcessService().remove(4);
725725
}
726726

727-
728-
public static void insertProcessForCalendarHierarchyTests() throws DAOException, DataException {
729-
Ruleset fivthRuleset = new Ruleset();
730-
fivthRuleset.setTitle("Newspaper");
731-
fivthRuleset.setFile("newspaper.xml");
732-
fivthRuleset.setOrderMetadataByRuleset(false);
733-
fivthRuleset.setClient(ServiceManager.getClientService().getById(1));
734-
ServiceManager.getRulesetService().save(fivthRuleset);
735-
736-
insertPlaceholderProcesses(4, 9);
737-
738-
Process tenthProcess = new Process();
739-
tenthProcess.setProject(ServiceManager.getProjectService().getById(1));
740-
tenthProcess.setTemplate(ServiceManager.getTemplateService().getById(1));
741-
tenthProcess.setRuleset(ServiceManager.getRulesetService().getById(5));
742-
tenthProcess.setTitle("NewspaperOverallProcess");
743-
ServiceManager.getProcessService().save(tenthProcess);
727+
/**
728+
* Insert ruleset.
729+
* @param rulesetTitle ruleset title
730+
* @param rulesetFilename ruleset filename
731+
* @param clientId client id
732+
* @return id of ruleset
733+
* @throws DAOException when retrieving client by ID fails
734+
* @throws DataException when saving ruleset failed
735+
*/
736+
public static int insertRuleset(String rulesetTitle, String rulesetFilename, int clientId) throws DAOException,
737+
DataException {
738+
Ruleset ruleset = new Ruleset();
739+
ruleset.setTitle(rulesetTitle);
740+
ruleset.setFile(rulesetFilename);
741+
ruleset.setOrderMetadataByRuleset(false);
742+
ruleset.setClient(ServiceManager.getClientService().getById(clientId));
743+
ServiceManager.getRulesetService().save(ruleset);
744+
return ruleset.getId();
744745
}
745746

746747
private static void insertTemplates() throws DAOException, DataException {
@@ -1061,12 +1062,13 @@ private static void insertFolders() throws DAOException, DataException {
10611062
/**
10621063
* Insert dummy process into database.
10631064
* @param dummyProcessId id used in dummy process title
1065+
* @param projectId id of project to which the dummy process is added
10641066
* @return database ID of created dummy process
10651067
* @throws DAOException when loading test project fails
10661068
* @throws DataException when saving dummy process fails
10671069
*/
1068-
public static int insertDummyProcess(int dummyProcessId) throws DAOException, DataException {
1069-
Project firstProject = ServiceManager.getProjectService().getById(2);
1070+
public static int insertDummyProcess(int dummyProcessId, int projectId) throws DAOException, DataException {
1071+
Project firstProject = ServiceManager.getProjectService().getById(projectId);
10701072
Template template = firstProject.getTemplates().get(0);
10711073
Process dummyProcess = new Process();
10721074
dummyProcess.setTitle("Dummy_process_" + dummyProcessId);
@@ -1127,6 +1129,31 @@ public static int insertTestProcessIntoSecondProject(String processTitle) throws
11271129
return mediaReferencesProcess.getId();
11281130
}
11291131

1132+
/**
1133+
* Insert test process.
1134+
* @param processTitle process title of test process
1135+
* @param projectId project id of test process
1136+
* @param templateId template id of test process
1137+
* @param rulesetId ruleset id of test process
1138+
* @return id of test process
1139+
* @throws DAOException when retrieving project, template or ruleset fails
1140+
* @throws DataException when saving test process fails
1141+
*/
1142+
public static int insertTestProcess(String processTitle, int projectId, int templateId, int rulesetId)
1143+
throws DAOException, DataException {
1144+
Project project = ServiceManager.getProjectService().getById(projectId);
1145+
Template template = ServiceManager.getTemplateService().getById(templateId);
1146+
Ruleset ruleset = ServiceManager.getRulesetService().getById(rulesetId);
1147+
Process process = new Process();
1148+
process.setTitle(processTitle);
1149+
process.setProject(project);
1150+
process.setTemplate(template);
1151+
process.setRuleset(ruleset);
1152+
process.setDocket(template.getDocket());
1153+
ServiceManager.getProcessService().save(process);
1154+
return process.getId();
1155+
}
1156+
11301157
/**
11311158
* Insert folders into database and add them to second test project.
11321159
* @throws DAOException when loading project or template fails

Kitodo/src/test/java/org/kitodo/production/metadata/MetadataEditorIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void shouldAddLink() throws Exception {
8787
MetadataEditor.addLink(ServiceManager.getProcessService().getById(4), "0", 7);
8888

8989
assertTrue("The link was not added correctly!",
90-
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(37), 7));
90+
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(36), 7));
9191

9292
FileUtils.writeLines(metaXmlFile, StandardCharsets.UTF_8.toString(), metaXmlContentBefore);
9393
FileUtils.deleteQuietly(new File("src/test/resources/metadata/4/meta.xml.1"));

Kitodo/src/test/java/org/kitodo/production/process/NewspaperProcessesGeneratorIT.java

+71-56
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
import java.io.File;
1515
import java.io.IOException;
1616
import java.net.URI;
17-
import java.nio.file.DirectoryStream;
18-
import java.nio.file.Files;
19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
2117
import java.time.MonthDay;
2218
import java.util.Arrays;
19+
import java.util.LinkedList;
2320
import java.util.List;
2421
import java.util.Objects;
2522
import java.util.stream.Collectors;
2623

27-
import org.apache.commons.io.FileUtils;
2824
import org.apache.commons.lang3.SystemUtils;
2925
import org.awaitility.Awaitility;
3026
import org.junit.After;
27+
import org.junit.AfterClass;
3128
import org.junit.Assert;
3229
import org.junit.Before;
30+
import org.junit.BeforeClass;
3331
import org.junit.Rule;
3432
import org.junit.Test;
3533
import org.junit.rules.ExpectedException;
@@ -38,7 +36,6 @@
3836
import org.kitodo.MockDatabase;
3937
import org.kitodo.NewspaperCourse;
4038
import org.kitodo.SecurityTestUtils;
41-
import org.kitodo.TreeDeleter;
4239
import org.kitodo.api.MetadataEntry;
4340
import org.kitodo.api.dataformat.LogicalDivision;
4441
import org.kitodo.api.dataformat.Workpiece;
@@ -48,6 +45,7 @@
4845
import org.kitodo.data.database.beans.Process;
4946
import org.kitodo.data.database.beans.User;
5047
import org.kitodo.data.database.exceptions.DAOException;
48+
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
5149
import org.kitodo.data.exceptions.DataException;
5250
import org.kitodo.production.dto.ProcessDTO;
5351
import org.kitodo.production.helper.tasks.GeneratesNewspaperProcessesThread;
@@ -56,13 +54,19 @@
5654
import org.kitodo.production.services.ServiceManager;
5755
import org.kitodo.production.services.data.ProcessService;
5856
import org.kitodo.production.services.dataformat.MetsService;
57+
import org.kitodo.utils.ProcessTestUtils;
5958

6059
public class NewspaperProcessesGeneratorIT {
6160
private static final ProcessService processService = ServiceManager.getProcessService();
6261
private static final MetsService metsService = ServiceManager.getMetsService();
6362

6463
private static final String firstProcess = "First process";
6564
private static final File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
65+
private static List<Integer> dummyProcessIds = new LinkedList<>();
66+
private static int newspaperTestProcessId = -1;
67+
private static int rulesetId = -1;
68+
private static final String NEWSPAPER_TEST_METADATA_FILE = "testmetaNewspaper.xml";
69+
private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess";
6670

6771
@Rule
6872
public ExpectedException expectedEx = ExpectedException.none();
@@ -73,16 +77,17 @@ public class NewspaperProcessesGeneratorIT {
7377
* @throws Exception
7478
* if that does not work
7579
*/
76-
@Before
77-
public void setUp() throws Exception {
80+
@BeforeClass
81+
public static void setUp() throws Exception {
7882
if (!SystemUtils.IS_OS_WINDOWS) {
7983
ExecutionPermission.setExecutePermission(script);
8084
}
8185
FileLoader.createConfigProjectsFileForCalendarHierarchyTests();
8286
MockDatabase.startNode();
8387
MockDatabase.insertProcessesFull();
84-
MockDatabase.insertProcessForCalendarHierarchyTests();
88+
MockDatabase.insertFoldersForSecondProject();
8589
MockDatabase.setUpAwaitility();
90+
rulesetId = MockDatabase.insertRuleset("Newspaper", "newspaper.xml", 1);
8691
User userOne = ServiceManager.getUserService().getById(1);
8792
SecurityTestUtils.addUserDataToSecurityContext(userOne, 1);
8893
Awaitility.await().until(() -> {
@@ -94,8 +99,8 @@ public void setUp() throws Exception {
9499
/**
95100
* The test environment is cleaned up and the database is closed.
96101
*/
97-
@After
98-
public void tearDown() throws Exception {
102+
@AfterClass
103+
public static void tearDown() throws Exception {
99104
MockDatabase.stopNode();
100105
MockDatabase.cleanDatabase();
101106
KitodoConfigFile.PROJECT_CONFIGURATION.getFile().delete();
@@ -105,37 +110,74 @@ public void tearDown() throws Exception {
105110
}
106111
}
107112

113+
/**
114+
* Create dummy processes and newspaper test process and copy corresponding meta.xml file.
115+
* @throws DAOException when inserting test or dummy processes fails
116+
* @throws DataException when inserting test or dummy processes fails
117+
* @throws IOException when copying test metadata file fails
118+
*/
119+
@Before
120+
public void prepareNewspaperProcess() throws DAOException, DataException, IOException {
121+
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(2);
122+
newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId);
123+
ProcessTestUtils.copyTestFiles(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE);
124+
}
125+
126+
/**
127+
* Remove dummy and newspaper test processes.
128+
* @throws DAOException when removing dummy processes from database fails
129+
* @throws CustomResponseException when removing dummy processes from index fails
130+
* @throws DataException when deleting newspaper test processes fails
131+
* @throws IOException when deleting metadata test files fails
132+
*/
133+
@After
134+
public void cleanupNewspaperProcess() throws DAOException, CustomResponseException, DataException, IOException {
135+
for (int processId : dummyProcessIds) {
136+
ServiceManager.getProcessService().removeFromDatabase(processId);
137+
ServiceManager.getProcessService().removeFromIndex(processId, false);
138+
}
139+
if (newspaperTestProcessId > 0) {
140+
deleteProcessHierarchy(ServiceManager.getProcessService().getById(newspaperTestProcessId));
141+
}
142+
}
143+
144+
private static void deleteProcessHierarchy(Process process) throws DAOException, DataException, IOException {
145+
for (Process childProcess : process.getChildren()) {
146+
deleteProcessHierarchy(childProcess);
147+
}
148+
ProcessService.deleteProcess(process.getId());
149+
}
150+
108151
/**
109152
* Perform the test. A long-running task is simulated: Progress and number
110153
* of steps are queried and the next step is performed.
111154
*/
112155
@Test
113156
public void shouldGenerateNewspaperProcesses() throws Exception {
114-
// create backup of meta data file as this file is modified inside test
115-
File metaFile = new File("src/test/resources/metadata/10/meta.xml");
116-
File backupFile = new File("src/test/resources/metadata/10/meta.xml.1");
117-
FileUtils.copyFile(metaFile, backupFile);
118-
119-
Process completeEdition = ServiceManager.getProcessService().getById(10);
157+
Process completeEdition = ServiceManager.getProcessService().getById(newspaperTestProcessId);
120158
Course course = NewspaperCourse.getCourse();
121159
course.splitInto(Granularity.DAYS);
122160
NewspaperProcessesGenerator underTest = new NewspaperProcessesGenerator(completeEdition, course);
123161
while (underTest.getProgress() < underTest.getNumberOfSteps()) {
124162
underTest.nextStep();
125163
}
164+
int maxId = getChildProcessWithLargestId(completeEdition, 0);
126165
Assert.assertEquals("The newspaper processes generator has not been completed!", underTest.getNumberOfSteps(),
127166
underTest.getProgress());
128167
Assert.assertEquals("Process title missing in newspaper's meta.xml", "NewspaperOverallProcess",
129-
readProcessTitleFromMetadata(10, false));
168+
readProcessTitleFromMetadata(newspaperTestProcessId, false));
130169
Assert.assertEquals("Process title missing in year's meta.xml", "NewspaperOverallProcess_1703",
131-
readProcessTitleFromMetadata(11, false));
170+
readProcessTitleFromMetadata(newspaperTestProcessId + 1, false));
132171
Assert.assertEquals("Process title missing in issue's meta.xml", "NewspaperOverallProcess_17050127",
133-
readProcessTitleFromMetadata(28, true));
172+
readProcessTitleFromMetadata(maxId, true));
173+
}
134174

135-
// restore backuped meta data file
136-
FileUtils.deleteQuietly(metaFile);
137-
FileUtils.moveFile(backupFile, metaFile);
138-
cleanUp();
175+
private int getChildProcessWithLargestId(Process process, int maxId) {
176+
maxId = Math.max(maxId, process.getId());
177+
for (Process childProcess : process.getChildren()) {
178+
maxId = getChildProcessWithLargestId(childProcess, maxId);
179+
}
180+
return maxId;
139181
}
140182

141183
/*
@@ -162,13 +204,7 @@ private String readProcessTitleFromMetadata(int processId, boolean issue) throws
162204
*/
163205
@Test
164206
public void shouldGenerateSeasonProcesses() throws Exception {
165-
// create backup of meta data file as this file is modified inside test
166-
File metaFile = new File("src/test/resources/metadata/10/meta.xml");
167-
File backupFile = new File("src/test/resources/metadata/10/meta.xml.1");
168-
FileUtils.copyFile(metaFile, backupFile);
169-
170-
// Set base type in metadata/10/meta.xml to "Season"
171-
Process seasonProcess = ServiceManager.getProcessService().getById(10);
207+
Process seasonProcess = ServiceManager.getProcessService().getById(newspaperTestProcessId);
172208
URI seasonUri = processService.getMetadataFileUri(seasonProcess);
173209
Workpiece seasonMets = metsService.loadWorkpiece(seasonUri);
174210
seasonMets.getLogicalStructure().setType("Season");
@@ -197,32 +233,26 @@ public void shouldGenerateSeasonProcesses() throws Exception {
197233
*/
198234
String twoYears = workpiece.getLogicalStructure().getOrderlabel();
199235
List<String> years = Arrays.asList(twoYears.split("/", 2));
200-
Assert.assertTrue("Bad season-year in " + seasonProcess + ": " + twoYears,
201-
Integer.parseInt(years.get(0)) + 1 == Integer.parseInt(years.get(1)));
236+
Assert.assertEquals("Bad season-year in " + seasonProcess + ": " + twoYears,
237+
Integer.parseInt(years.get(0)) + 1, Integer.parseInt(years.get(1)));
202238

203239
// more tests
204240
monthChecksOfShouldGenerateSeasonProcesses(seasonProcess, workpiece, twoYears, years);
205241
dayChecksOfShouldGenerateSeasonProcesses(seasonProcess, workpiece);
206242
}
207243
}
208-
209-
// restore backed-up meta data file
210-
FileUtils.deleteQuietly(metaFile);
211-
FileUtils.moveFile(backupFile, metaFile);
212-
cleanUp();
213244
}
214245

215246
@Test
216247
public void shouldNotGenerateDuplicateProcessTitle() throws DAOException, DataException {
217-
Process completeEdition = ServiceManager.getProcessService().getById(10);
248+
Process completeEdition = ServiceManager.getProcessService().getById(newspaperTestProcessId);
218249
Course course = NewspaperCourse.getDuplicatedCourse();
219250
course.splitInto(Granularity.DAYS);
220251
GeneratesNewspaperProcessesThread generatesNewspaperProcessesThread = new GeneratesNewspaperProcessesThread(completeEdition, course);
221-
generatesNewspaperProcessesThread.run();
252+
generatesNewspaperProcessesThread.start();
222253

223254
ProcessDTO byId = ServiceManager.getProcessService().findById(11);
224255
Assert.assertNull("Process should not have been created", byId.getTitle());
225-
226256
}
227257

228258
private void dayChecksOfShouldGenerateSeasonProcesses(Process seasonProcess, Workpiece seasonYearWorkpiece) {
@@ -291,19 +321,4 @@ private void monthChecksOfShouldGenerateSeasonProcesses(Process seasonProcess, W
291321
previousMonthValue = monthValue;
292322
}
293323
}
294-
295-
/**
296-
* To clean up after the end of the test. All metadata directories >10 will
297-
* be deleted.
298-
*/
299-
private static void cleanUp() throws IOException {
300-
Path dirProcesses = Paths.get(ConfigCore.getParameter(ParameterCore.DIR_PROCESSES));
301-
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dirProcesses);
302-
for (Path path : directoryStream) {
303-
String fileName = path.getFileName().toString();
304-
if (fileName.matches("\\d+") && Integer.valueOf(fileName) > 10) {
305-
TreeDeleter.deltree(path);
306-
}
307-
}
308-
}
309324
}

Kitodo/src/test/java/org/kitodo/production/services/command/KitodoScriptServiceIT.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ public void shouldCopyMultipleDataToChildren() throws Exception {
366366
backupHierarchieFiles();
367367
String metadataKey = "DigitalCollection";
368368
HashMap<String, String> metadataSearchMap = new HashMap<>();
369-
metadataSearchMap.put(metadataKey,"Kollektion1");
370-
metadataSearchMap.put(metadataKey,"Kollektion2");
369+
metadataSearchMap.put(metadataKey, "Kollektion2");
371370

372371
final List<ProcessDTO> processByMetadata = ServiceManager.getProcessService().findByMetadata(metadataSearchMap);
373372
Assert.assertEquals("should not contain metadata beforehand", 1, processByMetadata.size() );

Kitodo/src/test/java/org/kitodo/production/services/file/FileServiceIT.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class FileServiceIT {
5757
private static final String RENAME_MEDIA_PROCESS_1 = "renameMediaProcess1";
5858
private static final String RENAME_MEDIA_PROCESS_2 = "renameMediaProcess2";
5959
private static final String RENAME_MEDIA_REVERT_PROCESS = "revertMediaRenamingProcess";
60+
private static final int PROJECT_ID = 2;
6061
private static List<Integer> dummyProcessIds = new LinkedList<>();
6162
private static int mediaRenamingFirstProcessId = -1;
6263
private static int mediaRenamingSecondProcessId = -1;
@@ -139,9 +140,9 @@ public void testMetadataImageComparator() {
139140

140141
@Test
141142
public void testRenamingOfMultipleProcesses() throws DAOException, DataException, IOException, InterruptedException {
142-
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
143+
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
143144
mediaRenamingFirstProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_PROCESS_1);
144-
ProcessTestUtils.insertDummyProcesses();
145+
ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
145146
mediaRenamingSecondProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_PROCESS_2);
146147
ProcessTestUtils.copyTestFiles(mediaRenamingFirstProcessId, TEST_RENAME_MEDIA_FILE);
147148
ProcessTestUtils.copyTestFiles(mediaRenamingSecondProcessId, TEST_RENAME_MEDIA_FILE);
@@ -163,7 +164,7 @@ public void testRenamingOfMultipleProcesses() throws DAOException, DataException
163164
@Test
164165
public void testRevertingOriginalFilenamesAfterRenamingError() throws DAOException, DataException, IOException,
165166
InterruptedException {
166-
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
167+
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
167168
revertMediaRenamingProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_REVERT_PROCESS);
168169
ProcessTestUtils.copyTestFiles(revertMediaRenamingProcessId, TEST_RENAME_MEDIA_FILE);
169170
Path processScansDir = Paths.get(ConfigCore.getKitodoDataDirectory(), revertMediaRenamingProcessId

0 commit comments

Comments
 (0)