brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/3] brooklyn-server git commit: TestHttpCall entity can now perform requests other than GET, setup headers and body
Date Thu, 10 Nov 2016 14:27:47 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master db21f6a69 -> a8d81ed58


TestHttpCall entity can now perform requests other than GET, setup headers and body


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

Branch: refs/heads/master
Commit: 8f4148592ace33ab26c8c9f13f436dcab84bf9e8
Parents: fd00978
Author: Thomas Bouron <thomas.bouron@cloudsoftcorp.com>
Authored: Tue Nov 8 09:20:48 2016 +0000
Committer: Thomas Bouron <thomas.bouron@cloudsoftcorp.com>
Committed: Wed Nov 9 11:55:22 2016 +0000

----------------------------------------------------------------------
 .../brooklyn/test/framework/TestHttpCall.java   | 29 ++++++++
 .../test/framework/TestHttpCallImpl.java        | 72 ++++++++++++++++++--
 .../org/apache/brooklyn/util/http/HttpTool.java |  2 +-
 3 files changed, 98 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8f414859/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
index 353e8b4..8dd5774 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
@@ -18,11 +18,15 @@
  */
 package org.apache.brooklyn.test.framework;
 
+import java.util.Map;
+
 import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
 
+import com.google.common.reflect.TypeToken;
+
 /**
  * Entity that makes a HTTP Request and tests the response
  */
