You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by bd...@apache.org on 2011/02/08 16:29:50 UTC
svn commit: r1068442 - in /incubator/stanbol/trunk:
commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/
enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/
Author: bdelacretaz
Date: Tue Feb 8 15:29:49 2011
New Revision: 1068442
URL: http://svn.apache.org/viewvc?rev=1068442&view=rev
Log:
STANBOL-15 - prototype RESTful API documentation generator based on integration tests
Added:
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java (with props)
Modified:
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java
incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java
Added: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java?rev=1068442&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java (added)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java Tue Feb 8 15:29:49 2011
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.stanbol.commons.testing.http;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.http.Header;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+
+
+/** Generate RESTful API documentation based on actual requests
+ * executed during integration tests, enhanced with user-supplied
+ * bits of documentation.
+ */
+public class RequestDocumentor {
+ public static final String OUTPUT_BASE = "./target/" + RequestDocumentor.class.getSimpleName();
+ private final String name;
+
+ public RequestDocumentor(String name) {
+ this.name = name;
+ }
+
+ public String toString() {
+ return getClass().getSimpleName() + " (" + name + ")";
+ }
+
+ void generateDocumentation(RequestExecutor executor, String [] metadata) throws IOException {
+ final File f = getOutputFile();
+ final File dir = f.getParentFile();
+ dir.mkdirs();
+ if(!dir.isDirectory()) {
+ throw new IOException("Failed to create output folder " + dir.getAbsolutePath());
+ }
+ final PrintWriter pw = new PrintWriter(new FileWriter(f, true));
+ try {
+ System.out.println("Appending documentation of " + executor + " to " + f.getAbsolutePath());
+ documentRequest(pw, executor, metadata);
+ } finally {
+ pw.flush();
+ pw.close();
+ }
+ }
+
+ protected File getOutputFile() {
+ return new File(OUTPUT_BASE + "/" + name + ".txt");
+ }
+
+ protected void documentRequest(PrintWriter pw, RequestExecutor executor, String [] metadataArray) throws IOException {
+ // Convert metadata to more convenient Map
+ final Map<String, String> m = new HashMap<String, String>();
+ if(metadataArray.length % 2 != 0) {
+ throw new IllegalArgumentException("Metadata array must be of even size, got " + metadataArray.length);
+ }
+ for(int i=0 ; i < metadataArray.length; i += 2) {
+ m.put(metadataArray[i], metadataArray[i+1]);
+ }
+
+ // TODO use velocity or other templates? Just a rough prototype for now
+ // Also need to filter overly long input/output, binary etc.
+ pw.println();
+ pw.println("====================================================================================");
+ pw.print("=== ");
+ pw.print(m.get("title"));
+ pw.println(" ===");
+ pw.println(m.get("description"));
+
+ pw.print("\n=== ");
+ pw.print("REQUEST");
+ pw.println(" ===");
+
+ pw.print("Method: ");
+ pw.println(executor.getRequest().getMethod());
+ pw.print("URI: ");
+ pw.println(executor.getRequest().getURI());
+
+ final Header [] allHeaders = executor.getRequest().getAllHeaders();
+ if(allHeaders != null && allHeaders.length > 0) {
+ pw.println("Headers:");
+ for(Header h : allHeaders) {
+ pw.print(h.getName());
+ pw.print(":");
+ pw.println(h.getValue());
+ }
+ }
+
+ if(executor.getRequest() instanceof HttpEntityEnclosingRequestBase) {
+ final HttpEntityEnclosingRequestBase heb = (HttpEntityEnclosingRequestBase)executor.getRequest();
+ if(heb.getEntity() != null) {
+ pw.print("Content-Type:");
+ pw.println(heb.getEntity().getContentType().getValue());
+ pw.println("Content:");
+ final InputStream is = heb.getEntity().getContent();
+ final byte[] buffer = new byte[16384];
+ int count = 0;
+ while( (count = is.read(buffer, 0, buffer.length)) > 0) {
+ // TODO encoding??
+ pw.write(new String(buffer, 0, count));
+ }
+ pw.println();
+ }
+ }
+
+ pw.print("\n=== ");
+ pw.print("RESPONSE");
+ pw.println(" ===");
+ pw.print("Content-Type:");
+ pw.println(executor.getResponse().getEntity().getContentType().getValue());
+ pw.println("Content:");
+ pw.println(executor.getContent());
+
+ pw.println("====================================================================================");
+ }
+}
Propchange: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestDocumentor.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java?rev=1068442&r1=1068441&r2=1068442&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java (original)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java Tue Feb 8 15:29:49 2011
@@ -30,6 +30,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
/** Executes a Request and provides convenience methods
@@ -37,6 +38,7 @@ import org.apache.http.util.EntityUtils;
*/
public class RequestExecutor {
private final HttpClient httpClient;
+ private HttpUriRequest request;
private HttpResponse response;
private HttpEntity entity;
private String content;
@@ -46,7 +48,8 @@ public class RequestExecutor {
}
public RequestExecutor execute(Request r) throws ClientProtocolException, IOException {
- response = httpClient.execute(r.getRequest());
+ request = r.getRequest();
+ response = httpClient.execute(request);
entity = response.getEntity();
if(entity != null) {
// We fully read the content every time, not super efficient but
@@ -107,4 +110,24 @@ public class RequestExecutor {
}
return this;
}
+
+ public void generateDocumentation(RequestDocumentor documentor, String...metadata) throws IOException {
+ documentor.generateDocumentation(this, metadata);
+ }
+
+ HttpUriRequest getRequest() {
+ return request;
+ }
+
+ HttpResponse getResponse() {
+ return response;
+ }
+
+ HttpEntity getEntity() {
+ return entity;
+ }
+
+ String getContent() {
+ return content;
+ }
}
Modified: incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java?rev=1068442&r1=1068441&r2=1068442&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java (original)
+++ incubator/stanbol/trunk/enhancer/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/StatelessEngineTest.java Tue Feb 8 15:29:49 2011
@@ -16,12 +16,15 @@
*/
package org.apache.stanbol.enhancer.it;
+import org.apache.stanbol.commons.testing.http.RequestDocumentor;
import org.apache.stanbol.commons.testing.stanbol.StanbolTestBase;
import org.junit.Test;
/** Test the stateless text enhancement engines */
public class StatelessEngineTest extends StanbolTestBase {
+ private final RequestDocumentor documentor = new RequestDocumentor(getClass().getName());
+
@Test
public void testSimpleEnhancement() throws Exception {
executor.execute(
@@ -38,7 +41,15 @@ public class StatelessEngineTest extends
"http://fise.iks-project.eu/ontology/entity-label.*Paris",
"http://purl.org/dc/terms/creator.*NamedEntityExtractionEnhancementEngine",
"http://fise.iks-project.eu/ontology/entity-label.*Bob Marley"
- );
+ )
+ .generateDocumentation(
+ documentor,
+ "title",
+ "Stateless text analysis",
+ "description",
+ "A POST request to ${request.path} (TODO should be replaced by actual path) returns triples representing enhancements "
+ + " of the POSTed text. Output format is defined by the Accept header."
+ );
}
@Test
@@ -74,7 +85,11 @@ public class StatelessEngineTest extends
)
.assertStatus(200)
.assertContentType(formats[i+1])
- .assertContentRegexp(formats[i+2]);
+ .assertContentRegexp(formats[i+2])
+ .generateDocumentation(documentor,
+ "title", "Output format: " + formats[i],
+ "description", "Demonstrate " + formats[i] + " output"
+ );
}
}