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