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