Skip to content

Commit

Permalink
Changed FedoraChildren to use injected Session
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 8, 2013
1 parent d14cb8a commit 76574a7
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 306 deletions.
120 changes: 68 additions & 52 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraChildren.java
@@ -1,9 +1,12 @@

package org.fcrepo.api;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.utils.FedoraJcrTypes.FEDORA_DATASTREAM;
import static org.fcrepo.utils.FedoraJcrTypes.FEDORA_OBJECT;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand All @@ -21,67 +24,80 @@
import javax.ws.rs.core.Response;

import org.fcrepo.AbstractResource;
import org.fcrepo.session.InjectedSession;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;


@Component
@Scope("prototype")
@Path("/rest/{path: .*}/fcr:children")
public class FedoraChildren extends AbstractResource {

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

/**
* Returns a list of the first-generation
* descendants of an object, filtered by
* an optional mixin parameter
*
* @param pathList
* @param mixin
* @return 200
* @throws javax.jcr.RepositoryException
* @throws java.io.IOException
*/
@GET
@Timed
@Produces({TEXT_XML, APPLICATION_JSON, TEXT_HTML})
public Response getObjects(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("mixin") @DefaultValue("") String mixin
) throws RepositoryException, IOException {


final Session session = getAuthenticatedSession();

try {
final String path = toPath(pathList);
logger.info("getting children of {}", path);
if ("".equals(mixin)) {
mixin = null;
}
else if (FedoraJcrTypes.FEDORA_OBJECT.equals(mixin)) {
mixin = "nt:folder";
} else if (FedoraJcrTypes.FEDORA_DATASTREAM.equals(mixin)) {
mixin = "nt:file";
}
return ok(objectService.getObjectNames(session, path, mixin).toString()).build();
} finally {
session.logout();
}
}


public ObjectService getObjectService() {
return objectService;
}


public void setObjectService(ObjectService objectService) {
this.objectService = objectService;
}
@InjectedSession
private Session session;

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

/**
* Returns a list of the first-generation descendants of an object,
* filtered by an optional mixin parameter
*
* @param pathList
* @param mixin
* @return 200
* @throws javax.jcr.RepositoryException
* @throws java.io.IOException
*/
@GET
@Timed
@Produces({TEXT_XML, APPLICATION_JSON, TEXT_HTML})
public Response getObjects(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("mixin")
@DefaultValue("")
String mixin) throws RepositoryException, IOException {
try {
final String path = toPath(pathList);
logger.debug("Entering getObjects() with path: {} and mixin: {}",
path, mixin);
if (mixin.isEmpty()) {
logger.trace("No mixin specified.");
mixin = null;
} else {
switch (mixin) {
case FEDORA_OBJECT:
mixin = "nt:folder";
break;
case FEDORA_DATASTREAM:
mixin = "nt:file";
break;
default:
/* accept mixin as given */
}
logger.trace("Searching for children with mixin: {}", mixin);
}

return ok(
objectService.getObjectNames(session, path, mixin)
.toString()).build();
} finally {
session.logout();
}
}

public ObjectService getObjectService() {
return objectService;
}

public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

public void setSession(final Session session) {
this.session = session;
}

}
@@ -1,3 +1,4 @@

package org.fcrepo.api;

import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
Expand Down Expand Up @@ -31,45 +32,49 @@

