Navigation Menu

Skip to content

Commit

Permalink
Update tests that extract RDF graphs from responses to:
Browse files Browse the repository at this point in the history
 - validate it receives RDF
 - select an appropriate RDF serialization for the Content-Type
 - consistently fetch the graph using test helpers
  • Loading branch information
cbeer committed Nov 7, 2013
1 parent 3901662 commit ae52cb6
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 301 deletions.
Expand Up @@ -16,18 +16,19 @@

package org.fcrepo.integration.http.api;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.util.concurrent.TimeUnit.SECONDS;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.junit.Assert.assertEquals;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import com.hp.hpl.jena.update.GraphStore;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
Expand All @@ -44,8 +45,6 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hp.hpl.jena.rdf.model.Model;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/test-container.xml")
public abstract class AbstractResourceIT {
Expand Down Expand Up @@ -111,7 +110,7 @@ protected static HttpPut putDSMethod(final String pid, final String ds,
protected HttpResponse execute(final HttpUriRequest method)
throws ClientProtocolException, IOException {
logger.debug("Executing: " + method.getMethod() + " to " +
method.getURI());
method.getURI());
return client.execute(method);
}

Expand All @@ -125,15 +124,34 @@ protected int getStatus(final HttpUriRequest method)
return result;
}

protected Model extract(final String serialization) throws IOException {
logger.debug("Reading RDF:\n{}", serialization);
try (

final InputStream rdf =
new ByteArrayInputStream(serialization.getBytes(Charset
.forName("UTF8")))) {
return createDefaultModel().read(rdf, null);
protected GraphStore getGraphStore(final HttpClient client, final HttpUriRequest method) throws IOException {

if (method.getFirstHeader("Accept") == null) {
method.addHeader("Accept", "text/n3");
}

HttpResponse response = client.execute(method);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
return parseTriples(response.getEntity());

}

protected GraphStore getGraphStore(final HttpUriRequest method) throws IOException {
return getGraphStore(client, method);
}

protected HttpResponse createObject(final String pid) throws IOException {
final HttpResponse response = client.execute(postObjMethod(pid));
assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
return response;
}

protected HttpResponse createDatastream(final String pid, final String dsid, final String content) throws IOException {
final HttpResponse response = client.execute(postDSMethod(pid, dsid, content));
assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
return response;
}

}
Expand Up @@ -21,7 +21,6 @@
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static junit.framework.TestCase.assertFalse;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -41,26 +40,15 @@ public class FedoraDatastreamsIT extends AbstractResourceIT {

@Test
public void testMultipleDatastreams() throws Exception {
final HttpPost createObjMethod =
postObjMethod("FedoraDatastreamsTest7");
assertEquals(201, getStatus(createObjMethod));

final HttpPost createDS1Method =
postDSMethod("FedoraDatastreamsTest7", "ds1",
"marbles for everyone");
assertEquals(201, getStatus(createDS1Method));
final HttpPost createDS2Method =
postDSMethod("FedoraDatastreamsTest7", "ds2",
"marbles for no one");
assertEquals(201, getStatus(createDS2Method));
createObject("FedoraDatastreamsTest7");

createDatastream("FedoraDatastreamsTest7", "ds1", "marbles for everyone");
createDatastream("FedoraDatastreamsTest7", "ds2", "marbles for no one");

final HttpGet getDSesMethod =
new HttpGet(serverAddress + "FedoraDatastreamsTest7");
getDSesMethod.addHeader("Accept", "text/n3");
final HttpResponse response = client.execute(getDSesMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
final GraphStore result =
parseTriples(response.getEntity().getContent());
final GraphStore result = getGraphStore(getDSesMethod);

logger.debug("Received triples: \n{}", result.toString());
final String subjectURI = serverAddress + "FedoraDatastreamsTest7";

Expand All @@ -72,14 +60,8 @@ public void testMultipleDatastreams() throws Exception {

@Test
public void testModifyMultipleDatastreams() throws Exception {
final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest8");

assertEquals(201, getStatus(objMethod));

final HttpPost createDSVOIDMethod =
postDSMethod("FedoraDatastreamsTest8", "ds_void",
"marbles for everyone");
assertEquals(201, getStatus(createDSVOIDMethod));
createObject("FedoraDatastreamsTest8");
createDatastream("FedoraDatastreamsTest8", "ds_void", "marbles for everyone");

final HttpPost post =
new HttpPost(serverAddress
Expand All @@ -97,13 +79,9 @@ public void testModifyMultipleDatastreams() throws Exception {

final HttpGet getDSesMethod =
new HttpGet(serverAddress + "FedoraDatastreamsTest8");
getDSesMethod.addHeader("Accept", "text/n3");
final HttpResponse response = client.execute(getDSesMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
final GraphStore result = getGraphStore(getDSesMethod);

final String subjectURI = serverAddress + "FedoraDatastreamsTest8";
final GraphStore result =
parseTriples(response.getEntity().getContent());
assertTrue("Didn't find the first datastream! ", result.contains(ANY,
createURI(subjectURI), ANY, createURI(subjectURI + "/ds1")));
assertTrue("Didn't find the second datastream! ", result.contains(ANY,
Expand All @@ -115,9 +93,8 @@ public void testModifyMultipleDatastreams() throws Exception {

@Test
public void testRetrieveMultipartDatastreams() throws Exception {
createObject("FedoraDatastreamsTest9");

final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest9");
assertEquals(201, getStatus(objMethod));
final HttpPost post =
new HttpPost(serverAddress
+ "FedoraDatastreamsTest9/fcr:datastreams/");
Expand Down Expand Up @@ -149,9 +126,8 @@ public void testRetrieveMultipartDatastreams() throws Exception {

@Test
public void testRetrieveFIlteredMultipartDatastreams() throws Exception {
createObject("FedoraDatastreamsTest10");

final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest10");
assertEquals(201, getStatus(objMethod));
final HttpPost post =
new HttpPost(serverAddress
+ "FedoraDatastreamsTest10/fcr:datastreams");
Expand Down Expand Up @@ -183,13 +159,10 @@ public void testRetrieveFIlteredMultipartDatastreams() throws Exception {

@Test
public void testBatchDeleteDatastream() throws Exception {
execute(postObjMethod("FedoraDatastreamsTest12"));
final HttpPost method1 =
postDSMethod("FedoraDatastreamsTest12", "ds1", "foo1");
assertEquals(201, getStatus(method1));
final HttpPost method2 =
postDSMethod("FedoraDatastreamsTest12", "ds2", "foo2");
assertEquals(201, getStatus(method2));
createObject("FedoraDatastreamsTest12");

createDatastream("FedoraDatastreamsTest12", "ds1", "foo1");
createDatastream("FedoraDatastreamsTest12", "ds2", "foo2");

final HttpDelete dmethod =
new HttpDelete(
Expand Down
Expand Up @@ -20,7 +20,6 @@
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral;
import static com.hp.hpl.jena.vocabulary.RDF.nil;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_OF_RESULT;
import static org.fcrepo.kernel.RdfLexicon.NEXT_PAGE;
import static org.fcrepo.kernel.RdfLexicon.PAGE_OF;
Expand All @@ -37,7 +36,6 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.junit.Ignore;
Expand Down Expand Up @@ -84,10 +82,7 @@ public void testSearchResultsHtml() throws Exception {
@Test
public void testSearchRdf() throws Exception {
/* first post an object which can be used for the search */
final HttpPost postObj = postObjMethod("testobj");
final HttpResponse postResp = execute(postObj);
postObj.releaseConnection();
assertEquals(201, postResp.getStatusLine().getStatusCode());
createObject("testobj");

/* and add a dc title to the object so the query returns a result */
final HttpPatch postDc = new HttpPatch(serverAddress + "testobj");
Expand All @@ -103,21 +98,16 @@ public void testSearchRdf() throws Exception {
postDc.releaseConnection();

final HttpGet method = new HttpGet(serverAddress + "fcr:search");
method.setHeader("Accept", "application/n3");
final URI uri =
new URIBuilder(method.getURI()).addParameter("q", "testobj")
.addParameter("offset", "0").addParameter("limit", "1")
.build();

method.setURI(uri);

final HttpResponse resp = execute(method);

final GraphStore graphStore =
parseTriples(resp.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);

logger.debug("Got search results graph: {}", graphStore);
assertEquals(200, resp.getStatusLine().getStatusCode());
assertTrue(graphStore.contains(ANY, createResource(
serverAddress + "fcr:search?q=testobj").asNode(),
SEARCH_HAS_TOTAL_RESULTS.asNode(), createTypedLiteral(1)
Expand Down Expand Up @@ -148,21 +138,16 @@ public void testSearchRdf() throws Exception {
public void testSearchSubmitPaging() throws Exception {

final HttpGet method = new HttpGet(serverAddress + "fcr:search");
method.setHeader("Accept", "application/n3");
final URI uri =
new URIBuilder(method.getURI()).addParameter("q", "testobj")
.addParameter("offset", "1").addParameter("limit", "1")
.build();

method.setURI(uri);

final HttpResponse resp = execute(method);

final GraphStore graphStore =
parseTriples(resp.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);

logger.debug("Got search results graph: {}", graphStore);
assertEquals(200, resp.getStatusLine().getStatusCode());
assertFalse(graphStore.contains(ANY, createResource(
serverAddress + "fcr:search?q=testobj").asNode(),
HAS_MEMBER_OF_RESULT.asNode(), ANY));
Expand Down
Expand Up @@ -20,18 +20,13 @@
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createPlainLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.fcrepo.kernel.RdfLexicon.HAS_COMPUTED_CHECKSUM;
import static org.fcrepo.kernel.RdfLexicon.HAS_COMPUTED_SIZE;
import static org.fcrepo.kernel.RdfLexicon.HAS_FIXITY_STATE;
import static org.fcrepo.kernel.RdfLexicon.IS_FIXITY_RESULT_OF;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.junit.Test;

import com.hp.hpl.jena.update.GraphStore;
Expand All @@ -40,20 +35,14 @@ public class FedoraFixityIT extends AbstractResourceIT {

@Test
public void testCheckDatastreamFixity() throws Exception {
final HttpPost objMethod = postObjMethod("FedoraDatastreamsTest11");
assertEquals(201, getStatus(objMethod));
final HttpPost method1 =
postDSMethod("FedoraDatastreamsTest11", "zxc", "foo");
assertEquals(201, getStatus(method1));
final HttpGet method2 =
createObject("FedoraDatastreamsTest11");
createDatastream("FedoraDatastreamsTest11", "zxc", "foo");

final HttpGet method =
new HttpGet(serverAddress
+ "FedoraDatastreamsTest11/zxc/fcr:fixity");
method2.setHeader("Accept", "application/n3");
final HttpResponse response = execute(method2);
assertEquals(200, response.getStatusLine().getStatusCode());
final HttpEntity entity = response.getEntity();
final GraphStore graphStore = parseTriples(entity.getContent());

final GraphStore graphStore = getGraphStore(method);
logger.info("Got triples {}", graphStore);

assertTrue(graphStore.contains(ANY, ANY, IS_FIXITY_RESULT_OF.asNode(),
Expand Down
Expand Up @@ -20,14 +20,12 @@
import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.fcrepo.http.commons.test.util.TestHelpers.parseTriples;
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_OF_RESULT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.junit.Test;

Expand All @@ -46,11 +44,8 @@ public void testGetNextPidResponds() throws Exception {
@Test
public void testGetNextHasAPid() throws IOException {
final HttpPost method = new HttpPost(serverAddress + "fcr:pid");
method.setHeader("Accept", "application/n3");
final HttpResponse response = client.execute(method);
logger.debug("Executed testGetNextHasAPid()");
final GraphStore graphStore =
parseTriples(response.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);
assertTrue("Didn't find a single dang PID!", graphStore.contains(ANY,
ResourceFactory.createResource(serverAddress + "fcr:pid")
.asNode(), HAS_MEMBER_OF_RESULT.asNode(), ANY));
Expand All @@ -62,10 +57,8 @@ public void testGetNextHasTwoPids() throws IOException {
final HttpPost method =
new HttpPost(serverAddress + "fcr:pid?numPids=2");
method.setHeader("Accept", "application/n3");
final HttpResponse response = client.execute(method);
logger.debug("Executed testGetNextHasTwoPids()");
final GraphStore graphStore =
parseTriples(response.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);
assertEquals("Didn't find two dang PIDs!", 2, size(graphStore.find(ANY,
createResource(serverAddress + "fcr:pid").asNode(),
HAS_MEMBER_OF_RESULT.asNode(), ANY)));
Expand All @@ -82,11 +75,8 @@ public void testGetNextPidRespondsWithPath() throws Exception {
@Test
public void testGetNextHasAPidWithPath() throws IOException {
final HttpPost method = new HttpPost(serverAddress + "fcr:pid");
method.setHeader("Accept", "application/n3");
final HttpResponse response = client.execute(method);
logger.debug("Executed testGetNextHasAPidWithPath()");
final GraphStore graphStore =
parseTriples(response.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);
assertTrue("Didn't find a single dang PID!", graphStore.contains(ANY,
createResource(serverAddress + "fcr:pid").asNode(),
HAS_MEMBER_OF_RESULT.asNode(), ANY));
Expand All @@ -97,11 +87,8 @@ public void testGetNextHasAPidWithPath() throws IOException {
public void testGetNextHasTwoPidsWithPath() throws IOException {
final HttpPost method =
new HttpPost(serverAddress + "fcr:pid?numPids=2");
method.setHeader("Accept", "application/n3");
final HttpResponse response = client.execute(method);
logger.debug("Executed testGetNextHasTwoPidsWithPath()");
final GraphStore graphStore =
parseTriples(response.getEntity().getContent());
final GraphStore graphStore = getGraphStore(method);
assertEquals("Didn't find two dang PIDs!", 2, size(graphStore.find(ANY,
createResource(serverAddress + "fcr:pid").asNode(),
HAS_MEMBER_OF_RESULT.asNode(), ANY)));
Expand Down

0 comments on commit ae52cb6

Please sign in to comment.