You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/04/13 15:23:17 UTC

camel git commit: CAMEL-11136: update docs, CS and tests

Repository: camel
Updated Branches:
  refs/heads/master bc6391e91 -> 24f216450


CAMEL-11136: update docs, CS and tests

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/24f21645
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/24f21645
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/24f21645

Branch: refs/heads/master
Commit: 24f2164508f1be78f7df942116ceb3d0d8340003
Parents: bc6391e
Author: Preben Asmussen <pr...@gmail.com>
Authored: Thu Apr 13 17:09:58 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Apr 13 17:23:08 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/pubnub-component.adoc         | 63 ++++++++++++++++--
 .../component/pubnub/PubNubConfiguration.java   |  2 +-
 .../camel/component/pubnub/PubNubConstants.java |  8 +--
 .../camel/component/pubnub/PubNubProducer.java  |  3 +-
 .../component/pubnub/PubNubOperationsTest.java  |  7 +-
 .../component/pubnub/PubNubPublishTest.java     |  8 ---
 .../example/PubNubGeoLocationExample.java       | 23 ++++++-
 .../pubnub/example/PubNubStateExample.java      | 69 ++++++++++++++++++++
 8 files changed, 158 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/main/docs/pubnub-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/main/docs/pubnub-component.adoc b/components/camel-pubnub/src/main/docs/pubnub-component.adoc
index 9523d05..a721368 100644
--- a/components/camel-pubnub/src/main/docs/pubnub-component.adoc
+++ b/components/camel-pubnub/src/main/docs/pubnub-component.adoc
@@ -33,7 +33,7 @@ for this component:
 
 [source,java]
 ------------------------
-pubnub://channel[?options]
+pubnub:channel[?options]
 ------------------------
 
 Where *channel* is the PubNub channel to publish or subscribe to.
@@ -72,7 +72,7 @@ with the following path and query parameters:
 | **withPresence** (consumer) | Also subscribe to related presence information | false | boolean
 | **exceptionHandler** (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | **exchangePattern** (consumer) | Sets the exchange pattern when the consumer creates an exchange. |  | ExchangePattern
-| **operation** (producer) | The operation to perform. PUBLISH: Default. Send a message to all subscribers of a channel. FIRE: allows the client to send a message to BLOCKS Event Handlers. These messages will go directly to any Event Handlers registered on the channel that you fire to and will trigger their execution HERENOW: Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count. WHERENOW: Obtain information about the current list of channels to which a uuid is subscribed to. GETSTATE: Used to get key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs SETSTATE: Used to set key/value pairs specific to a subscriber uuid GETHISTORY: Fetches historical messages of a channel. |  | String
+| **operation** (producer) | The operation to perform. PUBLISH: Default. Send a message to all subscribers of a channel. FIRE: allows the client to send a message to BLOCKS Event Handlers. These messages will go directly to any Event Handlers registered on the channel. HERENOW: Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count. WHERENOW: Obtain information about the current list of channels to which a uuid is subscribed to. GETSTATE: Used to get key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs SETSTATE: Used to set key/value pairs specific to a subscriber uuid GETHISTORY: Fetches historical messages of a channel. |  | String
 | **pubnub** (advanced) | Reference to a Pubnub client in the registry. |  | PubNub
 | **synchronous** (advanced) | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). | false | boolean
 | **authKey** (security) | If Access Manager is utilized client will use this authKey in all restricted requests. |  | String
@@ -107,7 +107,7 @@ When receiving the message body utilize objects provided by the PubNub API.
 #### Publishing events
 
 Default operation when producing.
-The following snippet publish the event DeviceInfo to the channel iot.
+The following snippet publish the event generated by PojoBean to the channel iot.
 
 [source,java]
 ----
@@ -117,6 +117,22 @@ from("timer:mytimer")
     .to("pubnub:iot?publishKey=mypublishKey");
 ----
 
