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
This commit is contained in:
parent
d61bdeb823
commit
e943796a67
5 changed files with 103 additions and 21 deletions
sca-java-2.x/trunk/modules/binding-rest-runtime/src
main/java/org/apache/tuscany/sca/binding/rest
test/java
org/apache/tuscany/sca/binding/rest/wireformat/json
services
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue