Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Unit tests for new skolemization classes
- Loading branch information
Showing
3 changed files
with
188 additions
and
10 deletions.
There are no files selected for viewing
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
159 changes: 159 additions & 0 deletions
159
fcrepo-kernel-impl/src/test/java/org/fcrepo/kernel/impl/rdf/DeskolemizerTest.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,159 @@ | ||
|
||
package org.fcrepo.kernel.impl.rdf; | ||
|
||
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; | ||
import static com.hp.hpl.jena.graph.NodeFactory.createURI; | ||
import static com.hp.hpl.jena.graph.Triple.create; | ||
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel; | ||
import static java.util.UUID.randomUUID; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.fcrepo.kernel.identifiers.IdentifierConverter; | ||
import org.fcrepo.kernel.models.FedoraResource; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import com.hp.hpl.jena.graph.Node; | ||
import com.hp.hpl.jena.graph.Triple; | ||
import com.hp.hpl.jena.rdf.model.Model; | ||
import com.hp.hpl.jena.rdf.model.RDFNode; | ||
import com.hp.hpl.jena.rdf.model.Resource; | ||
|
||
/** | ||
* @author ajs6f | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class DeskolemizerTest { | ||
|
||
@Mock | ||
private IdentifierConverter<Resource, FedoraResource> mockIdTranslator; | ||
|
||
@Mock | ||
private FedoraResource mockSkolemFedoraResource, mockNonSkolemFedoraResource; | ||
|
||
private static final Model testModel = createDefaultModel(); | ||
|
||
private static final Node skolemNode = createURI("info:skolem"); | ||
|
||
@Before | ||
public void setUp() { | ||
when(mockSkolemFedoraResource.hasType("fedora:Skolem")).thenReturn(true); | ||
when(mockNonSkolemFedoraResource.hasType("fedora:Skolem")).thenReturn(false); | ||
} | ||
|
||
@Test | ||
public void tripleWithNoBNodeOperations() { | ||
when(mockIdTranslator.inDomain(any(Resource.class))).thenReturn(true); | ||
when(mockIdTranslator.asString(any(Resource.class))).thenReturn("non-fcr-URI"); | ||
when(mockIdTranslator.convert(any(Resource.class))).thenReturn(mockSkolemFedoraResource); | ||
when(mockSkolemFedoraResource.hasType("fedora:Skolem")).thenReturn(false); | ||
final Deskolemizer testDeskolemizer = new Deskolemizer(mockIdTranslator, null); | ||
|
||
final Triple testTriple = create(randomURIResource(), randomURIResource(), randomURIResource()); | ||
assertEquals(testTriple, testDeskolemizer.apply(testTriple)); | ||
|
||
final Triple testTripleWithLiteralObject = | ||
create(randomURIResource(), randomURIResource(), createLiteral("some literal value")); | ||
assertEquals(testTripleWithLiteralObject, testDeskolemizer.apply(testTripleWithLiteralObject)); | ||
|
||
final Triple testTripleWithObjectWithURIWithQueryString = | ||
create(randomURIResource(), randomURIResource(), createURI("info:test?with=query-string")); | ||
assertEquals(testTripleWithObjectWithURIWithQueryString, testDeskolemizer | ||
.apply(testTripleWithObjectWithURIWithQueryString)); | ||
|
||
final Node outOfDomainNode = createURI("info:out-of-domain"); | ||
final Triple testTripleWithObjectWithOutOfDomainURI = | ||
create(randomURIResource(), randomURIResource(), outOfDomainNode); | ||
final Resource outOfDomainObject = testModel.asRDFNode(outOfDomainNode).asResource(); | ||
when(mockIdTranslator.inDomain(outOfDomainObject)).thenReturn(false); | ||
assertEquals(testTripleWithObjectWithOutOfDomainURI, testDeskolemizer | ||
.apply(testTripleWithObjectWithOutOfDomainURI)); | ||
|
||
final Node fcrContainingNode = createURI("info:test/fcr:metadata-containing-URI"); | ||
final Triple testTripleWithObjectWithfcrContainingURI = | ||
create(randomURIResource(), randomURIResource(), fcrContainingNode); | ||
final Resource fcrContainingObject = testModel.asRDFNode(fcrContainingNode).asResource(); | ||
when(mockIdTranslator.inDomain(any(Resource.class))).thenReturn(true); | ||
when(mockIdTranslator.asString(fcrContainingObject)).thenReturn("foo/fcr:metadata"); | ||
assertEquals(testTripleWithObjectWithfcrContainingURI, testDeskolemizer | ||
.apply(testTripleWithObjectWithfcrContainingURI)); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void tripleWithBadSubject() { | ||
when(mockIdTranslator.inDomain(any(Resource.class))).thenThrow(new RuntimeException("Expected.")); | ||
final Deskolemizer testDeskolemizer = new Deskolemizer(mockIdTranslator, null); | ||
final Triple testTriple = create(randomURIResource(), randomURIResource(), randomURIResource()); | ||
testDeskolemizer.apply(testTriple); | ||
} | ||
|
||
@Test | ||
public void tripleWithSkolemSubjectShouldBeChanged() { | ||
final Triple testTriple = create(skolemNode, randomURIResource(), randomURIResource()); | ||
|
||
when(mockIdTranslator.inDomain(any(Resource.class))).thenReturn(true); | ||
when(mockIdTranslator.asString(any(Resource.class))).thenReturn("non-fcr-URI"); | ||
final Resource testSubject = testModel.asRDFNode(testTriple.getSubject()).asResource(); | ||
when(mockIdTranslator.convert(testSubject)).thenReturn(mockSkolemFedoraResource); | ||
final Resource testObject = testModel.asRDFNode(testTriple.getObject()).asResource(); | ||
when(mockIdTranslator.convert(testObject)).thenReturn(mockNonSkolemFedoraResource); | ||
|
||
final Deskolemizer testDeskolemizer = new Deskolemizer(mockIdTranslator, testModel); | ||
final Triple result = testDeskolemizer.apply(testTriple); | ||
assertNotEquals(testTriple, result); | ||
final Node deskolem = result.getSubject(); | ||
assertTrue(deskolem.isBlank()); | ||
} | ||
|
||
@Test | ||
public void statementWithSkolemObjectShouldBeChanged() { | ||
final Triple testTriple = create(randomURIResource(), randomURIResource(), skolemNode); | ||
|
||
when(mockIdTranslator.inDomain(any(Resource.class))).thenReturn(true); | ||
when(mockIdTranslator.asString(any(Resource.class))).thenReturn("non-fcr-URI"); | ||
final Resource testSubject = testModel.asRDFNode(testTriple.getSubject()).asResource(); | ||
when(mockIdTranslator.convert(testSubject)).thenReturn(mockNonSkolemFedoraResource); | ||
final Resource testObject = testModel.asRDFNode(testTriple.getObject()).asResource(); | ||
when(mockIdTranslator.convert(testObject)).thenReturn(mockSkolemFedoraResource); | ||
|
||
final Deskolemizer testDeskolemizer = new Deskolemizer(mockIdTranslator, testModel); | ||
final Triple result = testDeskolemizer.apply(testTriple); | ||
assertNotEquals(testTriple, result); | ||
final Node deskolem = result.getObject(); | ||
assertTrue(deskolem.isBlank()); | ||
} | ||
|
||
@Test | ||
public void statementWithSameSkolemSubjectAndObjectShouldBeChanged() { | ||
|
||
final Triple testTriple = create(skolemNode, randomURIResource(), skolemNode); | ||
|
||
when(mockIdTranslator.inDomain(any(Resource.class))).thenReturn(true); | ||
when(mockIdTranslator.asString(any(Resource.class))).thenReturn("non-fcr-URI"); | ||
final Resource testSubject = testModel.asRDFNode(testTriple.getSubject()).asResource(); | ||
when(mockIdTranslator.convert(testSubject)).thenReturn(mockSkolemFedoraResource); | ||
final Resource testObject = testModel.asRDFNode(testTriple.getObject()).asResource(); | ||
when(mockIdTranslator.convert(testObject)).thenReturn(mockSkolemFedoraResource); | ||
|
||
final Deskolemizer testDeskolemizer = new Deskolemizer(mockIdTranslator, testModel); | ||
final Triple result = testDeskolemizer.apply(testTriple); | ||
assertNotEquals(testTriple, result); | ||
final Node subject = result.getSubject(); | ||
assertTrue(subject.isBlank()); | ||
final Node object = result.getObject(); | ||
assertTrue(object.isBlank()); | ||
assertEquals(subject, object); | ||
} | ||
|
||
private static Node randomURIResource() { | ||
return createURI("test:/" + randomUUID()); | ||
} | ||
} |
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