@@ -32,9 +36,34 @@ public interface TestHttpCall extends BaseTest {
     @SetFromFlag(nullable = false)
     ConfigKey<String> TARGET_URL = ConfigKeys.newStringConfigKey("url", "URL to test");
 
+    @SetFromFlag(nullable = false)
+    ConfigKey<HttpMethod> TARGET_METHOD = ConfigKeys.builder(HttpMethod.class)
+            .name("method")
+            .description("Method to request the URL: GET, POST, PUT, DELETE, etc")
+            .defaultValue(HttpMethod.GET)
+            .build();
+
+    @SetFromFlag
+    ConfigKey<Map<String, String>> TARGET_HEADERS = ConfigKeys.builder(new TypeToken<Map<String,
String>>() {})
+            .name("headers")
+            .description("Headers to add to the request")
+            .build();
+
+    @SetFromFlag
+    ConfigKey<String> TARGET_BODY = ConfigKeys.newStringConfigKey("body", "The request
body to send (only for POST and PUT requests)");
+
     ConfigKey<HttpAssertionTarget> ASSERTION_TARGET = ConfigKeys.newConfigKey(HttpAssertionTarget.class,
"applyAssertionTo",
         "The HTTP field to apply the assertion to [body,status]", HttpAssertionTarget.body);
 
+    enum HttpMethod {
+        GET, POST, PUT, DELETE, HEAD;
+
+        @Override
+        public String toString() {
+            return this.name();
+        }
+    }
+
     enum HttpAssertionTarget {
         body("body"), status("status");
         private final String httpAssertionTarget;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8f414859/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
index 53bec37..acf9f23 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
@@ -20,6 +20,8 @@ package org.apache.brooklyn.test.framework;
 
 import static org.apache.brooklyn.test.framework.TestFrameworkAssertions.getAssertions;
 
+import java.net.URI;
+import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -29,8 +31,11 @@ import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.test.framework.TestFrameworkAssertions.AssertionOptions;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.http.HttpTool;
 import org.apache.brooklyn.util.time.Duration;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpRequestBase;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,11 +55,17 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
      */
     public void start(Collection<? extends Location> locations) {
         String url = null;
+        HttpMethod method = null;
+        Map<String, String> headers = null;
+        String body = null;
         
         ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
 
         try {
             url = getRequiredConfig(TARGET_URL);
+            method = getRequiredConfig(TARGET_METHOD);
+            headers = config().get(TARGET_HEADERS);
+            body = config().get(TARGET_BODY);
             final List<Map<String, Object>> assertions = getAssertions(this,
ASSERTIONS);
             final Duration timeout = getConfig(TIMEOUT);
             final HttpAssertionTarget target = getRequiredConfig(ASSERTION_TARGET);
@@ -62,7 +73,7 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
                 throw new RuntimeException(String.format("The entity [%s] cannot have child
entities", getClass().getName()));
             }
             
-            doRequestAndCheckAssertions(ImmutableMap.of("timeout", timeout), assertions,
target, url);
+            doRequestAndCheckAssertions(ImmutableMap.of("timeout", timeout), assertions,
target, method, url, headers, body);
             setUpAndRunState(true, Lifecycle.RUNNING);
 
         } catch (Throwable t) {
@@ -77,13 +88,18 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
     }
 
     private void doRequestAndCheckAssertions(Map<String, Duration> flags, List<Map<String,
Object>> assertions,
-                                             HttpAssertionTarget target, final String url)
{
+                                             HttpAssertionTarget target, final HttpMethod
method, final String url, final Map<String, String> headers, final String body) {
         switch (target) {
             case body:
                 Supplier<String> getBody = new Supplier<String>() {
                     @Override
                     public String get() {
-                        return HttpTool.getContent(url);
+                        try {
+                            return HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(),
createHttpMethod(method, url, headers, body)).getContentAsString();
+                        } catch (Exception e) {
+                            LOG.info("HTTP call to [{}] failed due to [{}]", url, e.getMessage());
+                            throw Exceptions.propagate(e);
+                        }
                     }
                 };
                 TestFrameworkAssertions.checkAssertionsEventually(new AssertionOptions(target.toString(),
getBody)
@@ -94,7 +110,12 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
                     @Override
                     public Integer get() {
                         try {
-                            return HttpTool.getHttpStatusCode(url);
+                            final Maybe<HttpResponse> response = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(),
createHttpMethod(method, url, headers, body)).getResponse();
+                            if (response.isPresentAndNonNull()) {
+                                return response.get().getStatusLine().getStatusCode();
+                            } else {
+                                throw new Exception("HTTP call did not return any reponse");
+                            }
                         } catch (Exception e) {
                             LOG.info("HTTP call to [{}] failed due to [{}]", url, e.getMessage());
                             throw Exceptions.propagate(e);
@@ -109,6 +130,49 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
         }
     }
 
+    private HttpRequestBase createHttpMethod(HttpMethod method, String url, Map<String,
String> headers, String body) throws Exception {
+        switch (method) {
+            case GET:
+                HttpTool.HttpGetBuilder httpGetBuilder = new HttpTool.HttpGetBuilder(new
URI(url));
+                if (headers != null) {
+                    httpGetBuilder.headers(headers);
+                }
+                return httpGetBuilder.build();
+            case POST:
+                HttpTool.HttpPostBuilder httpPostBuilder = new HttpTool.HttpPostBuilder(new
URI(url));
+                if (headers != null) {
+                    httpPostBuilder.headers(headers);
+                }
+                if (body != null) {
+                    httpPostBuilder.body(body.getBytes(Charset.forName("UTF-8")));
+                }
+                return httpPostBuilder.build();
+            case PUT:
+                HttpTool.HttpPutBuilder httpPutBuilder = new HttpTool.HttpPutBuilder(new
URI(url));
+                if (headers != null) {
+                    httpPutBuilder.headers(headers);
+                }
+                if (body != null) {
+                    httpPutBuilder.body(body.getBytes(Charset.forName("UTF-8")));
+                }
+                return httpPutBuilder.build();
+            case DELETE:
+                HttpTool.HttpDeleteBuilder httpDeleteBuilder = new HttpTool.HttpDeleteBuilder(new
URI(url));
+                if (headers != null) {
+                    httpDeleteBuilder.headers(headers);
+                }
+                return httpDeleteBuilder.build();
+            case HEAD:
+                final HttpTool.HttpHeadBuilder httpHeadBuilder = new HttpTool.HttpHeadBuilder(new
URI(url));
+                if (headers != null) {
+                    httpHeadBuilder.headers(headers);
+                }
+                return httpHeadBuilder.build();
+            default:
+                throw new Exception(method  + " not supported");
+        }
+    }
+
     /**
      * {@inheritDoc}
      */

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8f414859/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java b/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java
index e704886..354ade9 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java
@@ -462,7 +462,7 @@ public class HttpTool {
     }
     
     public static class HttpPostBuilder extends HttpEntityEnclosingRequestBaseBuilder<HttpPostBuilder,
HttpPost> {
-        HttpPostBuilder(URI uri) {
+        public HttpPostBuilder(URI uri) {
             super(new HttpPost(uri));
         }
     }


Mime
View raw message