# HG changeset patch # User Babis Nikolaou # Date 1351634321 -7200 # Node ID 50c80c0f01a26f5df4af92120b44134823bd1814 # Parent 24a6e7d423ebac4db587bbaf7bc2fcf967bae2c8 added test for StrabonEndpoint.query() method diff -r 24a6e7d423eb -r 50c80c0f01a2 endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java Tue Oct 30 22:38:05 2012 +0200 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java Tue Oct 30 23:58:41 2012 +0200 @@ -65,8 +65,7 @@ this.host = host; this.port = port; - // make the name of the endpoint end with a slash - this.endpointName = (endpointName != null && !endpointName.endsWith("/")) ? endpointName+"/":""; + this.endpointName = (endpointName == null ? "":endpointName); // create a connection manager for allowing the users of this class use threads connectionManager = new MultiThreadedHttpConnectionManager(); diff -r 24a6e7d423eb -r 50c80c0f01a2 endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java Tue Oct 30 22:38:05 2012 +0200 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java Tue Oct 30 23:58:41 2012 +0200 @@ -11,6 +11,7 @@ import java.io.IOException; import java.net.URL; +import java.net.URLEncoder; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.PostMethod; @@ -28,31 +29,34 @@ 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 { // create a post method to execute - HttpMethod method = new PostMethod(getConnectionURL()); + HttpMethod method = new PostMethod(getConnectionURL() + "/Query"); // set the query parameter - method.getParams().setParameter("query", sparqlQuery); + method.setQueryString("query="+ URLEncoder.encode(sparqlQuery, "UTF-8")); + + // set the content type + method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); + //System.out.println(method.getRequestHeader("Content-type")); // set the accept format - method.setRequestHeader("Accept", format.getDefaultMIMEType()); + method.addRequestHeader("Accept", format.getDefaultMIMEType()); + //System.out.println(method.getRequestHeader("Accept")); + //System.out.println(method.getURI()); + try { // execute the method int statusCode = hc.executeMethod(method); - - // Read the response body. - byte[] responseBody = method.getResponseBody(); - - // Deal with the response. - // Use caution: ensure correct character encoding and is not binary - // data - String response = new String(responseBody); - return new StrabonEndpointResult(statusCode, method.getStatusText(), response); + return new StrabonEndpointResult(statusCode, method.getStatusText(), method.getResponseBodyAsString()); } catch (IOException e) { throw e; diff -r 24a6e7d423eb -r 50c80c0f01a2 endpoint/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/endpoint/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java Tue Oct 30 23:58:41 2012 +0200 @@ -0,0 +1,69 @@ +/** + * 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 org.junit.Before; +import org.junit.Test; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +import eu.earthobservatory.org.StrabonEndpoint.client.EndpointResult; +import eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint; + +/** + * @author Charalampos Nikolaou + * + */ +public class TestStrabonEndpoint { + + private StrabonEndpoint endpoint; + + @Before + public void init() { + endpoint = new StrabonEndpoint("test.strabon.di.uoa.gr", 80, "DLR"); + } + + /** + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. + */ + @Test + public void testQuery() { + for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) { + try { + String query = "" + + "PREFIX teleios:\n" + + "SELECT ?s ?g WHERE {\n" + + " ?s teleios:hasGeometry ?g\n" + + "}" + + "\nLIMIT 1"; + + if (format instanceof stSPARQLQueryResultFormat) { + EndpointResult response = endpoint.query(query, (stSPARQLQueryResultFormat) format); + + if (response.getStatusCode() != 200) { + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); + + } + assertTrue(response.getStatusCode() == 200); + + } + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + } +}