Commit 94886606 authored by acoburn's avatar acoburn
Browse files

Add working tests for the PCDM extension

parent 57f1ec61
......@@ -15,7 +15,7 @@
*/
package edu.amherst.acdc.exts.pcdm;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.HTTP_PATH;
......@@ -67,7 +67,8 @@ public class PcdmRouter extends RouteBuilder {
.removeHeader("breadcrumbId")
.process(exchange -> {
final String contentType = exchange.getIn().getHeader(PCDM_ACCEPT, String.class);
final Optional<String> rdfLang = of(contentType).map(RDFLanguages::contentTypeToLang).map(Lang::getName);
final Optional<String> rdfLang = ofNullable(contentType).map(RDFLanguages::contentTypeToLang)
.map(Lang::getName);
exchange.getIn().setHeader(CONTENT_TYPE, rdfLang.isPresent() ? contentType : DEFAULT_CONTENT_TYPE);
})
.to("direct:serialize");
......
......@@ -44,7 +44,6 @@ class RelatedProcessor implements Processor {
@Override
public void process(final Exchange exchange) throws Exception {
final Set<String> ids = new HashSet<>();
@SuppressWarnings("unchecked")
final List<String> members = exchange.getIn().getHeader(PCDM_MEMBERS, emptyList(), List.class);
@SuppressWarnings("unchecked")
......@@ -52,6 +51,7 @@ class RelatedProcessor implements Processor {
@SuppressWarnings("unchecked")
final List<String> relatedObjects = exchange.getIn().getHeader(PCDM_RELATED_OBJECTS, emptyList(), List.class);
final Set<String> ids = new HashSet<>();
ids.addAll(members);
ids.addAll(files);
ids.addAll(relatedObjects);
......
......@@ -8,7 +8,7 @@
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
<!-- OSGI blueprint property placeholder -->
<cm:property-placeholder persistent-id="edu.amherst.acdc.exts.jsonld" update-strategy="reload">
<cm:property-placeholder persistent-id="edu.amherst.acdc.exts.pcdm" update-strategy="reload">
<cm:default-properties>
<cm:property name="rest.port" value="9107"/>
<cm:property name="rest.prefix" value="/pcdm"/>
......
......@@ -79,6 +79,12 @@
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-osgi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>javax.inject</artifactId>
......@@ -187,12 +193,13 @@
<portName>fcrepo.dynamic.test.port</portName>
<portName>fcrepo.dynamic.jms.port</portName>
<portName>fcrepo.dynamic.stomp.port</portName>
<portName>karaf.jsonld.port</portName>
<portName>karaf.template.port</portName>
<portName>karaf.metadata.port</portName>
<portName>karaf.image.port</portName>
<portName>karaf.fits.port</portName>
<portName>karaf.idiomatic.port</portName>
<portName>karaf.image.port</portName>
<portName>karaf.jsonld.port</portName>
<portName>karaf.metadata.port</portName>
<portName>karaf.pcdm.port</portName>
<portName>karaf.template.port</portName>
<portName>karaf.rmiRegistry.port</portName>
<portName>karaf.rmiServer.port</portName>
<portName>karaf.ssh.port</portName>
......@@ -222,12 +229,15 @@
<fcrepo.dynamic.test.port>${fcrepo.dynamic.test.port}</fcrepo.dynamic.test.port>
<fcrepo.dynamic.jms.port>${fcrepo.dynamic.jms.port}</fcrepo.dynamic.jms.port>
<project.build.outputDirectory>${project.build.outputDirectory}</project.build.outputDirectory>
<karaf.jsonld.port>${karaf.jsonld.port}</karaf.jsonld.port>
<karaf.template.port>${karaf.template.port}</karaf.template.port>
<karaf.fits.port>${karaf.fits.port}</karaf.fits.port>
<karaf.idiomatic.port>${karaf.idiomatic.port}</karaf.idiomatic.port>
<karaf.metadata.port>${karaf.metadata.port}</karaf.metadata.port>
<karaf.image.port>${karaf.image.port}</karaf.image.port>
<karaf.fits.port>${karaf.fits.port}</karaf.fits.port>
<karaf.jsonld.port>${karaf.jsonld.port}</karaf.jsonld.port>
<karaf.metadata.port>${karaf.metadata.port}</karaf.metadata.port>
<karaf.pcdm.port>${karaf.pcdm.port}</karaf.pcdm.port>
<karaf.template.port>${karaf.template.port}</karaf.template.port>
<karaf.ssh.port>${karaf.ssh.port}</karaf.ssh.port>
<karaf.rmiRegistry.port>${karaf.rmiRegistry.port}</karaf.rmiRegistry.port>
<karaf.rmiServer.port>${karaf.rmiServer.port}</karaf.rmiServer.port>
......
......@@ -34,6 +34,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
......@@ -52,7 +53,9 @@ import org.slf4j.Logger;
*/
public abstract class AbstractOSGiIT {
private static Logger LOGGER = getLogger(AbstractOSGiIT.class);
private static final Logger LOGGER = getLogger(AbstractOSGiIT.class);
private final CloseableHttpClient httpclient = createDefault();
@Inject
protected FeaturesService featuresService;
......@@ -67,7 +70,6 @@ public abstract class AbstractOSGiIT {
}
protected String post(final String url, final InputStream stream, final String contentType) {
final CloseableHttpClient httpclient = createDefault();
try {
final HttpPost req = new HttpPost(url);
if (stream != null) {
......@@ -79,13 +81,32 @@ public abstract class AbstractOSGiIT {
assertEquals(SC_CREATED, response.getStatusLine().getStatusCode());
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (final IOException ex) {
LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex);
return "";
LOGGER.error("Unable to extract HttpEntity response into an InputStream: ", ex);
}
return "";
}
protected boolean put(final String url) {
return put(url, null, null);
}
protected boolean put(final String url, final InputStream stream, final String contentType) {
try {
final HttpPut req = new HttpPut(url);
if (stream != null) {
req.setHeader("Content-Type", contentType);
req.setEntity(new InputStreamEntity(stream));
}
final HttpResponse response = httpclient.execute(req);
assertEquals(SC_CREATED, response.getStatusLine().getStatusCode());
return true;
} catch (final IOException ex) {
LOGGER.error("Error patching to {}: {}", url, ex.getMessage());
}
return false;
}
protected boolean patch(final String url, final String sparql) {
final CloseableHttpClient httpclient = createDefault();
try {
final HttpPatch req = new HttpPatch(url);
req.addHeader("Content-Type", "application/sparql-update");
......@@ -94,35 +115,40 @@ public abstract class AbstractOSGiIT {
assertEquals(SC_NO_CONTENT, response.getStatusLine().getStatusCode());
return true;
} catch (final IOException ex) {
LOGGER.warn("Error patching to {}: {}", url, ex.getMessage());
LOGGER.error("Error patching to {}: {}", url, ex.getMessage());
}
return false;
}
protected String get(final String url) {
final CloseableHttpClient httpclient = createDefault();
return get(url, null);
}
protected String get(final String url, final String accept) {
try {
final HttpGet req = new HttpGet(url);
if (accept != null) {
req.addHeader("Accept", accept);
}
final HttpResponse response = httpclient.execute(req);
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (final IOException ex) {
LOGGER.warn("Unable to extract HttpEntity response into an InputStream: ", ex);
return "";
LOGGER.error("Unable to extract HttpEntity response into an InputStream: ", ex);
}
return "";
}
protected String options(final String url) {
final CloseableHttpClient httpclient = createDefault();
try {
final HttpOptions req = new HttpOptions(url);
final HttpResponse response = httpclient.execute(req);
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (final IOException ex) {
LOGGER.warn("Unable to extract HttpEntity response into an InputStream: ", ex);
return "";
LOGGER.error("Unable to extract HttpEntity response into an InputStream: ", ex);
}
return "";
}
protected <T> T getOsgiService(final Class<T> type, final String filter, final long timeout) {
......
/*
* Copyright 2016 Amherst College
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.amherst.acdc.itests;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Set;
import org.apache.camel.CamelContext;
import org.apache.jena.rdf.model.Model;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.ConfigurationManager;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.slf4j.Logger;
/**
* @author Aaron Coburn
* @since May 2, 2016
*/
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class AcrepoPcdmIT extends AbstractOSGiIT {
private static Logger LOGGER = getLogger(AcrepoPcdmIT.class);
@Configuration
public Option[] config() {
final ConfigurationManager cm = new ConfigurationManager();
final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port");
final String pcdmExtPort = cm.getProperty("karaf.pcdm.port");
final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port");
final String rmiServerPort = cm.getProperty("karaf.rmiServer.port");
final String fcrepoBaseUrl = "http://localhost:" + fcrepoPort + "/fcrepo/rest";
final String sshPort = cm.getProperty("karaf.ssh.port");
return new Option[] {
karafDistributionConfiguration()
.frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf")
.versionAsInProject().type("zip"))
.unpackDirectory(new File("target", "exam"))
.useDeployFolder(false),
logLevel(LogLevel.INFO),
keepRuntimeFolder(),
configureConsole().ignoreLocalConsole(),
features(maven().groupId("org.apache.karaf.features").artifactId("standard")
.versionAsInProject().classifier("features").type("xml"), "scr"),
features(maven().groupId("edu.amherst.acdc").artifactId("acrepo-karaf")
.type("xml").classifier("features").versionAsInProject(),
"acrepo-exts-pcdm"),
systemProperty("karaf.pcdm.port").value(pcdmExtPort),
systemProperty("fcrepo.port").value(fcrepoPort),
editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort),
editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort),
editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.pcdm.cfg", "fcrepo.baseUrl", fcrepoBaseUrl),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.pcdm.cfg", "rest.port", pcdmExtPort)
};
}
@Test
public void testInstallation() throws Exception {
assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-core")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("fcrepo-camel")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-pcdm")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-services-pcdm")));
}
@Test
public void testPcdmOptions() throws Exception {
// make sure that the camel context has started up.
final CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.name=AcrepoExtPcdm)",
10000);
assertNotNull(ctx);
final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
final String baseSvcUrl = "http://localhost:" + System.getProperty("karaf.pcdm.port") + "/pcdm";
assertTrue(options(baseSvcUrl).contains("apix:bindsTo pcdm:Object"));
}
@Test
public void testPcdmObjectTurtle() throws Exception {
// make sure that the camel context has started up.
final CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.name=AcrepoExtPcdm)",
10000);
assertNotNull(ctx);
final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
final String baseSvcUrl = "http://localhost:" + System.getProperty("karaf.pcdm.port") + "/pcdm";
final String pcdmObj = post(baseUrl, getClass().getResourceAsStream("/resource.ttl"), "text/turtle");
final String members = pcdmObj + "/members";
assertTrue(put(members, getClass().getResourceAsStream("/members.ttl"), "text/turtle"));
final String page1 = post(members);
final String page2 = post(members);
final String page3 = post(members);
final String page4 = post(members);
final String response = get(baseSvcUrl + pcdmObj.replace(baseUrl, ""));
final InputStream input = new ByteArrayInputStream(response.getBytes(UTF_8));
final Model model = createDefaultModel();
model.read(input, null, "TTL");
final Set<String> subjects = model.listSubjects().mapWith(x -> x.getURI()).toSet();
assertTrue(subjects.contains(pcdmObj));
assertTrue(subjects.contains(page1));
assertTrue(subjects.contains(page2));
assertTrue(subjects.contains(page3));
assertTrue(subjects.contains(page4));
assertFalse(subjects.contains(members));
}
@Test
public void testPcdmObjectJsonLD() throws Exception {
// make sure that the camel context has started up.
final CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.name=AcrepoExtPcdm)",
10000);
assertNotNull(ctx);
final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
final String baseSvcUrl = "http://localhost:" + System.getProperty("karaf.pcdm.port") + "/pcdm";
final String pcdmObj = post(baseUrl, getClass().getResourceAsStream("/resource.ttl"), "text/turtle");
final String members = pcdmObj + "/members";
assertTrue(put(members, getClass().getResourceAsStream("/members.ttl"), "text/turtle"));
final String page1 = post(members);
final String page2 = post(members);
final String page3 = post(members);
final String page4 = post(members);
final String response = get(baseSvcUrl + pcdmObj.replace(baseUrl, ""), "application/ld+json");
LOGGER.info(response);
final InputStream input = new ByteArrayInputStream(response.getBytes(UTF_8));
final Model model = createDefaultModel();
model.read(input, null, "JSONLD");
final Set<String> subjects = model.listSubjects().mapWith(x -> x.getURI()).toSet();
assertTrue(subjects.contains(pcdmObj));
assertTrue(subjects.contains(page1));
assertTrue(subjects.contains(page2));
assertTrue(subjects.contains(page3));
assertTrue(subjects.contains(page4));
assertFalse(subjects.contains(members));
}
}
......@@ -58,6 +58,7 @@ public class AcrepoServicesIT extends AbstractOSGiIT {
final String metadataPort = cm.getProperty("karaf.metadata.port");
final String templatePort = cm.getProperty("karaf.template.port");
final String jsonldPort = cm.getProperty("karaf.jsonld.port");
final String pcdmPort = cm.getProperty("karaf.pcdm.port");
final String imagePort = cm.getProperty("karaf.image.port");
final String fitsPort = cm.getProperty("karaf.fits.port");
......@@ -77,17 +78,22 @@ public class AcrepoServicesIT extends AbstractOSGiIT {
features(maven().groupId("org.apache.activemq").artifactId("activemq-karaf")
.type("xml").classifier("features").versionAsInProject(), "activemq-camel"),
features(maven().groupId("edu.amherst.acdc").artifactId("acrepo-karaf")
.type("xml").classifier("features").versionAsInProject(), "acrepo-connector-idiomatic",
"acrepo-connector-idiomatic-pgsql", "acrepo-exts-fits", "acrepo-exts-image",
"acrepo-exts-jsonld", "acrepo-exts-serialize-xml", "acrepo-exts-template",
.type("xml").classifier("features").versionAsInProject(),
"acrepo-connector-broadcast", "acrepo-connector-idiomatic", "acrepo-connector-idiomatic-pgsql",
"acrepo-exts-fits", "acrepo-exts-image", "acrepo-exts-jsonld", "acrepo-exts-pcdm",
"acrepo-exts-serialize-xml", "acrepo-exts-template",
"acrepo-libs-jena", "acrepo-libs-sesame", "acrepo-libs-jsonld",
"acrepo-libs-jackson", "acrepo-libs-marmotta", "acrepo-services-activemq",
"acrepo-services-inference", "acrepo-services-jsonld", "acrepo-services-ldcache",
"acrepo-services-mint", "acrepo-services-pcdm", "acrepo-connector-broadcast"),
"acrepo-libs-jackson", "acrepo-libs-marmotta",
"acrepo-services-activemq", "acrepo-services-inference", "acrepo-services-jsonld",
"acrepo-services-ldcache", "acrepo-services-mint", "acrepo-services-pcdm"),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.fits.cfg", "rest.port", fitsPort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.image.cfg", "rest.port", imagePort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.jsonld.cfg", "rest.port", jsonldPort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.pcdm.cfg", "rest.port", pcdmPort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.serialize.xml.cfg", "rest.port", metadataPort),
editConfigurationFilePut("etc/edu.amherst.acdc.exts.template.cfg", "rest.port", templatePort),
editConfigurationFilePut("etc/edu.amherst.acdc.connector.idiomatic.cfg", "rest.port", idiomaticPort),
......@@ -107,6 +113,7 @@ public class AcrepoServicesIT extends AbstractOSGiIT {
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-fits")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-image")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-jsonld")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-pcdm")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-serialize-xml")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-exts-template")));
assertTrue(featuresService.isInstalled(featuresService.getFeature("acrepo-libs-jackson")));
......
@prefix pcdm: <http://pcdm.org/models#> .
@prefix ldp: <http://www.w3.org/ns/ldp#> .
<> a ldp:DirectContainer ;
ldp:membershipResource <.> ;
ldp:hasMemberRelation pcdm:hasFile .
@prefix pcdm: <http://pcdm.org/models#> .
@prefix ldp: <http://www.w3.org/ns/ldp#> .
<> a ldp:DirectContainer ;
ldp:membershipResource <.> ;
ldp:hasMemberRelation pcdm:hasMember .
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment