HttpRequestSensor: handling non-json

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/b571592d
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/b571592d
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/b571592d

Branch: refs/heads/master
Commit: b571592d86dcf8fbf35db0aa6b473ed5cb0e990c
Parents: d5cd336
Author: Aled Sage <aled.s...@gmail.com>
Authored: Fri Aug 4 19:14:57 2017 +0100
Committer: Aled Sage <aled.s...@gmail.com>
Committed: Fri Aug 4 19:23:48 2017 +0100

----------------------------------------------------------------------
 .../core/sensor/http/HttpRequestSensor.java     | 15 +++++-
 .../brooklyn/feed/http/JsonFunctions.java       |  4 +-
 .../core/sensor/http/HttpRequestSensorTest.java | 55 ++++++++++++++++++--
 3 files changed, 69 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b571592d/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java
 
b/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java
index 107d9a4..5485471 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java
@@ -27,15 +27,20 @@ import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.config.MapConfigKey;
 import org.apache.brooklyn.core.effector.AddSensor;
 import org.apache.brooklyn.core.entity.EntityInitializers;
+import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.sensor.ssh.SshCommandSensor;
 import org.apache.brooklyn.feed.http.HttpFeed;
 import org.apache.brooklyn.feed.http.HttpPollConfig;
 import org.apache.brooklyn.feed.http.HttpValueFunctions;
 import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.core.config.ResolvingConfigBag;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.text.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Function;
 import com.google.common.base.Functions;
 import com.google.common.base.Supplier;
 
@@ -92,10 +97,18 @@ public final class HttpRequestSensor<T> extends 
AddSensor<T> {
         final Map<String, String> headers = 
EntityInitializers.resolve(allConfig, HEADERS);
         final Boolean preemptiveBasicAuth = 
EntityInitializers.resolve(allConfig, PREEMPTIVE_BASIC_AUTH);
         
+        Function<? super HttpToolResponse, T> successFunction;
+        if (Strings.isBlank(jsonPath)) {
+            // TODO Should also coerce to type `allConfig.get(SENSOR_TYPE)` 
(would need to class-load that, using the entity's context)
+            successFunction = (Function) 
HttpValueFunctions.stringContentsFunction();
+        } else {
+            successFunction = 
HttpValueFunctions.<T>jsonContentsFromPath(jsonPath);
+        }
+        
         HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor)
                 .checkSuccess(HttpValueFunctions.responseCodeEquals(200))
                 .onFailureOrException(Functions.constant((T) null))
-                
.onSuccess(HttpValueFunctions.<T>jsonContentsFromPath(jsonPath))
+                .onSuccess(successFunction)
                 .period(period);
 
         HttpFeed.Builder httpRequestBuilder = HttpFeed.builder().entity(entity)

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b571592d/core/src/main/java/org/apache/brooklyn/feed/http/JsonFunctions.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/feed/http/JsonFunctions.java 
b/core/src/main/java/org/apache/brooklyn/feed/http/JsonFunctions.java
index a5a9761..0945f14 100644
--- a/core/src/main/java/org/apache/brooklyn/feed/http/JsonFunctions.java
+++ b/core/src/main/java/org/apache/brooklyn/feed/http/JsonFunctions.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.feed.http;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.lang.reflect.Array;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -309,7 +311,7 @@ public class JsonFunctions {
         private final String path;
 
         public GetPath(String path) {
-            this.path = path;
+            this.path = checkNotNull(path, "path");
         }
         @SuppressWarnings("unchecked")
         @Override public T apply(JsonElement input) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b571592d/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java
 
b/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java
index 876b190..bf6ff1e 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java
@@ -47,7 +47,9 @@ import com.google.common.collect.Iterables;
 
 public class HttpRequestSensorTest {
     final static AttributeSensor<String> SENSOR_STRING = 
Sensors.newStringSensor("aString");
-    final static String TARGET_TYPE = "java.lang.String";
+    final static AttributeSensor<Object> SENSOR_OBJ = 
Sensors.newSensor(Object.class, "anObj");
+    final static String STRING_TARGET_TYPE = "java.lang.String";
+    final static String OBJECT_TARGET_TYPE = "java.lang.Object";
 
     private TestApplication app;
     private Entity entity;
@@ -61,6 +63,8 @@ public class HttpRequestSensorTest {
         HttpRequestHandler handler = new 
TestHttpRequestHandler().header("Content-Type", 
"application/json").response("{\"myKey\":\"myValue\"}");
         recordingHandler = new RecordingHttpRequestHandler(handler);
         server = new TestHttpServer()
+            .handler("/nonjson", new 
TestHttpRequestHandler().header("Content-Type", 
"text/plain").response("myresponse"))
+            .handler("/jsonstring", new 
TestHttpRequestHandler().header("Content-Type", 
"application/json").response("\"myValue\""))
             .handler("/myKey/myValue", recordingHandler)
             .start();
         serverUrl = server.getUrl();
@@ -83,7 +87,7 @@ public class HttpRequestSensorTest {
         HttpRequestSensor<Integer> sensor = new 
HttpRequestSensor<Integer>(ConfigBag.newInstance()
                 .configure(HttpRequestSensor.SENSOR_PERIOD, 
Duration.millis(100))
                 .configure(HttpRequestSensor.SENSOR_NAME, 
SENSOR_STRING.getName())
-                .configure(HttpRequestSensor.SENSOR_TYPE, TARGET_TYPE)
+                .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE)
                 .configure(HttpRequestSensor.JSON_PATH, "$.myKey")
                 .configure(HttpRequestSensor.SENSOR_URI, serverUrl + 
"/myKey/myValue"));
         sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity);
@@ -92,6 +96,21 @@ public class HttpRequestSensorTest {
         EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, 
"myValue");
     }
 
+    // TODO Fails because doesn't pick up default value of `JSON_PATH`, which 
is `$`
+    @Test(groups="Broken")
+    @SuppressWarnings("deprecation")
+    public void testDefaultJsonPath() throws Exception {
+        HttpRequestSensor<Integer> sensor = new 
HttpRequestSensor<Integer>(ConfigBag.newInstance()
+                .configure(HttpRequestSensor.SENSOR_PERIOD, 
Duration.millis(100))
+                .configure(HttpRequestSensor.SENSOR_NAME, 
SENSOR_STRING.getName())
+                .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE)
+                .configure(HttpRequestSensor.SENSOR_URI, serverUrl + 
"/jsonstring"));
+        sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity);
+        entity.sensors().set(Attributes.SERVICE_UP, true);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, 
"myValue");
+    }
+
     @Test
     @SuppressWarnings("deprecation")
     public void testPreemptiveBasicAuth() throws Exception {
@@ -101,7 +120,7 @@ public class HttpRequestSensorTest {
                 .configure(HttpRequestSensor.PASSWORD, "mypass")
                 .configure(HttpRequestSensor.SENSOR_PERIOD, 
Duration.minutes(1))
                 .configure(HttpRequestSensor.SENSOR_NAME, 
SENSOR_STRING.getName())
-                .configure(HttpRequestSensor.SENSOR_TYPE, TARGET_TYPE)
+                .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE)
                 .configure(HttpRequestSensor.JSON_PATH, "$.myKey")
                 .configure(HttpRequestSensor.SENSOR_URI, serverUrl + 
"/myKey/myValue"));
         sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity);
