From afa61ca151560bbdca5471e4e0d1bdd460d2e08e Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 10 Sep 2012 22:14:59 +0000 Subject: Enable field name based filtering for json serialization git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1383129 13f79535-47bb-0310-9956-ffa450edef68 --- .../wireformat/json/CatalogServiceTestCase.java | 40 ++++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest') 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 b3cf3f1975..c09871241c 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 @@ -42,11 +42,14 @@ import com.meterware.httpunit.WebResponse; public class CatalogServiceTestCase { private static final String SERVICE_URL = "http://localhost:8085/Catalog"; - private static final String GET_RESPONSE = "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"}]}"; + private static final String GET_RESPONSE = + "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"}]}"; private static final String NEW_ITEM = "{\"price\":\"$4.35\",\"name\":\"Grape\"}\""; - private static final String GET_NEW_RESPONSE = "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$4.35\",\"name\":\"Grape\"}]}"; + private static final String GET_NEW_RESPONSE = + "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$4.35\",\"name\":\"Grape\"}]}"; private static final String UPDATED_ITEM = "{\"price\":\"$1.35\",\"name\":\"Grape\"}\""; - private static final String GET_UPDATED_RESPONSE = "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.35\",\"name\":\"Grape\"}]}"; + private static final String GET_UPDATED_RESPONSE = + "{\"items\":[{\"price\":\"$1.55\",\"name\":\"Pear\"},{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.35\",\"name\":\"Grape\"}]}"; private static Node node; @@ -64,7 +67,7 @@ public class CatalogServiceTestCase { @AfterClass public static void destroy() throws Exception { - if(node != null) { + if (node != null) { node.stop(); } } @@ -84,15 +87,29 @@ public class CatalogServiceTestCase { Assert.assertEquals(200, response.getResponseCode()); Assert.assertNotNull(response.getText()); - Assert.assertTrue(validateJsonResponse(GET_RESPONSE,response.getText())); + Assert.assertTrue(validateJsonResponse(GET_RESPONSE, response.getText())); } + @Test + public void testGetInvocationWithFilter() throws Exception { + WebConversation wc = new WebConversation(); + WebRequest request = new GetMethodWebRequest(SERVICE_URL + "?excludedFields=price"); + request.setHeaderField("Content-Type", "application/json"); + WebResponse response = wc.getResource(request); + + Assert.assertEquals(200, response.getResponseCode()); + String json = response.getText(); + Assert.assertNotNull(json); + Assert.assertFalse(json.contains("price")); + } @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"); + WebRequest request = + new PostMethodWebRequest(SERVICE_URL, new ByteArrayInputStream(NEW_ITEM.getBytes("UTF-8")), + "application/json"); request.setHeaderField("Content-Type", "application/json"); WebResponse response = wc.getResource(request); @@ -109,14 +126,16 @@ public class CatalogServiceTestCase { Assert.assertEquals(200, response.getResponseCode()); Assert.assertNotNull(response.getText()); - Assert.assertTrue(validateJsonResponse(GET_NEW_RESPONSE,response.getText())); + Assert.assertTrue(validateJsonResponse(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"); + WebRequest request = + new PostMethodWebRequest(SERVICE_URL, new ByteArrayInputStream(UPDATED_ITEM.getBytes("UTF-8")), + "application/json"); request.setHeaderField("Content-Type", "application/json"); WebResponse response = wc.getResource(request); @@ -133,15 +152,14 @@ public class CatalogServiceTestCase { Assert.assertEquals(200, response.getResponseCode()); Assert.assertNotNull(response.getText()); - Assert.assertTrue(validateJsonResponse(GET_UPDATED_RESPONSE,response.getText())); + Assert.assertTrue(validateJsonResponse(GET_UPDATED_RESPONSE, response.getText())); } - private boolean validateJsonResponse(String expected, String actual) throws JSONException { JSONObject jsonExpected = new JSONObject(expected); JSONObject jsonActual = new JSONObject(actual); - if(jsonExpected.getJSONArray("items").length() != jsonActual.getJSONArray("items").length()) { + if (jsonExpected.getJSONArray("items").length() != jsonActual.getJSONArray("items").length()) { return false; } -- cgit v1.2.3