From dddd08f6ebd3ce4cda3586c35ace36f2d38b2cbb Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Tue, 24 Sep 2024 14:26:59 +0200 Subject: [PATCH] Add integration test for re-import of process metadata --- .../forms/dataeditor/PaginationPanel.java | 1 - .../dataeditor/UpdateMetadataDialog.java | 16 +++++- .../dataeditor/DataEditorService.java | 10 +--- .../catalogimport/CatalogImportIT.java | 57 +++++++++++++++++++ .../services/data/DataEditorServiceIT.java | 14 ++--- .../kitodo/test/utils/ProcessTestUtils.java | 15 +++++ .../org/kitodo/test/utils/TestConstants.java | 1 + .../importRecords/reimportMetadata.xml | 37 ++++++++++++ .../metadataFiles/testmetaReimport.xml | 33 +++++++++++ 9 files changed, 168 insertions(+), 16 deletions(-) create mode 100644 Kitodo/src/test/resources/importRecords/reimportMetadata.xml create mode 100644 Kitodo/src/test/resources/metadata/metadataFiles/testmetaReimport.xml diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/PaginationPanel.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/PaginationPanel.java index dac9751538d..623629c8884 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/PaginationPanel.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/PaginationPanel.java @@ -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; diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/UpdateMetadataDialog.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/UpdateMetadataDialog.java index f51864acbf4..8165782fe48 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/UpdateMetadataDialog.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/UpdateMetadataDialog.java @@ -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 { @@ -43,8 +48,9 @@ public List getMetadataComparisons() { */ public void updateCatalogMetadata() { try { + HashSet 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 { @@ -55,4 +61,12 @@ public void updateCatalogMetadata() { Helper.setErrorMessage(e.getMessage()); } } + + private HashSet getMetadata(TreeNode treeNode) throws InvalidMetadataValueException { + HashSet processDetails = new HashSet<>(); + for (TreeNode child : treeNode.getChildren()) { + processDetails.addAll(((ProcessDetail) child.getData()).getMetadata(false)); + } + return processDetails; + } } diff --git a/Kitodo/src/main/java/org/kitodo/production/services/dataeditor/DataEditorService.java b/Kitodo/src/main/java/org/kitodo/production/services/dataeditor/DataEditorService.java index 703570091a2..e2ccc44e229 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/dataeditor/DataEditorService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/dataeditor/DataEditorService.java @@ -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 reimportCatalogMetadata(Process process, Workpiece workpiece, TreeNode treeNode) + public static List reimportCatalogMetadata(Process process, Workpiece workpiece, + HashSet oldMetadataSet) throws IOException, UnsupportedFormatException, XPathExpressionException, NoRecordFoundException, ProcessGenerationException, ParserConfigurationException, URISyntaxException, InvalidMetadataValueException, TransformerException, NoSuchMetadataFieldException, SAXException { @@ -448,12 +450,6 @@ public static List reimportCatalogMetadata(Process process, ProcessHelper.setMetadataDomain(metadata, ruleset); } ProcessHelper.generateAtstslFields(updatedProcess, Collections.emptyList(), EDIT, false); - - HashSet oldMetadataSet = new HashSet<>(); - for (TreeNode child : treeNode.getChildren()) { - Collection metadata = ((ProcessDetail) child.getData()).getMetadata(false); - oldMetadataSet.addAll(metadata); - } return initializeMetadataComparisons(process, oldMetadataSet, updatedProcess.getWorkpiece().getLogicalStructure().getMetadata()); } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/CatalogImportIT.java b/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/CatalogImportIT.java index 4b003bb79a3..ba1963c821c 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/CatalogImportIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/catalogimport/CatalogImportIT.java @@ -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 { @@ -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 { @@ -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 processes = ServiceManager.getImportService().importProcessHierarchy(CHILD_RECORD_ID, @@ -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 existingMetadata = workpiece.getLogicalStructure().getMetadata(); + List 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); } } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/data/DataEditorServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/data/DataEditorServiceIT.java index 64dde208471..6ec72dae204 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/data/DataEditorServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/data/DataEditorServiceIT.java @@ -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; @@ -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; @@ -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 @@ -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"); } diff --git a/Kitodo/src/test/java/org/kitodo/test/utils/ProcessTestUtils.java b/Kitodo/src/test/java/org/kitodo/test/utils/ProcessTestUtils.java index 85792903497..13866ed928f 100644 --- a/Kitodo/src/test/java/org/kitodo/test/utils/ProcessTestUtils.java +++ b/Kitodo/src/test/java/org/kitodo/test/utils/ProcessTestUtils.java @@ -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; @@ -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"; @@ -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); + } } diff --git a/Kitodo/src/test/java/org/kitodo/test/utils/TestConstants.java b/Kitodo/src/test/java/org/kitodo/test/utils/TestConstants.java index dd77acfaa2a..ce275e93c2a 100644 --- a/Kitodo/src/test/java/org/kitodo/test/utils/TestConstants.java +++ b/Kitodo/src/test/java/org/kitodo/test/utils/TestConstants.java @@ -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"; diff --git a/Kitodo/src/test/resources/importRecords/reimportMetadata.xml b/Kitodo/src/test/resources/importRecords/reimportMetadata.xml new file mode 100644 index 00000000000..96ba31f9d03 --- /dev/null +++ b/Kitodo/src/test/resources/importRecords/reimportMetadata.xml @@ -0,0 +1,37 @@ + + + + 1.2 + 1 + + + mods + xml + + + http://www.test.id + + 789 + 2020-11-09 + 2020-11-09 + + + Test Publisher + + + + + + diff --git a/Kitodo/src/test/resources/metadata/metadataFiles/testmetaReimport.xml b/Kitodo/src/test/resources/metadata/metadataFiles/testmetaReimport.xml new file mode 100644 index 00000000000..a6859df585e --- /dev/null +++ b/Kitodo/src/test/resources/metadata/metadataFiles/testmetaReimport.xml @@ -0,0 +1,33 @@ + + + + + + + + Test Publisher + Monograph + 2019 + 789 + + + + + + + + + + + + +