001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.api.rdf;
019
020import static java.util.Arrays.asList;
021import static java.util.stream.Collectors.toList;
022import static java.util.stream.Stream.of;
023import static org.apache.jena.graph.NodeFactory.createURI;
024import static org.junit.Assert.assertEquals;
025
026import java.util.List;
027
028import org.apache.jena.graph.Node;
029import org.apache.jena.graph.Triple;
030import org.fcrepo.kernel.api.RdfStream;
031import org.junit.Test;
032
033/**
034 *
035 * @author acoburn
036 */
037public class DefaultRdfStreamTest {
038
039    @Test
040    @SuppressWarnings("static-method")
041    public final void testMap() {
042        final Node subject = createURI("subject");
043        try (final RdfStream stream = new DefaultRdfStream(subject, getTriples(subject).stream())) {
044
045            final List<String> objs = stream.map(Triple::getObject).map(Node::getURI).collect(toList());
046
047            assertEquals(6, objs.size());
048            assertEquals("obj1", objs.get(0));
049            assertEquals("obj2", objs.get(1));
050            assertEquals("obj3", objs.get(2));
051        }
052    }
053
054    @Test
055    @SuppressWarnings("static-method")
056    public final void testFlatMap() {
057        final Node subject = createURI("subject");
058
059        final List<String> objs = of(subject, subject, subject)
060            .flatMap(x -> new DefaultRdfStream(x, getTriples(x).stream()))
061            .map(Triple::getObject)
062            .map(Node::getURI)
063            .collect(toList());
064
065        assertEquals(18, objs.size());
066        assertEquals("obj1", objs.get(0));
067        assertEquals("obj1", objs.get(6));
068    }
069
070    private static List<Triple> getTriples(final Node subject) {
071        final Node prop1 = createURI("prop1");
072        final Node prop2 = createURI("prop2");
073        return asList(
074                new Triple(subject, prop1, createURI("obj1")),
075                new Triple(subject, prop1, createURI("obj2")),
076                new Triple(subject, prop1, createURI("obj3")),
077                new Triple(subject, prop2, createURI("obj1")),
078                new Triple(subject, prop2, createURI("obj2")),
079                new Triple(subject, prop2, createURI("obj3")));
080    }
081}