/**
 * 
 */
package com.github.podd.impl.purl.test;

import java.io.InputStream;

import junit.framework.Assert;

import org.junit.Test;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.query.GraphQuery;
import org.openrdf.query.GraphQueryResult;
import org.openrdf.query.QueryLanguage;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.sail.memory.MemoryStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.podd.api.PoddRdfProcessorFactory;
import com.github.podd.api.purl.PoddPurlProcessor;
import com.github.podd.api.purl.PoddPurlProcessorPrefixes;
import com.github.podd.api.test.AbstractPoddRdfProcessorFactoryTest;
import com.github.podd.impl.purl.SimpleUUIDPurlProcessorFactory;
import com.github.podd.utils.PoddRdfUtils;

/**
 * Concrete unit test for SimpleUUIDPurlProcessorFactory
 * 
 * @author kutila
 * 
 */
public class SimpleUUIDPurlProcessorFactoryTest extends AbstractPoddRdfProcessorFactoryTest<PoddPurlProcessor>
{
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Override
    protected PoddRdfProcessorFactory<PoddPurlProcessor> getNewPoddRdfProcessorFactory()
    {
        return new SimpleUUIDPurlProcessorFactory();
    }
    
    /**
     * Test that the SPARQL query which can be constructed based on the parts returned by this
     * factory can correctly execute against a test RDF graph
     * 
     * @throws Exception
     */
    @Test
    public void testSPARQLQueryViaExecution() throws Exception
    {
        final PoddRdfProcessorFactory<PoddPurlProcessor> rdfProcessorFactory = this.getNewPoddRdfProcessorFactory();
        
        // build SPARQL query
        final String sparql = PoddRdfUtils.buildSparqlConstructQuery(rdfProcessorFactory);
        this.log.info("Generated SPARQL <{}> ", sparql);
        
        final Repository repository = new SailRepository(new MemoryStore());
        RepositoryConnection repositoryConnection = null;
        try
        {
            repository.initialize();
            repositoryConnection = repository.getConnection();
            repositoryConnection.setAutoCommit(false);
            
            // load RDF graph into Repository
            final String artifactResourcePath = "/test/artifacts/basicProject-1-internal-object.rdf";
            final InputStream inputStream = this.getClass().getResourceAsStream(artifactResourcePath);
            Assert.assertNotNull("Could not find resource", inputStream);
            
            repositoryConnection.add(inputStream, "", RDFFormat.RDFXML);
            repositoryConnection.commit();
            
            final GraphQuery query = repositoryConnection.prepareGraphQuery(QueryLanguage.SPARQL, sparql);
            
            // verify SPARQL generated a graph as expected
            final GraphQueryResult result = query.evaluate();
            int count = 0;
            while(result.hasNext())
            {
                final Statement statement = result.next();
                count++;
                boolean tempUriFound = false;
                if(statement.getSubject().stringValue().startsWith(PoddPurlProcessorPrefixes.UUID.getTemporaryPrefix())
                        || statement.getObject().stringValue()
                                .startsWith(PoddPurlProcessorPrefixes.UUID.getTemporaryPrefix()))
                {
                    tempUriFound = true;
                    break;
                }
                Assert.assertTrue("Graph generated by SPARQL contains statement without temporary URIs: ", tempUriFound);
            }
            Assert.assertEquals("SPARQL query did not match expected number of statements", 19, count);
            result.close();
            repositoryConnection.rollback();
            repositoryConnection.close();
        }
        finally
        {
            if(repositoryConnection != null)
            {
                repositoryConnection.close();
            }
            repository.shutDown();
        }
    }
    
    @Test
    public void testSPARQLQueryWithSubjectViaExecution() throws Exception
    {
        final PoddRdfProcessorFactory<PoddPurlProcessor> rdfProcessorFactory = this.getNewPoddRdfProcessorFactory();
        
        // build SPARQL query
        final URI subject = ValueFactoryImpl.getInstance().createURI("urn:temp:object:2966");
        final String sparql = PoddRdfUtils.buildSparqlConstructQuery(rdfProcessorFactory, subject);
        this.log.info("Generated SPARQL <{}> ", sparql);
        
        // verify SPARQL generated a graph as expected
        final Repository repository = new SailRepository(new MemoryStore());
        RepositoryConnection repositoryConnection = null;
        try
        {
            repository.initialize();
            repositoryConnection = repository.getConnection();
            repositoryConnection.setAutoCommit(false);
            
            // load RDF graph into Repository
            final String artifactResourcePath = "/test/artifacts/basicProject-1-internal-object.rdf";
            final InputStream inputStream = this.getClass().getResourceAsStream(artifactResourcePath);
            Assert.assertNotNull("Could not find resource", inputStream);
            
            repositoryConnection.add(inputStream, "", RDFFormat.RDFXML);
            repositoryConnection.commit();
            
            final GraphQuery query = repositoryConnection.prepareGraphQuery(QueryLanguage.SPARQL, sparql);
            
            // verify SPARQL generated a graph as expected
            final GraphQueryResult result = query.evaluate();
            int count = 0;
            while(result.hasNext())
            {
                final Statement statement = result.next();
                Assert.assertEquals("Subject was not what was requested", subject, statement.getSubject());
                count++;
            }
            Assert.assertEquals("SPARQL query did not match expected number of statements", 13, count);
            result.close();
            repositoryConnection.rollback();
            repositoryConnection.close();
        }
        finally
        {
            if(repositoryConnection != null)
            {
                repositoryConnection.close();
            }
            repository.shutDown();
        }
    }
    
    private void checkQueryWithRepository() throws Exception
    {
        // TODO: extract common content from above two test methods to here
    }
}

@@ -3604,26 +3604,37 @@
    t
-empUriFound = true
+his.log.info(%22%7B%7D%22, statement)
 ;%0A  
@@ -3655,13 +3655,27 @@
     
-break
+tempUriFound = true
 ;%0A  
@@ -4581,16 +4581,21 @@
 rn:temp:
+uuid:
 object:2
@@ -5983,32 +5983,80 @@
  result.next();%0A
+                this.log.info(%22%7B%7D%22, statement);%0A
                 

fix tests