Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Providing RDF stream that distinguishes managed and unmanaged triples
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
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
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
47 changes: 47 additions & 0 deletions
47
fcrepo-kernel/src/main/java/org/fcrepo/kernel/utils/iterators/UnmanagedRdfStream.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,47 @@ | ||
|
||
package org.fcrepo.kernel.utils.iterators; | ||
|
||
import static com.google.common.base.Predicates.not; | ||
import static com.google.common.collect.Iterators.filter; | ||
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel; | ||
import static org.fcrepo.kernel.RdfLexicon.isManagedPredicate; | ||
|
||
import java.util.Iterator; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.hp.hpl.jena.graph.Triple; | ||
import com.hp.hpl.jena.rdf.model.Model; | ||
|
||
/** | ||
* This wraps an {@link Iterator} of {@link Triple}s and produces only those | ||
* safe for persistence into the repo as properties. It does this by filtering | ||
* all triples with managed predicates, as defined in {@link RdfLexicon}. | ||
* | ||
* @author ajs6f | ||
* @date Oct 23, 2013 | ||
*/ | ||
public class UnmanagedRdfStream extends RdfStream { | ||
|
||
private static final Model model = createDefaultModel(); | ||
|
||
private static final Predicate<Triple> isManagedTriple = | ||
new Predicate<Triple>() { | ||
|
||
@Override | ||
public boolean apply(final Triple t) { | ||
return isManagedPredicate.apply(model.asStatement(t) | ||
.getPredicate()); | ||
} | ||
|
||
}; | ||
|
||
public UnmanagedRdfStream(final Iterator<Triple> triples) { | ||
super(triples); | ||
} | ||
|
||
@Override | ||
protected Iterator<Triple> delegate() { | ||
return filter(triples, not(isManagedTriple)); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
fcrepo-kernel/src/test/java/org/fcrepo/kernel/utils/iterators/UnmanagedRdfStreamTest.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,48 @@ | ||
|
||
package org.fcrepo.kernel.utils.iterators; | ||
|
||
import static com.hp.hpl.jena.graph.NodeFactory.createAnon; | ||
import static com.hp.hpl.jena.graph.Triple.create; | ||
import static org.fcrepo.kernel.RdfLexicon.HAS_CHILD; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
import java.util.Iterator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import com.hp.hpl.jena.graph.Triple; | ||
|
||
public class UnmanagedRdfStreamTest { | ||
|
||
private final static Triple managedTriple = create(createAnon(), HAS_CHILD | ||
.asNode(), createAnon()); | ||
|
||
private final static Triple unManagedTriple = create(createAnon(), | ||
createAnon(), createAnon()); | ||
|
||
@Mock | ||
private Iterator<Triple> mockIterator; | ||
|
||
private UnmanagedRdfStream testStream; | ||
|
||
@Before | ||
public void setUp() { | ||
initMocks(this); | ||
testStream = new UnmanagedRdfStream(mockIterator); | ||
} | ||
|
||
@Test | ||
public void testFiltering() { | ||
when(mockIterator.hasNext()).thenReturn(true, true, false); | ||
when(mockIterator.next()).thenReturn(managedTriple, unManagedTriple); | ||
assertEquals("Didn't get unmanaged triple!", unManagedTriple, | ||
testStream.next()); | ||
assertFalse("Failed to filter managed triple!", testStream.hasNext()); | ||
} | ||
|
||
} |