/**
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.integration;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSet;

import org.fcrepo.kernel.api.models.Container;
import org.fcrepo.kernel.api.services.ContainerService;
import org.fcrepo.kernel.api.utils.iterators.RdfStream;
import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext;
import org.fcrepo.transform.transformations.SparqlQueryTransform;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertFalse;


/**
 * <p>SparqlQueryTransformIT class.</p>
 *
 * @author cbeer
 * @author ajs6f
 * @author awoods
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-test/master.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class SparqlQueryTransformIT extends AbstractResourceIT {

    @Inject
    private ContainerService containerService;

    @Inject
    private Repository repo;

    private SparqlQueryTransform testObj;

    @Test
    public void testQueryNoPrimaryType() throws RepositoryException, IOException {
        final Session session = repo.login();
        try {
            final Container object = containerService.findOrCreate(session, "/testObject-" + randomUUID());

            final String s = "SELECT ?x ?type\n" +
                    "WHERE { ?x  <" + REPOSITORY_NAMESPACE + "primaryType> ?type }";
            try (final InputStream stringReader = new ByteArrayInputStream(s.getBytes())) {

                testObj = new SparqlQueryTransform(stringReader);

                final RdfStream stream = object.getTriples(new DefaultIdentifierTranslator(session),
                        PropertiesRdfContext.class);
                try (final QueryExecution qexec = testObj.apply(stream)) {
                    final ResultSet results = requireNonNull(qexec).execSelect();
                    assertFalse(requireNonNull(results).hasNext());
                }
            }
        } finally {
            session.logout();
        }
    }

    @Test
    public void testQueryNoUUID() throws RepositoryException, IOException {
        final Session session = repo.login();
        try {
            final Container object = containerService.findOrCreate(session, "/testObject-" + randomUUID());

            final String s = "SELECT ?x ?uuid\n" +
                    "WHERE { ?x  <" + REPOSITORY_NAMESPACE + "uuid> ?uuid }";
            try (final InputStream stringReader = new ByteArrayInputStream(s.getBytes())) {

                testObj = new SparqlQueryTransform(stringReader);

                final RdfStream stream = object.getTriples(new DefaultIdentifierTranslator(session),
                        PropertiesRdfContext.class);
                try (final QueryExecution qexec = testObj.apply(stream)) {
                    final ResultSet results = requireNonNull(qexec).execSelect();
                    assertFalse(requireNonNull(results).hasNext());
                }
            }
        } finally {
            session.logout();
        }
    }
}

@@ -1002,32 +1002,92 @@
 tiesRdfContext;%0A
+import org.fcrepo.kernel.modeshape.rdf.impl.TypeRdfContext;%0A
 import org.fcrep
@@ -1136,16 +1136,16 @@
 nsform;%0A
-
 %0Aimport 
@@ -1829,16 +1829,78 @@
 ESPACE;%0A
+import static org.fcrepo.kernel.api.RdfLexicon.RDF_NAMESPACE;%0A
 import s
@@ -1935,16 +1935,59 @@
 tFalse;%0A
+import static org.junit.Assert.assertTrue;%0A
 %0A%0A/**%0A *
@@ -2475,17 +2475,11 @@
 uery
-NoPrimary
+Rdf
 Type
@@ -2778,33 +2778,26 @@
 %7B ?x  %3C%22 + R
-EPOSITORY
+DF
 _NAMESPACE +
@@ -2802,16 +2802,9 @@
  + %22
-primaryT
+t
 ype%3E
@@ -3095,34 +3095,28 @@
             
-Properties
+Type
 RdfContext.c
@@ -3297,36 +3297,35 @@
           assert
-Fals
+Tru
 e(requireNonNull

update test with a more meaningful property check