Skip to content

Commit

Permalink
Refactor MetadataEditorIT to load test files dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Dec 14, 2023
1 parent c1ec3aa commit 1121736
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
12 changes: 11 additions & 1 deletion Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
import java.time.Duration;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
Expand All @@ -39,12 +40,16 @@
import org.kitodo.data.database.beans.User;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.data.ProcessService;
import org.kitodo.utils.ProcessTestUtils;


public class MetadataEditorIT {
private static final ProcessService processService = ServiceManager.getProcessService();

private static final String firstProcess = "First process";
private static Map<String, Integer> testProcessIds;
private static final String TEST_METADATA_CHILD_PROCESS_TO_ADD = "testMetadataForNonBlockingParallelTasksTest.xml";
private static final String TEST_METADATA_FILE = "testMetadataFileServiceTest.xml";
private static final String TEST_PROCESS_TITLE = "Test process";

/**
* Is running before the class runs.
Expand All @@ -53,7 +58,9 @@ public class MetadataEditorIT {
public static void prepareDatabase() throws Exception {
MockDatabase.startNode();
MockDatabase.insertProcessesFull();
MockDatabase.insertProcessesForHierarchyTests();
testProcessIds = MockDatabase.insertProcessesForHierarchyTests();
ProcessTestUtils.copyHierarchyTestFiles(testProcessIds);
ProcessTestUtils.copyTestFiles(testProcessIds.get(MockDatabase.HIERARCHY_CHILD_TO_ADD), TEST_METADATA_CHILD_PROCESS_TO_ADD);
MockDatabase.setUpAwaitility();
User userOne = ServiceManager.getUserService().getById(1);
SecurityTestUtils.addUserDataToSecurityContext(userOne, 1);
Expand All @@ -68,6 +75,8 @@ public static void prepareDatabase() throws Exception {
*/
@AfterClass
public static void cleanDatabase() throws Exception {
ProcessTestUtils.removeTestProcess(testProcessIds.get(MockDatabase.HIERARCHY_PARENT));
ProcessTestUtils.removeTestProcess(testProcessIds.get(MockDatabase.HIERARCHY_CHILD_TO_ADD));
MockDatabase.stopNode();
MockDatabase.cleanDatabase();
}
Expand All @@ -77,22 +86,25 @@ public static void cleanDatabase() throws Exception {

@Test
public void shouldAddLink() throws Exception {
File metaXmlFile = new File("src/test/resources/metadata/4/meta.xml");
int parentId = testProcessIds.get(MockDatabase.HIERARCHY_PARENT);
int childId = testProcessIds.get(MockDatabase.HIERARCHY_CHILD_TO_ADD);
File metaXmlFile = new File("src/test/resources/metadata/" + parentId + "/meta.xml");
List<String> metaXmlContentBefore = FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8);

MetadataEditor.addLink(ServiceManager.getProcessService().getById(4), "0", 7);
MetadataEditor.addLink(ServiceManager.getProcessService().getById(parentId), "0", childId);

assertTrue("The link was not added correctly!",
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(36), 7));
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(36), childId));

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

@Test
public void shouldAddMultipleStructuresWithoutMetadata() throws Exception {

File metaXmlFile = new File("src/test/resources/metadata/2/meta.xml");
int testProcessId = MockDatabase.insertTestProcess(TEST_PROCESS_TITLE, 1, 1, 1);
ProcessTestUtils.copyTestMetadataFile(testProcessId, TEST_METADATA_FILE);
File metaXmlFile = new File("src/test/resources/metadata/" + testProcessId + "/meta.xml");
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlFile.toURI());

int oldNrLogicalDivisions = workpiece.getAllLogicalDivisions().size();
Expand All @@ -104,6 +116,7 @@ public void shouldAddMultipleStructuresWithoutMetadata() throws Exception {

List<LogicalDivision> logicalDivisions = workpiece.getAllLogicalDivisions();
assertTrue("Metadata should be empty", logicalDivisions.get(newNrDivisions - 1).getMetadata().isEmpty());
ProcessTestUtils.removeTestProcess(testProcessId);
}

@Test
Expand All @@ -115,7 +128,9 @@ public void shouldAddMultipleStructuresWithMetadataGroup() throws Exception {
Locale.LanguageRange.parse("en"));
String metadataKey = "TitleDocMain";