public class FedoraChildrenTest {

FedoraChildren testObj;

FedoraChildren testObj;

ObjectService mockObjects;
ObjectService mockObjects;

DatastreamService mockDatastreams;
DatastreamService mockDatastreams;

Repository mockRepo;
Repository mockRepo;

Session mockSession;
Session mockSession;

LowLevelStorageService mockLow;
LowLevelStorageService mockLow;

@Before
public void setUp() throws LoginException, RepositoryException {
mockObjects = mock(ObjectService.class);
mockDatastreams = mock(DatastreamService.class);
mockLow = mock(LowLevelStorageService.class);
testObj = new FedoraChildren();
testObj.setObjectService(mockObjects);
mockSession = TestHelpers.mockSession(testObj);
mockRepo = mock(Repository.class);
}
@Before
public void setUp() throws LoginException, RepositoryException {
mockObjects = mock(ObjectService.class);
mockDatastreams = mock(DatastreamService.class);
mockLow = mock(LowLevelStorageService.class);
testObj = new FedoraChildren();
testObj.setObjectService(mockObjects);
mockSession = TestHelpers.mockSession(testObj);
testObj.setSession(mockSession);
mockRepo = mock(Repository.class);
}

@Test
public void testGetObjects() throws RepositoryException, IOException {
final String pid = "testObject";
final String childPid = "testChild";
final String path = "/" + pid;
final FedoraObject mockObj = mock(FedoraObject.class);
when(mockObj.getName()).thenReturn(pid);
Set<String> mockNames = new HashSet<String>(Arrays.asList(new String[]{childPid}));
when(mockObjects.getObjectNames(mockSession, path)).thenReturn(mockNames);
when(mockObjects.getObjectNames(eq(mockSession), eq(path), any(String.class))).thenReturn(mockNames);
Response actual = testObj.getObjects(createPathList(pid), null);
assertNotNull(actual);
String content = (String) actual.getEntity();
assertTrue(content, content.contains(childPid));
verify(mockSession, never()).save();
}
@Test
public void testGetObjects() throws RepositoryException, IOException {
final String pid = "testObject";
final String childPid = "testChild";
final String path = "/" + pid;
final FedoraObject mockObj = mock(FedoraObject.class);
when(mockObj.getName()).thenReturn(pid);
final Set<String> mockNames =
new HashSet<String>(Arrays.asList(new String[] {childPid}));
when(mockObjects.getObjectNames(mockSession, path)).thenReturn(
mockNames);
when(
mockObjects.getObjectNames(eq(mockSession), eq(path),
any(String.class))).thenReturn(mockNames);
final Response actual = testObj.getObjects(createPathList(pid), "");
assertNotNull(actual);
final String content = (String) actual.getEntity();
assertTrue(content, content.contains(childPid));
verify(mockSession, never()).save();
}

}
@@ -1,6 +1,8 @@

package org.fcrepo.integration.api;

import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.web.WebDelegatingSmartContextLoader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -32,14 +34,15 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/test-container.xml")
@WebAppConfiguration
@ContextConfiguration(locations = {"/spring-test/test-container.xml"}, loader = WebDelegatingSmartContextLoader.class)
public abstract class AbstractResourceIT {

protected Logger logger;

protected JAXBContext context;

protected String OBJECT_PATH = "objects";
protected String OBJECT_PATH = "objects";

@Before
public void setLogger() {
Expand Down Expand Up @@ -90,15 +93,15 @@ protected static HttpPost postObjMethod(final String pid) {
protected static HttpPost postDSMethod(final String pid, final String ds,
final String content) throws UnsupportedEncodingException {
final HttpPost post =
new HttpPost(serverAddress + "objects/" + pid +
"/" + ds + "/fcr:content");
new HttpPost(serverAddress + "objects/" + pid + "/" + ds +
"/fcr:content");
post.setEntity(new StringEntity(content));
return post;
}

protected static HttpPut putDSMethod(final String pid, final String ds) {
return new HttpPut(serverAddress + "objects/" + pid +
"/" + ds + "/fcr:content");
return new HttpPut(serverAddress + "objects/" + pid + "/" + ds +
"/fcr:content");
}

protected HttpResponse execute(final HttpUriRequest method)
Expand All @@ -110,9 +113,9 @@ protected HttpResponse execute(final HttpUriRequest method)

protected int getStatus(final HttpUriRequest method)
throws ClientProtocolException, IOException {
HttpResponse response = execute(method);
int result = response.getStatusLine().getStatusCode();
if (!(result > 199) || !(result < 400)){
final HttpResponse response = execute(method);
final int result = response.getStatusLine().getStatusCode();
if (!(result > 199) || !(result < 400)) {
logger.warn(EntityUtils.toString(response.getEntity()));
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-http-api/src/test/resources/logback-test.xml
Expand Up @@ -13,7 +13,7 @@
<logger name="org.modeshape" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.http.client" additivity="false" level="DEBUG">
<logger name="com.sun.jersey" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<root additivity="false" level="INFO">
Expand Down
27 changes: 14 additions & 13 deletions fcrepo-http-api/src/test/resources/spring-test/rest.xml
@@ -1,23 +1,24 @@
<?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.session, org.fcrepo.exceptionhandlers"/>


<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 @@ -15,7 +15,7 @@
<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.session, org.fcrepo.exceptionhandlers" />
</bean>

</beans>
10 changes: 4 additions & 6 deletions fcrepo-http-commons/pom.xml
Expand Up @@ -69,9 +69,11 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
Expand All @@ -82,10 +84,6 @@
<artifactId>jersey-grizzly</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
Expand Down

0 comments on commit 76574a7

Please sign in to comment.