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.http.responses;
019
020import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
021import static com.hp.hpl.jena.graph.NodeFactory.createURI;
022import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
023import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
024import static javax.ws.rs.core.MediaType.valueOf;
025import static org.apache.jena.riot.WebContent.contentTypeResultsXML;
026import static org.fcrepo.kernel.api.RdfLexicon.JCR_NAMESPACE;
027import static org.fcrepo.kernel.modeshape.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
028import static org.junit.Assert.assertEquals;
029import static org.junit.Assert.assertFalse;
030import static org.junit.Assert.assertTrue;
031import static org.mockito.Mockito.mock;
032import java.io.ByteArrayOutputStream;
033import java.lang.reflect.Type;
034
035import javax.ws.rs.WebApplicationException;
036import javax.ws.rs.core.MultivaluedMap;
037
038import org.junit.Test;
039import org.junit.runner.RunWith;
040import org.mockito.Mock;
041import org.mockito.runners.MockitoJUnitRunner;
042
043import com.hp.hpl.jena.graph.Triple;
044import com.hp.hpl.jena.query.Dataset;
045import com.hp.hpl.jena.query.Query;
046import com.hp.hpl.jena.query.QueryExecution;
047import com.hp.hpl.jena.query.QueryExecutionFactory;
048import com.hp.hpl.jena.query.QueryFactory;
049import com.hp.hpl.jena.sparql.core.DatasetImpl;
050
051/**
052 * <p>QueryExecutionProviderTest class.</p>
053 *
054 * @author cbeer
055 */
056@RunWith(MockitoJUnitRunner.class)
057public class QueryExecutionProviderTest {
058
059    @Mock
060    private MultivaluedMap<String, Object> mockMultivaluedMap;
061
062    final QueryExecutionProvider testObj = new QueryExecutionProvider();
063
064    Dataset testData = new DatasetImpl(createDefaultModel());
065
066    {
067        testData.asDatasetGraph().getDefaultGraph().add(
068                new Triple(createURI("test:subject"),
069                        createURI("test:predicate"),
070                        createLiteral("test:object")));
071        testData.asDatasetGraph().getDefaultGraph().add(
072                new Triple(createURI("test:subject"),
073                        createURI(getRDFNamespaceForJcrNamespace(JCR_NAMESPACE) + "primaryType"),
074                        createLiteral("nt:file")));
075
076    }
077
078    @Test
079    public void testWriteTo() throws WebApplicationException,
080            IllegalArgumentException {
081
082        final Query sparqlQuery =
083            QueryFactory.create("SELECT ?x WHERE { ?x ?y ?z }");
084
085        try (final QueryExecution testResult =
086                QueryExecutionFactory.create(sparqlQuery, testData)) {
087
088            final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
089
090            testObj.writeTo(testResult, QueryExecution.class, mock(Type.class),
091                    null, valueOf(contentTypeResultsXML),
092                    mockMultivaluedMap, outStream);
093            final byte[] results = outStream.toByteArray();
094            assertTrue("Got no output from serialization!", results.length > 0);
095            assertTrue("Couldn't find test RDF-object mentioned!", new String(
096                    results).contains("test:subject"));
097        }
098    }
099
100    @Test
101    public void testGetSize() {
102        assertEquals("Returned wrong size from QueryExecutionProvider!",
103                testObj.getSize(null, null, null, null, null), -1);
104
105    }
106
107    @Test
108    public void testIsWritable() {
109        assertTrue(
110                "Gave false response to QueryExecutionProvider.isWriteable() that contained a legitimate combination " +
111                        "of parameters!",
112                testObj.isWriteable(QueryExecution.class, QueryExecution.class,
113                                    null, valueOf(contentTypeResultsXML)));
114        assertFalse(
115                "RdfProvider.isWriteable() should return false if asked to serialize anything other than " +
116                        "QueryExecution!",
117                testObj.isWriteable(QueryExecutionProvider.class,
118                                    QueryExecutionProvider.class, null,
119                                    valueOf(contentTypeResultsXML)));
120        assertFalse(
121                "RdfProvider.isWriteable() should return false to text/html!",
122                testObj.isWriteable(QueryExecution.class, QueryExecution.class,
123                        null, TEXT_HTML_TYPE));
124
125    }
126}