You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2010/04/28 05:57:14 UTC

svn commit: r938773 - in /tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src: main/java/org/apache/tuscany/sca/binding/rest/provider/ main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/ test/java/org/apache/tuscany/sca/bind...

Author: lresende
Date: Wed Apr 28 03:57:14 2010
New Revision: 938773

URL: http://svn.apache.org/viewvc?rev=938773&view=rev
Log:
Tweaking binding implementation and adding tests for GET, PUT, POST operation mapping with the JSON wire format

Modified:
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java?rev=938773&r1=938772&r2=938773&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java Wed Apr 28 03:57:14 2010
@@ -114,11 +114,14 @@ public class RESTBindingListenerServlet 
                 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);

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java?rev=938773&r1=938772&r2=938773&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/json/provider/JSONWireFormatInterceptor.java Wed Apr 28 03:57:14 2010
@@ -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 i
     }
 
     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);
     }
 

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java?rev=938773&r1=938772&r2=938773&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java Wed Apr 28 03:57:14 2010
@@ -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());
+    }
 }

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java?rev=938773&r1=938772&r2=938773&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java Wed Apr 28 03:57:14 2010
@@ -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);
 }

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java?rev=938773&r1=938772&r2=938773&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java Wed Apr 28 03:57:14 2010
@@ -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 implement
     @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);
+        }
+    }
 }