Skip to content

Commit

Permalink
Created new module sparql, externalized related classes and tests fro…
Browse files Browse the repository at this point in the history
…m module basil. See #5
  • Loading branch information
enridaga committed Feb 20, 2021
1 parent 8bc4d89 commit b2362d5
Show file tree
Hide file tree
Showing 50 changed files with 478 additions and 10 deletions.
5 changes: 5 additions & 0 deletions basil/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
<artifactId>rendering</artifactId>
<version>${basil.version}</version>
</dependency>
<dependency>
<groupId>io.github.basilapi</groupId>
<artifactId>sparql</artifactId>
<version>${basil.version}</version>
</dependency>
</dependencies>

</project>
Expand Down
41 changes: 41 additions & 0 deletions basil/src/test/java/io/github/basilapi/basil/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.basilapi.basil;

import io.github.basilapi.basil.sparql.QueryParameter;
import io.github.basilapi.basil.sparql.Specification;
import io.github.basilapi.basil.sparql.SpecificationFactory;
import io.github.basilapi.basil.sparql.UnknownQueryTypeException;
import org.apache.commons.io.IOUtils;

import java.io.IOException;

public class TestUtils {
//
public static String loadQueryString(String qname) throws IOException {
return IOUtils.toString(TestUtils.class.getClassLoader().getResourceAsStream("./sparql/" + qname + ".txt"),
"UTF-8");
}
//
public static String endpoint(String qname) {
int pos = qname.indexOf("X-Basil-Endpoint:");
int len = ("X-Basil-Endpoint:").length();
int eol = qname.indexOf('\n', pos);
return qname.substring(pos + len, eol).trim();
}
//
public static Specification loadQuery(String fileName) throws IOException {
String sparql = loadQueryString(fileName);
String endpoint = endpoint(sparql);
// System.out.println(endpoint);
try {
return SpecificationFactory.create(endpoint, sparql);
} catch (UnknownQueryTypeException e) {
throw new IOException(e);
}
}


public static String loadTemplate(String type, String qname) throws IOException {
return IOUtils.toString(
TestUtils.class.getClassLoader().getResourceAsStream("./" + type + "/" + qname + ".tmpl"), "UTF-8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.Set;

import io.github.basilapi.basil.sparql.TestUtils;
import io.github.basilapi.basil.TestUtils;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void before() throws IOException {
@Test
public void serializeQueryParameter() throws IOException,
ClassNotFoundException {
QueryParameter param = TestUtils.buildQueryParameter("param1",
QueryParameter param = QueryParameter.buildQueryParameter("param1",
Type.LangedLiteral, "en", null);
store.write(testName.getMethodName(), param, "qp");
QueryParameter param2 = (QueryParameter) store.read(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.basilapi.basil.test;

import io.github.basilapi.basil.sparql.TestUtils;
import io.github.basilapi.basil.TestUtils;
import org.apache.shiro.config.Ini;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.Map;

import io.github.basilapi.basil.sparql.TestUtils;
import io.github.basilapi.basil.TestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.Map;

import io.github.basilapi.basil.sparql.TestUtils;
import io.github.basilapi.basil.TestUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down
5 changes: 3 additions & 2 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@

<!-- github server corresponds to entry in ~/.m2/settings.xml -->
<github.global.server>github</github.global.server>
<localRepo>file://${project.build.directory}/mvn-repo</localRepo>
</properties>

<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
<url>${localRepo}</url>
</repository>
</distributionManagement>

Expand Down Expand Up @@ -294,7 +295,7 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
<altDeploymentRepository>internal.repo::default::${localRepo}</altDeploymentRepository>
</configuration>
</plugin>

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>rendering</module>
<module>basil</module>
<module>server</module>
<module>sparql</module>
</modules>
<profiles>
<profile>
Expand Down
61 changes: 61 additions & 0 deletions sparql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>reactor</artifactId>
<groupId>io.github.basilapi</groupId>
<version>0.8.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sparql</artifactId>
<name>BASIL :: SPARQL</name>


<dependencies>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,19 @@ public void setNumber() {
this.datatype = null;
this.lang = null;
}

public static QueryParameter buildQueryParameter(String name, QueryParameter.Type type, String lang, String datatype) {
QueryParameter qp = new QueryParameter();
qp.setName(name);
if (type == Type.IRI) {
qp.setIri();
} else if (type == Type.TypedLiteral) {
qp.setDatatype(datatype);
} else if (type == Type.LangedLiteral) {
qp.setLang(lang);
} else if (type == Type.PlainLiteral) {
qp.setPlainLiteral();
}
return qp;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.basilapi.basil.sparql;

import org.apache.commons.io.IOUtils;
import io.github.basilapi.basil.rest.core.Headers;
//import io.github.basilapi.basil.rest.core.Headers;
import io.github.basilapi.basil.sparql.QueryParameter.Type;

import java.io.IOException;
Expand All @@ -14,8 +14,8 @@ public static String loadQueryString(String qname) throws IOException {
}

public static String endpoint(String qname) {
int pos = qname.indexOf(Headers.Endpoint + ":");
int len = (Headers.Endpoint + ":").length();
int pos = qname.indexOf("X-Basil-Endpoint:");
int len = ("X-Basil-Endpoint:").length();
int eol = qname.indexOf('\n', pos);
return qname.substring(pos + len, eol).trim();
}
Expand Down
10 changes: 10 additions & 0 deletions sparql/src/test/resources/sparql/ask_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# X-Basil-Endpoint: http://data.open.ac.uk/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX mlo: <http://purl.org/net/mlo/>
PREFIX aiiso: <http://purl.org/vocab/aiiso/schema#>

# Eg value for ?_geoid 2328926
ASK {
BIND(iri(CONCAT('http://sws.geonames.org/',?_geoid,'/')) as ?location) .
?course mlo:location ?location . ?course a aiiso:Module
}
12 changes: 12 additions & 0 deletions sparql/src/test/resources/sparql/construct_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# X-Basil-Endpoint: http://data.open.ac.uk/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX mlo: <http://purl.org/net/mlo/>
PREFIX aiiso: <http://purl.org/vocab/aiiso/schema#>

# Eg value for ?_geoid 2328926
CONSTRUCT {
?course mlo:location ?location . ?course a aiiso:Module
}WHERE{
BIND(iri(CONCAT('http://sws.geonames.org/',?_geoid,'/')) as ?location) .
?course mlo:location ?location . ?course a aiiso:Module
}
7 changes: 7 additions & 0 deletions sparql/src/test/resources/sparql/delete_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>

DELETE DATA
{
<http://example/book2> dc:title ?_title ;
dc:creator ?_creator .
}
10 changes: 10 additions & 0 deletions sparql/src/test/resources/sparql/delete_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

DELETE
{ ?book ?p ?v }
WHERE
{ ?book dc:date ?date .
FILTER ( ?date > ?_date_dateTime )
?book ?p ?v
}
5 changes: 5 additions & 0 deletions sparql/src/test/resources/sparql/delete_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

WITH <http://example/addresses>
DELETE { ?person ?property ?value }
WHERE { ?person ?property ?value ; foaf:givenName ?_givenName ; rdfs:label "$This $is ?not a ?_variable" }
7 changes: 7 additions & 0 deletions sparql/src/test/resources/sparql/delete_insert_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>
DELETE DATA
{ GRAPH <http://example/bookStore> { <http://example/book1> dc:title ?_delete } } ;

PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{ GRAPH <http://example/bookStore> { <http://example/book1> dc:title ?_insert } }
7 changes: 7 additions & 0 deletions sparql/src/test/resources/sparql/delete_insert_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

WITH <http://example/addresses>
DELETE { ?person foaf:givenName ?_delete }
INSERT { ?person foaf:givenName ?_insert }
WHERE
{ ?person foaf:givenName ?_delete }
3 changes: 3 additions & 0 deletions sparql/src/test/resources/sparql/insert_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# X-Basil-Endpoint: http://data.open.ac.uk/sparql
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT { <http://example/egbook> dc:title ?_title } WHERE {}
6 changes: 6 additions & 0 deletions sparql/src/test/resources/sparql/insert_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title ?_title_string ;
dc:creator ?_creator .
}
4 changes: 4 additions & 0 deletions sparql/src/test/resources/sparql/insert_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
INSERT DATA
{ GRAPH <http://example/bookStore> { <http://example/book1> ns:price ?_price_number } }
11 changes: 11 additions & 0 deletions sparql/src/test/resources/sparql/insert_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

INSERT
{ GRAPH ?_target_iri { ?book ?p ?v } }
WHERE
{ GRAPH <http://example/bookStore>
{ ?book dc:date ?date .
FILTER ( ?date > ?_date_dateTime )
?book ?p ?v
} }
11 changes: 11 additions & 0 deletions sparql/src/test/resources/sparql/select_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# X-Basil-Endpoint: http://data.open.ac.uk/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX mlo: <http://purl.org/net/mlo/>
PREFIX aiiso: <http://purl.org/vocab/aiiso/schema#>

# Eg value for ?_geoid 2328926
SELECT ?course FROM <http://data.open.ac.uk/context/course>
WHERE {
BIND(iri(CONCAT('http://sws.geonames.org/',?_geoid,'/')) as ?location) .
?course mlo:location ?location . ?course a aiiso:Module
}
11 changes: 11 additions & 0 deletions sparql/src/test/resources/sparql/select_10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# X-Basil-Endpoint: http://dbpedia.org/sparql

PREFIX some: <http://www.example.com/insert#> .

select ?subject ?label ?nationality where {
?subject a <http://dbpedia.org/ontology/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> ?label ;
<http://dbpedia.org/property/dateOfBirth> ?_year_number ;
<http://dbpedia.org/ontology/nationality> ?nationality .
filter(lang(?label)="en")
}
15 changes: 15 additions & 0 deletions sparql/src/test/resources/sparql/select_11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# X-Basil-Endpoint: http://dbpedia.org/sparql

# a DELETE command should be considered in a comment

select

?insert ?subject ?label ?nationality where { # insert a comment here


?subject a <http://dbpedia.org/ontology/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> ?label ;
<http://dbpedia.org/property/dateOfBirth> ?_year_number ;
<http://dbpedia.org/ontology/nationality> ?nationality .
filter(lang(?label)="en")
}
9 changes: 9 additions & 0 deletions sparql/src/test/resources/sparql/select_12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# X-Basil-Endpoint: http://dbpedia.org/sparql
select ?subject ?label ?nationality where {
?subject a <http://dbpedia.org/ontology/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> ?label ;
<http://dbpedia.org/property/dateOfBirth> 1964 ;
<http://dbpedia.org/ontology/nationality> ?nationality .
filter(lang(?label)="en")
}
LIMIT ?_limit_number
10 changes: 10 additions & 0 deletions sparql/src/test/resources/sparql/select_13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# X-Basil-Endpoint: http://dbpedia.org/sparql
select ?subject ?label ?nationality where {
?subject a <http://dbpedia.org/ontology/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> ?label ;
<http://dbpedia.org/property/dateOfBirth> 1964 ;
<http://dbpedia.org/ontology/nationality> ?nationality .
filter(lang(?label)="en")
}
LIMIT ?_limit_number
OFFSET ?_offset_number
Loading

0 comments on commit b2362d5

Please sign in to comment.