Skip to content

Commit

Permalink
Merge pull request kitodo#5919 from effective-webwork/import-service-…
Browse files Browse the repository at this point in the history
…tests

Add integration and unit tests for ImportService
  • Loading branch information
solth authored Feb 19, 2024
2 parents 9c7f777 + 7fe8e6e commit c723b1c
Show file tree
Hide file tree
Showing 14 changed files with 918 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void getRecordHierarchy() {
importChildren(projectId, templateId, importConfiguration, processes);
}

if (createProcessForm.getProcesses().size() > 0 && additionalImport) {
if (!createProcessForm.getProcesses().isEmpty() && additionalImport) {
extendsMetadataTableOfMetadataTab(processes);
} else {
createProcessForm.setProcesses(processes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ private void createProcessHierarchy()
this.saveChildProcessLinks();

// if a process is selected in 'TitleRecordLinkTab' link it as parent with the first process in the list
if (this.processes.size() > 0 && Objects.nonNull(titleRecordLinkTab.getTitleRecordProcess())) {
if (!this.processes.isEmpty() && Objects.nonNull(titleRecordLinkTab.getTitleRecordProcess())) {
MetadataEditor.addLink(titleRecordLinkTab.getTitleRecordProcess(),
titleRecordLinkTab.getSelectedInsertionPosition(), this.processes.get(0).getProcess().getId());
ProcessService.setParentRelations(titleRecordLinkTab.getTitleRecordProcess(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class ImportService {
private static final String VOLUME = "Volume";
private static final String MULTI_VOLUME_WORK = "MultiVolumeWork";

private static Collection<RecordIdentifierMissingDetail> recordIdentifierMissingDetails = new ArrayList<>();
private static final Collection<RecordIdentifierMissingDetail> recordIdentifierMissingDetails = new ArrayList<>();
private String tiffDefinition = "";
private boolean usingTemplates;

Expand Down Expand Up @@ -208,7 +208,7 @@ private ExternalDataImportInterface initializeImportModule() {
public List<String> getAvailableSearchFields(ImportConfiguration importConfiguration) {
try {
if (SearchInterfaceType.FTP.name().equals(importConfiguration.getInterfaceType())) {
// FTP server do not support query parameters but only use the filename for OPAC search!
// FTP servers do not support query parameters but only use the filename for OPAC search!
return Collections.singletonList(Helper.getTranslation("filename"));
} else if (SearchInterfaceType.OAI.name().equals(importConfiguration.getInterfaceType())) {
// OAI PMH interfaces do not support query parameters but only use the ID of the record to retrieve it!
Expand Down Expand Up @@ -359,8 +359,9 @@ private String getRecordDocType(Document record, Ruleset ruleset) throws IOExcep
Collection<String> doctypes = getDocTypeMetadata(ruleset);
Element root = record.getDocumentElement();
NodeList kitodoNodes = root.getElementsByTagNameNS(KITODO_NAMESPACE, KITODO_STRING);
if (kitodoNodes.getLength() > 0 && !doctypes.isEmpty()) {
NodeList importedMetadata = kitodoNodes.item(0).getChildNodes();
if (kitodoNodes.getLength() > 0 && !doctypes.isEmpty() && kitodoNodes.item(0) instanceof Element) {
Element kitodoElement = (Element) kitodoNodes.item(0);
NodeList importedMetadata = kitodoElement.getElementsByTagNameNS(KITODO_NAMESPACE, "metadata");
for (int i = 0; i < importedMetadata.getLength(); i++) {
Node metadataNode = importedMetadata.item(i);
Element metadataElement = (Element) metadataNode;
Expand Down
91 changes: 89 additions & 2 deletions Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Date;
import java.sql.SQLException;
Expand All @@ -40,6 +41,8 @@
import javax.json.JsonObject;
import javax.json.JsonReader;

import com.xebialabs.restito.semantics.Action;
import com.xebialabs.restito.server.StubServer;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -103,6 +106,10 @@
import org.kitodo.production.workflow.model.Converter;
import org.kitodo.test.utils.ProcessTestUtils;

import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
import static com.xebialabs.restito.semantics.Condition.get;
import static com.xebialabs.restito.semantics.Condition.parameter;

/**
* Insert data to test database.
*/
Expand Down Expand Up @@ -1683,6 +1690,11 @@ public static void insertImportConfigurations() throws DAOException, DataExcepti
gbvConfiguration.setInterfaceType(SearchInterfaceType.SRU.name());
gbvConfiguration.setSruVersion("1.2");
gbvConfiguration.setSruRecordSchema("mods");
gbvConfiguration.setItemFieldXpath(".//*[local-name()='datafield'][@tag='954']");
gbvConfiguration.setItemFieldOwnerSubPath(".//*[local-name()='subfield'][@code='0']");
gbvConfiguration.setItemFieldOwnerMetadata("itemOwner");
gbvConfiguration.setItemFieldSignatureSubPath(".//*[local-name()='subfield'][@code='d']");
gbvConfiguration.setItemFieldSignatureMetadata("itemSignatur");

SearchField ppnField = new SearchField();
ppnField.setValue("pica.ppn");
Expand Down Expand Up @@ -1717,10 +1729,20 @@ public static void insertImportConfigurations() throws DAOException, DataExcepti
idSearchFieldKalliope.setLabel("Identifier");
idSearchFieldKalliope.setImportConfiguration(kalliopeConfiguration);

kalliopeConfiguration.setSearchFields(Collections.singletonList(idSearchFieldKalliope));
SearchField parentIdSearchFieldKalliope = new SearchField();
parentIdSearchFieldKalliope.setValue("context.ead.id");
parentIdSearchFieldKalliope.setLabel("Parent ID");
parentIdSearchFieldKalliope.setImportConfiguration(kalliopeConfiguration);

List<SearchField> kalliopeSearchFields = new LinkedList<>();
kalliopeSearchFields.add(idSearchFieldKalliope);
kalliopeSearchFields.add(parentIdSearchFieldKalliope);

kalliopeConfiguration.setSearchFields(kalliopeSearchFields);
ServiceManager.getImportConfigurationService().saveToDatabase(kalliopeConfiguration);

kalliopeConfiguration.setIdSearchField(kalliopeConfiguration.getSearchFields().get(0));
kalliopeConfiguration.setParentSearchField(kalliopeConfiguration.getSearchFields().get(1));
ServiceManager.getImportConfigurationService().saveToDatabase(kalliopeConfiguration);

// add K10Plus import configuration, including id search field
Expand All @@ -1745,10 +1767,20 @@ public static void insertImportConfigurations() throws DAOException, DataExcepti
idSearchFieldK10Plus.setLabel("PPN");
idSearchFieldK10Plus.setImportConfiguration(k10plusConfiguration);

k10plusConfiguration.setSearchFields(Collections.singletonList(idSearchFieldK10Plus));
SearchField parentIdSearchFieldK10Plus = new SearchField();
parentIdSearchFieldK10Plus.setValue("pica.parentId");
parentIdSearchFieldK10Plus.setLabel("Parent ID");
parentIdSearchFieldK10Plus.setImportConfiguration(k10plusConfiguration);

List<SearchField> k10SearchFields = new LinkedList<>();
k10SearchFields.add(idSearchFieldK10Plus);
k10SearchFields.add(parentIdSearchFieldK10Plus);

k10plusConfiguration.setSearchFields(k10SearchFields);
ServiceManager.getImportConfigurationService().saveToDatabase(k10plusConfiguration);

k10plusConfiguration.setIdSearchField(k10plusConfiguration.getSearchFields().get(0));
k10plusConfiguration.setParentSearchField(k10plusConfiguration.getSearchFields().get(1));
ServiceManager.getImportConfigurationService().saveToDatabase(k10plusConfiguration);

for (Project project : ServiceManager.getProjectService().getAll()) {
Expand Down Expand Up @@ -2058,6 +2090,16 @@ public static HashMap<String, Integer> getRemovableObjectIDs() {
return removableObjectIDs;
}

/**
* Return GBV ImportConfiguration.
*
* @return GBV ImportConfiguration
* @throws DAOException when GBV ImportConfiguration cannot be loaded from database
*/
public static ImportConfiguration getGbvImportConfiguration() throws DAOException {
return ServiceManager.getImportConfigurationService().getById(1);
}

/**
* Return Kalliope ImportConfiguration.
*
Expand Down Expand Up @@ -2113,4 +2155,49 @@ public static Process addProcess(String processTitle, int projectId, int templat
ServiceManager.getProcessService().save(process);
return process;
}

/**
* Adds a REST endpoint for a simulated SRU search interface stub server with the given parameters.
* @param server the stub server to which the REST endpoint is added
* @param query URL parameter containing query associated with this endpoint
* @param filePath path to file containing response to be returned by stub server when requesting this endpoint
* @param format URL parameter containing record schema format associated with this endpoint
* @param numberOfRecords URL parameter containing maximum number of records associated with this endpoint
* @throws IOException when reading the response file fails
*/
public static void addRestEndPointForSru(StubServer server, String query, String filePath, String format,
int numberOfRecords)
throws IOException {
try (InputStream inputStream = Files.newInputStream(Paths.get(filePath))) {
String serverResponse = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
whenHttp(server)
.match(get("/sru"),
parameter("operation", "searchRetrieve"),
parameter("recordSchema", format),
parameter("maximumRecords", String.valueOf(numberOfRecords)),
parameter("query", query))
.then(Action.ok(), Action.contentType("text/xml"), Action.stringContent(serverResponse));
}
}

/**
* Adds a REST endpoint for a simulated CUSTOM search interface stub server with the given parameters.
* @param server the stub server to which the REST endpoint is added
* @param filePath path to file containing response to be returned by stub server when requesting this endpoint
* @param recordId URL parameter containing record ID associated with this endpoint
* @param customParameterValue URL parameter containing custom URL parameter value associated with this endpoint
* @throws IOException when reading the response file fails
*/
public static void addRestEndPointForCustom(StubServer server, String filePath, String recordId,
String customParameterValue)
throws IOException {
try (InputStream inputStream = Files.newInputStream(Paths.get(filePath))) {
String serverResponse = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
whenHttp(server)
.match(get("/custom"),
parameter("firstKey", customParameterValue),
parameter("id", recordId))
.then(Action.ok(), Action.contentType("text/xml"), Action.stringContent(serverResponse));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,12 @@

package org.kitodo.production.services.catalogimport;

import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
import static com.xebialabs.restito.semantics.Action.contentType;
import static com.xebialabs.restito.semantics.Action.ok;
import static com.xebialabs.restito.semantics.Action.stringContent;
import static com.xebialabs.restito.semantics.Condition.get;
import static com.xebialabs.restito.semantics.Condition.parameter;

import com.xebialabs.restito.server.StubServer;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.LinkedList;

import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -79,22 +67,8 @@ public void shouldImportProcessHierarchy() throws Exception {

private static void setupServer() throws IOException {
server = new StubServer(PORT).run();
addRestEndPointForImport("ead.id=" + CHILD_RECORD_ID, CHILD_RECORD_PATH, 1);
addRestEndPointForImport("ead.id=" + PARENT_RECORD_ID, PARENT_RECORD_PATH, 1);
addRestEndPointForImport("ead.title=test", HITLIST_RECORD_PATH, 10);
}

private static void addRestEndPointForImport(String query, String filePath, int numberOfRecords) throws IOException {
try (InputStream inputStream = Files.newInputStream(Paths.get(filePath))) {
String serverResponse = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
whenHttp(server)
.match(get("/sru"),
parameter("version", "1.2"),
parameter("operation", "searchRetrieve"),
parameter("recordSchema", "mods"),
parameter("maximumRecords", String.valueOf(numberOfRecords)),
parameter("query", query))
.then(ok(), contentType("text/xml"), stringContent(serverResponse));
}
MockDatabase.addRestEndPointForSru(server, "ead.id=" + CHILD_RECORD_ID, CHILD_RECORD_PATH, "mods", 1);
MockDatabase.addRestEndPointForSru(server, "ead.id=" + PARENT_RECORD_ID, PARENT_RECORD_PATH, "mods", 1);
MockDatabase.addRestEndPointForSru(server, "ead.title=test", HITLIST_RECORD_PATH, "mods",10);
}
}
Loading

0 comments on commit c723b1c

Please sign in to comment.