@@ -114,4 +133,34 @@ public class HttpRequestSensorTest {
         String expectedVal = HttpFeedTest.getBasicAuthHeaderVal("myuser", 
"mypass");
         assertEquals(headerVal, expectedVal);
     }
+    
+    @Test
+    @SuppressWarnings("deprecation")
+    public void testDoNotParseJson() throws Exception {
+        HttpRequestSensor<Integer> sensor = new 
HttpRequestSensor<Integer>(ConfigBag.newInstance()
+                .configure(HttpRequestSensor.SENSOR_PERIOD, 
Duration.minutes(1))
+                .configure(HttpRequestSensor.SENSOR_NAME, 
SENSOR_STRING.getName())
+                .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE)
+                .configure(HttpRequestSensor.JSON_PATH, "")
+                .configure(HttpRequestSensor.SENSOR_URI, serverUrl + 
"/myKey/myValue"));
+        sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity);
+        entity.sensors().set(Attributes.SERVICE_UP, true);
+        
+        EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, 
"{\"myKey\":\"myValue\"}");
+    }
+    
+    @Test
+    @SuppressWarnings("deprecation")
+    public void testNonJson() throws Exception {
+        HttpRequestSensor<Integer> sensor = new 
HttpRequestSensor<Integer>(ConfigBag.newInstance()
+                .configure(HttpRequestSensor.SENSOR_PERIOD, 
Duration.minutes(1))
+                .configure(HttpRequestSensor.SENSOR_NAME, 
SENSOR_STRING.getName())
+                .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE)
+                .configure(HttpRequestSensor.JSON_PATH, "")
+                .configure(HttpRequestSensor.SENSOR_URI, serverUrl + 
"/nonjson"));
+        sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity);
+        entity.sensors().set(Attributes.SERVICE_UP, true);
+        
+        EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, 
"myresponse");
+    }
 }

Reply via email to