File metaXmlFile = new File("src/test/resources/metadata/2/meta.xml");
int testProcessId = MockDatabase.insertTestProcess(TEST_PROCESS_TITLE, 1, 1, 1);
ProcessTestUtils.copyTestMetadataFile(testProcessId, TEST_METADATA_FILE);
File metaXmlFile = new File("src/test/resources/metadata/" + testProcessId + "/meta.xml");
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlFile.toURI());

int oldNrLogicalDivisions = workpiece.getAllLogicalDivisions().size();
Expand All @@ -128,15 +143,16 @@ public void shouldAddMultipleStructuresWithMetadataGroup() throws Exception {
MetadataEditor.addMultipleStructuresWithMetadata(addedDivisions, "Monograph", workpiece,
workpiece.getLogicalStructure(), InsertionPosition.FIRST_CHILD_OF_CURRENT_ELEMENT, mvi, "value");
LogicalDivision newSectionOne = workpiece.getAllLogicalDivisions().get(newNrDivisions - 2);
List<Metadata> metadataListOne = new ArrayList<Metadata>(newSectionOne.getMetadata());
List<Metadata> metadataListOne = new ArrayList<>(newSectionOne.getMetadata());
LogicalDivision newSectionTwo = workpiece.getAllLogicalDivisions().get(newNrDivisions - 1);
List<Metadata> metadataListTwo = new ArrayList<Metadata>(newSectionTwo.getMetadata());
List<Metadata> metadataListTwo = new ArrayList<>(newSectionTwo.getMetadata());
Metadata metadatumOne = metadataListOne.get(0);
Metadata metadatumTwo = metadataListTwo.get(0);

assertTrue("Metadata should be of type MetadataEntry", metadatumOne instanceof MetadataEntry);
assertTrue("Metadata value was incorrectly added", ((MetadataEntry) metadatumOne).getValue().equals("value 1")
&& ((MetadataEntry) metadatumTwo).getValue().equals("value 2"));
ProcessTestUtils.removeTestProcess(testProcessId);
}

@Test
Expand All @@ -147,7 +163,9 @@ public void shouldAddMultipleStructuresWithMetadataEntry() throws Exception {
Locale.LanguageRange.parse("en"));
String metadataKey = "Person";

File metaXmlFile = new File("src/test/resources/metadata/2/meta.xml");
int testProcessId = MockDatabase.insertTestProcess(TEST_PROCESS_TITLE, 1, 1, 1);
ProcessTestUtils.copyTestMetadataFile(testProcessId, TEST_METADATA_FILE);
File metaXmlFile = new File("src/test/resources/metadata/" + testProcessId + "/meta.xml");
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlFile.toURI());

int oldNrLogicalDivisions = workpiece.getAllLogicalDivisions().size();
Expand All @@ -170,6 +188,7 @@ public void shouldAddMultipleStructuresWithMetadataEntry() throws Exception {
metadatumOne instanceof MetadataGroup && metadatumTwo instanceof MetadataGroup);
assertTrue("Metadata value was incorrectly added",
metadatumOne.getKey().equals("Person") && metadatumTwo.getKey().equals("Person"));
ProcessTestUtils.removeTestProcess(testProcessId);
}

