From e943796a67d51eeb54ee818dc732ff4ecd2c5c74 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 28 Apr 2010 03:57:14 +0000 Subject: 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 --- .../wireformat/json/CatalogServiceTestCase.java | 62 ++++++++++++++++++---- .../src/test/java/services/Catalog.java | 8 +++ .../src/test/java/services/FruitsCatalogImpl.java | 26 ++++++--- 3 files changed, 80 insertions(+), 16 deletions(-) (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java') 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 catalog = new ArrayList(); + private Map catalog = new HashMap(); @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); + } + } } -- cgit v1.2.3