Strabon

changeset 672:50c80c0f01a2

added test for StrabonEndpoint.query() method
author Babis Nikolaou <charnik@di.uoa.gr>
date Tue Oct 30 23:58:41 2012 +0200 (2012-10-30)
parents 24a6e7d423eb
children 7fda2cf3acef
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java endpoint/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java	Tue Oct 30 22:38:05 2012 +0200
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java	Tue Oct 30 23:58:41 2012 +0200
     1.3 @@ -65,8 +65,7 @@
     1.4  		this.host = host;
     1.5  		this.port = port;
     1.6  		
     1.7 -		// make the name of the endpoint end with a slash
     1.8 -		this.endpointName = (endpointName != null && !endpointName.endsWith("/")) ? endpointName+"/":"";
     1.9 +		this.endpointName = (endpointName == null ? "":endpointName);
    1.10  		
    1.11  		// create a connection manager for allowing the users of this class use threads
    1.12  		connectionManager = new MultiThreadedHttpConnectionManager();
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java	Tue Oct 30 22:38:05 2012 +0200
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java	Tue Oct 30 23:58:41 2012 +0200
     2.3 @@ -11,6 +11,7 @@
     2.4  
     2.5  import java.io.IOException;
     2.6  import java.net.URL;
     2.7 +import java.net.URLEncoder;
     2.8  
     2.9  import org.apache.commons.httpclient.HttpMethod;
    2.10  import org.apache.commons.httpclient.methods.PostMethod;
    2.11 @@ -28,31 +29,34 @@
    2.12  	public StrabonEndpoint(String host, int port) {
    2.13  		super(host, port);
    2.14  	}
    2.15 +	
    2.16 +	public StrabonEndpoint(String host, int port, String endpointName) {
    2.17 +		super(host, port, endpointName);
    2.18 +	}
    2.19  
    2.20  	@Override
    2.21  	public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
    2.22  		// create a post method to execute
    2.23 -		HttpMethod method = new PostMethod(getConnectionURL());
    2.24 +		HttpMethod method = new PostMethod(getConnectionURL() + "/Query");
    2.25  		
    2.26  		// set the query parameter
    2.27 -		method.getParams().setParameter("query", sparqlQuery);
    2.28 +		method.setQueryString("query="+ URLEncoder.encode(sparqlQuery, "UTF-8"));
    2.29 +		
    2.30 +		// set the content type
    2.31 +		method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
    2.32 +		//System.out.println(method.getRequestHeader("Content-type"));
    2.33  		
    2.34  		// set the accept format
    2.35 -		method.setRequestHeader("Accept", format.getDefaultMIMEType());
    2.36 +		method.addRequestHeader("Accept", format.getDefaultMIMEType());
    2.37 +		//System.out.println(method.getRequestHeader("Accept"));
    2.38  		
    2.39 +		//System.out.println(method.getURI());
    2.40 +
    2.41  		try {
    2.42  			// execute the method
    2.43  			int statusCode = hc.executeMethod(method);
    2.44 -
    2.45 -			// Read the response body.
    2.46 -			byte[] responseBody = method.getResponseBody();
    2.47 -
    2.48 -			// Deal with the response.
    2.49 -			// Use caution: ensure correct character encoding and is not binary
    2.50 -			// data
    2.51 -			String response = new String(responseBody);
    2.52  			
    2.53 -			return new StrabonEndpointResult(statusCode, method.getStatusText(), response);
    2.54 +			return new StrabonEndpointResult(statusCode, method.getStatusText(), method.getResponseBodyAsString());
    2.55  
    2.56  		} catch (IOException e) {
    2.57  			throw e;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/endpoint/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestStrabonEndpoint.java	Tue Oct 30 23:58:41 2012 +0200
     3.3 @@ -0,0 +1,69 @@
     3.4 +/**
     3.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     3.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     3.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     3.8 + * 
     3.9 + * Copyright (C) 2012, Pyravlos Team
    3.10 + * 
    3.11 + * http://www.strabon.di.uoa.gr/
    3.12 + */
    3.13 +package eu.earthobservatory.org.StrabonEndpoint.client;
    3.14 +
    3.15 +import static org.junit.Assert.assertTrue;
    3.16 +
    3.17 +import java.io.IOException;
    3.18 +
    3.19 +import org.junit.Before;
    3.20 +import org.junit.Test;
    3.21 +import org.openrdf.query.resultio.TupleQueryResultFormat;
    3.22 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    3.23 +
    3.24 +import eu.earthobservatory.org.StrabonEndpoint.client.EndpointResult;
    3.25 +import eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint;
    3.26 +
    3.27 +/**
    3.28 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    3.29 + *
    3.30 + */
    3.31 +public class TestStrabonEndpoint {
    3.32 +
    3.33 +	private StrabonEndpoint endpoint; 
    3.34 +	
    3.35 +	@Before
    3.36 +	public void init() {
    3.37 +		endpoint = new StrabonEndpoint("test.strabon.di.uoa.gr", 80, "DLR");
    3.38 +	}
    3.39 +	
    3.40 +	/**
    3.41 +	 * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}.
    3.42 +	 */
    3.43 +	@Test
    3.44 +	public void testQuery() {
    3.45 +		for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) {
    3.46 +			try {
    3.47 +				String query = "" +
    3.48 +						"PREFIX teleios:<http://teleios.di.uoa.gr/ontologies/noaOntology.owl#>\n" +
    3.49 +						"SELECT ?s ?g WHERE {\n" +
    3.50 +						"	?s teleios:hasGeometry ?g\n" +
    3.51 +						"}" +
    3.52 +						"\nLIMIT 1";
    3.53 +				
    3.54 +				if (format instanceof stSPARQLQueryResultFormat) {
    3.55 +					EndpointResult response = endpoint.query(query, (stSPARQLQueryResultFormat) format);
    3.56 +					
    3.57 +					if (response.getStatusCode() != 200) {
    3.58 +						System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText());
    3.59 +						
    3.60 +					}
    3.61 +					assertTrue(response.getStatusCode() == 200);
    3.62 +					
    3.63 +				}
    3.64 +
    3.65 +			} catch (IOException e) {
    3.66 +				e.printStackTrace();
    3.67 +			}
    3.68 +			
    3.69 +		}
    3.70 +		
    3.71 +	}
    3.72 +}