Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #169 from futures/custom-properties
Add integration test for SPARQL query over previously undefined properties
  • Loading branch information
cbeer committed Nov 26, 2013
2 parents 436ff13 + 3b30e4c commit 1df8cea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
Expand Up @@ -20,7 +20,6 @@
import org.fcrepo.kernel.rdf.impl.DefaultGraphSubjects;
import org.fcrepo.transform.sparql.JQLConverter;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.JcrTools;
Expand Down Expand Up @@ -180,6 +179,13 @@ public void testIt() throws RepositoryException {
" ORDER BY DESC(?subject) ?object LIMIT 10 OFFSET 20";

JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT DISTINCT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_subject].[ns001:bb3652744n] AS object FROM [fedora:resource] AS [fedoraResource_subject] WHERE ((([fedoraResource_subject].[ns001:bb2765355h] = 'bf2765355h' AND [fedoraResource_subject].[ns001:bb3652744n] IS NOT NULL) AND [fedoraResource_subject].[ns001:bb3652744n] LIKE 'r') AND (([fedoraResource_subject].[ns001:bb3652744n] >= 'abc' AND [fedoraResource_subject].[ns001:bb3652744n] < 'efg') OR NOT ([fedoraResource_subject].[ns001:bb3652744n] = 'efg'))) ORDER BY [fedoraResource_subject].[jcr:path] DESC, [fedoraResource_subject].[ns001:bb3652744n] ASC LIMIT 10 OFFSET 20", testObj.getStatement());

final String expectedNs1 = "SELECT DISTINCT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_subject].[ns001:bb3652744n] AS object FROM [fedora:resource] AS [fedoraResource_subject] WHERE ((([fedoraResource_subject].[ns001:bb2765355h] = 'bf2765355h' AND [fedoraResource_subject].[ns001:bb3652744n] IS NOT NULL) AND [fedoraResource_subject].[ns001:bb3652744n] LIKE 'r') AND (([fedoraResource_subject].[ns001:bb3652744n] >= 'abc' AND [fedoraResource_subject].[ns001:bb3652744n] < 'efg') OR NOT ([fedoraResource_subject].[ns001:bb3652744n] = 'efg'))) ORDER BY [fedoraResource_subject].[jcr:path] DESC, [fedoraResource_subject].[ns001:bb3652744n] ASC LIMIT 10 OFFSET 20";
final String expectedNs2 = "SELECT DISTINCT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_subject].[ns002:bb3652744n] AS object FROM [fedora:resource] AS [fedoraResource_subject] WHERE ((([fedoraResource_subject].[ns002:bb2765355h] = 'bf2765355h' AND [fedoraResource_subject].[ns002:bb3652744n] IS NOT NULL) AND [fedoraResource_subject].[ns002:bb3652744n] LIKE 'r') AND (([fedoraResource_subject].[ns002:bb3652744n] >= 'abc' AND [fedoraResource_subject].[ns002:bb3652744n] < 'efg') OR NOT ([fedoraResource_subject].[ns002:bb3652744n] = 'efg'))) ORDER BY [fedoraResource_subject].[jcr:path] DESC, [fedoraResource_subject].[ns002:bb3652744n] ASC LIMIT 10 OFFSET 20";

final String stmt = testObj.getStatement();
if (!expectedNs1.equals(stmt) && !expectedNs2.equals(stmt)) {
assertEquals(expectedNs1, testObj.getStatement());
}
}
}
Expand Up @@ -56,12 +56,16 @@ public class FedoraSparqlIT extends AbstractResourceIT {
@Before
public void setUpTestData() throws RepositoryException {
session = repo.login();
session.setNamespacePrefix("zz", "http://zz.com/");
final ValueFactory valueFactory = session.getValueFactory();
final FedoraResource fedoraResource = new FedoraResource(session, "/abc", JcrConstants.NT_FOLDER);
final FedoraResource fedoraResource2 = new FedoraResource(session, "/xyz", JcrConstants.NT_FOLDER);
final FedoraResource fedoraResource3 = new FedoraResource(session, "/anobject", JcrConstants.NT_FOLDER);

fedoraResource.getNode().setProperty("dc:title", new Value[] { valueFactory.createValue("xyz") });
fedoraResource.getNode().setProperty("fedorarelsext:hasPart", new Value[] { valueFactory.createValue(fedoraResource2.getNode()) });
fedoraResource2.getNode().setProperty("fedorarelsext:isPartOf", new Value[] { valueFactory.createValue(fedoraResource.getNode()) });
fedoraResource3.getNode().setProperty("zz:name", new Value[] { valueFactory.createValue("junk") });
session.save();

}
Expand All @@ -86,16 +90,7 @@ public void itShouldWorkWithSimpleProperties() throws IOException, RepositoryExc

final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?subject WHERE { ?subject dc:title \"xyz\"}";

HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream(sparql));
sparqlRequest.setEntity(entity);
HttpResponse response = client.execute(sparqlRequest);
assertEquals(200, response.getStatusLine().getStatusCode());

String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);

String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));


Expand All @@ -111,16 +106,7 @@ public void itShouldWorkWithReferenceProperties() throws IOException, Repository

final String sparql = "PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#> SELECT ?subject ?part WHERE { ?subject fedorarelsext:hasPart ?part }";

HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream(sparql));
sparqlRequest.setEntity(entity);
HttpResponse response = client.execute(sparqlRequest);
assertEquals(200, response.getStatusLine().getStatusCode());

String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);

String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));


Expand All @@ -141,16 +127,7 @@ public void itShouldWorkWithJoinedQueries() throws IOException, RepositoryExcept
"SELECT ?part ?collectionTitle WHERE { ?part fedorarelsext:isPartOf ?collection .\n" +
" ?collection dc:title ?collectionTitle }";

HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream(sparql));
sparqlRequest.setEntity(entity);
HttpResponse response = client.execute(sparqlRequest);
assertEquals(200, response.getStatusLine().getStatusCode());

String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);

String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));


Expand All @@ -162,4 +139,29 @@ public void itShouldWorkWithJoinedQueries() throws IOException, RepositoryExcept
assertEquals(serverAddress + "/xyz", row.get("part").toString());
assertEquals("xyz", row.get("collectionTitle").asLiteral().getLexicalForm());
}

@Test
public void itShouldIndexCustomProperties() throws Exception {
final String sparql = "PREFIX zz: <http://zz.com/> SELECT ?subject WHERE { ?subject zz:name \"junk\"}";

String content = getResponseContent(sparql);

final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/anobject", resultSet.next().get("subject").toString());
}

private String getResponseContent(final String sparql) throws IOException {
HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream(sparql));
sparqlRequest.setEntity(entity);
HttpResponse response = client.execute(sparqlRequest);
assertEquals(200, response.getStatusLine().getStatusCode());

String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);
return content;
}
}

0 comments on commit 1df8cea

Please sign in to comment.