You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by geomacy <gi...@git.apache.org> on 2015/10/30 13:03:59 UTC
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
GitHub user geomacy opened a pull request:
https://github.com/apache/incubator-brooklyn/pull/994
Add EntityAsserts and HttpUtils.
This are added to allow core code to make assertions on entities without
without having a dependency on TestNG.
For now, deprecate EntityTestUtils and HttpTestUtils, and in the future these
can be removed in favour of the new classes.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/geomacy/incubator-brooklyn decouple-test-utils
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-brooklyn/pull/994.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #994
----
commit c316e2177b88b2f274637d812e5659e751aeea0d
Author: Geoff Macartney <ge...@cloudsoftcorp.com>
Date: 2015-10-28T12:10:37Z
Add EntityAsserts and HttpUtils.
This are added to allow core code to make assertions on entities without
without having a dependency on TestNG.
For now, deprecate EntityTestUtils and HttpTestUtils, and in the future these
can be removed in favour of the new classes.
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-156502758
Have added the changes in https://github.com/apache/incubator-brooklyn/pull/1032
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43644119
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpUtils.java ---
@@ -0,0 +1,387 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLSession;
+
+import org.apache.brooklyn.test.Asserts;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.crypto.SslTrustUtils;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.stream.Streams;
+import org.apache.brooklyn.util.time.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Throwables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+
+/**
+ * Utility methods to aid testing HTTP.
+ *
+ * @author aled
+ */
+public class HttpUtils {
--- End diff --
call this `HttpAsserts` to mirror `EntityAsserts` ? (and move the utility methods out?)
also note there is `HttpTool` where utility methods could perhaps go or be moved from. it would be nice not to have both `HttpUtils` and `HttpTool` although maybe that would be okay given classes the latter use.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43643773
--- Diff: usage/test-support/src/main/java/org/apache/brooklyn/test/EntityTestUtils.java ---
@@ -46,6 +45,12 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+/**
+ * A utility class containing tests on Entities.
+ *
+ * @deprecated Prefer core assertions org.apache.brooklyn.entity.EntityAsserts.
--- End diff --
missing `core` in package name - `o.a.b.core.e.EA`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153148702
hi Alex et al. I have made the changes related to the comments by Alex in the commits [above](https://github.com/apache/incubator-brooklyn/pull/994#commits-pushed-03dbd28/).
The only one I haven't yet done is to add unit tests for HttpUtils. I don't want to hold back the others so I propose to do this in a separate pull request. I will get on to the tests for HttpUtils tomorrow morning.
Note that to nicely divide up the HttpAsserts from the non assertion methods I have moved HttpTool out of core and into the utils module. This results in imports changing in numerous files, so change 03dbd28 looks scarier than it really is (47 changed files).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153172638
very minor, good to go once those two last items are addressed
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/incubator-brooklyn/pull/994
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-154085081
Moving them to integration will address the immediate problem so +1.
At some point we will start running the integration tests on apache infrastructure (read - **busy**, we already do, but they are failing for a number of other reasons), so the same issue will crop up - but can deal with it then.
---
The code could be restructured such that it doesn't use waits at all:
* tests which need non-running server be put in a separate test, which doesn't start the server by default
* no sleep on server start, if the server doesn't come up immediately, then always use `eventuallySucceeds`
* for the cases where there's `xxxxAfter(5); succeedsEventually`, could be changed to `failsNow; task = succeedsEventuallyInAnotherTask; startNow(); task.get()`. What could be a problem here is that connections to closed ports take a long time to timeout?
* The only place where a fixed wait is really needed is tests which expect to fail and have a timeout, those go in integration.
All of the above said, I am perfectly fine with moving all tests to integration as is.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43803617
--- Diff: usage/test-support/src/main/java/org/apache/brooklyn/test/EntityTestUtils.java ---
@@ -46,20 +45,25 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+/**
+ * A utility class containing tests on Entities.
+ *
+ * @deprecated Prefer core assertions class org.apache.brooklyn.core.entity.EntityAsserts.
--- End diff --
Can you include a "since 0.9.0" in the deprecated javadoc line. Same for anywhere else you've added `@deprecated`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-154087549
I will move them to integration now and add a note to restructure in future, but I will try to make that the near future while it's fresh in my head. Thanks.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r44011962
--- Diff: utils/common/src/test/java/org/apache/brooklyn/util/HttpAssertsTest.java ---
@@ -0,0 +1,293 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.apache.brooklyn.test.http.TestHttpRequestHandler;
+import org.apache.brooklyn.test.http.TestHttpServer;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.HttpAsserts;
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.localserver.RequestBasicAuth;
+import org.apache.http.localserver.ResponseBasicUnauthorized;
+import org.apache.http.protocol.ResponseServer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.*;
+
+import java.net.URI;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by geoff on 03/11/2015.
+ */
+public class HttpAssertsTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(HttpAssertsTest.class);
+ HttpClient httpClient;
+ URI baseUri;
+ private TestHttpServer server;
+ private String baseUrl;
+ private ScheduledExecutorService executor;
+
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception {
+ server = initializeServer();
+ baseUrl = server.getUrl();
+ httpClient = HttpTool.httpClientBuilder()
+ .uri(baseUrl)
+ .build();
+ baseUri = URI.create(baseUrl);
+ executor = Executors.newScheduledThreadPool(3);
+ TimeUnit.SECONDS.sleep(2);
+ }
+
+ private TestHttpServer initializeServer() {
+ return new TestHttpServer()
+ .interceptor(new ResponseServer())
+ .interceptor(new ResponseBasicUnauthorized())
+ .interceptor(new RequestBasicAuth())
+ .handler("/simple", new TestHttpRequestHandler().response("OK"))
+ .handler("/empty", new TestHttpRequestHandler().code(HttpStatus.SC_NO_CONTENT))
+ .handler("/missing", new TestHttpRequestHandler().code(HttpStatus.SC_NOT_FOUND).response("Missing"))
+ .handler("/redirect", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "/simple"))
+ .handler("/cycle", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "/cycle"))
+ .handler("/secure", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "https://0.0.0.0/"))
+ .start();
+ }
+
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception {
+ if (executor != null) executor.shutdownNow();
+ server.stop();
+ TimeUnit.SECONDS.sleep(2);
+ }
+
+ // schedule a stop of server after n seconds
+ private void stopAfter(int delay) {
+ executor.schedule(new Runnable() {
+ @Override
+ public void run() {
+ server.stop();
+ }
+ }, delay, TimeUnit.SECONDS);
+ }
+
+ // stop server and pause to wait for it to finish
+ private void stopServer() {
+ server.stop();
+ try {
+ TimeUnit.SECONDS.sleep(2);
+ } catch (InterruptedException e) {
+ throw Exceptions.propagate(e);
+ }
+ }
+
+ // schedule a start of server after n seconds
+ private void startAfter(int delay) {
+ executor.schedule(new Runnable() {
+ @Override
+ public void run() {
+ server = initializeServer();
+ }
+ }, delay, TimeUnit.SECONDS);
+ }
+
+ private HttpToolResponse doGet(String str) {
+ final URI address = baseUri.resolve(str);
+ return HttpTool.httpGet(httpClient, address,
+ ImmutableMap.<String, String>of());
+ }
+
+ @Test
+ public void shouldAssertHealthyStatusCode() {
+ final HttpToolResponse response = doGet("/simple");
+ HttpAsserts.assertHealthyStatusCode(response.getResponseCode());
+ }
+
+ @Test
+ public void shouldAssertUrlReachable() {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachable() {
+ stopServer();
+ HttpAsserts.assertUrlUnreachable(testUri("/simple"));
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachableEventually() {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ stopAfter(1);
+ HttpAsserts.assertUrlUnreachableEventually(baseUrl);
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachableEventuallyWithFlags() throws Exception {
+ stopAfter(5);
+ TimeUnit.SECONDS.sleep(3);
+ HttpAsserts.assertUrlReachable(baseUrl);
+ HttpAsserts.assertUrlUnreachableEventually(ImmutableMap.of("timeout", "10s"), baseUrl);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void shouldAssertUrlUnreachableEventuallyWithTimeout() throws Exception {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ HttpAsserts.assertUrlUnreachableEventually(ImmutableMap.of("timeout", "3s"), baseUrl);
+ }
+
+
+ @Test
+ public void shouldAssertHttpStatusCodeEquals() {
+ HttpAsserts.assertHttpStatusCodeEquals(baseUrl, 500, 501);
+ HttpAsserts.assertHttpStatusCodeEquals(testUri("/simple"), 201, 200);
+ HttpAsserts.assertHttpStatusCodeEquals(testUri("/missing"), 400, 404);
+ }
+
+ @Test
+ public void shouldAssertHttpStatusCodeEventuallyEquals() throws Exception {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertHttpStatusCodeEventuallyEquals(simple, 200);
+ }
+
+ private String testUri(String str) {
+ return baseUri.resolve(str).toString();
+ }
+
+ @Test
+ public void shouldAssertContentContainsText() {
+ HttpAsserts.assertContentContainsText(testUri("/simple"), "OK");
+ }
+
+ @Test
+ public void shouldAssertContentNotContainsText() {
+ HttpAsserts.assertContentNotContainsText(testUri("/simple"), "Bad");
+ }
+
+ @Test
+ public void shouldAssertErrorContentsContainsText() {
+ HttpAsserts.assertErrorContentContainsText(testUri("/missing"), "Missing");
+ }
+
+ @Test
+ public void shouldAssertErrorContentNotContainsText() {
+ HttpAsserts.assertErrorContentNotContainsText(testUri("/missing"), "Bogus");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyContainsText() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyContainsText(simple, "OK");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyContainsTextWithFlags() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyContainsText(ImmutableMap.of("timeout", "3s"), simple, "OK");
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void shouldAssertContentEventuallyContainsTextWithTimeout() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(4);
+ HttpAsserts.assertContentEventuallyContainsText(ImmutableMap.of("timeout", "3s"), simple, "OK");
+ }
+
+
+ @Test
+ public void shouldAssertContentMatches() {
+ HttpAsserts.assertContentMatches(testUri("/simple"), "[Oo][Kk]");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyMatches() throws Exception {
+ stopServer();
+ TimeUnit.SECONDS.sleep(2);
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ TimeUnit.SECONDS.sleep(2);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyMatches(testUri("/simple"), "[Oo][Kk]");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyMatchesWithFlags() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyMatches(ImmutableMap.of("timeout", "3s"), testUri("/simple"), "[Oo][Kk]");
--- End diff --
A couple of example failures:
* https://builds.apache.org/job/incubator-brooklyn-pull-requests/2069/console
* https://builds.apache.org/job/incubator-brooklyn-pull-requests/2067/console
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-156472799
hi @neykov, sorry I had indeed missed this comment. Will make changes as you say.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153063539
need tests for the http assertions i think.
apart from that this looks real good and only minor comments.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r44706399
--- Diff: core/src/main/java/org/apache/brooklyn/util/core/http/HttpTool.java ---
@@ -76,311 +76,10 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
-public class HttpTool {
-
- private static final Logger LOG = LoggerFactory.getLogger(HttpTool.class);
-
- /** Apache HTTP commons utility for trusting all.
- * <p>
- * For generic java HTTP usage, see {@link SslTrustUtils#trustAll(java.net.URLConnection)}
- * and static constants in the same class. */
- public static class TrustAllStrategy implements TrustStrategy {
- @Override
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- return true;
- }
- }
-
- public static HttpClientBuilder httpClientBuilder() {
- return new HttpClientBuilder();
- }
-
- public static class HttpClientBuilder {
- private ClientConnectionManager clientConnectionManager;
- private HttpParams httpParams;
- private URI uri;
- private Integer port;
- private Credentials credentials;
- private boolean laxRedirect;
- private Boolean https;
- private SchemeSocketFactory socketFactory;
- private ConnectionReuseStrategy reuseStrategy;
- private boolean trustAll;
- private boolean trustSelfSigned;
-
- public HttpClientBuilder clientConnectionManager(ClientConnectionManager val) {
- this.clientConnectionManager = checkNotNull(val, "clientConnectionManager");
- return this;
- }
- public HttpClientBuilder httpParams(HttpParams val) {
- checkState(httpParams == null, "Must not call httpParams multiple times, or after other methods like connectionTimeout");
- this.httpParams = checkNotNull(val, "httpParams");
- return this;
- }
- public HttpClientBuilder connectionTimeout(Duration val) {
- if (httpParams == null) httpParams = new BasicHttpParams();
- long millis = checkNotNull(val, "connectionTimeout").toMilliseconds();
- if (millis > Integer.MAX_VALUE) throw new IllegalStateException("HttpClient only accepts upto max-int millis for connectionTimeout, but given "+val);
- HttpConnectionParams.setConnectionTimeout(httpParams, (int) millis);
- return this;
- }
- public HttpClientBuilder socketTimeout(Duration val) {
- if (httpParams == null) httpParams = new BasicHttpParams();
- long millis = checkNotNull(val, "socketTimeout").toMilliseconds();
- if (millis > Integer.MAX_VALUE) throw new IllegalStateException("HttpClient only accepts upto max-int millis for socketTimeout, but given "+val);
- HttpConnectionParams.setSoTimeout(httpParams, (int) millis);
- return this;
- }
- public HttpClientBuilder reuseStrategy(ConnectionReuseStrategy val) {
- this.reuseStrategy = checkNotNull(val, "reuseStrategy");
- return this;
- }
- public HttpClientBuilder uri(String val) {
- return uri(URI.create(checkNotNull(val, "uri")));
- }
- public HttpClientBuilder uri(URI val) {
- this.uri = checkNotNull(val, "uri");
- if (https == null) https = ("https".equalsIgnoreCase(uri.getScheme()));
- return this;
- }
- public HttpClientBuilder port(int val) {
- this.port = val;
- return this;
- }
- public HttpClientBuilder credentials(Credentials val) {
- this.credentials = checkNotNull(val, "credentials");
- return this;
- }
- public void credential(Optional<Credentials> val) {
- if (val.isPresent()) credentials = val.get();
- }
- /** similar to curl --post301 -L` */
- public HttpClientBuilder laxRedirect(boolean val) {
- this.laxRedirect = val;
- return this;
- }
- public HttpClientBuilder https(boolean val) {
- this.https = val;
- return this;
- }
- public HttpClientBuilder socketFactory(SchemeSocketFactory val) {
- this.socketFactory = checkNotNull(val, "socketFactory");
- return this;
- }
- public HttpClientBuilder trustAll() {
- this.trustAll = true;
- return this;
- }
- public HttpClientBuilder trustSelfSigned() {
- this.trustSelfSigned = true;
- return this;
- }
- public HttpClient build() {
- final DefaultHttpClient httpClient = new DefaultHttpClient(clientConnectionManager);
- httpClient.setParams(httpParams);
-
- // support redirects for POST (similar to `curl --post301 -L`)
- // http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect
- if (laxRedirect) {
- httpClient.setRedirectStrategy(new LaxRedirectStrategy());
- }
- if (reuseStrategy != null) {
- httpClient.setReuseStrategy(reuseStrategy);
- }
- if (https == Boolean.TRUE || (uri!=null && uri.toString().startsWith("https:"))) {
- try {
- if (port == null) {
- port = (uri != null && uri.getPort() >= 0) ? uri.getPort() : 443;
- }
- if (socketFactory == null) {
- if (trustAll) {
- TrustStrategy trustStrategy = new TrustAllStrategy();
- X509HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
- socketFactory = new SSLSocketFactory(trustStrategy, hostnameVerifier);
- } else if (trustSelfSigned) {
- TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
- X509HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
- socketFactory = new SSLSocketFactory(trustStrategy, hostnameVerifier);
- } else {
- // Using default https scheme: based on default java truststore, which is pretty strict!
- }
- }
- if (socketFactory != null) {
- Scheme sch = new Scheme("https", port, socketFactory);
- httpClient.getConnectionManager().getSchemeRegistry().register(sch);
- }
- } catch (Exception e) {
- LOG.warn("Error setting trust for uri {}", uri);
- throw Exceptions.propagate(e);
- }
- }
-
- // Set credentials
- if (uri != null && credentials != null) {
- String hostname = uri.getHost();
- int port = uri.getPort();
- httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port), credentials);
- }
- if (uri==null && credentials!=null) {
- LOG.warn("credentials have no effect in builder unless URI for host is specified");
- }
-
- return httpClient;
- }
- }
-
- protected static abstract class HttpRequestBuilder<B extends HttpRequestBuilder<B, R>, R extends HttpRequest> {
- protected R req;
-
- protected HttpRequestBuilder(R req) {
- this.req = req;
- }
- @SuppressWarnings("unchecked")
- protected B self() {
- return (B) this;
- }
- public B headers(Map<String,String> headers) {
- if (headers!=null) {
- for (Map.Entry<String,String> entry : headers.entrySet()) {
- req.addHeader(entry.getKey(), entry.getValue());
- }
- }
- return self();
- }
- public B headers(Multimap<String,String> headers) {
- if (headers!=null) {
- for (Map.Entry<String,String> entry : headers.entries()) {
- req.addHeader(entry.getKey(), entry.getValue());
- }
- }
- return self();
- }
- public R build() {
- return req;
- }
- }
-
- protected static abstract class HttpEntityEnclosingRequestBaseBuilder<B extends HttpEntityEnclosingRequestBaseBuilder<B,R>, R extends HttpEntityEnclosingRequestBase> extends HttpRequestBuilder<B, R> {
- protected HttpEntityEnclosingRequestBaseBuilder(R req) {
- super(req);
- }
- public B body(byte[] body) {
- if (body != null) {
- HttpEntity httpEntity = new ByteArrayEntity(body);
- req.setEntity(httpEntity);
- }
- return self();
- }
- }
-
- public static class HttpGetBuilder extends HttpRequestBuilder<HttpGetBuilder, HttpGet> {
- public HttpGetBuilder(URI uri) {
- super(new HttpGet(uri));
- }
- }
-
- public static class HttpHeadBuilder extends HttpRequestBuilder<HttpHeadBuilder, HttpHead> {
- public HttpHeadBuilder(URI uri) {
- super(new HttpHead(uri));
- }
- }
-
- public static class HttpDeleteBuilder extends HttpRequestBuilder<HttpDeleteBuilder, HttpDelete> {
- public HttpDeleteBuilder(URI uri) {
- super(new HttpDelete(uri));
- }
- }
-
- public static class HttpPostBuilder extends HttpEntityEnclosingRequestBaseBuilder<HttpPostBuilder, HttpPost> {
- HttpPostBuilder(URI uri) {
- super(new HttpPost(uri));
- }
- }
-
- public static class HttpFormPostBuilder extends HttpRequestBuilder<HttpFormPostBuilder, HttpPost> {
- HttpFormPostBuilder(URI uri) {
- super(new HttpPost(uri));
- }
-
- public HttpFormPostBuilder params(Map<String, String> params) {
- if (params != null) {
- Collection<NameValuePair> httpParams = new ArrayList<NameValuePair>(params.size());
- for (Entry<String, String> param : params.entrySet()) {
- httpParams.add(new BasicNameValuePair(param.getKey(), param.getValue()));
- }
- req.setEntity(new UrlEncodedFormEntity(httpParams));
- }
- return self();
- }
- }
-
- public static class HttpPutBuilder extends HttpEntityEnclosingRequestBaseBuilder<HttpPutBuilder, HttpPut> {
- public HttpPutBuilder(URI uri) {
- super(new HttpPut(uri));
- }
- }
-
- public static HttpToolResponse httpGet(HttpClient httpClient, URI uri, Map<String,String> headers) {
- HttpGet req = new HttpGetBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPost(HttpClient httpClient, URI uri, Map<String,String> headers, byte[] body) {
- HttpPost req = new HttpPostBuilder(uri).headers(headers).body(body).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPut(HttpClient httpClient, URI uri, Map<String, String> headers, byte[] body) {
- HttpPut req = new HttpPutBuilder(uri).headers(headers).body(body).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpPost(HttpClient httpClient, URI uri, Map<String,String> headers, Map<String, String> params) {
- HttpPost req = new HttpFormPostBuilder(uri).headers(headers).params(params).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpDelete(HttpClient httpClient, URI uri, Map<String,String> headers) {
- HttpDelete req = new HttpDeleteBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse httpHead(HttpClient httpClient, URI uri, Map<String,String> headers) {
- HttpHead req = new HttpHeadBuilder(uri).headers(headers).build();
- return execAndConsume(httpClient, req);
- }
-
- public static HttpToolResponse execAndConsume(HttpClient httpClient, HttpUriRequest req) {
- long startTime = System.currentTimeMillis();
- try {
- HttpResponse httpResponse = httpClient.execute(req);
-
- try {
- return new HttpToolResponse(httpResponse, startTime);
- } finally {
- EntityUtils.consume(httpResponse.getEntity());
- }
- } catch (Exception e) {
- throw Exceptions.propagate(e);
- }
- }
-
- public static boolean isStatusCodeHealthy(int code) { return (code>=200 && code<=299); }
-
- public static String toBasicAuthorizationValue(UsernamePasswordCredentials credentials) {
- return "Basic "+Base64.encodeBase64String( (credentials.getUserName()+":"+credentials.getPassword()).getBytes() );
- }
+/**
+ * @deprecated since 0.9.0. Prefer org.apache.brooklyn.util.http.HttpTool
+ */
+@Deprecated
+public class HttpTool extends org.apache.brooklyn.util.http.HttpTool {
--- End diff --
This doesn't make the class backwards compatible because return types will be coming from the new package, while users of this will be expecting the old classes.
HttpTool is used frequently so better handle this gracefully (had a downstream project build failure). @geomacy Can you get the old implementation of `HttpTool` back here while keeping it marked as `@Deprecated`. Same for `HttpToolResponse`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153171583
For backwards compatibility we need to leave an `HttpTool` class in the old place, and same for `HttpToolResponse`. I think these can just extend the newly created classes, so that any methods and creation still work. And add the usual `@Deprecation` markers to these classes.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153647425
Thanks @ahgittin and @aledsage for the comments. I have made some further updates which I think address them all, but let me know if I've missed anything.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43643799
--- Diff: usage/test-support/src/main/java/org/apache/brooklyn/test/EntityTestUtils.java ---
@@ -46,6 +45,12 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+/**
+ * A utility class containing tests on Entities.
+ *
+ * @deprecated Prefer core assertions org.apache.brooklyn.entity.EntityAsserts.
+ */
+@Deprecated
public class EntityTestUtils {
// TODO would be nice to have this... perhaps moving this class, or perhaps this whole project, to core/src/test ?
--- End diff --
can remove the TODO :)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43643555
--- Diff: core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java ---
@@ -0,0 +1,192 @@
+/*
+ * 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.core.entity;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Objects;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.Group;
+import org.apache.brooklyn.api.mgmt.SubscriptionHandle;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
+import org.apache.brooklyn.api.sensor.SensorEvent;
+import org.apache.brooklyn.api.sensor.SensorEventListener;
+import org.apache.brooklyn.config.ConfigKey;
+import org.apache.brooklyn.test.Asserts;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.apache.brooklyn.test.Asserts.assertEquals;
+
+/**
+ * Convenience class containing assertions that may be made about entities.
+ */
+public class EntityAsserts {
+
+
+ public static <T> void assertAttributeEquals(Entity entity, AttributeSensor<T> attribute, T expected) {
+ assertEquals(entity.getAttribute(attribute), expected, "entity=" + entity + "; attribute=" + attribute);
+ }
+
+ public static <T> void assertConfigEquals(Entity entity, ConfigKey<T> configKey, T expected) {
+ assertEquals(entity.getConfig(configKey), expected, "entity=" + entity + "; configKey=" + configKey);
+ }
+
+ public static <T> void assertAttributeEqualsEventually(final Entity entity, final AttributeSensor<T> attribute, final T expected) {
+ assertAttributeEqualsEventually(Maps.newLinkedHashMap(), entity, attribute, expected);
+ }
+
+ public static <T> void assertAttributeEqualsEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) {
+ // Not using assertAttributeEventually(predicate) so get nicer error message
+ Asserts.succeedsEventually((Map) flags, new Runnable() {
+ @Override
+ public void run() {
+ assertAttributeEquals(entity, attribute, expected);
+ }
+ });
+ }
+
+ public static <T> T assertAttributeEventuallyNonNull(final Entity entity, final AttributeSensor<T> attribute) {
+ return assertAttributeEventuallyNonNull(Maps.newLinkedHashMap(), entity, attribute);
+ }
+
+ public static <T> T assertAttributeEventuallyNonNull(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute) {
+ return assertAttributeEventually(flags, entity, attribute, Predicates.notNull());
+ }
+
+ public static <T> T assertAttributeEventually(final Entity entity, final AttributeSensor<T> attribute, Predicate<? super T> predicate) {
+ return assertAttributeEventually(ImmutableMap.of(), entity, attribute, predicate);
+ }
+
+ public static <T> T assertAttributeEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate) {
+ final AtomicReference<T> result = new AtomicReference<T>();
+ Asserts.succeedsEventually((Map)flags, new Runnable() {
+ @Override public void run() {
+ T val = entity.getAttribute(attribute);
+ Asserts.assertTrue(predicate.apply(val), "val=" + val);
+ result.set(val);
+ }});
+ return result.get();
+ }
+
+ public static <T> T assertAttribute(final Entity entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate) {
+ T val = entity.getAttribute(attribute);
+ Asserts.assertTrue(predicate.apply(val), "val=" + val);
+ return val;
+ }
+
+
+ public static <T extends Entity> void assertPredicateEventuallyTrue(final T entity, final Predicate<? super T> predicate) {
+ assertPredicateEventuallyTrue(Maps.newLinkedHashMap(), entity, predicate);
+ }
+
+ public static <T extends Entity> void assertPredicateEventuallyTrue(Map<?,?> flags, final T entity, final Predicate<? super T> predicate) {
+ Asserts.succeedsEventually((Map)flags, new Runnable() {
+ @Override public void run() {
+ Asserts.assertTrue(predicate.apply(entity), "predicate unsatisfied");
+ }});
+ }
+
+ public static <T> void assertAttributeEqualsContinually(final Entity entity, final AttributeSensor<T> attribute, final T expected) {
+ assertAttributeEqualsContinually(Maps.newLinkedHashMap(), entity, attribute, expected);
+ }
+
+ public static <T> void assertAttributeEqualsContinually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) {
+ Asserts.succeedsContinually(flags, new Runnable() {
+ @Override public void run() {
+ assertAttributeEquals(entity, attribute, expected);
+ }});
+ }
+
+ public static void assertGroupSizeEqualsEventually(final Group group, int expected) {
+ assertGroupSizeEqualsEventually(ImmutableMap.of(), group, expected);
+ }
+
+ public static void assertGroupSizeEqualsEventually(Map<?,?> flags, final Group group, final int expected) {
+ Asserts.succeedsEventually((Map)flags, new Runnable() {
+ @Override public void run() {
+ Collection<Entity> members = group.getMembers();
+ assertEquals(members.size(), expected, "members=" + members);
+ }});
+ }
+
+
+ /**
+ * Asserts that the entity's value for this attribute changes, by registering a subscription and checking the value.
+ *
+ * @param entity The entity whose attribute will be checked.
+ * @param attribute The attribute to check on the entity.
+ *
+ * @throws AssertionError if the assertion fails.
+ */
+ public static void assertAttributeChangesEventually(final Entity entity, final AttributeSensor<?> attribute) {
+ final Object origValue = entity.getAttribute(attribute);
+ final AtomicBoolean changed = new AtomicBoolean();
+ SubscriptionHandle handle = entity.subscriptions().subscribe(entity, attribute, new SensorEventListener<Object>() {
+ @Override public void onEvent(SensorEvent<Object> event) {
+ if (!Objects.equal(origValue, event.getValue())) {
+ changed.set(true);
+ }
+ }});
+ try {
+ Asserts.succeedsEventually(new Runnable() {
+ @Override public void run() {
+ Asserts.assertTrue(changed.get(), entity + " -> " + attribute + " not changed");
+ }});
+ } finally {
+ entity.subscriptions().unsubscribe(entity, handle);
+ }
+ }
+
+
+ @Beta @SafeVarargs
+ public static <T> void assertAttributeNever(final Entity entity, final AttributeSensor<T> attribute, T... disallowed) {
--- End diff --
can we call this `assertAttributeContinuallyNotEqualTo`?
add javadoc noting that this loops checking the value, it does not currently set up a subscription so a disallowed value might come up very quickly and be missed. (we could fix this if needed.)
cleaner would be for this to call the next method, `assertAttribute...(Map, ...)`, with `null` flags, or perhaps to introduce an `assertAttributeContinually(..., Predicate)` and build up a predicate of `not(or(equalTo(disallowed[0]), equalTo(disallowed[1]), ...))`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-153767785
looks great, merging
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r43643649
--- Diff: core/src/main/resources/org/apache/brooklyn/core/mgmt/persist/deserializingClassRenames.properties ---
@@ -1415,5 +1415,5 @@ brooklyn.management.internal.UsageManager
brooklyn.policy.basic.ConfigMapImpl : org.apache.brooklyn.core.objs.AdjunctConfigMap
brooklyn.util.internal.Repeater : org.apache.brooklyn.util.core.internal.Repeater
brooklyn.entity.nosql.redis.RedisClusterImpl : org.apache.brooklyn.entity.nosql.redis.RedisClusterImpl
-brooklyn.test.TrustingSslSocketFactory : org.apache.brooklyn.test.TrustingSslSocketFactory
+brooklyn.test.TrustingSslSocketFactory : org.apache.brooklyn.util.http.TrustingSslSocketFactory
--- End diff --
also add a line:
org.apache.brooklyn.test.TrustingSslSocketFactory : org.apache.brooklyn.util.http.TrustingSslSocketFactory
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#issuecomment-154074295
The sleeps are there to allow the test HTTP server time to shut down. That's not required in all tests, so I could speed things up a bit by changing the test condition, but it is needed in some cases (for the 'eventually becomes unavailable' type tests). I could add a timeout to the shutdown, but it occurs to me that it might be better to mark these as integration tests, because
- (a) they are not going to run any faster (they already take , and
- (b) the code under test is fairly 'low level' and not likely to break because of other code within Brooklyn changing
so it may not add a lot of value to run these tests on every build, and it will slow things down a bit.
What do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-brooklyn pull request: Add EntityAsserts and HttpUtils.
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/994#discussion_r44011834
--- Diff: utils/common/src/test/java/org/apache/brooklyn/util/HttpAssertsTest.java ---
@@ -0,0 +1,293 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.apache.brooklyn.test.http.TestHttpRequestHandler;
+import org.apache.brooklyn.test.http.TestHttpServer;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.HttpAsserts;
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.localserver.RequestBasicAuth;
+import org.apache.http.localserver.ResponseBasicUnauthorized;
+import org.apache.http.protocol.ResponseServer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.*;
+
+import java.net.URI;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by geoff on 03/11/2015.
+ */
+public class HttpAssertsTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(HttpAssertsTest.class);
+ HttpClient httpClient;
+ URI baseUri;
+ private TestHttpServer server;
+ private String baseUrl;
+ private ScheduledExecutorService executor;
+
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception {
+ server = initializeServer();
+ baseUrl = server.getUrl();
+ httpClient = HttpTool.httpClientBuilder()
+ .uri(baseUrl)
+ .build();
+ baseUri = URI.create(baseUrl);
+ executor = Executors.newScheduledThreadPool(3);
+ TimeUnit.SECONDS.sleep(2);
+ }
+
+ private TestHttpServer initializeServer() {
+ return new TestHttpServer()
+ .interceptor(new ResponseServer())
+ .interceptor(new ResponseBasicUnauthorized())
+ .interceptor(new RequestBasicAuth())
+ .handler("/simple", new TestHttpRequestHandler().response("OK"))
+ .handler("/empty", new TestHttpRequestHandler().code(HttpStatus.SC_NO_CONTENT))
+ .handler("/missing", new TestHttpRequestHandler().code(HttpStatus.SC_NOT_FOUND).response("Missing"))
+ .handler("/redirect", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "/simple"))
+ .handler("/cycle", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "/cycle"))
+ .handler("/secure", new TestHttpRequestHandler().code(HttpStatus.SC_MOVED_TEMPORARILY).response("Redirect").header("Location", "https://0.0.0.0/"))
+ .start();
+ }
+
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception {
+ if (executor != null) executor.shutdownNow();
+ server.stop();
+ TimeUnit.SECONDS.sleep(2);
+ }
+
+ // schedule a stop of server after n seconds
+ private void stopAfter(int delay) {
+ executor.schedule(new Runnable() {
+ @Override
+ public void run() {
+ server.stop();
+ }
+ }, delay, TimeUnit.SECONDS);
+ }
+
+ // stop server and pause to wait for it to finish
+ private void stopServer() {
+ server.stop();
+ try {
+ TimeUnit.SECONDS.sleep(2);
+ } catch (InterruptedException e) {
+ throw Exceptions.propagate(e);
+ }
+ }
+
+ // schedule a start of server after n seconds
+ private void startAfter(int delay) {
+ executor.schedule(new Runnable() {
+ @Override
+ public void run() {
+ server = initializeServer();
+ }
+ }, delay, TimeUnit.SECONDS);
+ }
+
+ private HttpToolResponse doGet(String str) {
+ final URI address = baseUri.resolve(str);
+ return HttpTool.httpGet(httpClient, address,
+ ImmutableMap.<String, String>of());
+ }
+
+ @Test
+ public void shouldAssertHealthyStatusCode() {
+ final HttpToolResponse response = doGet("/simple");
+ HttpAsserts.assertHealthyStatusCode(response.getResponseCode());
+ }
+
+ @Test
+ public void shouldAssertUrlReachable() {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachable() {
+ stopServer();
+ HttpAsserts.assertUrlUnreachable(testUri("/simple"));
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachableEventually() {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ stopAfter(1);
+ HttpAsserts.assertUrlUnreachableEventually(baseUrl);
+ }
+
+ @Test
+ public void shouldAssertUrlUnreachableEventuallyWithFlags() throws Exception {
+ stopAfter(5);
+ TimeUnit.SECONDS.sleep(3);
+ HttpAsserts.assertUrlReachable(baseUrl);
+ HttpAsserts.assertUrlUnreachableEventually(ImmutableMap.of("timeout", "10s"), baseUrl);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void shouldAssertUrlUnreachableEventuallyWithTimeout() throws Exception {
+ HttpAsserts.assertUrlReachable(baseUrl);
+ HttpAsserts.assertUrlUnreachableEventually(ImmutableMap.of("timeout", "3s"), baseUrl);
+ }
+
+
+ @Test
+ public void shouldAssertHttpStatusCodeEquals() {
+ HttpAsserts.assertHttpStatusCodeEquals(baseUrl, 500, 501);
+ HttpAsserts.assertHttpStatusCodeEquals(testUri("/simple"), 201, 200);
+ HttpAsserts.assertHttpStatusCodeEquals(testUri("/missing"), 400, 404);
+ }
+
+ @Test
+ public void shouldAssertHttpStatusCodeEventuallyEquals() throws Exception {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertHttpStatusCodeEventuallyEquals(simple, 200);
+ }
+
+ private String testUri(String str) {
+ return baseUri.resolve(str).toString();
+ }
+
+ @Test
+ public void shouldAssertContentContainsText() {
+ HttpAsserts.assertContentContainsText(testUri("/simple"), "OK");
+ }
+
+ @Test
+ public void shouldAssertContentNotContainsText() {
+ HttpAsserts.assertContentNotContainsText(testUri("/simple"), "Bad");
+ }
+
+ @Test
+ public void shouldAssertErrorContentsContainsText() {
+ HttpAsserts.assertErrorContentContainsText(testUri("/missing"), "Missing");
+ }
+
+ @Test
+ public void shouldAssertErrorContentNotContainsText() {
+ HttpAsserts.assertErrorContentNotContainsText(testUri("/missing"), "Bogus");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyContainsText() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyContainsText(simple, "OK");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyContainsTextWithFlags() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyContainsText(ImmutableMap.of("timeout", "3s"), simple, "OK");
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void shouldAssertContentEventuallyContainsTextWithTimeout() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(4);
+ HttpAsserts.assertContentEventuallyContainsText(ImmutableMap.of("timeout", "3s"), simple, "OK");
+ }
+
+
+ @Test
+ public void shouldAssertContentMatches() {
+ HttpAsserts.assertContentMatches(testUri("/simple"), "[Oo][Kk]");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyMatches() throws Exception {
+ stopServer();
+ TimeUnit.SECONDS.sleep(2);
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ TimeUnit.SECONDS.sleep(2);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyMatches(testUri("/simple"), "[Oo][Kk]");
+ }
+
+ @Test
+ public void shouldAssertContentEventuallyMatchesWithFlags() {
+ stopServer();
+ final String simple = testUri("/simple");
+ HttpAsserts.assertUrlUnreachable(simple);
+ startAfter(2);
+ HttpAsserts.assertContentEventuallyMatches(ImmutableMap.of("timeout", "3s"), testUri("/simple"), "[Oo][Kk]");
--- End diff --
Causes test failures on the apache jenkins CI. The servers there are quite overloaded so delays tend not to be exact, usually much longer.
Also tests which have explicit delays are usually marked as integration as those seconds easily add up. Is it possible to restructure the tests so that they don't need fixed sleeps?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---