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