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.utils;
017
018import java.util.stream.Stream;
019
020import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
021import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
022import com.hp.hpl.jena.graph.Graph;
023import com.hp.hpl.jena.graph.Node;
024import com.hp.hpl.jena.graph.Triple;
025import com.hp.hpl.jena.sparql.graph.GraphFactory;
026import org.junit.Test;
027
028import static java.util.stream.Stream.of;
029import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
030import static com.hp.hpl.jena.graph.NodeFactory.createURI;
031import static org.junit.Assert.assertTrue;
032import static org.junit.Assert.assertEquals;
033
034/**
035 * <p>DifferencingIteratorTest class.</p>
036 *
037 * @author ksclarke
038 */
039public class GraphDifferencerTest {
040
041    private final Node subject = createURI("x");
042    private final Triple t_xyz = new Triple(createURI("x"), createURI("y"), createURI("z"));
043    private final Triple t_abc = new Triple(createURI("a"), createURI("b"), createURI("c"));
044    private final Triple t_typed_string = new Triple(createURI("i"),
045                                               createURI("j"),
046                                               createLiteral("k", XSDDatatype.XSDstring));
047    private final Triple t_untyped_string = new Triple(createURI("i"),
048                                                 createURI("j"),
049                                                 createLiteral("k"));
050    private final Triple t_int = new Triple(createURI("i"),
051            createURI("j"),
052            createLiteral("0", XSDDatatype.XSDint));
053    private final Triple t_int_equivalent = new Triple(createURI("i"),
054            createURI("j"),
055            createLiteral("000", XSDDatatype.XSDint));
056
057
058    @Test
059    public void testAllCommon() {
060
061        final Graph graph = GraphFactory.createDefaultGraph();
062        graph.add(t_xyz);
063
064        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_xyz))) {
065            final GraphDifferencer diff = new GraphDifferencer(graph, original);
066
067            final Stream<Triple> removed = diff.difference();
068            final Stream<Triple> added = diff.notCommon();
069            final Stream<Triple> common = diff.common();
070
071            assertEquals(0, removed.count());
072
073            assertEquals(0, added.count());
074
075            assertTrue(common.filter(x -> x.equals(t_xyz)).findFirst().isPresent());
076        }
077    }
078
079    @Test
080    public void testRemoveOne() {
081
082        final Node subject = createURI("subject");
083        final Graph graph = GraphFactory.createDefaultGraph();
084        graph.add(t_xyz);
085
086        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_xyz, t_abc))) {
087            final GraphDifferencer diff = new GraphDifferencer(graph, original);
088
089            final Stream<Triple> removed = diff.difference();
090            final Stream<Triple> added = diff.notCommon();
091            final Stream<Triple> common = diff.common();
092
093            assertTrue(removed.filter(x -> x.equals(t_abc)).findFirst().isPresent());
094
095            assertEquals(0, added.count());
096
097            assertTrue(common.filter(x -> x.equals(t_xyz)).findFirst().isPresent());
098        }
099    }
100
101    @Test
102    public void testAddOne() {
103
104        final Graph graph = GraphFactory.createDefaultGraph();
105        graph.add(t_abc);
106        graph.add(t_xyz);
107
108        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_xyz))) {
109            final GraphDifferencer diff = new GraphDifferencer(graph, original);
110
111            final Stream<Triple> removed = diff.difference();
112            final Stream<Triple> added = diff.notCommon();
113            final Stream<Triple> common = diff.common();
114
115            assertEquals(0, removed.count());
116
117            assertTrue(added.filter(x -> x.equals(t_abc)).findFirst().isPresent());
118
119            assertTrue(common.filter(x -> x.equals(t_xyz)).findFirst().isPresent());
120        }
121    }
122
123    @Test
124    public void testAllDifferent() {
125
126        final Graph graph = GraphFactory.createDefaultGraph();
127        graph.add(t_xyz);
128
129        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_abc))) {
130            final GraphDifferencer diff = new GraphDifferencer(graph, original);
131
132            final Stream<Triple> removed = diff.difference();
133            final Stream<Triple> added = diff.notCommon();
134            final Stream<Triple> common = diff.common();
135
136            assertTrue(removed.filter(x -> x.equals(t_abc)).findFirst().isPresent());
137
138            assertTrue(added.filter(x -> x.equals(t_xyz)).findFirst().isPresent());
139
140            assertEquals(0, common.count());
141        }
142    }
143
144    @Test
145    public void testCommonRDFEqualStrings() {
146
147        final Graph graph = GraphFactory.createDefaultGraph();
148        graph.add(t_untyped_string);
149
150        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_typed_string))) {
151            final GraphDifferencer diff = new GraphDifferencer(graph, original);
152
153            final Stream<Triple> removed = diff.difference();
154            final Stream<Triple> added = diff.notCommon();
155            final Stream<Triple> common = diff.common();
156
157            assertEquals(0, removed.count());
158            assertEquals(0, added.count());
159            assertTrue(common.filter(x -> x.equals(t_typed_string)).findFirst().isPresent());
160        }
161    }
162
163    @Test
164    public void testCommonRDFEqualIntegers() {
165
166        final Graph graph = GraphFactory.createDefaultGraph();
167        graph.add(t_int_equivalent);
168
169        try (final DefaultRdfStream original = new DefaultRdfStream(subject, of(t_int))) {
170            final GraphDifferencer diff = new GraphDifferencer(graph, original);
171
172            final Stream<Triple> removed = diff.difference();
173            final Stream<Triple> added = diff.notCommon();
174            final Stream<Triple> common = diff.common();
175
176            assertEquals(0, removed.count());
177            assertEquals(0, added.count());
178            assertTrue(common.filter(x -> x.equals(t_int)).findFirst().isPresent());
179        }
180    }
181}