private boolean isInternalMetsLink(String lineOfMets, int recordNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,13 @@ public class KitodoScriptServiceIT {

private static final String metadataWithDuplicatesTestFile = "testMetaWithDuplicateMetadata.xml";
private static final String directoryForDerivateGeneration = "testFilesForDerivativeGeneration";
private static final String testFileForHierarchyParent = "multivalued_metadata.xml";
private static final String testFileChildProcessToKeep = "testMetadataForChildProcessToKeep.xml";
private static final String testFileChildProcessToRemove = "testMetadataForKitodoScript.xml";
private static int kitodoScriptTestProcessId = -1;
private static final String testProcessTitle = "Second process";
private static final int projectId = 1;
private static final int templateId = 1;
private static final int rulesetId = 1;
private static final int userId = 1;
private static final int clientId = 1;
private static final Map<String, String> hierarchyProcessTitlesAndFiles;
static {
hierarchyProcessTitlesAndFiles = Map.of(
MockDatabase.HIERARCHY_PARENT, testFileForHierarchyParent,
MockDatabase.HIERARCHY_CHILD_TO_KEEP, testFileChildProcessToKeep,
MockDatabase.HIERARCHY_CHILD_TO_REMOVE, testFileChildProcessToRemove);
}

private static final File scriptCreateDirMeta = new File(
ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
Expand Down Expand Up @@ -358,20 +348,10 @@ public void shouldNotDeleteDataWithWrongType() throws Exception {
Assert.assertEquals("metadata should not have been deleted", 1, metadataOfChapter.size() );
}

private void copyHierarchyTestFiles(Map<String, Integer> processTitlesAndIds) throws IOException, DAOException, DataException {
for (Map.Entry<String, String> hierarchyProcess : hierarchyProcessTitlesAndFiles.entrySet()) {
int processId = processTitlesAndIds.get(hierarchyProcess.getKey());
ProcessTestUtils.copyTestFiles(processId, hierarchyProcess.getValue());
// re-save to index metadata file
Process testProcess = ServiceManager.getProcessService().getById(processId);
ServiceManager.getProcessService().save(testProcess);
}
}

@Test
public void shouldCopyMultipleDataToChildren() throws Exception {
Map<String, Integer> testProcessIds = MockDatabase.insertProcessesForHierarchyTests();
copyHierarchyTestFiles(testProcessIds);
ProcessTestUtils.copyHierarchyTestFiles(testProcessIds);
String metadataKey = "DigitalCollection";
HashMap<String, String> metadataSearchMap = new HashMap<>();
metadataSearchMap.put(metadataKey, "Kollektion2");
Expand All @@ -388,9 +368,7 @@ public void shouldCopyMultipleDataToChildren() throws Exception {
final List<ProcessDTO> processByMetadataAfter = ServiceManager.getProcessService()
.findByMetadata(metadataSearchMap);
Assert.assertEquals("does not contain metadata", 3, processByMetadataAfter.size() );
for (int processId : testProcessIds.values().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList())) {
ProcessTestUtils.removeTestProcess(processId);
}
ProcessTestUtils.removeTestProcess(testProcessIds.get(MockDatabase.HIERARCHY_PARENT));
}

@Test
Expand Down
25 changes: 22 additions & 3 deletions Kitodo/src/test/java/org/kitodo/utils/ProcessTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -47,6 +48,16 @@ public class ProcessTestUtils {
public static final String META_XML = "/meta.xml";
private static final int TEST_PROJECT_ID = 1;
private static final int TEST_TEMPLATE_ID = 1;
private static final String testFileForHierarchyParent = "multivalued_metadata.xml";
private static final String testFileChildProcessToKeep = "testMetadataForChildProcessToKeep.xml";
private static final String testFileChildProcessToRemove = "testMetadataForKitodoScript.xml";
private static final Map<String, String> hierarchyProcessTitlesAndFiles;
static {
hierarchyProcessTitlesAndFiles = Map.of(
MockDatabase.HIERARCHY_PARENT, testFileForHierarchyParent,
MockDatabase.HIERARCHY_CHILD_TO_KEEP, testFileChildProcessToKeep,
MockDatabase.HIERARCHY_CHILD_TO_REMOVE, testFileChildProcessToRemove);
}
private static final Logger logger = LogManager.getLogger(ProcessTestUtils.class);

/**
Expand Down Expand Up @@ -204,10 +215,8 @@ public static void cleanupDummyProcesses(List<Integer> dummyProcessIds) throws D
* Delete test process with given ID including potential parent processes.
* @param testProcessId ID of process to delete
* @throws DAOException when retrieving process from database for deletion fails
* @throws DataException when deleting process fails
* @throws IOException when deleting process resources from the file system fails
*/
public static void removeTestProcess(int testProcessId) throws DAOException, DataException, IOException {
public static void removeTestProcess(int testProcessId) throws DAOException {
if (testProcessId > 0) {
deleteProcessHierarchy(ServiceManager.getProcessService().getById(testProcessId));
}
Expand All @@ -224,4 +233,14 @@ private static void deleteProcessHierarchy(Process process){
e.printStackTrace();
}
}

public static void copyHierarchyTestFiles(Map<String, Integer> processTitlesAndIds) throws IOException, DAOException, DataException {
for (Map.Entry<String, String> hierarchyProcess : hierarchyProcessTitlesAndFiles.entrySet()) {
int processId = processTitlesAndIds.get(hierarchyProcess.getKey());
ProcessTestUtils.copyTestFiles(processId, hierarchyProcess.getValue());
// re-save to index metadata file
Process testProcess = ServiceManager.getProcessService().getById(processId);
ServiceManager.getProcessService().save(testProcess);
}
}
}

0 comments on commit 1121736

Please sign in to comment.