brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/2] brooklyn-server git commit: Package name change due to collision when used inside OSGI
Date Tue, 06 Sep 2016 16:19:22 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master 76177f056 -> f97b9eb9b


Package name change due to collision when used inside OSGI

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

Branch: refs/heads/master
Commit: ad853a0675366ade10b4eca613d22a688fd945fa
Parents: 76177f0
Author: Yavor Yanchev <yavor@yanchev.com>
Authored: Tue Sep 6 18:33:55 2016 +0300
Committer: Yavor Yanchev <yavor@yanchev.com>
Committed: Tue Sep 6 18:33:55 2016 +0300

----------------------------------------------------------------------
 .../org/apache/brooklyn/feed/http/HttpFeed.java |   2 +-
 .../util/executor/HttpExecutorFactory.java      |  31 +++
 .../util/executor/HttpExecutorFactoryImpl.java  |  72 +++++++
 .../util/http/executor/HttpExecutorFactory.java |  29 ---
 .../http/executor/HttpExecutorFactoryImpl.java  |  71 ------
 .../util/executor/HttpExecutorImplTest.java     | 216 +++++++++++++++++++
 .../http/executor/HttpExecutorImplTest.java     | 210 ------------------
 7 files changed, 320 insertions(+), 311 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java b/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
index 0396204..a0566ad 100644
--- a/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
+++ b/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
@@ -43,12 +43,12 @@ import org.apache.brooklyn.core.feed.DelegatingPollHandler;
 import org.apache.brooklyn.core.feed.Poller;
 import org.apache.brooklyn.core.location.Locations;
 import org.apache.brooklyn.core.location.Machines;
+import org.apache.brooklyn.util.executor.HttpExecutorFactory;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.http.HttpToolResponse;
 import org.apache.brooklyn.util.http.executor.UsernamePassword;
 import org.apache.brooklyn.util.http.executor.HttpConfig;
 import org.apache.brooklyn.util.http.executor.HttpExecutor;
-import org.apache.brooklyn.util.http.executor.HttpExecutorFactory;
 import org.apache.brooklyn.util.http.executor.HttpRequest;
 import org.apache.brooklyn.util.http.executor.HttpResponse;
 import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactory.java
