Skip to content

Commit

Permalink
Add integration test for re-import of process metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Sep 24, 2024
1 parent ab8b2d3 commit dddd08f
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.kitodo.api.dataformat.View;
import org.kitodo.config.ConfigCore;
import org.kitodo.config.enums.ParameterCore;
import org.kitodo.data.database.beans.User;
import org.kitodo.exceptions.InvalidImagesException;
import org.kitodo.exceptions.InvalidMetadataValueException;
import org.kitodo.exceptions.MediaNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

package org.kitodo.production.forms.dataeditor;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import org.kitodo.api.Metadata;
import org.kitodo.exceptions.InvalidMetadataValueException;
import org.kitodo.production.forms.createprocess.ProcessDetail;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.MetadataComparison;
import org.kitodo.production.services.dataeditor.DataEditorService;
import org.primefaces.PrimeFaces;
import org.primefaces.model.TreeNode;

public class UpdateMetadataDialog {

Expand All @@ -43,8 +48,9 @@ public List<MetadataComparison> getMetadataComparisons() {
*/
public void updateCatalogMetadata() {
try {
HashSet<Metadata> existingMetadata = getMetadata(dataEditor.getMetadataPanel().getLogicalMetadataRows());
metadataComparisons = DataEditorService.reimportCatalogMetadata(dataEditor.getProcess(),
dataEditor.getWorkpiece(), dataEditor.getMetadataPanel().getLogicalMetadataRows());
dataEditor.getWorkpiece(), existingMetadata);
if (metadataComparisons.isEmpty()) {
PrimeFaces.current().executeScript("PF('metadataUnchangedDialog').show();");
} else {
Expand All @@ -55,4 +61,12 @@ public void updateCatalogMetadata() {
Helper.setErrorMessage(e.getMessage());
}
}

private HashSet<Metadata> getMetadata(TreeNode treeNode) throws InvalidMetadataValueException {
HashSet<Metadata> processDetails = new HashSet<>();
for (TreeNode child : treeNode.getChildren()) {
processDetails.addAll(((ProcessDetail) child.getData()).getMetadata(false));
}
return processDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,11 @@ public static boolean canUpdateCatalogMetadata(Process process, Workpiece workpi
* to the user.
* @param process Process for which metadata update is performed
* @param workpiece Workpiece of given process
* @param oldMetadataSet Set containing old metadata
* @return list of metadata comparisons
*/
public static List<MetadataComparison> reimportCatalogMetadata(Process process, Workpiece workpiece, TreeNode treeNode)
public static List<MetadataComparison> reimportCatalogMetadata(Process process, Workpiece workpiece,
HashSet<Metadata> oldMetadataSet)
throws IOException, UnsupportedFormatException, XPathExpressionException, NoRecordFoundException,
ProcessGenerationException, ParserConfigurationException, URISyntaxException, InvalidMetadataValueException,
TransformerException, NoSuchMetadataFieldException, SAXException {
Expand All @@ -448,12 +450,6 @@ public static List<MetadataComparison> reimportCatalogMetadata(Process process,
ProcessHelper.setMetadataDomain(metadata, ruleset);
}
ProcessHelper.generateAtstslFields(updatedProcess, Collections.emptyList(), EDIT, false);

HashSet<Metadata> oldMetadataSet = new HashSet<>();
for (TreeNode child : treeNode.getChildren()) {
Collection<Metadata> metadata = ((ProcessDetail) child.getData()).getMetadata(false);
oldMetadataSet.addAll(metadata);
}
return initializeMetadataComparisons(process, oldMetadataSet,
updatedProcess.getWorkpiece().getLogicalStructure().getMetadata());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,35 @@
package org.kitodo.production.services.catalogimport;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.xebialabs.restito.server.StubServer;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kitodo.MockDatabase;
import org.kitodo.SecurityTestUtils;
import org.kitodo.api.Metadata;
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.ImportConfiguration;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.production.helper.MetadataComparison;
import org.kitodo.production.helper.TempProcess;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.dataeditor.DataEditorService;
import org.kitodo.test.utils.ProcessTestUtils;
import org.kitodo.test.utils.TestConstants;

public class CatalogImportIT {
Expand All @@ -34,9 +49,14 @@ public class CatalogImportIT {
private static final int PORT = 8888;
private static final String CHILD_RECORD_ID = "1";
private static final String PARENT_RECORD_ID = "2";
private static final String REIMPORT_RECORD_ID = "789";
private static final int PROJECT_ID = 1;
private static final int TEMPLATE_ID = 1;
private static final int IMPORT_DEPTH = 2;
private static final String PUBLICATION_START_KEY = "PublicationStart";
private static final String PUBLICATION_VALUE_OLD = "2019";
private static final String PUBLICATION_VALUE_NEW = "2020";
private int testProcessId = 0;

@BeforeAll
public static void setup() throws Exception {
Expand All @@ -56,6 +76,14 @@ public static void shutdown() throws Exception {
server.stop();
}

@AfterEach
public void removeTestProcess() throws DAOException {
if (testProcessId > 0) {
ProcessTestUtils.removeTestProcess(testProcessId);
testProcessId = 0;
}
}

@Test
public void shouldImportProcessHierarchy() throws Exception {
LinkedList<TempProcess> processes = ServiceManager.getImportService().importProcessHierarchy(CHILD_RECORD_ID,
Expand All @@ -64,10 +92,39 @@ public void shouldImportProcessHierarchy() throws Exception {
assertEquals(IMPORT_DEPTH, processes.size());
}

@Test
public void shouldSuccessfullyUpdateMetadata() throws Exception {
testProcessId = MockDatabase.insertTestProcess("Reimport-Test-Process", PROJECT_ID, TEMPLATE_ID, 1);
ProcessTestUtils.copyTestMetadataFile(testProcessId, "testmetaReimport.xml");
Process testProcess = ServiceManager.getProcessService().getById(testProcessId);
URI processUri = ServiceManager.getProcessService().getMetadataFileUri(testProcess);
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(processUri);
ImportConfiguration configuration = MockDatabase.getKalliopeImportConfiguration();
testProcess.setImportConfiguration(configuration);
HashSet<Metadata> existingMetadata = workpiece.getLogicalStructure().getMetadata();
List<MetadataComparison> metadataComparisons = DataEditorService.reimportCatalogMetadata(testProcess, workpiece,
existingMetadata);
assertFalse(metadataComparisons.isEmpty(), "List of metadata comparisons should not be empty");
MetadataComparison firstComparison = metadataComparisons.get(0);
assertEquals(PUBLICATION_START_KEY, firstComparison.getMetadataKey(),
String.format("Changed metadata should be '%s'", PUBLICATION_START_KEY));
MetadataEntry publicationMetadataOld = new MetadataEntry();
publicationMetadataOld.setKey(PUBLICATION_START_KEY);
publicationMetadataOld.setValue(PUBLICATION_VALUE_OLD);
MetadataEntry publicationMetadataNew = new MetadataEntry();
publicationMetadataNew.setKey(PUBLICATION_START_KEY);
publicationMetadataNew.setValue(PUBLICATION_VALUE_NEW);
assertTrue(firstComparison.getOldValues().contains(publicationMetadataOld),
String.format("Old values should contain publication year %s", publicationMetadataOld.getValue()));
assertTrue(firstComparison.getNewValues().contains(publicationMetadataNew),
String.format("New values should contain publication year %s", publicationMetadataNew.getValue()));
}

private static void setupServer() throws IOException {
server = new StubServer(PORT).run();
MockDatabase.addRestEndPointForSru(server, "ead.id=" + CHILD_RECORD_ID, TestConstants.CHILD_RECORD_PATH, "mods", 1);
MockDatabase.addRestEndPointForSru(server, "ead.id=" + PARENT_RECORD_ID, TestConstants.PARENT_RECORD_PATH, "mods", 1);
MockDatabase.addRestEndPointForSru(server, "ead.title=test", TestConstants.HITLIST_RECORD_PATH, "mods",10);
MockDatabase.addRestEndPointForSru(server, "ead.id=" + REIMPORT_RECORD_ID, TestConstants.REIMPORT_RECORD_PATH, "mods", 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kitodo.MockDatabase;
import org.kitodo.api.MetadataEntry;
import org.kitodo.SecurityTestUtils;
import org.kitodo.api.dataeditor.rulesetmanagement.RulesetManagementInterface;
import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface;
import org.kitodo.api.dataformat.LogicalDivision;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.Ruleset;
import org.kitodo.data.database.beans.User;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.MetadataException;
Expand All @@ -59,8 +60,6 @@ public class DataEditorServiceIT {
private static final String ENGLISH = "en";
private static final String EDIT = "edit";
private static final String CONTRIBUTOR_PERSON = "ContributorPerson";
private static final String RECORD_ID_METADATA_KEY = "CatalogIDDigital";
private static final String RECORD_ID = "1234567890";
private static final String EXPECTED_EXCEPTION_MESSAGE = "Unable to update metadata of process %d; " +
"(either import configuration or record identifier are missing)";
private int testProcessId = 0;
Expand All @@ -69,6 +68,10 @@ public class DataEditorServiceIT {
public static void prepareDatabase() throws Exception {
MockDatabase.startNode();
MockDatabase.insertProcessesFull();
MockDatabase.insertMappingFiles();
MockDatabase.insertImportConfigurations();
User userOne = ServiceManager.getUserService().getById(1);
SecurityTestUtils.addUserDataToSecurityContext(userOne, 1);
}

@AfterAll
Expand Down Expand Up @@ -172,10 +175,7 @@ public void shouldGetRecordIdentifierValueOfProcess() throws DAOException, IOExc
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(processUri);
String recordIdentifier = DataEditorService.getRecordIdentifierValueOfProcess(testProcess, workpiece);
assertNull(recordIdentifier, "RecordIdentifier should be null");
MetadataEntry recordIdMetadata = new MetadataEntry();
recordIdMetadata.setKey(RECORD_ID_METADATA_KEY);
recordIdMetadata.setValue(RECORD_ID);
workpiece.getLogicalStructure().getMetadata().add(recordIdMetadata);
ProcessTestUtils.addRecordIdentifierToLogicalRoot(workpiece,"1234567890");
recordIdentifier = DataEditorService.getRecordIdentifierValueOfProcess(testProcess, workpiece);
assertNotNull(recordIdentifier, "RecordIdentifier should not be null");
}
Expand Down
15 changes: 15 additions & 0 deletions Kitodo/src/test/java/org/kitodo/test/utils/ProcessTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.kitodo.MockDatabase;
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataeditor.rulesetmanagement.SimpleMetadataViewInterface;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.config.ConfigCore;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.exceptions.DAOException;
Expand All @@ -48,6 +49,7 @@ public class ProcessTestUtils {
private static final int TEST_PROJECT_ID = 1;
private static final int TEST_TEMPLATE_ID = 1;
private static final String ID_PLACEHOLDER = "IDENTIFIER_PLACEHOLDER";
private static final String RECORD_ID_METADATA_KEY = "CatalogIDDigital";
public static final String testFileForHierarchyParent = "multivalued_metadata.xml";
public static final String testFileForLongNumbers = "testMetadataWithLongNumbers.xml";
public static final String testFileChildProcessToKeep = "testMetadataForChildProcessToKeep.xml";
Expand Down Expand Up @@ -249,4 +251,17 @@ public static void logTestProcessInfo(Process process) {
logger.info(" Process '" + process.getTitle() + "' has ID " + process.getId());
logger.info(" ************* ");
}

/**
* Add record identifier metadata with provided value to given workpiece.
*
* @param workpiece Workpiece to which record identifier metadata is added
* @param idValue value of record identifier metadata to set
*/
public static void addRecordIdentifierToLogicalRoot(Workpiece workpiece, String idValue) {
MetadataEntry recordIdMetadata = new MetadataEntry();
recordIdMetadata.setKey(RECORD_ID_METADATA_KEY);
recordIdMetadata.setValue(idValue);
workpiece.getLogicalStructure().getMetadata().add(recordIdMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class TestConstants {
public static final String NUMBER_OF_CHILD_RECORDS_PATH = "src/test/resources/importRecords/sruNumberOfChildRecords.xml";
public static final String MULTIPLE_CHILD_RECORDS_PATH = "src/test/resources/importRecords/importMultipleChildRecords.xml";
public static final String CHILD_RECORD_PATH = "src/test/resources/importRecords/importChildRecord.xml";
public static final String REIMPORT_RECORD_PATH = "src/test/resources/importRecords/reimportMetadata.xml";
public static final String HITLIST_RECORD_PATH = "src/test/resources/importRecords/importHitlist.xml";
public static final String EAD_ID = "ead.id";
public static final String EAD_PARENT_ID = "context.ead.id";
Expand Down
37 changes: 37 additions & 0 deletions Kitodo/src/test/resources/importRecords/reimportMetadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
-->
<srw:searchRetrieveResponse xmlns:srw="http://www.loc.gov/zing/srw/">
<srw:version>1.2</srw:version>
<srw:numberOfRecords>1</srw:numberOfRecords>
<srw:records>
<srw:record>
<srw:recordSchema>mods</srw:recordSchema>
<srw:recordPacking>xml</srw:recordPacking>
<srw:recordData>
<mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.loc.gov/mods/v3"
version="3.4"
xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">
<identifier type="uri">http://www.test.id</identifier>
<recordInfo>
<recordIdentifier source="1">789</recordIdentifier>
<recordCreationDate encoding="marc">2020-11-09</recordCreationDate>
<recordChangeDate encoding="iso8601">2020-11-09</recordChangeDate>
</recordInfo>
<originInfo eventType="publication">
<publisher>Test Publisher</publisher>
</originInfo>
</mods>
</srw:recordData>
</srw:record>
</srw:records>
</srw:searchRetrieveResponse>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
-->
<mets:mets xmlns:mets="http://www.loc.gov/METS/">
<mets:dmdSec ID="DMDLOG_0000">
<mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="KITODO">
<mets:xmlData>
<kitodo:kitodo xmlns:kitodo="http://meta.kitodo.org/v1/" version="1.0">
<kitodo:metadata name="PublisherName">Test Publisher</kitodo:metadata>
<kitodo:metadata name="document_type">Monograph</kitodo:metadata>
<kitodo:metadata name="PublicationStart">2019</kitodo:metadata>
<kitodo:metadata name="CatalogIDDigital">789</kitodo:metadata>
</kitodo:kitodo>
</mets:xmlData>
</mets:mdWrap>
</mets:dmdSec>
<mets:fileSec/>
<mets:structMap TYPE="LOGICAL">
<mets:div DMDID="DMDLOG_0000" ID="LOG_0000" TYPE="Monograph"/>
</mets:structMap>
<mets:structMap TYPE="PHYSICAL">
<mets:div DMDID="DMDPHYS_0000" ID="PHYS_0000" TYPE="physSequence"/>
</mets:structMap>
<mets:structLink/>
</mets:mets>

0 comments on commit dddd08f

Please sign in to comment.