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