# HG changeset patch # User Kallirroi Dogani # Date 1365260736 -10800 # Node ID 34a6c207f0d38db8cc8db1596dfce26308078e03 # Parent 20ead981bfef818043ea7e1d6870441ea5ec383a changed StrabonEndpoint to SPARQLEndpoint, added SpatialEndpoint to support KML format for all spatial endpoints (Virtuoso, Parliament, Strabon), added tests for SpatialEndpoint, tests for SPARQLEndpoint with Parliament and Virtuoso. Tests need to be added for Virtuoso endpoint to test which formats are not supported diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/pom.xml --- a/endpoint-client/pom.xml Sat Apr 06 15:55:32 2013 +0300 +++ b/endpoint-client/pom.xml Sat Apr 06 18:05:36 2013 +0300 @@ -37,6 +37,12 @@ junit test + + org.openrdf.sesame + + sesame-queryresultio-spatial-sparqlkml + + diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/Endpoint.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/Endpoint.java Sat Apr 06 15:55:32 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2013, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package eu.earthobservatory.org.StrabonEndpoint.client; - -import java.io.IOException; -import java.net.URL; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.rio.RDFFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public interface Endpoint { - /** - * Executes a SPARQL query on the Endpoint and get the results - * in the format specified by T. Format T - * should be an instance of class (or a subclass) {@link TupleQueryResultFormat}. - * - * @param sparqlQuery - * @param format - * @return - * @throws IOException - */ - public EndpointResult query(String sparqlQuery, T format) throws IOException; - - /** - * Stores the RDF data which are in the RDF format - * format in the named graph specified by the URL - * namedGraph. - * - * @param data - * @param format - * @param namedGraph - * @return true if store was successful, false otherwise - */ - public boolean store(String data, RDFFormat format, URL namedGraph); - - /** - * Stores the RDF data located at data which are in the - * RDF format format in the named graph specified by the - * URL namedGraph. - * - * @param data - * @param format - * @param namedGraph - * @return true if store was successful, false otherwise - */ - public boolean store(URL data, RDFFormat format, URL namedGraph); - - /** - * Executes the SPARQL Update query specified in sparqlUpdate. - * - * @param sparqlUpdate - * @return true if store was successful, false otherwise - */ - public boolean update(String sparqlUpdate); - - public EndpointResult describe(String sparqlDescribe); - - public EndpointResult construct(String sparqlConstruct); - - public EndpointResult ask(String sparqlAsk); -} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/EndpointResult.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/EndpointResult.java Sat Apr 06 15:55:32 2013 +0300 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/EndpointResult.java Sat Apr 06 18:05:36 2013 +0300 @@ -13,23 +13,27 @@ * @author Charalampos Nikolaou * */ -public interface EndpointResult { +public class EndpointResult { - /** - * Returns the HTTP status code as returned by the endpoint. - * @return - */ - public int getStatusCode(); + private int statusCode; + private String statusText; + private String response; - /** - * Returns the status text corresponding to the status code. - * @return - */ - public String getStatusText(); + public EndpointResult(int statusCode, String statusLine, String response) { + this.statusCode = statusCode; + this.statusText = statusLine; + this.response = response; + } - /** - * Returns the response of the endpoint. - * @return - */ - public String getResponse(); + public int getStatusCode() { + return statusCode; + } + + public String getStatusText() { + return statusText; + } + + public String getResponse() { + return response; + } } diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/HTTPClient.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/HTTPClient.java Sat Apr 06 15:55:32 2013 +0300 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/HTTPClient.java Sat Apr 06 18:05:36 2013 +0300 @@ -33,7 +33,7 @@ /** * The name of the endpoint. * - * This is useful for {@link StrabonEndpoint} instances that are usually + * This is useful for {@link SPARQLEndpoint} instances that are usually * deployed in a tomcat container as web applications. */ protected String endpointName; diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java Sat Apr 06 15:55:32 2013 +0300 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java Sat Apr 06 18:05:36 2013 +0300 @@ -3,23 +3,39 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * Copyright (C) 2013, Pyravlos Team + * Copyright (C) 2012, Pyravlos Team * * http://www.strabon.di.uoa.gr/ */ package eu.earthobservatory.org.StrabonEndpoint.client; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.message.BasicNameValuePair; import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; import org.openrdf.rio.RDFFormat; /** + * This class is the implementation of a java client for accessing + * SPARQLEndpoint instances. + * * @author Charalampos Nikolaou - * + * @author Kallirroi Dogani { +public class SPARQLEndpoint extends HTTPClient{ public SPARQLEndpoint(String host, int port) { super(host, port); @@ -29,38 +45,186 @@ super(host, port, endpointName); } - @Override - public EndpointResult query(String sparqlQuery, TupleQueryResultFormat format) throws IOException { - throw new UnsupportedOperationException(); + + /** + * Executes a SPARQL query on the Endpoint and get the results + * in the format specified by stSPARQLQueryResultFormat, which is + * an instance of class (or a subclass) {@link TupleQueryResultFormat}. + * + * @param sparqlQuery + * @param format + * @return + * @throws IOException + */ + public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException { + assert(format != null); + + // create a post method to execute + HttpPost method = new HttpPost(getConnectionURL()); + + // set the query parameter + List params = new ArrayList(); + params.add(new BasicNameValuePair("query", sparqlQuery)); + UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset()); + method.setEntity(encodedEntity); + + // set the content type + method.setHeader("Content-Type", "application/x-www-form-urlencoded"); + + // set the accept format + method.addHeader("Accept", format.getDefaultMIMEType()); + + try { + // response that will be filled next + String responseBody = ""; + + // execute the method + HttpResponse response = hc.execute(method); + int statusCode = response.getStatusLine().getStatusCode(); + + // If the response does not enclose an entity, there is no need + // to worry about connection release + HttpEntity entity = response.getEntity(); + if (entity != null) { + InputStream instream = entity.getContent(); + try { + + BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); + StringBuffer strBuf = new StringBuffer(); + + // do something useful with the response + String nextLine; + while ((nextLine = reader.readLine()) != null) { + strBuf.append(nextLine + "\n"); + } + + // remove last newline character + if (strBuf.length() > 0) { + strBuf.setLength(strBuf.length() - 1); + } + + responseBody = strBuf.toString(); + + } catch (IOException ex) { + // In case of an IOException the connection will be released + // back to the connection manager automatically + throw ex; + + } catch (RuntimeException ex) { + // In case of an unexpected exception you may want to abort + // the HTTP request in order to shut down the underlying + // connection and release it back to the connection manager. + method.abort(); + throw ex; + + } finally { + // Closing the input stream will trigger connection release + instream.close(); + } + } + + return new EndpointResult(statusCode, response.getStatusLine().getReasonPhrase(), responseBody); + + } catch (IOException e) { + throw e; + + } finally { + // release the connection. + method.releaseConnection(); + } } - @Override + /** + * Stores the RDF data which are in the RDF format + * format in the named graph specified by the URL + * namedGraph. + * + * @param data + * @param format + * @param namedGraph + * @return true if store was successful, false otherwise + */ + public boolean store(String data, RDFFormat format, URL namedGraph) { throw new UnsupportedOperationException(); } - @Override + /** + * Stores the RDF data located at data which are in the + * RDF format format in the named graph specified by the + * URL namedGraph. + * + * @param data + * @param format + * @param namedGraph + * @return true if store was successful, false otherwise + */ + public boolean store(URL data, RDFFormat format, URL namedGraph) { throw new UnsupportedOperationException(); } - @Override + /** + * Executes the SPARQL Update query specified in sparqlUpdate. + * + * @param sparqlUpdate + * @return true if store was successful, false otherwise + */ + public boolean update(String sparqlUpdate) { throw new UnsupportedOperationException(); } - @Override public EndpointResult describe(String sparqlDescribe) { throw new UnsupportedOperationException(); } - @Override public EndpointResult construct(String sparqlConstruct) { throw new UnsupportedOperationException(); } - - @Override + public EndpointResult ask(String sparqlAsk) { throw new UnsupportedOperationException(); } + + + public static void main(String args[]) { + if (args.length < 4) { + System.err.println("Usage: eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint []"); + System.err.println(" where is the hostname of the Strabon Endpoint"); + System.err.println(" is the port to connect to on the host"); + System.err.println(" is the application name of Strabon Endpoint as deployed in the Tomcat container"); + System.err.println(" is the query to execute on the endpoint"); + System.err.println(" [] is the format of your results. Should be one of XML (default), KML, KMZ, GeoJSON, TSV, or HTML."); + System.exit(1); + } + + String host = args[0]; + Integer port = new Integer(args[1]); + String appName = args[2]; + String query = args[3]; + String format = ""; + + if (args.length == 5) { + format = args[4]; + + } else { + format = "XML"; + } + + SPARQLEndpoint endpoint = new SPARQLEndpoint(host, port, appName); + + try { + EndpointResult result = endpoint.query(query, stSPARQLQueryResultFormat.valueOf(format)); + + System.out.println("Status code: " + result.getStatusCode()); + System.out.println("Status text: " + result.getStatusText()); + System.out.println("<----- Result ----->"); + System.out.println(result.getResponse().replaceAll("\n", "\n\t")); + System.out.println("<----- Result ----->"); + + } catch (IOException e) { + e.printStackTrace(); + } + } } diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java Sat Apr 06 15:55:32 2013 +0300 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java Sat Apr 06 18:05:36 2013 +0300 @@ -9,16 +9,37 @@ */ package eu.earthobservatory.org.StrabonEndpoint.client; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringReader; +import java.util.Vector; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.openrdf.model.URI; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.ValueFactoryImpl; +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.algebra.evaluation.QueryBindingSet; import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; /** - * Every SPARQL endpoint that supports storing and querying of - * spatial RDF data should extend the {@link SpatialEndpoint} - * abstract class. + * SpatialEndpoint is a SPARQLEndpoint which can store and + * query for spatial data. It also supports KML format for + * this kind of data. * * @author Charalampos Nikolaou + * @author Kallirroi Dogani */ -public abstract class SpatialEndpoint extends HTTPClient implements Endpoint { +public class SpatialEndpoint extends SPARQLEndpoint { public SpatialEndpoint(String host, int port) { super(host, port); @@ -27,4 +48,99 @@ public SpatialEndpoint(String host, int port, String endpointName) { super(host, port, endpointName); } + + public EndpointResult queryForKML(String sparqlQuery) throws IOException, TupleQueryResultHandlerException{ + + EndpointResult xmlResult = query(sparqlQuery, stSPARQLQueryResultFormat.XML); + + if (xmlResult.getStatusCode() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + xmlResult.getStatusCode() + " " + xmlResult.getStatusText()); + } + + String xml = xmlResult.getResponse(); + + Vector bindingSets = xmlToBindingSet(xml); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + stSPARQLResultsKMLWriter kmlWriter = new stSPARQLResultsKMLWriter(outputStream); + + kmlWriter.startQueryResult(new Vector()); + + for(int i=0; i xmlToBindingSet(String xml){ + + Vector bindingSetList = new Vector(); + + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(new InputSource(new StringReader(xml))); + + doc.getDocumentElement().normalize(); + + Node resultsNode = doc.getElementsByTagName("results").item(0); + Element resultsElement = (Element) resultsNode; + NodeList resultsList = resultsElement.getElementsByTagName("result"); + + for (int i = 0; i < resultsList.getLength(); i++) { + + Node resultItem = resultsList.item(i); + Element resultElement = (Element) resultItem; + NodeList bindingNamesList = resultElement.getElementsByTagName("binding"); + + QueryBindingSet bindingSet = new QueryBindingSet(); + ValueFactoryImpl valueFactImpl = new ValueFactoryImpl(); + + for (int j=0; j - */ -public class StrabonEndpoint extends SpatialEndpoint { - - public StrabonEndpoint(String host, int port) { - super(host, port); - } - - public StrabonEndpoint(String host, int port, String endpointName) { - super(host, port, endpointName); - } - - @Override - public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException { - assert(format != null); - - // create a post method to execute - HttpPost method = new HttpPost(getConnectionURL() + "/Query"); - - // set the query parameter - List params = new ArrayList(); - params.add(new BasicNameValuePair("query", sparqlQuery)); - UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset()); - method.setEntity(encodedEntity); - - // set the content type - method.setHeader("Content-Type", "application/x-www-form-urlencoded"); - - // set the accept format - method.addHeader("Accept", format.getDefaultMIMEType()); - - try { - // response that will be filled next - String responseBody = ""; - - // execute the method - HttpResponse response = hc.execute(method); - int statusCode = response.getStatusLine().getStatusCode(); - - // If the response does not enclose an entity, there is no need - // to worry about connection release - HttpEntity entity = response.getEntity(); - if (entity != null) { - InputStream instream = entity.getContent(); - try { - - BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); - StringBuffer strBuf = new StringBuffer(); - - // do something useful with the response - String nextLine; - while ((nextLine = reader.readLine()) != null) { - strBuf.append(nextLine + "\n"); - } - - // remove last newline character - if (strBuf.length() > 0) { - strBuf.setLength(strBuf.length() - 1); - } - - responseBody = strBuf.toString(); - - } catch (IOException ex) { - // In case of an IOException the connection will be released - // back to the connection manager automatically - throw ex; - - } catch (RuntimeException ex) { - // In case of an unexpected exception you may want to abort - // the HTTP request in order to shut down the underlying - // connection and release it back to the connection manager. - method.abort(); - throw ex; - - } finally { - // Closing the input stream will trigger connection release - instream.close(); - } - } - - return new StrabonEndpointResult(statusCode, response.getStatusLine().getReasonPhrase(), responseBody); - - } catch (IOException e) { - throw e; - - } finally { - // release the connection. - method.releaseConnection(); - } - } - - @Override - public boolean store(String data, RDFFormat format, URL namedGraph) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean store(URL data, RDFFormat format, URL namedGraph) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean update(String sparqlUpdate) { - throw new UnsupportedOperationException(); - } - - @Override - public EndpointResult describe(String sparqlDescribe) { - throw new UnsupportedOperationException(); - } - - @Override - public EndpointResult construct(String sparqlConstruct) { - throw new UnsupportedOperationException(); - } - - @Override - public EndpointResult ask(String sparqlAsk) { - throw new UnsupportedOperationException(); - } - - public static void main(String args[]) { - if (args.length < 4) { - System.err.println("Usage: eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint []"); - System.err.println(" where is the hostname of the Strabon Endpoint"); - System.err.println(" is the port to connect to on the host"); - System.err.println(" is the application name of Strabon Endpoint as deployed in the Tomcat container"); - System.err.println(" is the query to execute on the endpoint"); - System.err.println(" [] is the format of your results. Should be one of XML (default), KML, KMZ, GeoJSON, TSV, or HTML."); - System.exit(1); - } - - String host = args[0]; - Integer port = new Integer(args[1]); - String appName = args[2]; - String query = args[3]; - String format = ""; - - if (args.length == 5) { - format = args[4]; - - } else { - format = "XML"; - } - - StrabonEndpoint endpoint = new StrabonEndpoint(host, port, appName); - - try { - EndpointResult result = endpoint.query(query, stSPARQLQueryResultFormat.valueOf(format)); - - System.out.println("Status code: " + result.getStatusCode()); - System.out.println("Status text: " + result.getStatusText()); - System.out.println("<----- Result ----->"); - System.out.println(result.getResponse().replaceAll("\n", "\n\t")); - System.out.println("<----- Result ----->"); - - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpointResult.java --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpointResult.java Sat Apr 06 15:55:32 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package eu.earthobservatory.org.StrabonEndpoint.client; - -/** - * @author Charalampos Nikolaou - * - */ -public class StrabonEndpointResult implements EndpointResult { - - private int statusCode; - private String statusText; - private String response; - - public StrabonEndpointResult(int statusCode, String statusLine, String response) { - this.statusCode = statusCode; - this.statusText = statusLine; - this.response = response; - } - - @Override - public int getStatusCode() { - return statusCode; - } - - @Override - public String getStatusText() { - return statusText; - } - - @Override - public String getResponse() { - return response; - } -} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithParliament.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithParliament.java Sat Apr 06 18:05:36 2013 +0300 @@ -0,0 +1,75 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2013, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.org.StrabonEndpoint.client; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Kallirroi Dogani + * + */ + +public class TestSPARQLEndpointWithParliament { + + private SPARQLEndpoint endpoint; + private String query; + + @Before + public void init() { + // initialize endpoint + endpoint = new SPARQLEndpoint("luna.di.uoa.gr", 8080, "parliament/sparql"); + + // set query + query = "PREFIX ex: \n" + + "SELECT ?k ?g WHERE {\n" + + " ex:pol1 ?k ?g\n" + + "}" + + "\nLIMIT 1"; + + } + + /** + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + */ + @Test + public void testQuery() { + try { + EndpointResult response = endpoint.query(query, stSPARQLQueryResultFormat.XML); + + System.out.println(response.getResponse()); + + if (response.getStatusCode() != 200) { + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); + + } + + assertTrue(response.getStatusCode() == 200); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + /** + * Test method for testing that method {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + * returns an IOException when it should do so. + */ + @Test(expected= IOException.class) + public void testIOException() throws Exception { + SPARQLEndpoint ep = new SPARQLEndpoint("blabla.dgr", 80, "bla"); + ep.query(query, stSPARQLQueryResultFormat.XML); + } +} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithStrabon.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithStrabon.java Sat Apr 06 18:05:36 2013 +0300 @@ -0,0 +1,91 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.org.StrabonEndpoint.client; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Vector; + +import org.junit.Before; +import org.junit.Test; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class TestSPARQLEndpointWithStrabon { + + private SPARQLEndpoint endpoint; + private String query; + private Vector formats = new Vector(); + + @Before + public void init() { + // initialize endpoint + endpoint = new SPARQLEndpoint("geo.linkedopendata.gr", 9090, "gag-endpoint/Query"); + + // set query + query = "PREFIX rdf: " + + "PREFIX rdfs: " + + "PREFIX gag: " + + + "SELECT ?geometry " + + "WHERE {" + + + " ?m rdf:type gag:Δήμος . " + + " ?m rdfs:label \"ΔΗΜΟΣ ΑΘΗΝΑΙΩΝ\" . " + + " ?m gag:έχει_γεωμετρία ?geometry. " + + " } " ; + + // initialized formats + for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) { + if (format instanceof stSPARQLQueryResultFormat) { + formats.add((stSPARQLQueryResultFormat) format); + } + } + + } + + /** + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + */ + @Test + public void testQuery() { + for (stSPARQLQueryResultFormat format : formats) { + try { + EndpointResult response = endpoint.query(query, format); + + if (response.getStatusCode() != 200) { + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); + + } + + assertTrue(response.getStatusCode() == 200); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + /** + * Test method for testing that method {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + * returns an IOException when it should do so. + */ + @Test(expected= IOException.class) + public void testIOException() throws Exception { + SPARQLEndpoint ep = new SPARQLEndpoint("blabla.dgr", 80, "bla"); + ep.query(query, formats.get(0)); + } +} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithVirtuoso.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointWithVirtuoso.java Sat Apr 06 18:05:36 2013 +0300 @@ -0,0 +1,88 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2013, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.org.StrabonEndpoint.client; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Vector; + +import org.junit.Before; +import org.junit.Test; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Kallirroi Dogani + * + */ + +//Virtuso endpoint also needs to be tested for all formats included in stSPARQLQueryResultFormat +//because some of them are not supported +public class TestSPARQLEndpointWithVirtuoso { + + private SPARQLEndpoint endpoint; + private String query; + private Vector formats = new Vector(); + + @Before + public void init() { + // initialize endpoint + endpoint = new SPARQLEndpoint("luna.di.uoa.gr", 8890, "sparql"); + + // set query + query = "PREFIX ex: \n" + + "SELECT ?k ?g WHERE {\n" + + " ex:pol1 ?k ?g\n" + + "}" + + "\nLIMIT 1"; + + // initialized formats + for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) { + if (format instanceof stSPARQLQueryResultFormat) { + formats.add((stSPARQLQueryResultFormat) format); + } + } + + } + + /** + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + */ + @Test + public void testQuery() { + try { + EndpointResult response = endpoint.query(query, stSPARQLQueryResultFormat.XML); + + System.out.println(response.getResponse()); + + if (response.getStatusCode() != 200) { + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); + + } + + assertTrue(response.getStatusCode() == 200); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + /** + * Test method for testing that method {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + * returns an IOException when it should do so. + */ + @Test(expected= IOException.class) + public void testIOException() throws Exception { + SPARQLEndpoint ep = new SPARQLEndpoint("blabla.dgr", 80, "bla"); + ep.query(query, formats.get(0)); + } +} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSpatialEndpoint.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSpatialEndpoint.java Sat Apr 06 18:05:36 2013 +0300 @@ -0,0 +1,70 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2013, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.org.StrabonEndpoint.client; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; +import org.openrdf.query.TupleQueryResultHandlerException; + +/** + * @author Kallirroi Dogani + * + */ + +public class TestSpatialEndpoint { + + private SpatialEndpoint endpoint; + private String query; + + @Before + public void init() { + // initialize endpoint + endpoint = new SpatialEndpoint("luna.di.uoa.gr", 8890, "sparql"); + + // set query + query = "PREFIX ex: \n" + + "SELECT ?k ?g WHERE {\n" + + " ?k ex:geometry ?g\n" + + "}" + + "\nLIMIT 1"; + } + + /** + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SpatialEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + * @throws TupleQueryResultHandlerException + */ + @Test + public void testQuery() { + try { + EndpointResult response = endpoint.queryForKML(query); + + System.out.println("KML format:"); + System.out.println(response.getResponse()); + + if (response.getStatusCode() != 200) { + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); + + } + + assertTrue(response.getStatusCode() == 200); + + } + catch (TupleQueryResultHandlerException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + + } +} diff -r 20ead981bfef -r 34a6c207f0d3 endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java --- a/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java Sat Apr 06 15:55:32 2013 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -///** -// * This Source Code Form is subject to the terms of the Mozilla Public -// * License, v. 2.0. If a copy of the MPL was not distributed with this -// * file, You can obtain one at http://mozilla.org/MPL/2.0/. -// * -// * Copyright (C) 2012, Pyravlos Team -// * -// * http://www.strabon.di.uoa.gr/ -// */ -//package eu.earthobservatory.org.StrabonEndpoint.client; -// -//import static org.junit.Assert.assertTrue; -// -//import java.io.IOException; -//import java.util.Vector; -// -//import org.junit.Before; -//import org.junit.Test; -//import org.openrdf.query.resultio.TupleQueryResultFormat; -//import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -// -///** -// * @author Charalampos Nikolaou -// * -// */ -//public class TestStrabonEndpoint { -// -// private StrabonEndpoint endpoint; -// private String query; -// private Vector formats = new Vector(); -// -// @Before -// public void init() { -// // initialize endpoint -// endpoint = new StrabonEndpoint("test.strabon.di.uoa.gr", 80, "DLR"); -// -// // set query -// query = "PREFIX teleios:\n" + -// "SELECT ?s ?g WHERE {\n" + -// " ?s teleios:hasGeometry ?g\n" + -// "}" + -// "\nLIMIT 1"; -// -// // initialized formats -// for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) { -// if (format instanceof stSPARQLQueryResultFormat) { -// formats.add((stSPARQLQueryResultFormat) format); -// } -// } -// -// } -// -// /** -// * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. -// */ -// @Test -// public void testQuery() { -// for (stSPARQLQueryResultFormat format : formats) { -// try { -// EndpointResult response = endpoint.query(query, format); -// -// if (response.getStatusCode() != 200) { -// System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); -// -// } -// -// assertTrue(response.getStatusCode() == 200); -// -// } catch (IOException e) { -// e.printStackTrace(); -// } -// -// } -// } -// -// /** -// * Test method for testing that method {@link eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. -// * returns an IOException when it should do so. -// */ -// @Test(expected= IOException.class) -// public void testIOException() throws Exception { -// StrabonEndpoint ep = new StrabonEndpoint("blabla.dgr", 80, "bla"); -// ep.query(query, formats.get(0)); -// } -//}