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.integration;
019
020import com.hp.hpl.jena.query.QueryExecution;
021import com.hp.hpl.jena.query.ResultSet;
022
023import org.fcrepo.kernel.api.RdfStream;
024import org.fcrepo.kernel.api.models.Container;
025import org.fcrepo.kernel.api.services.ContainerService;
026import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
027import org.fcrepo.transform.transformations.SparqlQueryTransform;
028
029import org.junit.Test;
030import org.junit.runner.RunWith;
031import org.springframework.test.annotation.DirtiesContext;
032import org.springframework.test.annotation.DirtiesContext.ClassMode;
033import org.springframework.test.context.ContextConfiguration;
034import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
035
036import javax.inject.Inject;
037import javax.jcr.Repository;
038import javax.jcr.RepositoryException;
039import javax.jcr.Session;
040
041import java.io.ByteArrayInputStream;
042import java.io.IOException;
043import java.io.InputStream;
044
045import static java.util.Objects.requireNonNull;
046import static java.util.UUID.randomUUID;
047import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES;
048import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
049import static org.fcrepo.kernel.api.RdfLexicon.RDF_NAMESPACE;
050import static org.junit.Assert.assertFalse;
051import static org.junit.Assert.assertTrue;
052
053
054/**
055 * <p>SparqlQueryTransformIT class.</p>
056 *
057 * @author cbeer
058 * @author ajs6f
059 * @author awoods
060 */
061@RunWith(SpringJUnit4ClassRunner.class)
062@ContextConfiguration({"/spring-test/master.xml"})
063@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
064public class SparqlQueryTransformIT extends AbstractResourceIT {
065
066    @Inject
067    private ContainerService containerService;
068
069    @Inject
070    private Repository repo;
071
072    private SparqlQueryTransform testObj;
073
074    @Test
075    public void testQueryRdfType() throws RepositoryException, IOException {
076        final Session session = repo.login();
077        try {
078            final Container object = containerService.findOrCreate(session, "/testObject-" + randomUUID());
079
080            final String s = "SELECT ?x ?type\n" +
081                    "WHERE { ?x  <" + RDF_NAMESPACE + "type> ?type }";
082            try (final InputStream stringReader = new ByteArrayInputStream(s.getBytes())) {
083
084                testObj = new SparqlQueryTransform(stringReader);
085
086                final RdfStream stream = object.getTriples(new DefaultIdentifierTranslator(session),
087                        PROPERTIES);
088                try (final QueryExecution qexec = testObj.apply(stream)) {
089                    final ResultSet results = requireNonNull(qexec).execSelect();
090                    assertTrue(requireNonNull(results).hasNext());
091                }
092            }
093        } finally {
094            session.logout();
095        }
096    }
097
098    @Test
099    public void testQueryNoUUID() throws RepositoryException, IOException {
100        final Session session = repo.login();
101        try {
102            final Container object = containerService.findOrCreate(session, "/testObject-" + randomUUID());
103
104            final String s = "SELECT ?x ?uuid\n" +
105                    "WHERE { ?x  <" + REPOSITORY_NAMESPACE + "uuid> ?uuid }";
106            try (final InputStream stringReader = new ByteArrayInputStream(s.getBytes())) {
107
108                testObj = new SparqlQueryTransform(stringReader);
109
110                final RdfStream stream = object.getTriples(new DefaultIdentifierTranslator(session),
111                        PROPERTIES);
112                try (final QueryExecution qexec = testObj.apply(stream)) {
113                    final ResultSet results = requireNonNull(qexec).execSelect();
114                    assertFalse(requireNonNull(results).hasNext());
115                }
116            }
117        } finally {
118            session.logout();
119        }
120    }
121}