Skip to content

Commit

Permalink
Added failing i-test to make pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 4, 2013
1 parent 1714808 commit 6fe9331
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 4 deletions.
57 changes: 57 additions & 0 deletions pom.xml
Expand Up @@ -11,6 +11,10 @@
<name>fcrepo-bagit-modeshape-federation-connector</name>
<description>Connects ModeShape to a filesystem directory containing BagIt directories.</description>

<properties>
<modeshape.version>3.2-SNAPSHOT</modeshape.version>
</properties>

<dependencies>
<dependency>
<groupId>org.fcrepo</groupId>
Expand All @@ -32,5 +36,58 @@
</exclusion>
</exclusions>
</dependency>
<!-- test gear -->
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- This dependency is for compile-time: it keeps this module independent
of any given choice of JAX-RS implementation. It must be _after_ the test
gear. Otherwise it will get loaded during test phase, but because this is
just an API, the tests will not be able to execute. -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
18 changes: 14 additions & 4 deletions src/main/java/org/fcrepo/federation/bagit/BagItConnector.java
Expand Up @@ -73,9 +73,7 @@ public class BagItConnector extends FileSystemConnector {
public void initialize(NamespaceRegistry registry,
NodeTypeManager nodeTypeManager) throws RepositoryException,
IOException {

super.initialize(registry, nodeTypeManager);

getLogger().trace("Initializing...");
// Initialize the directory path field that has been set via reflection when this method is called...
checkFieldNotNull(directoryPath, "directoryPath");
directory = new File(directoryPath);
Expand All @@ -92,23 +90,30 @@ public void initialize(NamespaceRegistry registry,
throw new RepositoryException(msg);
}
directoryAbsolutePath = directory.getAbsolutePath();
getLogger().debug(
"Using filesystem directory: " + directoryAbsolutePath);
if (!directoryAbsolutePath.endsWith(FILE_SEPARATOR))
directoryAbsolutePath = directoryAbsolutePath + FILE_SEPARATOR;
directoryAbsolutePathLength =
directoryAbsolutePath.length() - FILE_SEPARATOR.length(); // does NOT include the separator

setExtraPropertiesStore(new BagItExtraPropertiesStore());
getLogger().trace("Initialized.");
}

@Override
public Document getDocumentById(String id) {
getLogger().trace("Entering getDocumentById()...");
getLogger().debug("Received request for document: " + id);
final File file = fileFor(id);
if (isExcluded(file) || !file.exists()) return null;
final boolean isRoot = isRoot(id);
final boolean isResource = isContentNode(id);
final DocumentWriter writer = newDocument(id);
File parentFile = file.getParentFile();
if (isResource) {
getLogger().debug(
"Determined document: " + id + " to be a binary resource.");
BinaryValue binaryValue = binaryFor(file);
writer.setPrimaryType(NT_RESOURCE);
writer.addProperty(JCR_DATA, binaryValue);
Expand All @@ -133,6 +138,8 @@ public Document getDocumentById(String id) {
writer.setNotQueryable();
parentFile = file;
} else if (file.isFile()) {
getLogger().debug(
"Determined document: " + id + " to be a datastream.");
writer.setPrimaryType(JcrConstants.NT_FILE);
writer.addMixinType(FedoraJcrTypes.FEDORA_DATASTREAM);
writer.addProperty(JCR_CREATED, factories().getDateFactory()
Expand All @@ -147,13 +154,16 @@ public Document getDocumentById(String id) {
isRoot ? JCR_CONTENT_SUFFIX : id + JCR_CONTENT_SUFFIX;
writer.addChild(childId, JCR_CONTENT);
} else {
getLogger().debug(
"Determined document: " + id + " to be a Fedora object.");
final File dataDir =
new File(file.getAbsolutePath() + DELIMITER + "data");
writer.setPrimaryType(NT_FOLDER);
writer.addMixinType(FEDORA_OBJECT);
writer.addProperty(JCR_CREATED, factories().getDateFactory()
.create(file.lastModified()));
writer.addProperty(JCR_CREATED_BY, null); // ignored
// get datastreams as children
for (File child : dataDir.listFiles()) {
// Only include as a datastream if we can access and read the file. Permissions might prevent us from
// reading the file, and the file might not exist if it is a broken symlink (see MODE-1768 for details).
Expand All @@ -177,7 +187,7 @@ public Document getDocumentById(String id) {
// Add the extra properties (if there are any), overwriting any properties with the same names
// (e.g., jcr:primaryType, jcr:mixinTypes, jcr:mimeType, etc.) ...
writer.addProperties(extraPropertiesStore().getProperties(id));

getLogger().trace("Leaving getDocumentById().");
return writer.document();
}

Expand Down
85 changes: 85 additions & 0 deletions src/test/java/org/fcrepo/federation/bagit/AbstractResourceIT.java
@@ -0,0 +1,85 @@

package org.fcrepo.federation.bagit;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-test/master.xml")
public abstract class AbstractResourceIT {

protected Logger logger;

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
}

protected static final int SERVER_PORT = Integer.parseInt(System
.getProperty("test.port", "8080"));

protected static final String HOSTNAME = "localhost";

protected static final String serverAddress = "http://" + HOSTNAME + ":" +
SERVER_PORT + "/rest/";

protected final PoolingClientConnectionManager connectionManager =
new PoolingClientConnectionManager();

protected static HttpClient client;

public AbstractResourceIT() {
connectionManager.setMaxTotal(Integer.MAX_VALUE);
connectionManager.setDefaultMaxPerRoute(5);
connectionManager.closeIdleConnections(3, TimeUnit.SECONDS);
client = new DefaultHttpClient(connectionManager);
}

protected static HttpPost postObjMethod(final String pid) {
return new HttpPost(serverAddress + "objects/" + pid);
}

protected static HttpPost postDSMethod(final String pid, final String ds,
final String content) throws UnsupportedEncodingException {
final HttpPost post =
new HttpPost(serverAddress + "objects/" + pid +
"/datastreams/" + ds);
post.setEntity(new StringEntity(content));
return post;
}

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

protected HttpResponse execute(HttpUriRequest method)
throws ClientProtocolException, IOException {
logger.debug("Executing: " + method.getMethod() + " to " +
method.getURI());
return client.execute(method);
}

protected int getStatus(HttpUriRequest method)
throws ClientProtocolException, IOException {
return execute(method).getStatusLine().getStatusCode();
}

}
28 changes: 28 additions & 0 deletions src/test/java/org/fcrepo/federation/bagit/BagItConnectorIT.java
@@ -0,0 +1,28 @@

package org.fcrepo.federation.bagit;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.fcrepo.federation.bagit.AbstractResourceIT;
import org.junit.Test;

public class BagItConnectorIT extends AbstractResourceIT {

@Test
public void tryOneObject() throws ClientProtocolException, IOException {
logger.debug("Found objects: " +
EntityUtils.toString(client.execute(
new HttpGet(serverAddress + "objects/")).getEntity()));
final String objName = "BagItFed1";
final HttpResponse response =
client.execute(new HttpGet(serverAddress + "objects/" + objName));
assertEquals("Couldn't find federated object!", 200, response
.getStatusLine().getStatusCode());
}
}
28 changes: 28 additions & 0 deletions src/test/resources/logback-test.xml
@@ -0,0 +1,28 @@
<?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="TRACE">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="gov.loc" additivity="false" level="TRACE">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.modeshape" additivity="false" level="WARN">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.apache.cxf" 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>
12 changes: 12 additions & 0 deletions src/test/resources/spring-test/master.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Master context for the test application. -->

<import resource="classpath:/spring-test/repo.xml"/>
<import resource="classpath:/spring-test/rest.xml"/>

</beans>
23 changes: 23 additions & 0 deletions src/test/resources/spring-test/repo.xml
@@ -0,0 +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"
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">

<!-- Context that supports the actual ModeShape JCR itself -->

<context:annotation-config/>

<bean name="modeshapeRepofactory"
class="org.fcrepo.spring.ModeShapeRepositoryFactoryBean">
<property name="repositoryConfiguration" value="${fcrepo.modeshape.configuration:test_repository.json}"/>
</bean>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.fcrepo.services.ObjectService"/>
<bean class="org.fcrepo.services.LowLevelStorageService"/>
<bean class="org.fcrepo.services.DatastreamService"/>

</beans>
54 changes: 54 additions & 0 deletions src/test/resources/spring-test/rest.xml
@@ -0,0 +1,54 @@
<?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:jaxrs="http://cxf.apache.org/jaxrs"
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://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml"/>

<context:property-placeholder/>

<!-- Context that houses JAX-RS Resources that compose the API
as well as some utility gear. -->

<context:annotation-config/>

<jaxrs:server address="http://localhost:${test.port:8080}/rest">
<jaxrs:serviceBeans>
<bean class="org.fcrepo.api.FedoraDatastreams"/>
<bean class="org.fcrepo.api.FedoraObjects"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</jaxrs:extensionMappings>
<jaxrs:providers>
<bean class="org.fcrepo.exceptionhandlers.PathNotFoundExceptionMapper"/>
<bean class="org.fcrepo.exceptionhandlers.AccessControlExceptionMapper"/>
<bean class="org.fcrepo.exceptionhandlers.WildcardExceptionMapper">
<property name="showStackTrace" value="true"/>
</bean>
<bean class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
<property name="marshallerProperties">
<util:map>
<entry key="jaxb.formatted.output">
<value type="java.lang.Boolean">true</value>
</entry>
</util:map>
</property>
</bean>
</jaxrs:providers>
</jaxrs:server>

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

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

</beans>
1 change: 1 addition & 0 deletions src/test/resources/test-objects/BagItFed1/testDS
@@ -0,0 +1 @@
Test datastream 1

0 comments on commit 6fe9331

Please sign in to comment.