From ab8b2d3a148fb08fcb3079dd351792dc315ae8e9 Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Tue, 24 Sep 2024 09:47:29 +0200 Subject: [PATCH] Add unit and integration tests for metadata re-import --- .../dataeditor/DataEditorService.java | 3 +- .../services/data/DataEditorServiceIT.java | 48 +++++++++++++++++++ .../dataeditor/DataEditorServiceTest.java | 32 ++++++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) 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 20ad0a80df4..703570091a2 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 @@ -526,7 +526,8 @@ public static String metadataToString(Metadata metadata) { return ((MetadataEntry) metadata).getValue(); } else if (metadata instanceof MetadataGroup) { StringBuilder groupString = new StringBuilder(); - for (Metadata groupMetadata : ((MetadataGroup) metadata).getMetadata()) { + for (Metadata groupMetadata : ((MetadataGroup) metadata).getMetadata().stream() + .sorted(Comparator.comparing(Metadata::getKey)).collect(Collectors.toList())) { if (groupMetadata instanceof MetadataEntry) { groupString.append(((MetadataEntry) groupMetadata).getValue()); } else { 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 8f55ae55a21..64dde208471 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 @@ -15,6 +15,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.kitodo.test.utils.TestConstants.TITLE_DOC_MAIN; @@ -32,6 +34,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.kitodo.MockDatabase; +import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataeditor.rulesetmanagement.RulesetManagementInterface; import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface; import org.kitodo.api.dataformat.LogicalDivision; @@ -40,6 +43,7 @@ import org.kitodo.data.database.beans.Ruleset; import org.kitodo.data.database.exceptions.DAOException; import org.kitodo.data.exceptions.DataException; +import org.kitodo.exceptions.MetadataException; import org.kitodo.production.forms.createprocess.ProcessFieldedMetadata; import org.kitodo.production.services.ServiceManager; import org.kitodo.production.services.dataeditor.DataEditorService; @@ -55,6 +59,10 @@ 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; @BeforeAll @@ -150,6 +158,46 @@ public void shouldGetAddableMetadataForStructureElement() throws IOException, DA assertFalse(addableMetadata.isEmpty(), "List of addable metadata should not be empty"); } + /** + * Test retrieving functional metadata of type 'recordIdentifier' from process. + * @throws DAOException when adding or loading test process fails + * @throws IOException when loading meta xml or ruleset file fails + * @throws DataException when adding test processes fails + */ + @Test + public void shouldGetRecordIdentifierValueOfProcess() throws DAOException, IOException, DataException { + addTestProcess(); + Process testProcess = ServiceManager.getProcessService().getById(testProcessId); + URI processUri = ServiceManager.getProcessService().getMetadataFileUri(testProcess); + 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); + recordIdentifier = DataEditorService.getRecordIdentifierValueOfProcess(testProcess, workpiece); + assertNotNull(recordIdentifier, "RecordIdentifier should not be null"); + } + + /** + * Test throwing 'MetadataException' when re-importing metadata is attempted without all necessary conditions for + * metadata re-import being met. + * @throws DAOException when adding or loading test process fails + * @throws DataException when adding test processes fails + * @throws IOException when loading meta xml or ruleset file fails + */ + @Test + public void shouldFailToUpdateMetadata() throws DAOException, DataException, IOException { + addTestProcess(); + Process testProcess = ServiceManager.getProcessService().getById(testProcessId); + URI processUri = ServiceManager.getProcessService().getMetadataFileUri(testProcess); + Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(processUri); + MetadataException thrown = assertThrows(MetadataException.class, () -> DataEditorService. + reimportCatalogMetadata(testProcess, workpiece, null)); + assertEquals(thrown.getMessage(), String.format(EXPECTED_EXCEPTION_MESSAGE, testProcessId)); + } + private Process addTestProcess() throws DAOException, DataException, IOException { testProcessId = MockDatabase.insertTestProcess(TEST_PROCESS_TITLE, 1, 1, 1); ProcessTestUtils.copyTestMetadataFile(testProcessId, TEST_METADATA_FILE); diff --git a/Kitodo/src/test/java/org/kitodo/production/services/dataeditor/DataEditorServiceTest.java b/Kitodo/src/test/java/org/kitodo/production/services/dataeditor/DataEditorServiceTest.java index 8a242250248..03aaab15c06 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/dataeditor/DataEditorServiceTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/dataeditor/DataEditorServiceTest.java @@ -12,6 +12,7 @@ package org.kitodo.production.services.dataeditor; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.File; @@ -23,6 +24,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.kitodo.api.MetadataEntry; +import org.kitodo.api.MetadataGroup; import org.kitodo.production.services.ServiceManager; public class DataEditorServiceTest { @@ -31,6 +34,8 @@ public class DataEditorServiceTest { private static byte[] testMetaOldFormat; private static final String pathOfOldMetaFormat = "src/test/resources/testmetaOldFormat.xml"; private static final String metadataFilesDir = "./src/test/resources/metadata/metadataFiles/"; + private static final String EXPECTED_ENTRY_STRING = "Test-Titel"; + private static final String EXPECTED_GROUP_STRING = "JohnDoeAuthor"; @BeforeEach public void saveFile() throws IOException { @@ -49,12 +54,35 @@ public void shouldReadMetadata() { } @Test - public void shouldReadOldMetadata() throws IOException { + public void shouldReadOldMetadata() { assertDoesNotThrow(() -> dataEditorService.readData(Paths.get(metadataFilesDir + "testmetaOldFormat.xml").toUri())); } @Test - public void shouldNotReadMetadataOfNotExistingFile() throws IOException { + public void shouldNotReadMetadataOfNotExistingFile() { assertThrows(IOException.class, () -> dataEditorService.readData(Paths.get("notExisting.xml").toUri())); } + + @Test + public void shouldConvertMetadataToString() { + MetadataEntry metadataEntry = new MetadataEntry(); + metadataEntry.setKey("TitleMainDoc"); + metadataEntry.setValue(EXPECTED_ENTRY_STRING); + assertEquals(EXPECTED_ENTRY_STRING, DataEditorService.metadataToString(metadataEntry)); + MetadataGroup metadataGroup = new MetadataGroup(); + metadataGroup.setKey("Person"); + MetadataEntry firstNameEntry = new MetadataEntry(); + firstNameEntry.setKey("FirstName"); + firstNameEntry.setValue("John"); + MetadataEntry lastNameEntry = new MetadataEntry(); + lastNameEntry.setKey("LastName"); + lastNameEntry.setValue("Doe"); + MetadataEntry roleEntry = new MetadataEntry(); + roleEntry.setKey("Role"); + roleEntry.setValue("Author"); + metadataGroup.getMetadata().add(firstNameEntry); + metadataGroup.getMetadata().add(lastNameEntry); + metadataGroup.getMetadata().add(roleEntry); + assertEquals(EXPECTED_GROUP_STRING, DataEditorService.metadataToString(metadataGroup)); + } }