Skip to content

Commit

Permalink
added retrieveDatastreamUris for better testability;added test for me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
fasseg committed Jun 6, 2013
1 parent 30a4982 commit d655e7e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import org.apache.jena.atlas.web.HttpException;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.fcrepo.RdfLexicon;
import org.fcrepo.fixity.model.DatastreamFixityError;
Expand Down Expand Up @@ -45,13 +46,17 @@ public class FedoraFixityClient {
* @return A {@link List} containing the URIs of the child objects
*/
public List<String> retrieveUris(String parentUri) throws IOException {
return retrieveUris(parentUri,parentUri);
}

public List<String> retrieveUris(String sourceUri, String parentUri) throws IOException {
/* fetch a RDF Description of the parent form the repository */
StmtIterator stmts = null;
try {
/* parse the RDF N3 response using Apache Jena */
final Model model = ModelFactory.createDefaultModel();
try {
RDFDataMgr.read(model, parentUri);
RDFDataMgr.read(model, sourceUri, Lang.N3);
} catch (HttpException e) {
stmts.close();
throw new IOException("Unable to fetch uris from " + parentUri,
Expand All @@ -73,8 +78,13 @@ public List<String> retrieveUris(String parentUri) throws IOException {
LOG.debug("adding '" + uri + "' to retrieveUris results");
}
return uris;
} catch (IOException e) {
LOG.error("Unable to fetch object urls", e);
throw e;
} finally {
stmts.close();
if (stmts != null) {
stmts.close();
}
}
}

Expand All @@ -89,7 +99,7 @@ public List<DatastreamFixityResult> requestFixityChecks(
for (final String uri : datastreamUris) {
/* parse the fixity part of the RDF response */
final Model model = ModelFactory.createDefaultModel();
RDFDataMgr.read(model, uri + "/fcr:fixity");
RDFDataMgr.read(model, uri + "/fcr:fixity",Lang.N3);
StmtIterator sts = null;
try {
sts = model.listStatements(model.createResource(uri),
Expand Down Expand Up @@ -141,7 +151,9 @@ public List<DatastreamFixityResult> requestFixityChecks(
"Unable to handle results of unknown type");
}
} finally {
sts.close();
if (sts != null) {
sts.close();
}
}
}
return results;
Expand All @@ -155,18 +167,19 @@ public List<String> retrieveDatatstreamUris(String objectUri) {
final Model model = ModelFactory.createDefaultModel();
RDFDataMgr.read(model, objectUri);
final StmtIterator sts =
model.listStatements(null, RdfLexicon.HAS_MIXIN_TYPE,
"fedora:datastream");
model.listStatements(null, RdfLexicon.HAS_MIXIN_TYPE, "fedora:datastream");
try {
List<String> result = new ArrayList<>();
while (sts.hasNext()) {
final Statement st = sts.next(); //NOSONAR
final String dsUri = st.getResource().getURI();
final String dsUri = st.getSubject().asResource().getURI();
result.add(dsUri);
}
return result;
} finally {
sts.close();
if (sts != null) {
sts.close();
}
}
}
}
Expand Up @@ -146,7 +146,7 @@ private ObjectFixityResult checkObjectFixity(final String uri)
* fetch a list of the object's datastreams for getting their fixity
* information
*/
final List<String> datastreamUris = this.fixityClient.retrieveUris(uri);
final List<String> datastreamUris = this.fixityClient.retrieveDatatstreamUris(uri);
LOG.debug("discovered {} datastream URIs for Object {}",
datastreamUris.size(), uri);

Expand Down
@@ -0,0 +1,48 @@
/**
*
*/

package org.fcrepo.fixity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.util.List;

import org.fcrepo.fixity.client.FedoraFixityClient;
import org.junit.Test;

/**
* @author frank asseg
*
*/
public class FedoraFixitClientTest {

private final FedoraFixityClient client = new FedoraFixityClient();

@Test
public void testRetrieveObjects() throws Exception {
URI responseUri = this.getClass().getClassLoader()
.getResource("mock-responses/response-turtle-objects.n3")
.toURI();
assertNotNull(responseUri);
assertEquals("file", responseUri.getScheme());
List<String> objectUris =
this.client.retrieveUris(responseUri.toASCIIString(),"http://localhost:8080/fcrepo/rest/objects");
assertNotNull(objectUris);
assertTrue(objectUris.size() == 3);
}
@Test
public void testRetrieveDatastreams() throws Exception {
URI responseUri = this.getClass().getClassLoader()
.getResource("mock-responses/response-turtle-datastreams.n3")
.toURI();
assertNotNull(responseUri);
assertEquals("file", responseUri.getScheme());
List<String> objectUris =this.client.retrieveDatatstreamUris(responseUri.toASCIIString());
assertNotNull(objectUris);
assertTrue(objectUris.size() == 4);
}
}
Expand Up @@ -45,11 +45,11 @@ public void testConsumeFixityMessage() throws Exception {

/* setup an appropriate response from the mock */
String parentUri = "http://localhost:8080/objects/testobj1";
Mockito.when(mockClient.retrieveUris(Mockito.any(String.class))).thenReturn(Arrays.asList(parentUri + "/ds1", parentUri + "/ds2"));
Mockito.when(mockClient.retrieveDatatstreamUris(Mockito.any(String.class))).thenReturn(Arrays.asList(parentUri + "/ds1", parentUri + "/ds2"));

this.fixityService.consumeFixityMessage(parentUri);

Mockito.verify(mockClient).retrieveUris(parentUri);
Mockito.verify(mockClient).retrieveDatatstreamUris(parentUri);
Mockito.verify(mockDb).addResult(Mockito.any(ObjectFixityResult.class));
}
}

0 comments on commit d655e7e

Please sign in to comment.