summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2010-04-28 03:57:14 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2010-04-28 03:57:14 +0000
commite943796a67d51eeb54ee818dc732ff4ecd2c5c74 (patch)
tree132a59686dfab81678c54760e4dca188c5e64269
parentd61bdeb8230194c005ef29f29b3cf87067e6deab (diff)
Tweaking binding implementation and adding tests for GET, PUT, POST operation mapping with the JSON wire format
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@938773 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java13
-rw-r--r--sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java15
-rw-r--r--sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java62
-rw-r--r--sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java8
-rw-r--r--sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java26
5 files changed, 103 insertions, 21 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java
index ab13d952e4..b96a45338a 100644
--- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java
+++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java
@@ -114,11 +114,14 @@ public class RESTBindingListenerServlet extends HttpServlet {
Throwable e = (Throwable)responseMessage.getBody();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
} else {
- byte[] bout;
- bout = responseMessage.<Object>getBody().toString().getBytes("UTF-8");
- response.getOutputStream().write(bout);
- response.getOutputStream().flush();
- response.getOutputStream().close();
+ //handle void operations
+ if(responseMessage.getBody() != null) {
+ byte[] bout;
+ bout = responseMessage.<Object>getBody().toString().getBytes("UTF-8");
+ response.getOutputStream().write(bout);
+ response.getOutputStream().flush();
+ response.getOutputStream().close();
+ }
}
} else {
super.service(request, response);
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java
index e5ca19b78f..353cf811a5 100644
--- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java
+++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java
@@ -19,11 +19,14 @@
package org.apache.tuscany.sca.binding.rest.wireformat.json.provider;
+import java.io.CharArrayWriter;
+
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.invocation.Interceptor;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
+import org.json.JSONObject;
/**
* JSON wire format Interceptor.
@@ -46,6 +49,18 @@ public class JSONWireFormatInterceptor implements Interceptor {
}
public Message invoke(Message msg) {
+ try {
+ if(msg.getBody() != null) {
+ Object[] args = msg.getBody();
+ CharArrayWriter data = (CharArrayWriter) args[0];
+
+ JSONObject jsonPayload = new JSONObject(data.toString());
+ msg.setBody(new Object[]{jsonPayload});
+ }
+ } catch(Exception e) {
+ throw new RuntimeException("Unable to parse json paylod: " + msg.getBody().toString());
+ }
+
return getNext().invoke(msg);
}
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java
index ad72a5a74f..80dbcff18b 100644
--- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java
+++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java
@@ -19,33 +19,34 @@
package org.apache.tuscany.sca.binding.rest.wireformat.json;
+import java.io.ByteArrayInputStream;
import java.net.Socket;
-import java.net.URLEncoder;
-import org.apache.axiom.om.util.Base64;
import org.apache.tuscany.sca.node.Contribution;
import org.apache.tuscany.sca.node.ContributionLocationHelper;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
-import org.json.JSONObject;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
-import org.junit.Ignore;
import org.junit.Test;
+import services.Catalog;
+
import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
-import services.Catalog;
-import services.Item;
-
public class CatalogServiceTestCase {
private static final String SERVICE_URL = "http://localhost:8085/Catalog";
-
- private static final String GET_RESPONSE = "[{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"},{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"}]";
+
+ private static final String GET_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"}]";
+ private static final String NEW_ITEM = "{\"price\":\"$4.35\",\"name\":\"Grape\"}\"";
+ private static final String GET_NEW_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"},{\"price\":\"$4.35\",\"name\":\"Grape\",\"javaClass\":\"services.Item\"}]";
+ private static final String UPDATED_ITEM = "{\"price\":\"$1.35\",\"name\":\"Grape\"}\"";
+ private static final String GET_UPDATED_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"},{\"price\":\"$1.35\",\"name\":\"Grape\",\"javaClass\":\"services.Item\"}]";
private static Node node;
private static Catalog catalogService;
@@ -86,4 +87,47 @@ public class CatalogServiceTestCase {
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(GET_RESPONSE, response.getText());
}
+
+
+ @Test
+ public void testPostInvocation() throws Exception {
+ //Add new item to catalog
+ WebConversation wc = new WebConversation();
+ WebRequest request = new PostMethodWebRequest(SERVICE_URL, new ByteArrayInputStream(NEW_ITEM.getBytes("UTF-8")),"application/json");
+ WebResponse response = wc.getResource(request);
+
+ Assert.assertEquals(200, response.getResponseCode());
+
+ //read new results and expect to get new item back in the response
+ request = new GetMethodWebRequest(SERVICE_URL);
+ response = wc.getResource(request);
+
+ //for debug purposes
+ //System.out.println(">>>" + GET_UPDATED_RESPONSE);
+ //System.out.println(">>>" + response.getText());
+
+ Assert.assertEquals(200, response.getResponseCode());
+ Assert.assertEquals(GET_NEW_RESPONSE, response.getText());
+ }
+
+ @Test
+ public void testPutInvocation() throws Exception {
+ //Add new item to catalog
+ WebConversation wc = new WebConversation();
+ WebRequest request = new PostMethodWebRequest(SERVICE_URL, new ByteArrayInputStream(UPDATED_ITEM.getBytes("UTF-8")),"application/json");
+ WebResponse response = wc.getResource(request);
+
+ Assert.assertEquals(200, response.getResponseCode());
+
+ //read new results and expect to get new item back in the response
+ request = new GetMethodWebRequest(SERVICE_URL);
+ response = wc.getResource(request);
+
+ //for debug purposes
+ //System.out.println(">>>" + GET_UPDATED_RESPONSE);
+ //System.out.println(">>>" + response.getText());
+
+ Assert.assertEquals(200, response.getResponseCode());
+ Assert.assertEquals(GET_UPDATED_RESPONSE, response.getText());
+ }
}
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java
index d13b325395..0c880b499e 100644
--- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java
+++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java
@@ -20,6 +20,8 @@
package services;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
import org.oasisopen.sca.annotation.Remotable;
@@ -28,4 +30,10 @@ public interface Catalog {
@GET
Item[] get();
+
+ @POST
+ void addItem(Item item);
+
+ @PUT
+ void updateItem(Item item);
}
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java
index d132a24b00..b97db14149 100644
--- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java
+++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java
@@ -19,13 +19,15 @@
package services;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
import org.oasisopen.sca.annotation.Init;
import org.oasisopen.sca.annotation.Property;
import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
+@Scope("COMPOSITE")
public class FruitsCatalogImpl implements Catalog {
@Property
@@ -34,19 +36,29 @@ public class FruitsCatalogImpl implements Catalog {
@Reference
public CurrencyConverter currencyConverter;
- private List<Item> catalog = new ArrayList<Item>();
+ private Map<String, Item> catalog = new HashMap<String, Item>();
@Init
public void init() {
String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
- catalog.add(new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99)));
- catalog.add(new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55)));
- catalog.add(new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55)));
+ catalog.put("Apple", new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99)));
+ catalog.put("Orange", new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55)));
+ catalog.put("Pear", new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55)));
}
public Item[] get() {
Item[] catalogArray = new Item[catalog.size()];
- catalog.toArray(catalogArray);
+ catalog.values().toArray(catalogArray);
return catalogArray;
}
+
+ public void addItem(Item item) {
+ catalog.put(item.getName(),item);
+ }
+
+ public void updateItem(Item item) {
+ if(catalog.get(item.getName()) != null) {
+ catalog.put(item.getName(), item);
+ }
+ }
}