Skip to content

Commit

Permalink
Add tests for converting with transactions and workspaces. fix up 9a6…
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 13, 2014
1 parent a209922 commit f717685
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
Expand Up @@ -372,7 +372,7 @@ private void setTranslationChain(final List<Converter<String, String>> chained)


private static final List<Converter<String,String>> minimalTranslationChain =
singletonList((Converter<String,String>) new NamespaceConverter());
singletonList((Converter<String, String>) new NamespaceConverter());

protected List<Converter<String,String>> getTranslationChain() {
final ApplicationContext context = getApplicationContext();
Expand Down Expand Up @@ -408,7 +408,7 @@ public WorkspaceIdentifierConverter(final Session session) {
protected String doForward(final String path) {
if (path.contains(WORKSPACE_PREFIX) && !path.contains(workspaceSegment())) {
throw new RepositoryRuntimeException("Path " + path
+ " is not in current workspace " + defaultWorkspace);
+ " is not in current workspace " + getWorkspaceName());
}
return replaceOnce(path, workspaceSegment(), EMPTY);
}
Expand All @@ -419,13 +419,17 @@ protected String doBackward(final String path) {
}

private String workspaceSegment() {
final String workspace = session.getWorkspace().getName();
final String workspace = getWorkspaceName();
if (!workspace.equals(defaultWorkspace)) {
return "/" + WORKSPACE_PREFIX + workspace;
} else {
return EMPTY;
}
}

private String getWorkspaceName() {
return session.getWorkspace().getName();
}
}

/**
Expand Down
Expand Up @@ -16,6 +16,7 @@
package org.fcrepo.http.commons.api.rdf;

import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.kernel.TxSession;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -48,6 +49,9 @@ public class UriAwareIdentifierConverterTest {
@Mock
private Session session;

@Mock
private TxSession txSession;

@Mock
private Node node;

Expand Down Expand Up @@ -117,6 +121,42 @@ public void testDoForwardWithDatastreamMetadata() throws Exception {
assertEquals(node, converted);
}

@Test
public void testDoForwardWithWorkspace() throws Exception {
final Resource resource = createResource("http://localhost:8080/some/workspace:xyz/" + path);
when(mockWorkspace.getName()).thenReturn("xyz");
final Node converted = converter.convert(resource);
assertEquals(node, converted);
}

@Test(expected = RepositoryRuntimeException.class)
public void testDoForwardInDifferentWorkspace() throws Exception {
final Resource resource = createResource("http://localhost:8080/some/workspace:xyz/" + path);
when(mockWorkspace.getName()).thenReturn("abc");
final Node converted = converter.convert(resource);
assertEquals(node, converted);
}

@Test
public void testDoForwardWithTransaction() throws Exception {
final UriAwareIdentifierConverter converter = new UriAwareIdentifierConverter(txSession,
UriBuilder.fromUri(uriTemplate));
when(txSession.getTxId()).thenReturn("xyz");
when(txSession.getNode("/" + path)).thenReturn(node);
when(txSession.getWorkspace()).thenReturn(mockWorkspace);
final Resource resource = createResource("http://localhost:8080/some/tx:xyz/" + path);
final Node converted = converter.convert(resource);
assertEquals(node, converted);
}

@Test
public void testDoForwardWithUuid() throws Exception {
final Resource resource = createResource("http://localhost:8080/some/[xyz]");
when(session.getNode("/[xyz]")).thenReturn(node);
final Node converted = converter.convert(resource);
assertEquals(node, converted);
}

@Test
public void testDoBackward() throws Exception {
final Resource converted = converter.reverse().convert(node);
Expand All @@ -136,6 +176,14 @@ public void testDoBackwardWithDatastreamMetadata() throws Exception {
assertEquals(metadataResource, converted);
}

@Test
public void testDoBackwardWithWorkspace() throws Exception {
final Resource resource = createResource("http://localhost:8080/some/workspace:xyz/" + path);
when(mockWorkspace.getName()).thenReturn("xyz");
final Resource converted = converter.reverse().convert(node);
assertEquals(resource, converted);
}

@Test
public void testDoForwardWithImplicitVersionedDatastream() throws Exception {
when(session.getNodeByIdentifier("x")).thenReturn(versionedNode);
Expand Down Expand Up @@ -178,4 +226,17 @@ public void testDoBackwardWithVersionedNode() throws Exception {
final Resource converted = converter.reverse().convert(versionedNode);
assertEquals(versionedResource, converted);
}

@Test
public void testDoBackwardWithTransaction() throws Exception {
final UriAwareIdentifierConverter converter = new UriAwareIdentifierConverter(txSession,
UriBuilder.fromUri(uriTemplate));
when(txSession.getTxId()).thenReturn("xyz");
when(txSession.getNode("/" + path)).thenReturn(node);
when(txSession.getWorkspace()).thenReturn(mockWorkspace);
when(node.getSession()).thenReturn(txSession);
final Resource resource = createResource("http://localhost:8080/some/tx:xyz/" + path);
final Resource converted = converter.reverse().convert(node);
assertEquals(resource, converted);
}
}

0 comments on commit f717685

Please sign in to comment.