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