Skip to content

Commit

Permalink
Allow passing path of local file as log message arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesname committed Oct 9, 2014
1 parent b9e3831 commit 2f46df0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
27 changes: 22 additions & 5 deletions ehri-extension/src/main/java/eu/ehri/extension/ImportResource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.ehri.extension;

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.hp.hpl.jena.shared.NoReaderForLangException;
Expand All @@ -14,6 +15,7 @@
import eu.ehri.project.models.UserProfile;
import eu.ehri.project.models.base.PermissionScope;
import eu.ehri.project.models.cvoc.Vocabulary;
import org.apache.commons.io.FileUtils;
import org.neo4j.graphdb.GraphDatabaseService;

import javax.ws.rs.*;
Expand Down Expand Up @@ -67,7 +69,8 @@ public ImportResource(@Context GraphDatabaseService database) {
*
* @param scopeId The id of the import scope (i.e. repository)
* @param tolerant Whether or not to die on the first validation error
* @param logMessage Log message for import
* @param logMessage Log message for import. If this refers to a local file
* its contents will be used.
* @param format The RDF format of the POSTed data
* @param stream A stream of SKOS data in a valid format.
* @return A JSON object showing how many records were created,
Expand Down Expand Up @@ -95,7 +98,7 @@ public Response importSkos(
ImportLog log = importer
.setFormat(format)
.setTolerant(tolerant)
.importFile(stream, logMessage);
.importFile(stream, getLogMessage(logMessage).orNull());

graph.getBaseGraph().commit();
return Response.ok(jsonMapper.writeValueAsBytes(log.getData())).build();
Expand Down Expand Up @@ -132,7 +135,8 @@ public Response importSkos(
*
* @param scopeId The id of the import scope (i.e. repository)
* @param tolerant Whether or not to die on the first validation error
* @param logMessage Log message for import
* @param logMessage Log message for import. If this refers to a local file
* its contents will be used.
* @param handlerClass The fully-qualified handler class name
* (defaults to IcaAtomEadHandler)
* @param importerClass The fully-qualified import class name
Expand Down Expand Up @@ -175,7 +179,7 @@ public Response importEad(
ImportLog log = new SaxImportManager(graph, scope, user, importer, handler)
.setProperties(propertyFile)
.setTolerant(tolerant)
.importFiles(paths, logMessage);
.importFiles(paths, getLogMessage(logMessage).orNull());

graph.getBaseGraph().commit();
return Response.ok(jsonMapper.writeValueAsBytes(log.getData())).build();
Expand Down Expand Up @@ -205,7 +209,7 @@ private static List<String> getFilePaths(String pathList) {
return files;
}

private static void checkPropertyFile(String properties) {
private static void checkPropertyFile(String properties) {
// Null properties are allowed
if (properties != null) {
File file = new File(properties);
Expand Down Expand Up @@ -263,4 +267,17 @@ private static Class<? extends AbstractImporter> getEadImporter(String importerN
return (Class<? extends AbstractImporter>) importer;
}
}

private Optional<String> getLogMessage(String logMessage) throws IOException {
if (logMessage == null || logMessage.isEmpty()) {
return Optional.absent();
} else {
File fileTest = new File(logMessage);
if (fileTest.exists()) {
return Optional.of(FileUtils.readFileToString(fileTest, "UTF-8"));
} else {
return Optional.of(logMessage);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import com.google.common.io.Resources;
import com.sun.jersey.api.client.ClientResponse;
import eu.ehri.project.importers.IcaAtomEadHandler;
import org.apache.commons.io.FileUtils;
import org.codehaus.jackson.JsonNode;
import org.junit.Test;

import javax.ws.rs.core.UriBuilder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -59,7 +57,8 @@ public void testImportEad() throws Exception {
// Get the path of an EAD file
InputStream payloadStream = getPayloadStream(SINGLE_EAD);

URI uri = getImportUrl("r1", "Testing import", false)
String logText = "Testing import";
URI uri = getImportUrl("r1", logText, false)
.queryParam(HANDLER_PARAM, IcaAtomEadHandler.class.getName())
.build();
ClientResponse response = callAs(getAdminUserProfileId(), uri)
Expand All @@ -74,6 +73,7 @@ public void testImportEad() throws Exception {
assertEquals(1, rootNode.path("created").asInt());
assertEquals(0, rootNode.path("updated").asInt());
assertEquals(0, rootNode.path("unchanged").asInt());
assertEquals(logText, rootNode.path("message").asText());
}

@Test
Expand Down Expand Up @@ -118,6 +118,30 @@ public void testImportEadWithBadClass() throws Exception {
.contains("not an instance of"));
}

@Test
public void testImportEadWithFileLogMessage() throws Exception {
// Get the path of an EAD file
InputStream payloadStream = getPayloadStream(SINGLE_EAD);

String logText = "Testing import";
URI uri = getImportUrl("r1", getTestLogFilePath(logText), false)
.queryParam(HANDLER_PARAM, IcaAtomEadHandler.class.getName())
.build();
ClientResponse response = callAs(getAdminUserProfileId(), uri)
.header("Content-Type", "text/plain")
.entity(payloadStream)
.post(ClientResponse.class);

assertStatus(ClientResponse.Status.OK, response);
String output = response.getEntity(String.class);

JsonNode rootNode = jsonMapper.readValue(output, JsonNode.class);
assertEquals(1, rootNode.path("created").asInt());
assertEquals(0, rootNode.path("updated").asInt());
assertEquals(0, rootNode.path("unchanged").asInt());
assertEquals(logText, rootNode.path("message").asText());
}

private UriBuilder getImportUrl(String scopeId, String log, boolean tolerant) {
return ehriUriBuilder("import", "ead")
.queryParam(LOG_PARAM, log)
Expand All @@ -136,4 +160,11 @@ private InputStream getPayloadStream(String... resources)
return new ByteArrayInputStream(
payloadText.getBytes("UTF-8"));
}

private String getTestLogFilePath(String text) throws IOException {
File temp = File.createTempFile("test-log", ".tmp");
temp.deleteOnExit();
FileUtils.writeStringToFile(temp, text);
return temp.getAbsolutePath();
}
}

0 comments on commit 2f46df0

Please sign in to comment.