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.transform.transformations; 019 020import com.hp.hpl.jena.query.QueryExecution; 021import com.hp.hpl.jena.query.ResultSet; 022import org.fcrepo.kernel.api.RdfStream; 023import org.fcrepo.kernel.api.rdf.DefaultRdfStream; 024import org.junit.Before; 025import org.junit.Test; 026import org.mockito.Mock; 027 028import javax.jcr.Node; 029import javax.jcr.RepositoryException; 030import javax.jcr.Session; 031 032import java.io.ByteArrayInputStream; 033import java.io.IOException; 034import java.io.InputStream; 035 036import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; 037import static com.hp.hpl.jena.graph.NodeFactory.createURI; 038import static com.hp.hpl.jena.graph.Triple.create; 039import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty; 040import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource; 041import static java.util.stream.Stream.of; 042import static org.junit.Assert.assertEquals; 043import static org.junit.Assert.assertTrue; 044import static org.mockito.Matchers.any; 045import static org.mockito.Mockito.doThrow; 046import static org.mockito.Mockito.mock; 047import static org.mockito.Mockito.when; 048import static org.mockito.MockitoAnnotations.initMocks; 049 050/** 051 * <p>SparqlQueryTransformTest class.</p> 052 * 053 * @author cbeer 054 */ 055public class SparqlQueryTransformTest { 056 057 @Mock 058 Node mockNode; 059 060 @Mock 061 Session mockSession; 062 063 SparqlQueryTransform testObj; 064 065 @Before 066 public void setUp() throws RepositoryException { 067 initMocks(this); 068 069 when(mockNode.getSession()).thenReturn(mockSession); 070 } 071 072 @Test 073 public void testApply() { 074 final RdfStream model = new DefaultRdfStream(createURI("ttp://example.org/book/book1"), of( 075 create(createResource("http://example.org/book/book1").asNode(), 076 createProperty("http://purl.org/dc/elements/1.1/title").asNode(), 077 createLiteral("some-title")))); 078 final InputStream query = new ByteArrayInputStream(("SELECT ?title WHERE\n" + 079 "{\n" + 080 " <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .\n" + 081 "} ").getBytes()); 082 testObj = new SparqlQueryTransform(query); 083 084 try (final QueryExecution apply = testObj.apply(model)) { 085 assert (apply != null); 086 final ResultSet resultSet = apply.execSelect(); 087 assertTrue(resultSet.hasNext()); 088 assertEquals("some-title", resultSet.nextSolution().get("title").asLiteral().getValue()); 089 } 090 } 091 092 @Test (expected = IllegalStateException.class) 093 public void testApplyException() { 094 final RdfStream model = mock(RdfStream.class); 095 testObj = new SparqlQueryTransform(null); 096 doThrow(IOException.class).when(model).collect(any()); 097 testObj.apply(model); 098 } 099}