Skip to content

Commit

Permalink
fcrepo-http-commons tests pass, now for fcrepo-http-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 16, 2013
1 parent 46d1e5d commit fdcce01
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 61 deletions.
Expand Up @@ -71,7 +71,7 @@ public class FedoraNodes extends AbstractResource {
@GET
@Produces({TEXT_HTML, N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON,
NTRIPLES})
public Dataset describeRdf(@PathParam("path")
public Dataset describe(@PathParam("path")
final List<PathSegment> pathList, @Context
final Request request) throws RepositoryException, IOException {

Expand Down
Expand Up @@ -15,7 +15,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

Expand All @@ -27,7 +26,6 @@
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.Variant;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -163,7 +161,7 @@ public void testDeleteObject() throws RepositoryException {

@SuppressWarnings("unchecked")
@Test
public void testDescribeRdfObject() throws RepositoryException, IOException {
public void testDescribeObject() throws RepositoryException, IOException {
final String pid = "FedoraObjectsRdfTest1";
final String path = "/" + pid;

Expand All @@ -184,12 +182,9 @@ public void testDescribeRdfObject() throws RepositoryException, IOException {
new Variant(MediaType.valueOf("application/n-triples"), null,
null));

final OutputStream mockStream = mock(OutputStream.class);
((StreamingOutput) testObj
.describeRdf(createPathList(pid), mockRequest))
.write(mockStream);

verify(mockModel).write(mockStream, "N-TRIPLES");
final Dataset dataset =
testObj.describe(createPathList(pid), mockRequest);
assertNotNull(dataset.getDefaultModel());

}

Expand Down
26 changes: 13 additions & 13 deletions fcrepo-http-api/src/test/resources/spring-test/rest.xml
@@ -1,23 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean class="org.fcrepo.session.SessionFactory" />

<!-- Mints PIDs-->
<bean class="org.fcrepo.identifiers.UUIDPidMinter"/>

<context:annotation-config />

<context:component-scan base-package="org.fcrepo.api, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>
<bean class="org.fcrepo.session.SessionFactory"/>

<!-- Mints PIDs-->
<bean class="org.fcrepo.identifiers.UUIDPidMinter"/>

<context:annotation-config/>

<context:component-scan
base-package="org.fcrepo.api, org.fcrepo.serialization, org.fcrepo.responses"/>

<util:map id="serializers" key-type="java.lang.String" map-class="java.util.HashMap"
value-type="org.fcrepo.serialization.FedoraObjectSerializer">
value-type="org.fcrepo.serialization.FedoraObjectSerializer">
<description>Map from formats to serializers for Fedora objects</description>
<entry key="jcr/xml">
<bean class="org.fcrepo.serialization.jcrxml.JcrXmlSerializer"/>
Expand Down
Expand Up @@ -6,16 +6,10 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:property-placeholder/>

<!-- show stack traces for easier debugging -->
<bean id="wildcardExceptionmapper" class="org.fcrepo.exceptionhandlers.WildcardExceptionMapper" >
<property name="showStackTrace" value="true" />
</bean>


<bean id="containerWrapper" class="org.fcrepo.test.util.ContainerWrapper" init-method="start" destroy-method="stop" >
<property name="port" value="${test.port:8080}"/>
<property name="contextConfigLocations" value="classpath:spring-test/master.xml" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.exceptionhandlers" />
<property name="packagesToScan" value="org.fcrepo.api, org.fcrepo.responses"/>
</bean>

</beans>
7 changes: 4 additions & 3 deletions fcrepo-http-commons/pom.xml
Expand Up @@ -59,16 +59,17 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -4,13 +4,15 @@
import static com.google.common.collect.ImmutableList.of;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Properties;

import javax.annotation.PostConstruct;
Expand All @@ -24,6 +26,7 @@
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.query.Dataset;
Expand All @@ -36,15 +39,19 @@ public class HtmlProvider implements MessageBodyWriter<Dataset> {

Template template;

private static final Logger logger = getLogger(HtmlProvider.class);

@PostConstruct
void init() throws IOException {
final Properties properties = new Properties();
try (final InputStream propertiesStream =
this.getClass().getResource("velocity.properties").openStream()) {
final URL propertiesUrl =
getClass().getResource("/velocity.properties");
logger.debug("Using Velocity configuration from {}", propertiesUrl);
try (final InputStream propertiesStream = propertiesUrl.openStream()) {
properties.load(propertiesStream);
}
velocity.init(properties);
template = velocity.getTemplate("html-template");
template = velocity.getTemplate("template.xhtml");
}

@Override
Expand All @@ -54,7 +61,7 @@ public void writeTo(final Dataset rdf, final Class<?> type,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream entityStream) throws IOException,
WebApplicationException {

logger.debug("Writing an HTML response for: {}", rdf);
// add a Content-type header
httpHeaders.put("Content-type", of((Object) TEXT_HTML));

Expand All @@ -70,8 +77,7 @@ public void writeTo(final Dataset rdf, final Class<?> type,
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return mediaType.equals(TEXT_HTML_TYPE) &&
(type.equals(Dataset.class) || genericType.getClass().equals(
Dataset.class));
Dataset.class.isAssignableFrom(type);
}

@Override
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions fcrepo-http-commons/src/main/resources/template.xhtml
@@ -0,0 +1,11 @@
<html>
<head>
<title></title>
</head>
<body>
<div>
<span>TEMPLATE</span>
</div>

</body>
</html>
@@ -1,35 +1,55 @@

package org.fcrepo.responses;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static com.hp.hpl.jena.graph.Node.createURI;
import static org.fcrepo.http.RDFMediaType.NTRIPLES_TYPE;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.jcr.RepositoryException;
import javax.ws.rs.WebApplicationException;

import org.fcrepo.http.RDFMediaType;
import org.fcrepo.responses.GraphStreamingOutput;
import org.junit.Test;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sparql.modify.GraphStoreNull;
import org.slf4j.Logger;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.mem.GraphMem;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.hp.hpl.jena.sparql.modify.GraphStoreBasic;
import com.hp.hpl.jena.update.GraphStore;

public class GraphStreamingOutputTest {

@Test
public void testStuff() throws WebApplicationException, IOException, RepositoryException {
GraphStore graph = new GraphStoreNull();

final Model model = spy(graph.toDataset().getDefaultModel());

GraphStreamingOutput test =
new GraphStreamingOutput(graph, RDFMediaType.NTRIPLES_TYPE);
OutputStream mockOut = mock(OutputStream.class);
test.write(mockOut);
verify(model.write(mockOut, "N-TRIPLES"));
}
private static final Logger logger =
getLogger(GraphStreamingOutputTest.class);

@Test
public void testStuff() throws WebApplicationException, IOException,
RepositoryException {
final GraphStore graphStore =
new GraphStoreBasic(new DatasetImpl(ModelFactory
.createDefaultModel()));
final Graph g = new GraphMem();
g.add(new Triple(createURI("test:subject"),
createURI("test:predicate"), createURI("test:object")));
graphStore.setDefaultGraph(g);
final GraphStreamingOutput test =
new GraphStreamingOutput(graphStore, NTRIPLES_TYPE);
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
test.write(out);
final String serialized = out.toString();
logger.debug("Created serialized RDF: \n {}", serialized);
assertTrue("Couldn't find test subject!", serialized
.contains("test:subject"));
assertTrue("Couldn't find test predicate!", serialized
.contains("test:predicate"));
assertTrue("Couldn't find test object!", serialized
.contains("test:object"));
}
}
}
22 changes: 22 additions & 0 deletions fcrepo-http-commons/src/test/resources/logback-test.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%p %d{HH:mm:ss.SSS} \(%c{0}\) %m%n</pattern>
</encoder>
</appender>

<logger name="org.fcrepo" additivity="false" level="DEBUG">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.modeshape" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.http.client" additivity="false" level="DEBUG">
<appender-ref ref="STDOUT"/>
</logger>
<root additivity="false" level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

0 comments on commit fdcce01

Please sign in to comment.