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