Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added version resource and simple integration test
- Loading branch information
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraVersions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.fcrepo.api; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import javax.jcr.ItemNotFoundException; | ||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.PathSegment; | ||
import javax.ws.rs.core.Response; | ||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlAttribute; | ||
import javax.xml.bind.annotation.XmlElementWrapper; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import javax.xml.bind.annotation.XmlType; | ||
|
||
import org.fcrepo.AbstractResource; | ||
import org.fcrepo.Datastream; | ||
import org.fcrepo.FedoraObject; | ||
import org.fcrepo.services.DatastreamService; | ||
import org.fcrepo.services.LowLevelStorageService; | ||
import org.fcrepo.services.ObjectService; | ||
import org.joda.time.DateTime; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Path("/rest/{path: .*}/fcr:versions") | ||
public class FedoraVersions extends AbstractResource{ | ||
private final Logger logger = getLogger(FedoraDatastreams.class); | ||
|
||
@Autowired | ||
private DatastreamService datastreamService; | ||
|
||
@Autowired | ||
private ObjectService objectService; | ||
|
||
@GET | ||
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) | ||
public List<Version> getVersionProfile(@PathParam("path") final List<PathSegment> segments) throws RepositoryException{ | ||
final String path = toPath(segments); | ||
if (path.contains("/fcr:datastreams/")){ | ||
Datastream ds = datastreamService.getDatastream(path); | ||
Version v = new Version(path, ds.getDsId(), ds.getCreatedDate().getTime()); | ||
return Arrays.asList(v); | ||
} | ||
FedoraObject obj = objectService.getObject(path); | ||
Version v = new Version(path,obj.getName(),new Date().getTime()); | ||
return Arrays.asList(v); | ||
} | ||
|
||
@Path("/{id}") | ||
@GET | ||
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML}) | ||
public Response getVersion(@PathParam("path") final List<PathSegment> segments, @PathParam("id") final String id) throws RepositoryException{ | ||
final String path = toPath(segments); | ||
if (path.contains("/fcr:datastreams/")){ | ||
Datastream ds = datastreamService.getDatastream(path); | ||
return Response.ok(ds).build(); | ||
} | ||
FedoraObject obj = objectService.getObject(path); | ||
return Response.ok(obj).build(); | ||
} | ||
|
||
|
||
|
||
/* this is just a inner static to mock the yet non existant versioning model */ | ||
/* TODO: use a real versioning mechanism */ | ||
@XmlRootElement(name="version",namespace="http://example.com/test/version") | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public static class Version { | ||
|
||
@XmlAttribute(name="path") | ||
private String path; | ||
@XmlAttribute(name="name") | ||
private String name; | ||
@XmlAttribute(name="timestamp") | ||
private long timestamp; | ||
|
||
private Version() { | ||
super(); | ||
} | ||
|
||
public Version(String path, String versionId, long timestamp) { | ||
this.name = versionId; | ||
this.timestamp = timestamp; | ||
this.path = path; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
fcrepo-http-api/src/test/java/org/fcrepo/integration/api/FedoraVersionsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.fcrepo.integration.api; | ||
|
||
import static java.util.regex.Pattern.DOTALL; | ||
import static java.util.regex.Pattern.compile; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.junit.Test; | ||
|
||
public class FedoraVersionsIT extends AbstractResourceIT { | ||
|
||
@Test | ||
public void testGetObjectVersionProfile() throws Exception { | ||
execute(postObjMethod("FedoraDatastreamsTest1")); | ||
final HttpGet method = new HttpGet(serverAddress + | ||
"objects/FedoraDatastreamsTest1/fcr:versions"); | ||
HttpResponse resp = execute(method); | ||
assertEquals(200, resp.getStatusLine().getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testGetDatastreamVersionProfile() throws Exception { | ||
execute(postObjMethod("FedoraDatastreamsTest1")); | ||
|
||
HttpPost postDs = postDSMethod("FedoraDatastreamsTest1","ds1","foo"); | ||
execute(postDs); | ||
assertEquals(201, getStatus(postDs)); | ||
|
||
final HttpGet getVersion = new HttpGet(serverAddress + | ||
"objects/FedoraDatastreamsTest1/ds1/fcr:versions"); | ||
HttpResponse resp =execute(getVersion); | ||
assertEquals(200, resp.getStatusLine().getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testGetDatastreamVersion() throws Exception { | ||
execute(postObjMethod("FedoraDatastreamsTest1")); | ||
|
||
HttpPost postDs = postDSMethod("FedoraDatastreamsTest1","ds1","foo"); | ||
execute(postDs); | ||
assertEquals(201, getStatus(postDs)); | ||
|
||
final HttpGet getVersion = new HttpGet(serverAddress + | ||
"objects/FedoraDatastreamsTest1/ds1/fcr:versions/lastVersion"); | ||
HttpResponse resp =execute(getVersion); | ||
assertEquals(200, resp.getStatusLine().getStatusCode()); | ||
} | ||
} |