b/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactory.java
new file mode 100644
index 0000000..4350cf4
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactory.java
@@ -0,0 +1,31 @@
+/*
+ * 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.brooklyn.util.executor;
+
+import java.util.Map;
+
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+
+public interface HttpExecutorFactory {
+    public static final String HTTP_EXECUTOR_CLASS_CONFIG = "httpExecutorClass";
+
+    public static final String HTTP_EXECUTOR_CLASS_CONFIG_PREFIX = "httpExecutorClass.";
+
+    HttpExecutor getHttpExecutor(Map<?, ?> props);
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactoryImpl.java
b/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactoryImpl.java
new file mode 100644
index 0000000..b652dba
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/util/executor/HttpExecutorFactoryImpl.java
@@ -0,0 +1,72 @@
+/*
+ * 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.brooklyn.util.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+    private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+    public HttpExecutorFactoryImpl() {
+        // no-op
+    }
+
+    @Override
+    public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+        HttpExecutor httpExecutor;
+
+        String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS_CONFIG);
+        if (httpExecutorClass != null) {
+            Map<String,Object> httpExecutorProps = MutableMap.of();
+            Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+            if (executorProps.size() > 0) {
+                for (Entry<?, ?> entry: executorProps.entrySet()) {
+                    String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+                    httpExecutorProps.put(keyName, entry.getValue());
+                }
+            }
+            try {
+                httpExecutor = (HttpExecutor) new ClassLoaderUtils(getClass()).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
+            } catch (Exception e) {
+                throw Exceptions.propagate(e);
+            }
+
+        } else {
+            LOG.info(HTTP_EXECUTOR_CLASS_CONFIG + " parameter not provided. Using the default
implementation " + HttpExecutorImpl.class.getName());
+            httpExecutor = HttpExecutorImpl.newInstance();
+        }
+
+        return httpExecutor;
+    }
+}
+
+

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactory.java
b/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactory.java
deleted file mode 100644
index 2a043a8..0000000
--- a/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactory.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * 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.brooklyn.util.http.executor;
-
-import java.util.Map;
-
-public interface HttpExecutorFactory {
-    public static final String HTTP_EXECUTOR_CLASS_CONFIG = "httpExecutorClass";
-
-    public static final String HTTP_EXECUTOR_CLASS_CONFIG_PREFIX = "httpExecutorClass.";
-
-    HttpExecutor getHttpExecutor(Map<?, ?> props);
-}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java
b/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java
deleted file mode 100644
index 8d8ed7a..0000000
--- a/core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * 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.brooklyn.util.http.executor;
-
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.core.ClassLoaderUtils;
-import org.apache.brooklyn.util.exceptions.Exceptions;
-import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
-import org.apache.brooklyn.util.text.StringPredicates;
-import org.apache.brooklyn.util.text.Strings;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.Maps;
-
-public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
-    private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
-
-    public HttpExecutorFactoryImpl() {
-        // no-op
-    }
-
-    @Override
-    public HttpExecutor getHttpExecutor(Map<?, ?> props) {
-        HttpExecutor httpExecutor;
-
-        String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS_CONFIG);
-        if (httpExecutorClass != null) {
-            Map<String,Object> httpExecutorProps = MutableMap.of();
-            Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
-            if (executorProps.size() > 0) {
-                for (Entry<?, ?> entry: executorProps.entrySet()) {
-                    String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
-                    httpExecutorProps.put(keyName, entry.getValue());
-                }
-            }
-            try {
-                httpExecutor = (HttpExecutor) new ClassLoaderUtils(getClass()).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
-            } catch (Exception e) {
-                throw Exceptions.propagate(e);
-            }
-
-        } else {
-            LOG.info(HTTP_EXECUTOR_CLASS_CONFIG + " parameter not provided. Using the default
implementation " + HttpExecutorImpl.class.getName());
-            httpExecutor = HttpExecutorImpl.newInstance();
-        }
-
-        return httpExecutor;
-    }
-}
-
-

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/test/java/org/apache/brooklyn/util/executor/HttpExecutorImplTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/util/executor/HttpExecutorImplTest.java
b/core/src/test/java/org/apache/brooklyn/util/executor/HttpExecutorImplTest.java
new file mode 100644
index 0000000..5837b4b
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/util/executor/HttpExecutorImplTest.java
@@ -0,0 +1,216 @@
+/*
+ * 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.brooklyn.util.executor;
+
+import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+
+import java.net.URL;
+import java.util.Map;
+import java.util.Random;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Collections2;
+import org.apache.brooklyn.util.core.http.BetterMockWebServer;
+import org.apache.brooklyn.util.executor.HttpExecutorFactoryImpl;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpRequestImpl;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.brooklyn.util.http.executor.UsernamePassword;
+import org.apache.commons.codec.binary.Base64;
+import org.bouncycastle.util.Strings;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.io.ByteStreams;
+import com.google.mockwebserver.MockResponse;
+import com.google.mockwebserver.RecordedRequest;
+
+import javax.annotation.Nullable;
+
+public class HttpExecutorImplTest {
+    protected BetterMockWebServer server;
+    protected URL baseUrl;
+    protected HttpExecutorFactoryImpl factory;
+
+    protected String HTTP_RESPONSE_HEADER_KEY = "content-type";
+    protected String HTTP_RESPONSE_HEADER_VALUE = "application/json";
+    protected String HTTP_BODY = "{\"foo\":\"myfoo\"}";
+
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        factory = new HttpExecutorFactoryImpl();
+        server = BetterMockWebServer.newInstanceLocalhost();
+        server.play();
+        baseUrl = server.getUrl("/");
+    }
+
+    @AfterMethod(alwaysRun=true)
+    public void afterMethod() throws Exception {
+        if (server != null) server.shutdown();
+    }
+
+    @Test
+    public void testHttpRequest() throws Exception {
+        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        HttpExecutor executor = factory.getHttpExecutor(getProps());
+        HttpRequest executorRequest = new HttpRequest.Builder()
+                .method("GET")
+                .uri(baseUrl.toURI())
+                .build();
+        HttpResponse executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+    }
+
+    @Test
+    public void testHttpPostRequest() throws Exception {
+        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        HttpExecutor executor = factory.getHttpExecutor(getProps());
+        HttpRequest executorRequest = new HttpRequest.Builder().headers(ImmutableMap.of("RequestHeader",
"RequestHeaderValue"))
+                .method("POST")
+                .body(HTTP_BODY.getBytes())
+                .uri(baseUrl.toURI())
+                .build();
+        HttpResponse executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+
+        // Big POST request with random bytes
+        serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY
+ "Test", HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        executor = factory.getHttpExecutor(getProps());
+        byte[] requestBody = new byte[10 * 1024 * 1024];
+        Random r = new Random();
+        for (int i = 0; i < requestBody.length; i++) {
+            requestBody[i] = (byte)(r.nextInt() % 256);
+        }
+        executorRequest = new HttpRequest.Builder()
+                .method("POST")
+                .body(requestBody)
+                .uri(baseUrl.toURI())
+                .build();
+        executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+    }
+
+    @Test
+    public void testHttpPutRequest() throws Exception {
+        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        HttpExecutor executor = factory.getHttpExecutor(getProps());
+        HttpRequest executorRequest = new HttpRequest.Builder()
+                .method("PUT")
+                .uri(baseUrl.toURI())
+                .build();
+        HttpResponse executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+    }
+
+    @Test
+    public void testHttpDeleteRequest() throws Exception {
+        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        HttpExecutor executor = factory.getHttpExecutor(getProps());
+        HttpRequest executorRequest = new HttpRequest.Builder()
+                .method("DELETE")
+                .uri(baseUrl.toURI())
+                .build();
+        HttpResponse executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+
+        // No Headers returned
+        serverResponse = new MockResponse().setResponseCode(200).setBody(HTTP_BODY);
+        server.enqueue(serverResponse);
+        executor = factory.getHttpExecutor(getProps());
+        executorRequest = new HttpRequest.Builder()
+                .method("DELETE")
+                .uri(baseUrl.toURI())
+                .build();
+        executorResponse = executor.execute(executorRequest);
+        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
+    }
+
+    @Test
+    public void testHttpPasswordRequest() throws Exception {
+        MockResponse firstServerResponse = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate",
"Basic realm=\"User Visible Realm\"").setBody("Not Authenticated");
+        server.enqueue(firstServerResponse);
+        MockResponse secondServerResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
+        server.enqueue(secondServerResponse);
+        final String USER = "brooklyn",
+                PASSWORD = "apache";
+        String authUnencoded = USER + ":" + PASSWORD;
+        String authEncoded = Base64.encodeBase64String(Strings.toByteArray(authUnencoded));
+        HttpExecutor executor = factory.getHttpExecutor(getProps());
+        HttpRequest executorRequest = new HttpRequest.Builder().headers(ImmutableMap.of("RequestHeader",
"RequestHeaderValue"))
+                .method("GET")
+                .uri(baseUrl.toURI())
+                .credentials(new UsernamePassword("brooklyn", "apache"))
+                .build();
+        HttpResponse executorResponse = executor.execute(executorRequest);
+
+        RecordedRequest recordedFirstRequest = server.takeRequest();
+        RecordedRequest recordedSecondRequest = server.takeRequest();
+
+        assertRequestAndResponse(recordedFirstRequest,
+                firstServerResponse,
+                executorRequest,
+                new HttpResponse.Builder()
+                        .header("WWW-Authenticate", "Basic realm=\"User Visible Realm\"")
+                        .header("Content-Length", "" + "Not Authenticated".length())
+                        .content("Not Authenticated".getBytes())
+                        .build());
+        ArrayListMultimap newHeaders = ArrayListMultimap.create(executorRequest.headers());
+        newHeaders.put("Authorization", "Basic " + authEncoded);
+        assertRequestAndResponse(recordedSecondRequest,
+                secondServerResponse,
+                new HttpRequest.Builder().from((HttpRequestImpl)executorRequest).headers(newHeaders).build(),
+                executorResponse);
+    }
+
+    private void assertRequestAndResponse(RecordedRequest serverRequest, MockResponse serverResponse,
HttpRequest executorRequest, HttpResponse executorResponse) throws Exception {
+        assertEquals(serverRequest.getMethod(), executorRequest.method());
+        Function<Map.Entry<String, String>, String> headersMapper = new Function<Map.Entry<String,
String>, String>() {
+            @Nullable
+            @Override
+            public String apply(@Nullable Map.Entry<String, String> input) {
+                return input.getKey() + ": " + input.getValue();
+            }
+        };
+        assertTrue(serverRequest.getHeaders().containsAll(Collections2.transform(executorRequest.headers().entries(),
headersMapper)));
+        assertEquals(serverRequest.getPath(), executorRequest.uri().getPath());
+        if (executorRequest.body() != null) {
+            assertEquals(serverRequest.getBody(), executorRequest.body());
+        } else {
+            assertEquals(serverRequest.getBody(), new byte[0]);
+        }
+
+        assertEquals(serverResponse.getBody(), ByteStreams.toByteArray(executorResponse.getContent()));
+        assertTrue(serverResponse.getHeaders().containsAll(Collections2.transform(executorResponse.headers().entries(),
headersMapper)));
+        assertTrue(Collections2.transform(executorResponse.headers().entries(), headersMapper).containsAll(serverResponse.getHeaders()));
+    }
+
+    protected Map<?, ?> getProps() {
+        return ImmutableMap.of();
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ad853a06/core/src/test/java/org/apache/brooklyn/util/http/executor/HttpExecutorImplTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/util/http/executor/HttpExecutorImplTest.java
b/core/src/test/java/org/apache/brooklyn/util/http/executor/HttpExecutorImplTest.java
deleted file mode 100644
index 1f18d32..0000000
--- a/core/src/test/java/org/apache/brooklyn/util/http/executor/HttpExecutorImplTest.java
+++ /dev/null
@@ -1,210 +0,0 @@
-/*
- * 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.brooklyn.util.http.executor;
-
-import static org.testng.AssertJUnit.assertTrue;
-import static org.testng.Assert.assertEquals;
-
-import java.net.URL;
-import java.util.Map;
-import java.util.Random;
-
-import com.google.common.base.Function;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Collections2;
-import org.apache.brooklyn.util.core.http.BetterMockWebServer;
-import org.apache.commons.codec.binary.Base64;
-import org.bouncycastle.util.Strings;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.io.ByteStreams;
-import com.google.mockwebserver.MockResponse;
-import com.google.mockwebserver.RecordedRequest;
-
-import javax.annotation.Nullable;
-
-public class HttpExecutorImplTest {
-    protected BetterMockWebServer server;
-    protected URL baseUrl;
-    protected HttpExecutorFactoryImpl factory;
-
-    protected String HTTP_RESPONSE_HEADER_KEY = "content-type";
-    protected String HTTP_RESPONSE_HEADER_VALUE = "application/json";
-    protected String HTTP_BODY = "{\"foo\":\"myfoo\"}";
-
-    @BeforeMethod(alwaysRun=true)
-    public void setUp() throws Exception {
-        factory = new HttpExecutorFactoryImpl();
-        server = BetterMockWebServer.newInstanceLocalhost();
-        server.play();
-        baseUrl = server.getUrl("/");
-    }
-
-    @AfterMethod(alwaysRun=true)
-    public void afterMethod() throws Exception {
-        if (server != null) server.shutdown();
-    }
-
-    @Test
-    public void testHttpRequest() throws Exception {
-        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        HttpExecutor executor = factory.getHttpExecutor(getProps());
-        HttpRequest executorRequest = new HttpRequest.Builder()
-                .method("GET")
-                .uri(baseUrl.toURI())
-                .build();
-        HttpResponse executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-    }
-
-    @Test
-    public void testHttpPostRequest() throws Exception {
-        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        HttpExecutor executor = factory.getHttpExecutor(getProps());
-        HttpRequest executorRequest = new HttpRequest.Builder().headers(ImmutableMap.of("RequestHeader",
"RequestHeaderValue"))
-                .method("POST")
-                .body(HTTP_BODY.getBytes())
-                .uri(baseUrl.toURI())
-                .build();
-        HttpResponse executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-
-        // Big POST request with random bytes
-        serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY
+ "Test", HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        executor = factory.getHttpExecutor(getProps());
-        byte[] requestBody = new byte[10 * 1024 * 1024];
-        Random r = new Random();
-        for (int i = 0; i < requestBody.length; i++) {
-            requestBody[i] = (byte)(r.nextInt() % 256);
-        }
-        executorRequest = new HttpRequest.Builder()
-                .method("POST")
-                .body(requestBody)
-                .uri(baseUrl.toURI())
-                .build();
-        executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-    }
-
-    @Test
-    public void testHttpPutRequest() throws Exception {
-        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        HttpExecutor executor = factory.getHttpExecutor(getProps());
-        HttpRequest executorRequest = new HttpRequest.Builder()
-                .method("PUT")
-                .uri(baseUrl.toURI())
-                .build();
-        HttpResponse executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-    }
-
-    @Test
-    public void testHttpDeleteRequest() throws Exception {
-        MockResponse serverResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        HttpExecutor executor = factory.getHttpExecutor(getProps());
-        HttpRequest executorRequest = new HttpRequest.Builder()
-                .method("DELETE")
-                .uri(baseUrl.toURI())
-                .build();
-        HttpResponse executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-
-        // No Headers returned
-        serverResponse = new MockResponse().setResponseCode(200).setBody(HTTP_BODY);
-        server.enqueue(serverResponse);
-        executor = factory.getHttpExecutor(getProps());
-        executorRequest = new HttpRequest.Builder()
-                .method("DELETE")
-                .uri(baseUrl.toURI())
-                .build();
-        executorResponse = executor.execute(executorRequest);
-        assertRequestAndResponse(server.takeRequest(), serverResponse, executorRequest, executorResponse);
-    }
-
-    @Test
-    public void testHttpPasswordRequest() throws Exception {
-        MockResponse firstServerResponse = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate",
"Basic realm=\"User Visible Realm\"").setBody("Not Authenticated");
-        server.enqueue(firstServerResponse);
-        MockResponse secondServerResponse = new MockResponse().setResponseCode(200).addHeader(HTTP_RESPONSE_HEADER_KEY,
HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
-        server.enqueue(secondServerResponse);
-        final String USER = "brooklyn",
-                PASSWORD = "apache";
-        String authUnencoded = USER + ":" + PASSWORD;
-        String authEncoded = Base64.encodeBase64String(Strings.toByteArray(authUnencoded));
-        HttpExecutor executor = factory.getHttpExecutor(getProps());
-        HttpRequest executorRequest = new HttpRequest.Builder().headers(ImmutableMap.of("RequestHeader",
"RequestHeaderValue"))
-                .method("GET")
-                .uri(baseUrl.toURI())
-                .credentials(new UsernamePassword("brooklyn", "apache"))
-                .build();
-        HttpResponse executorResponse = executor.execute(executorRequest);
-
-        RecordedRequest recordedFirstRequest = server.takeRequest();
-        RecordedRequest recordedSecondRequest = server.takeRequest();
-
-        assertRequestAndResponse(recordedFirstRequest,
-                firstServerResponse,
-                executorRequest,
-                new HttpResponse.Builder()
-                        .header("WWW-Authenticate", "Basic realm=\"User Visible Realm\"")
-                        .header("Content-Length", "" + "Not Authenticated".length())
-                        .content("Not Authenticated".getBytes())
-                        .build());
-        ArrayListMultimap newHeaders = ArrayListMultimap.create(executorRequest.headers());
-        newHeaders.put("Authorization", "Basic " + authEncoded);
-        assertRequestAndResponse(recordedSecondRequest,
-                secondServerResponse,
-                new HttpRequest.Builder().from((HttpRequestImpl)executorRequest).headers(newHeaders).build(),
-                executorResponse);
-    }
-
-    private void assertRequestAndResponse(RecordedRequest serverRequest, MockResponse serverResponse,
HttpRequest executorRequest, HttpResponse executorResponse) throws Exception {
-        assertEquals(serverRequest.getMethod(), executorRequest.method());
-        Function<Map.Entry<String, String>, String> headersMapper = new Function<Map.Entry<String,
String>, String>() {
-            @Nullable
-            @Override
-            public String apply(@Nullable Map.Entry<String, String> input) {
-                return input.getKey() + ": " + input.getValue();
-            }
-        };
-        assertTrue(serverRequest.getHeaders().containsAll(Collections2.transform(executorRequest.headers().entries(),
headersMapper)));
-        assertEquals(serverRequest.getPath(), executorRequest.uri().getPath());
-        if (executorRequest.body() != null) {
-            assertEquals(serverRequest.getBody(), executorRequest.body());
-        } else {
-            assertEquals(serverRequest.getBody(), new byte[0]);
-        }
-
-        assertEquals(serverResponse.getBody(), ByteStreams.toByteArray(executorResponse.getContent()));
-        assertTrue(serverResponse.getHeaders().containsAll(Collections2.transform(executorResponse.headers().entries(),
headersMapper)));
-        assertTrue(Collections2.transform(executorResponse.headers().entries(), headersMapper).containsAll(serverResponse.getHeaders()));
-    }
-
-    protected Map<?, ?> getProps() {
-        return ImmutableMap.of();
-    }
-}


Mime
View raw message