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.iterators; 017 018import static com.hp.hpl.jena.graph.NodeFactory.createAnon; 019import static com.hp.hpl.jena.graph.Triple.create; 020import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel; 021import static java.util.Collections.singletonList; 022import static java.util.Collections.singletonMap; 023import static org.fcrepo.kernel.api.utils.iterators.RdfStream.fromModel; 024import static org.junit.Assert.assertEquals; 025import static org.junit.Assert.assertNotEquals; 026import static org.junit.Assert.assertFalse; 027import static org.junit.Assert.assertTrue; 028import static org.mockito.Mockito.mock; 029import static org.mockito.Mockito.verify; 030import static org.mockito.Mockito.when; 031import static org.mockito.MockitoAnnotations.initMocks; 032 033import java.util.Iterator; 034import java.util.Map; 035 036import javax.jcr.Session; 037 038import org.junit.Before; 039import org.junit.Test; 040import org.mockito.Mock; 041import com.google.common.collect.ImmutableMap; 042import com.google.common.collect.ImmutableSet; 043import com.hp.hpl.jena.graph.Node; 044import com.hp.hpl.jena.graph.NodeFactory; 045import com.hp.hpl.jena.graph.Triple; 046import com.hp.hpl.jena.rdf.model.Model; 047 048/** 049 * <p>RdfStreamTest class.</p> 050 * 051 * @author ajs6f 052 */ 053public class RdfStreamTest { 054 055 private RdfStream testStream; 056 057 @Mock 058 private Iterator<Triple> mockIterator; 059 060 @Mock 061 private Iterator<Triple> differentMockIterator; 062 063 @Mock 064 private Iterable<Triple> mockIterable; 065 066 @Mock 067 private Triple triple1; 068 069 @Mock 070 private Triple triple2; 071 072 @Mock 073 private Triple triple3; 074 075 private static final Triple triple = create(createAnon(), createAnon(), 076 createAnon()); 077 078 private final static String prefix1 = "testNS"; 079 080 private final static String uri1 = "http://testNS"; 081 082 private final static String prefix2 = "testNS2"; 083 084 private final static String uri2 = "http://testNS2"; 085 086 private final Map<String, String> testNamespaces = ImmutableMap.of(prefix1, 087 uri1, prefix2, uri2); 088 089 @Before 090 public void setUp() { 091 initMocks(this); 092 when(mockIterable.iterator()).thenReturn(mockIterator); 093 testStream = new RdfStream(mockIterable); 094 } 095 096 @Test 097 public void testHasNext() { 098 when(mockIterator.hasNext()).thenReturn(true, false); 099 assertTrue(testStream.hasNext()); 100 assertFalse(testStream.hasNext()); 101 } 102 103 @Test 104 public void testNext() { 105 when(mockIterator.next()).thenReturn(triple1, triple2); 106 assertEquals(triple1, testStream.next()); 107 assertEquals(triple2, testStream.next()); 108 } 109 110 @Test 111 public void testRemove() { 112 testStream.remove(); 113 verify(mockIterator).remove(); 114 } 115 116 @Test 117 public void testIterator() { 118 assertEquals(testStream, testStream); 119 } 120 121 @Test 122 public void testDefaultConstructor() { 123 assertFalse(new RdfStream().hasNext()); 124 } 125 126 @Test 127 public void testIteratorConstructor() { 128 testStream = new RdfStream(ImmutableSet.of(triple1, triple2).iterator()); 129 assertEquals(triple1, testStream.next()); 130 } 131 132 @Test 133 public void testCollectionConstructor() { 134 testStream = new RdfStream(ImmutableSet.of(triple1, triple2)); 135 assertEquals(triple1, testStream.next()); 136 } 137 138 @Test 139 public void testVarargsConstructor() { 140 testStream = new RdfStream(triple1, triple2); 141 assertEquals(triple1, testStream.next()); 142 } 143 144 @Test 145 public void testWithThisContextIterator() { 146 testStream.namespace(prefix1, uri1); 147 testStream.topic(NodeFactory.createAnon()); 148 final RdfStream testStream2 = 149 testStream.withThisContext((Iterator<Triple>) new RdfStream()); 150 assertEquals(testStream.namespaces(), testStream2.namespaces()); 151 assertEquals(testStream.topic(), testStream2.topic()); 152 } 153 154 @Test 155 public void testWithThisContextIterable() { 156 testStream.namespace(prefix1, uri1); 157 testStream.topic(createAnon()); 158 final RdfStream testStream2 = testStream.withThisContext(new RdfStream().iterable()); 159 assertEquals(testStream.namespaces(), testStream2.namespaces()); 160 assertEquals(testStream.topic(), testStream2.topic()); 161 } 162 163 @Test 164 public void testConcat() { 165 when(mockIterator.next()).thenReturn(triple1, triple2); 166 final RdfStream testStream2 = new RdfStream(ImmutableSet.of(triple3)); 167 testStream.concat(testStream2); 168 assertEquals(triple3, testStream.next()); 169 } 170 171 @Test 172 public void testCollectionConcat() { 173 when(mockIterator.next()).thenReturn(triple1, triple2); 174 testStream.concat(ImmutableSet.of(triple3)); 175 assertEquals(triple3, testStream.next()); 176 } 177 178 @Test 179 public void testVarargsConcat() { 180 when(mockIterator.next()).thenReturn(triple1, triple2); 181 testStream.concat(new Triple[]{triple3}); 182 assertEquals(triple3, testStream.next()); 183 } 184 185 @Test 186 public void testSingletonConcat() { 187 when(mockIterator.next()).thenReturn(triple1, triple2); 188 testStream.concat(triple3); 189 assertEquals(triple3, testStream.next()); 190 } 191 192 @Test 193 public void testAddNamespace() { 194 testStream.namespace(prefix1, uri1); 195 assertTrue(testStream.namespaces().containsKey(prefix1)); 196 assertTrue(testStream.namespaces().containsValue(uri1)); 197 } 198 199 @Test 200 public void testAddNamespaces() { 201 testStream.namespaces(testNamespaces); 202 assertTrue(testStream.namespaces().containsKey(prefix1)); 203 assertTrue(testStream.namespaces().containsValue(uri1)); 204 assertTrue(testStream.namespaces().containsKey(prefix2)); 205 assertTrue(testStream.namespaces().containsValue(uri2)); 206 } 207 208 @Test 209 public void testAsModel() { 210 testStream = new RdfStream(singletonList(triple)); 211 testStream.namespaces(testNamespaces); 212 213 final Model testModel = testStream.asModel(); 214 assertEquals(testModel.getNsPrefixMap(), testNamespaces); 215 assertTrue(testModel.contains(testModel.asStatement(triple))); 216 } 217 218 @Test 219 public void testFromModel() { 220 final Model model = createDefaultModel(); 221 model.setNsPrefix(prefix1, uri1); 222 testStream = fromModel(model.add(model.asStatement(triple))); 223 assertEquals("Didn't find triple in stream from Model!", triple, 224 testStream.next()); 225 assertEquals("Didn't find namespace mapping in stream from Model!", 226 singletonMap(prefix1, uri1), testStream.namespaces()); 227 } 228 229 @Test 230 public void testLimit() { 231 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 232 when(mockIterator.next()).thenReturn(triple); 233 testStream = testStream.limit(2); 234 testStream.next(); 235 testStream.next(); 236 assertFalse(testStream.hasNext()); 237 assertEquals(testStream, testStream.limit(0)); 238 setUp(); 239 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 240 when(mockIterator.next()).thenReturn(triple); 241 assertEquals(testStream, testStream.limit(-1)); 242 } 243 244 @Test 245 public void testSkip() { 246 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 247 testStream = testStream.skip(3); 248 assertFalse(testStream.hasNext()); 249 setUp(); 250 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 251 assertEquals(testStream, testStream.skip(0)); 252 } 253 254 @Test 255 public void testFilter() { 256 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 257 when(mockIterator.next()).thenReturn(triple1, triple2, triple); 258 testStream = testStream.filter(p -> p.equals(triple)); 259 assertEquals(triple, testStream.next()); 260 } 261 262 @Test 263 public void testTransform() { 264 265 final String oneResult = "One result"; 266 267 final String otherResult = "The other result"; 268 269 when(mockIterator.hasNext()).thenReturn(true, true, true, false); 270 when(mockIterator.next()).thenReturn(triple1, triple2, triple); 271 final Iterator<String> testIterator = testStream.transform( 272 x -> x.equals(triple) ? oneResult : otherResult); 273 assertEquals(otherResult, testIterator.next()); 274 assertEquals(otherResult, testIterator.next()); 275 assertEquals(oneResult, testIterator.next()); 276 } 277 278 @Test 279 public void testCanContinue() { 280 when(mockIterator.hasNext()).thenReturn(true).thenThrow( 281 new RuntimeException("Expected.")).thenReturn(true); 282 assertTrue(mockIterator.hasNext()); 283 try { 284 mockIterator.hasNext(); 285 } catch (final RuntimeException ex) { 286 } 287 assertTrue("Couldn't continue with iteration!",mockIterator.hasNext()); 288 } 289 290 @Test 291 public void testEquals() { 292 assertNotEquals(testStream,new Object()); 293 RdfStream testStreamToCompare = new RdfStream(mockIterator); 294 assertEquals(testStream, testStreamToCompare); 295 testStreamToCompare.namespace(prefix1, uri1); 296 assertNotEquals(testStream, testStreamToCompare); 297 when(differentMockIterator.hasNext()).thenReturn(true, false); 298 when(differentMockIterator.next()).thenReturn(triple); 299 testStreamToCompare = new RdfStream(differentMockIterator); 300 assertNotEquals(testStream, testStreamToCompare); 301 } 302 303 @Test 304 public void testHashCode() { 305 final RdfStream testStreamToCompare = new RdfStream(mockIterator); 306 testStreamToCompare.namespace(prefix1, uri1); 307 assertNotEquals(testStream.hashCode(), testStreamToCompare.hashCode()); 308 } 309 310 @Test 311 public void testContext() { 312 final Session mockSession = mock(Session.class); 313 assertEquals("Didn't retrieve the session we stored!", mockSession, 314 testStream.session(mockSession).session()); 315 } 316 317 @Test 318 public void testTopic() { 319 final Node mockNode = mock(Node.class); 320 assertEquals("Didn't retrieve the session we stored!", mockNode, 321 testStream.topic(mockNode).topic()); 322 } 323}