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 2018/05/29 08:54:24 UTC

[camel] 01/03: CAMEL-12544 - Camel-Couchdb: Add a get method to have a complete CRUD support

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

commit ea12f9ca7e984006c572dbb2c28fb010e75340f9
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Tue May 29 10:49:42 2018 +0200

    CAMEL-12544 - Camel-Couchdb: Add a get method to have a complete CRUD support
---
 .../component/couchdb/CouchDbClientWrapper.java    |  4 ++++
 .../camel/component/couchdb/CouchDbOperations.java | 22 ++++++++++++++++++++++
 .../camel/component/couchdb/CouchDbProducer.java   | 21 ++++++++++++++++++++-
 .../component/couchdb/CouchDbProducerTest.java     | 16 ++++++++++++++++
 4 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbClientWrapper.java b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbClientWrapper.java
index 6dc492e..6919f18 100644
--- a/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbClientWrapper.java
+++ b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbClientWrapper.java
@@ -48,6 +48,10 @@ public class CouchDbClientWrapper {
     public Changes changes() {
         return client.changes();
     }
+    
+    public Object get(String id) {
+    	return client.find(id);
+    }
 
     public CouchDbContext context() {
         return client.context();
diff --git a/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbOperations.java b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbOperations.java
new file mode 100644
index 0000000..788c9ba
--- /dev/null
+++ b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbOperations.java
@@ -0,0 +1,22 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.couchdb;
+
+public enum CouchDbOperations {
+	DELETE,
+	GET
+}
diff --git a/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbProducer.java b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbProducer.java
index d5d5f95..0d3cbb9 100644
--- a/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbProducer.java
+++ b/components/camel-couchdb/src/main/java/org/apache/camel/component/couchdb/CouchDbProducer.java
@@ -51,7 +51,7 @@ public class CouchDbProducer extends DefaultProducer {
             exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, save.getRev());
             exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, save.getId());
         } else {
-            if (operation.equalsIgnoreCase("DELETE")) {
+            if (operation.equalsIgnoreCase(CouchDbOperations.DELETE.toString())) {
                 Response delete = deleteJsonElement(json);
                 if (delete == null) {
                     throw new CouchDbException("Could not delete document [unknown reason]", exchange);
@@ -63,6 +63,19 @@ public class CouchDbProducer extends DefaultProducer {
                 exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, delete.getRev());
                 exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, delete.getId());
             }
+            if (operation.equalsIgnoreCase(CouchDbOperations.GET.toString())) {
+                String docId = exchange.getIn().getHeader(CouchDbConstants.HEADER_DOC_ID, String.class);
+                if (docId == null) {
+                    throw new CouchDbException("Could not get document, document id is missing", exchange);
+                }
+                Object response = getElement(docId);
+
+                if (log.isTraceEnabled()) {
+                    log.trace("Document retrieved [_id={}]", docId);
+                }
+                
+                exchange.getIn().setBody(response);
+            }
         }
     }
 
@@ -106,4 +119,10 @@ public class CouchDbProducer extends DefaultProducer {
         }
         return delete;
     }
+    
+    private Object getElement(String id) {
+        Object response;
+        response = couchClient.get(id);
+        return response;
+    }   
 }
diff --git a/components/camel-couchdb/src/test/java/org/apache/camel/component/couchdb/CouchDbProducerTest.java b/components/camel-couchdb/src/test/java/org/apache/camel/component/couchdb/CouchDbProducerTest.java
index 5fceae2..46f49aa 100644
--- a/components/camel-couchdb/src/test/java/org/apache/camel/component/couchdb/CouchDbProducerTest.java
+++ b/components/camel-couchdb/src/test/java/org/apache/camel/component/couchdb/CouchDbProducerTest.java
@@ -114,6 +114,22 @@ public class CouchDbProducerTest {
         verify(msg).setHeader(CouchDbConstants.HEADER_DOC_ID, id);
         verify(msg).setHeader(CouchDbConstants.HEADER_DOC_REV, rev);
     }
+    
+    @Test
+    public void testGetResponse() throws Exception {
+        String id = UUID.randomUUID().toString();
+
+        JsonObject doc = new JsonObject();
+        doc.addProperty("_id", id);
+
+        when(msg.getHeader(CouchDbConstants.HEADER_METHOD, String.class)).thenReturn("GET");
+        when(msg.getHeader(CouchDbConstants.HEADER_DOC_ID, String.class)).thenReturn(id);
+        when(msg.getMandatoryBody()).thenReturn(doc);
+        when(client.get(id)).thenReturn(response);
+
+        producer.process(exchange);
+        verify(msg).getHeader(CouchDbConstants.HEADER_DOC_ID, String.class);
+    }
 
     @Test
     public void testStringBodyIsConvertedToJsonTree() throws Exception {

-- 
To stop receiving notification emails like this one, please contact
acosentino@apache.org.