001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.api.rdf;
007
008import static java.util.Arrays.asList;
009import static java.util.stream.Collectors.toList;
010import static java.util.stream.Stream.of;
011import static org.apache.jena.graph.NodeFactory.createURI;
012import static org.junit.Assert.assertEquals;
013
014import java.util.List;
015
016import org.apache.jena.graph.Node;
017import org.apache.jena.graph.Triple;
018import org.fcrepo.kernel.api.RdfStream;
019import org.junit.Test;
020
021/**
022 *
023 * @author acoburn
024 */
025public class DefaultRdfStreamTest {
026
027    @Test
028    @SuppressWarnings("static-method")
029    public final void testMap() {
030        final Node subject = createURI("subject");
031        try (final RdfStream stream = new DefaultRdfStream(subject, getTriples(subject).stream())) {
032
033            final List<String> objs = stream.map(Triple::getObject).map(Node::getURI).collect(toList());
034
035            assertEquals(6, objs.size());
036            assertEquals("obj1", objs.get(0));
037            assertEquals("obj2", objs.get(1));
038            assertEquals("obj3", objs.get(2));
039        }
040    }
041
042    @Test
043    @SuppressWarnings("static-method")
044    public final void testFlatMap() {
045        final Node subject = createURI("subject");
046
047        final List<String> objs = of(subject, subject, subject)
048            .flatMap(x -> new DefaultRdfStream(x, getTriples(x).stream()))
049            .map(Triple::getObject)
050            .map(Node::getURI)
051            .collect(toList());
052
053        assertEquals(18, objs.size());
054        assertEquals("obj1", objs.get(0));
055        assertEquals("obj1", objs.get(6));
056    }
057
058    private static List<Triple> getTriples(final Node subject) {
059        final Node prop1 = createURI("prop1");
060        final Node prop2 = createURI("prop2");
061        return asList(
062                new Triple(subject, prop1, createURI("obj1")),
063                new Triple(subject, prop1, createURI("obj2")),
064                new Triple(subject, prop1, createURI("obj3")),
065                new Triple(subject, prop2, createURI("obj1")),
066                new Triple(subject, prop2, createURI("obj2")),
067                new Triple(subject, prop2, createURI("obj3")));
068    }
069}