Skip to content

Commit

Permalink
Add unit and integration tests for metadata re-import
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Sep 24, 2024
1 parent c5ac8cc commit ab8b2d3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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));
}
}

0 comments on commit ab8b2d3

Please sign in to comment.