+#### Fire events aka BLOCKS Event Handlers
+
+See https://www.pubnub.com/blocks-catalog/ for all kind of serverless functions that can be invoked.
+Example of geolocation lookup 
+
+[source,java]
+----
+from("timer:geotimer")
+    .process(exchange -> exchange.getIn().setBody(new Foo("bar", "TEXT")))
+    .to("pubnub:eon-maps-geolocation-input?operation=fire&publishKey=mypubkey&subscribeKey=mysubkey");
+
+from("pubnub:eon-map-geolocation-output?subscribeKey=mysubkey)
+    // geolocation output will be logged here
+    .log("${body}");
+----
+
 #### Subscribing to events
 
 The following snippet listens for events on the iot channel.
@@ -129,6 +145,45 @@ from("pubnub:iot?subscribeKey=mySubscribeKey")
     .to("mock:result");
 ----
 
+#### Performing operations
+
+
+herenow : Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel
+
+[source,java]
+----
+from("direct:control")
+    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=herenow")
+    .to("mock:result");
+----
+
+wherenow : Obtain information about the current list of channels to which a uuid is subscribed
+
+[source,java]
+----
+from("direct:control")
+    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=wherenow&uuid=spyonme")
+    .to("mock:result");
+----
+
+setstate : Used to set key/value pairs specific to a subscriber uuid.
+
+[source,java]
+----
+from("direct:control")
+    .bean(StateGenerator.class)
+    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=setstate&uuid=myuuid");
+----
+
+gethistory : Fetches historical messages of a channel.
+
+[source,java]
+----
+from("direct:control")
+    .to("pubnub:myChannel?publishKey=mypublishKey&subscribeKey=mySubscribeKey&operation=gethistory");
+----
+
+
 There is a couple of examples in test directory that shows some of the PubNub features. 
 They require a PubNub account, from where you can obtain a publish- and subscribe key.
  
@@ -143,4 +198,4 @@ The example illustrates the PubNub component subscribing to a infinite stream of
 * link:endpoint.html[Endpoint]
 * link:getting-started.html[Getting Started]
 
-* link:rss.html[RSS]
\ No newline at end of file
+* link:rss.html[RSS]

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConfiguration.java b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConfiguration.java
index 88ae43c..ad68388 100644
--- a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConfiguration.java
+++ b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConfiguration.java
@@ -147,7 +147,7 @@ public class PubNubConfiguration {
      * The operation to perform.
      * <ul>
      * <li>PUBLISH: Default. Send a message to all subscribers of a channel.</li>
-     * <li>FIRE: allows the client to send a message to BLOCKS Event Handlers. These messages will go directly to any Event Handlers registered on the channel that you fire to and will trigger their execution,</li>
+     * <li>FIRE: allows the client to send a message to BLOCKS Event Handlers. These messages will go directly to any Event Handlers registered on the channel.</li>
      * <li>HERENOW: Obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count.</li>
      * <li>WHERENOW: Obtain information about the current list of channels to which a uuid is subscribed to.</li>
      * <li>GETSTATE: Used to get key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs</li>

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConstants.java b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConstants.java
index 9bfdb58..4294a4b 100644
--- a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConstants.java
+++ b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubConstants.java
@@ -17,8 +17,8 @@
 package org.apache.camel.component.pubnub;
 
 public abstract class PubNubConstants {
-    public static String OPERATION = "CamelPubNubOperation";
-    public static String TIMETOKEN = "CamelPubNubTimeToken";
-    public static String CHANNEL = "CamelPubNubChannel";
-    public static String UUID = "CamelPubNubUUID";
+    public static final String OPERATION = "CamelPubNubOperation";
+    public static final String TIMETOKEN = "CamelPubNubTimeToken";
+    public static final String CHANNEL = "CamelPubNubChannel";
+    public static final String UUID = "CamelPubNubUUID";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java
index bb20e1b..bc5bba4 100644
--- a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java
+++ b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java
@@ -185,7 +185,7 @@ public class PubNubProducer extends DefaultAsyncProducer {
                 @Override
                 public void onResponse(PNGetStateResult result, PNStatus status) {
                     LOG.debug("Got state [{}]", result.getStateByUUID());
-                    processMessage(exchange, callback, status, result.getStateByUUID());
+                    processMessage(exchange, callback, status, result);
                 }
             });
     }
@@ -221,7 +221,6 @@ public class PubNubProducer extends DefaultAsyncProducer {
     private void processMessage(Exchange exchange, AsyncCallback callback, PNStatus status, Object body) {
         if (status.isError()) {
             PNErrorData errorData = status.getErrorData();
-            callback.done(false);
             exchange.setException(errorData.getThrowable());
             if (errorData != null && errorData.getThrowable() instanceof PubNubException) {
                 PubNubException pubNubException = (PubNubException) errorData.getThrowable();

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubOperationsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubOperationsTest.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubOperationsTest.java
index 4258ff1..7fd1add 100644
--- a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubOperationsTest.java
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubOperationsTest.java
@@ -23,8 +23,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import com.google.gson.JsonObject;
 import com.pubnub.api.models.consumer.history.PNHistoryItemResult;
+import com.pubnub.api.models.consumer.presence.PNGetStateResult;
 import com.pubnub.api.models.consumer.presence.PNHereNowResult;
 import com.pubnub.api.models.consumer.presence.PNSetStateResult;
 
@@ -109,10 +109,9 @@ public class PubNubOperationsTest extends PubNubTestBase {
                       + "{ \"myChannel\": { \"age\" : 20, \"status\" : \"online\"}, \"ch2\": { \"age\": 100, \"status\": \"offline\" } }, \"service\": \"Presence\"}")));
         Map<String, Object> headers = new HashMap<String, Object>();
         headers.put(PubNubConstants.OPERATION, "GETSTATE");
-        @SuppressWarnings("unchecked")
-        Map<String, JsonObject> response = template.requestBodyAndHeaders("direct:publish", null, headers, Map.class);
+        PNGetStateResult response = template.requestBodyAndHeaders("direct:publish", null, headers, PNGetStateResult.class);
         assertNotNull(response);
-        assertNotNull(response.get("myChannel"));
+        assertNotNull(response.getStateByUUID().get("myChannel"));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubPublishTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubPublishTest.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubPublishTest.java
index 199e0f9..7d602f3 100644
--- a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubPublishTest.java
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/PubNubPublishTest.java
@@ -46,14 +46,6 @@ public class PubNubPublishTest extends PubNubTestBase {
     }
 
     @Test(expected = CamelExecutionException.class)
-    public void testPubNubException() throws Exception {
-        stubFor(post(urlPathEqualTo("/publish/myPublishKey/mySubscribeKey/0/someChannel/0")).willReturn(aResponse().withStatus(401).withBody("[0,\"Blabla\",\"14598111595318003\"]")));
-
-        template.sendBody("direct:publish", new Hello("Hi"));
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test(expected = CamelExecutionException.class)
     public void testPublishEmptyBody() throws Exception {
         template.sendBody("direct:publish", null);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubGeoLocationExample.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubGeoLocationExample.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubGeoLocationExample.java
index 09fb118..0aa4d78 100644
--- a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubGeoLocationExample.java
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubGeoLocationExample.java
@@ -1,3 +1,19 @@
+/**
+ * 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.pubnub.example;
 
 import org.apache.camel.builder.RouteBuilder;
@@ -10,7 +26,10 @@ import static org.apache.camel.component.pubnub.example.PubNubExampleConstants.P
  * Example of the use of GeoLocation Blocks https://www.pubnub.com/blocks-catalog/geolocation/
  */
 
-public class PubNubGeoLocationExample {
+public final class PubNubGeoLocationExample {
+
+    private PubNubGeoLocationExample() {
+    }
 
     public static void main(String[] args) throws Exception {
         Main main = new Main();
@@ -34,7 +53,7 @@ public class PubNubGeoLocationExample {
         String foo;
         String text;
 
-        public Foo(String foo, String text) {
+        Foo(String foo, String text) {
             super();
             this.foo = foo;
             this.text = text;

http://git-wip-us.apache.org/repos/asf/camel/blob/24f21645/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubStateExample.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubStateExample.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubStateExample.java
new file mode 100644
index 0000000..984801b
--- /dev/null
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubStateExample.java
@@ -0,0 +1,69 @@
+/**
+ * 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.pubnub.example;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.pubnub.api.models.consumer.presence.PNGetStateResult;
+import com.pubnub.api.models.consumer.presence.PNSetStateResult;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.pubnub.PubNubConstants;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.apache.camel.component.pubnub.example.PubNubExampleConstants.PUBNUB_PUBLISH_KEY;
+import static org.apache.camel.component.pubnub.example.PubNubExampleConstants.PUBNUB_SUBSCRIBE_KEY;
+
+@Ignore("Integration test that requires a pub/sub key to run")
+public class PubNubStateExample extends CamelTestSupport {
+
+    @Test
+    public void testStateChange() throws Exception {
+        Map<String, Object> myState = new HashMap<>();
+        myState.put("state", "online");
+        myState.put("name", "preben");
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(PubNubConstants.OPERATION, "SETSTATE");
+        PNSetStateResult response = template.requestBodyAndHeaders("direct:publish", myState, headers, PNSetStateResult.class);
+        assertNotNull(response);
+        assertNotNull(response.getState());
+        assertEquals("preben", response.getState().getAsJsonObject().get("name").getAsString());
+
+        resetMocks();
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        headers.clear();
+        headers.put(PubNubConstants.OPERATION, "GETSTATE");
+        PNGetStateResult getStateResult = template.requestBodyAndHeader("direct:publish", null, PubNubConstants.OPERATION, "GETSTATE", PNGetStateResult.class);
+        assertMockEndpointsSatisfied();
+        assertEquals("preben", getStateResult.getStateByUUID().get("iot").getAsJsonObject().get("name").getAsString());
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:publish").to("pubnub:iot?uuid=myuuid&publishKey=" + PUBNUB_PUBLISH_KEY + "&subscribeKey=" + PUBNUB_SUBSCRIBE_KEY)
+                    .to("mock:result");
+            }
+        };
+    }
+
+}