You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/11/27 12:20:25 UTC

[camel] branch master updated: [CAMEL-12036] camel-olingo4 support json string as payload

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 05a2a37  [CAMEL-12036] camel-olingo4 support json string as payload
05a2a37 is described below

commit 05a2a37214965bef1d9c3bfa696d1ce433ee47fc
Author: jpoth <po...@gmail.com>
AuthorDate: Fri Nov 24 16:44:51 2017 +0100

    [CAMEL-12036] camel-olingo4 support json string as payload
---
 .../component/olingo4/api/impl/Olingo4AppImpl.java | 15 ++++----
 .../olingo4/AbstractOlingo4TestSupport.java        |  2 +-
 .../component/olingo4/Olingo4ComponentTest.java    | 40 ++++++++++++++++++++++
 3 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java b/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
index c8c685d..eec95e5 100644
--- a/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
+++ b/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
@@ -62,6 +62,7 @@ import org.apache.http.concurrent.FutureCallback;
 import org.apache.http.config.MessageConstraints;
 import org.apache.http.entity.AbstractHttpEntity;
 import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
@@ -476,13 +477,9 @@ public final class Olingo4AppImpl implements Olingo4App {
                             UriResourceKind lastResourceKind = listResource.get(listResource.size() - 1).getKind();
                             switch (lastResourceKind) {
                             case entitySet:
-                                if (content instanceof ClientEntity) {
-                                    ClientEntity entity = odataReader.readEntity(result.getEntity().getContent(),
-                                                                                 ContentType.parse(result.getEntity().getContentType().getValue()));
-                                    responseHandler.onResponse((T)entity, headersToMap(result.getAllHeaders()));
-                                } else {
-                                    throw new ODataException("Unsupported content type: " + content);
-                                }
+                                ClientEntity entity = odataReader.readEntity(result.getEntity().getContent(),
+                                                                             ContentType.parse(result.getEntity().getContentType().getValue()));
+                                responseHandler.onResponse((T)entity, headersToMap(result.getAllHeaders()));
                                 break;
                             default:
                                 break;
@@ -515,6 +512,10 @@ public final class Olingo4AppImpl implements Olingo4App {
             case entitySet:
                 if (content instanceof ClientEntity) {
                     requestStream = odataWriter.writeEntity((ClientEntity)content, getResourceContentType(uriInfo));
+                } else if (content instanceof String) {
+                    httpEntity = new StringEntity((String) content, org.apache.http.entity.ContentType.APPLICATION_JSON);
+                    httpEntity.setChunked(false);
+                    return httpEntity;
                 } else {
                     throw new ODataException("Unsupported content type: " + content);
                 }
diff --git a/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/AbstractOlingo4TestSupport.java b/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/AbstractOlingo4TestSupport.java
index 48b5d66..b4073c0 100644
--- a/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/AbstractOlingo4TestSupport.java
+++ b/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/AbstractOlingo4TestSupport.java
@@ -70,7 +70,7 @@ public class AbstractOlingo4TestSupport extends CamelTestSupport {
      * Every request to the demo OData 4.0
      * (http://services.odata.org/TripPinRESTierService) generates unique
      * service URL with postfix like (S(tuivu3up5ygvjzo5fszvnwfv)) for each
-     * session This method makes reuest to the base URL and return URL with
+     * session This method makes request to the base URL and return URL with
      * generated postfix
      */
     @SuppressWarnings("deprecation")
diff --git a/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/Olingo4ComponentTest.java b/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/Olingo4ComponentTest.java
index 63ed723..c886af2 100644
--- a/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/Olingo4ComponentTest.java
+++ b/components/camel-olingo4/camel-olingo4-component/src/test/java/org/apache/camel/component/olingo4/Olingo4ComponentTest.java
@@ -62,6 +62,17 @@ public class Olingo4ComponentTest extends AbstractOlingo4TestSupport {
     private static final String TEST_CREATE_PEOPLE = PEOPLE + "(" + TEST_CREATE_KEY + ")";
     private static final String TEST_CREATE_RESOURCE_CONTENT_ID = "1";
     private static final String TEST_UPDATE_RESOURCE_CONTENT_ID = "2";
+    private static final String TEST_CREATE_JSON = "{\n"
+            + "  \"UserName\": \"lewisblack\",\n"
+            + "  \"FirstName\": \"Lewis\",\n"
+            + "  \"LastName\": \"Black\"\n"
+            + "}";
+    private static final String TEST_UPDATE_JSON = "{\n"
+            + "  \"UserName\": \"lewisblack\",\n"
+            + "  \"FirstName\": \"Lewis\",\n"
+            + "  \"MiddleName\": \"Black\",\n"
+            + "  \"LastName\": \"Black\"\n"
+            + "}";
 
     @Test
     public void testRead() throws Exception {
@@ -139,6 +150,35 @@ public class Olingo4ComponentTest extends AbstractOlingo4TestSupport {
         }
     }
 
+    @Test
+    public void testCreateUpdateDeleteFromJson() throws Exception {
+        ClientEntity entity = requestBody("direct://create-entity", TEST_CREATE_JSON);
+        assertNotNull(entity);
+        assertEquals("Lewis", entity.getProperty("FirstName").getValue().toString());
+        assertEquals("Black", entity.getProperty("LastName").getValue().toString());
+        assertEquals("lewisblack", entity.getProperty("UserName").getValue().toString());
+        assertEquals("", entity.getProperty("MiddleName").getValue().toString());
+
+        // update
+        HttpStatusCode status = requestBody("direct://update-entity", TEST_UPDATE_JSON);
+        assertNotNull("Update status", status);
+        assertEquals("Update status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
+        LOG.info("Update entity status: {}", status);
+
+        // delete
+        status = requestBody("direct://delete-entity", null);
+        assertNotNull("Delete status", status);
+        assertEquals("Delete status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
+        LOG.info("Delete status: {}", status);
+
+        // check for delete
+        try {
+            requestBody("direct://read-deleted-entity", null);
+        } catch (CamelExecutionException e) {
+            assertEquals("Resource Not Found [HTTP/1.1 404 Not Found]", e.getCause().getMessage());
+        }
+    }
+
     private ClientEntity createEntity() {
         ClientEntity clientEntity = objFactory.newEntity(null);
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].