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