Skip to content

Commit

Permalink
Removing POST from node type endpoint and slight factoring on HTTP ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 13, 2014
1 parent c0e6f16 commit fcb9ad4
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 249 deletions.
Expand Up @@ -19,9 +19,6 @@
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static javax.ws.rs.core.Response.status;
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
import static org.apache.http.HttpStatus.SC_NO_CONTENT;
import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
Expand All @@ -30,24 +27,17 @@
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;

import java.io.IOException;
import java.io.InputStream;

import javax.inject.Inject;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.responses.HtmlTemplate;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.springframework.context.annotation.Scope;

Expand Down Expand Up @@ -77,31 +67,4 @@ public class FedoraRepositoryNodeTypes extends AbstractResource {
public RdfStream getNodeTypes(@Context final UriInfo uriInfo) throws RepositoryException {
return nodeService.getNodeTypes(session).session(session);
}

/**
* Update the NodeTypes by POSTing a CND file/fragment
*
* @return 201
* @throws javax.jcr.RepositoryException
* @throws java.io.IOException
*/
@POST
@Consumes({"text/cnd"})
@Timed
public Response updateCnd(final InputStream requestBodyStream)
throws RepositoryException, IOException {

try {
nodeService.registerNodeTypes(session, requestBodyStream);

return status(SC_NO_CONTENT).build();
} catch ( RepositoryRuntimeException ex ) {
// this may be brittle, but the returned exception isn't an InvalideNodeTypeDefinitionException...
if ( ex.getCause().getMessage().indexOf("Reading the node definitions from the"
+ " supplied stream resulted in problems(s)") != -1 ) {
return status(SC_BAD_REQUEST).entity(ex.getMessage()).build();
}
throw ex;
}
}
}
Expand Up @@ -15,29 +15,28 @@
*/
package org.fcrepo.http.api.repository;

import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import static org.fcrepo.http.commons.test.util.TestHelpers.getUriInfoImpl;
import static org.fcrepo.http.commons.test.util.TestHelpers.mockSession;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.util.ReflectionTestUtils.setField;

import java.io.InputStream;
import java.net.URI;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

/**
*
* @author cbeer
Expand Down Expand Up @@ -85,11 +84,4 @@ public void itShouldRetrieveNodeTypes() throws RepositoryException {
assertEquals("Got wrong triples!", mockRdfStream, nodeTypes);

}

@Test
public void itShouldPersistIncomingCndFile() throws RepositoryException, IOException {
testObj.updateCnd(mockInputStream);

verify(mockNodes).registerNodeTypes(mockSession, mockInputStream);
}
}
Expand Up @@ -30,8 +30,6 @@
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import com.hp.hpl.jena.update.GraphStore;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
Expand All @@ -43,33 +41,34 @@
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
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.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hp.hpl.jena.update.GraphStore;

/**
* <p>Abstract AbstractResourceIT class.</p>
*
* @author awoods
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/test-container.xml")
@UseStockConfiguration
public abstract class AbstractResourceIT {

protected static Logger logger;
Expand Down
@@ -0,0 +1,129 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* 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 org.fcrepo.integration.http.api;

import static com.google.common.collect.Lists.newArrayList;
import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;
import static org.fcrepo.kernel.RdfLexicon.EMBED_CONTAINS;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
import static org.fcrepo.kernel.RdfLexicon.VERSIONING_POLICY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.util.EntityUtils;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.update.GraphStore;

/**
* Framework for running versioning integration tests.
*
* @author ajs6f
*/
public abstract class AbstractVersioningIT extends AbstractResourceIT {

protected void testDatastreamContentUpdatesCreateNewVersions(final String objName, final String dsName)
throws IOException {
final String firstVersionText = "foo";
final String secondVersionText = "bar";
createDatastream(objName, dsName, firstVersionText);
final GraphStore dsInitialVersion = getContent(serverAddress + objName + "/" + dsName + "/" + FCR_METADATA);
assertTrue("Should find auto-created versioning policy",
dsInitialVersion.contains(ANY,
createResource(serverAddress + objName + "/" + dsName + "/" + FCR_METADATA)
.asNode(),
VERSIONING_POLICY.asNode(),
createLiteral("auto-version")));

mutateDatastream(objName, dsName, secondVersionText);
final HttpGet retrieveMutatedDataStreamMethod =
new HttpGet(serverAddress +
objName + "/" + dsName);
assertEquals("Datastream didn't accept mutation!", secondVersionText,
EntityUtils.toString(
execute(
retrieveMutatedDataStreamMethod).getEntity()));

final HttpGet getVersion =
new HttpGet(serverAddress + objName + "/" + dsName + "/fcr:versions");
logger.debug("Retrieved version profile:");

final GraphStore results = getGraphStore(getVersion);
final Resource subject =
createResource(serverAddress + objName + "/" + dsName);
assertTrue("Didn't find a version triple!",
results.contains(ANY, subject.asNode(), HAS_VERSION.asNode(), ANY));

verifyVersions(results, subject.asNode(), firstVersionText, secondVersionText);
}

protected GraphStore getContent(final String url) throws IOException {
final HttpGet getVersion = new HttpGet(url);
getVersion.addHeader("Prefer", "return=representation; include=\"" + EMBED_CONTAINS.toString() + "\"");
return getGraphStore(getVersion);
}

public void mutateDatastream(final String objName, final String dsName, final String contentText)
throws IOException {
final HttpPut mutateDataStreamMethod =
putDSMethod(objName, dsName, contentText);
final HttpResponse response = execute(mutateDataStreamMethod);
final int status = response.getStatusLine().getStatusCode();
if (status != NO_CONTENT.getStatusCode()) {
logger.error(EntityUtils.toString(response.getEntity()));
}
assertEquals("Couldn't mutate a datastream!", NO_CONTENT.getStatusCode(), status);

}

/**
* Verifies that one version exists with each supplied value. This method makes assertions that each of the
* provided values is the content of a version node and nothing else. Order isn't important, and no assumption is
* made about whether extra versions exist.
*/
protected void verifyVersions(final GraphStore graph, final Node subject, final String... values)
throws IOException {
final ArrayList<String> remainingValues = newArrayList(values);
final Iterator<Quad> versionIt = graph.find(ANY, subject, HAS_VERSION.asNode(), ANY);

while (versionIt.hasNext() && !remainingValues.isEmpty()) {
final String value =
EntityUtils.toString(execute(new HttpGet(versionIt.next().getObject().getURI()))
.getEntity());
remainingValues.remove(value);
}

if (!remainingValues.isEmpty()) {
fail(remainingValues.get(0) + " was not preserved in the version history!");
}
}

}
@@ -0,0 +1,56 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* 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 org.fcrepo.integration.http.api;

import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.kernel.RdfLexicon.VERSIONING_POLICY;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;

import com.hp.hpl.jena.update.GraphStore;

/**
* <p>FedoraVersionsIT class.</p>
*
* @author awoods
*/
@UseAutoVersioningConfiguration
public class FedoraAutoVersioningIT extends AbstractVersioningIT {

@Test
public void testRepositoryWideAutoVersioning() throws IOException {
final String objName = getRandomUniquePid();
final String dsName = "datastream";

createObject(objName);

final GraphStore initialVersion = getContent(serverAddress + objName);
assertTrue("Should find auto-created versioning policy",
initialVersion.contains(ANY,
createResource(serverAddress + objName).asNode(),
VERSIONING_POLICY.asNode(),
createLiteral("auto-version")));

testDatastreamContentUpdatesCreateNewVersions(objName, dsName);

}

}

0 comments on commit fcb9ad4

Please sign in to comment.