public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int... expected) { List<Integer> expectedList = Lists.newArrayList(); for (int e : expected) { expectedList.add(e); } return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.in(expectedList))); }
public static Function<HttpToolResponse, Boolean> responseCodeEquals(final int expected) { return Functionals.chain(HttpValueFunctions.responseCode(), Functions.forPredicate(Predicates.equalTo(expected))); }
@Test public void testResponseCode() throws Exception { assertEquals(HttpValueFunctions.responseCode().apply(response), Integer.valueOf(responseCode)); }
@Test public void testSetsConnectionTimeout() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); }
@Test public void testPreemptiveBasicAuthFailsIfNoCredentials() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContains(e, "Must not enable preemptiveBasicAuth when there are no credentials"); } }
@Test public void testFailsIfUsernameNull() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .credentials(null, "Pa55w0rd") .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContainsIgnoreCase(e, "may not be null"); } }
@Test public void testPreemptiveBasicAuthFailsIfUserContainsColon() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .credentials("userWith:colon", "Pa55w0rd") .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContains(e, "must not contain colon"); } }
@Test(groups = {"Integration"}) public void testPollsAndParsesHttpGetResponseWithSsl() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+"), true).start(); URI baseUrl = new URI(httpService.getUrl()); assertEquals(baseUrl.getScheme(), "https", "baseUrl="+baseUrl); feed = HttpFeed.builder() .entity(entity) .baseUri(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.contains("Hello, World"), "val="+val); }}); }
@Test(groups="Integration") // marked integration as it takes a wee while public void testStartSuspended() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .suspended() .build(); Asserts.continually(MutableMap.of("timeout", 500), Entities.attributeSupplier(entity, SENSOR_INT), Predicates.<Integer>equalTo(null)); int countWhenSuspended = server.getRequestCount(); feed.resume(); Asserts.eventually(Entities.attributeSupplier(entity, SENSOR_INT), Predicates.<Integer>equalTo(200)); if (server.getRequestCount() <= countWhenSuspended) Assert.fail("Request count failed to increment when feed was resumed, from "+countWhenSuspended+", still at "+server.getRequestCount()); log.info("RUN: "+countWhenSuspended+" - "+server.getRequestCount()); }
@Test public void testPollsAndParsesHttpGetResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
@Test(groups="Integration") // marked integration as it takes a wee while public void testSuspendResume() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); feed.suspend(); final int countWhenSuspended = server.getRequestCount(); Thread.sleep(500); if (server.getRequestCount() > countWhenSuspended+1) Assert.fail("Request count continued to increment while feed was suspended, from "+countWhenSuspended+" to "+server.getRequestCount()); feed.resume(); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertTrue(server.getRequestCount() > countWhenSuspended + 1, "Request count failed to increment when feed was resumed, from " + countWhenSuspended + ", still at " + server.getRequestCount()); } }); }
protected void runPollsAndParsesHttpGetResponseWithBasicAuthentication(boolean preemptiveBasicAuth) throws Exception { final String username = "brooklyn"; final String password = "hunter2"; httpService = new HttpService(PortRanges.fromString("9000+")) .basicAuthentication(username, password) .start(); URI baseUrl = new URI(httpService.getUrl()); assertEquals(baseUrl.getScheme(), "http", "baseUrl="+baseUrl); feed = HttpFeed.builder() .entity(entity) .baseUri(baseUrl) .credentials(username, password) .preemptiveBasicAuth(preemptiveBasicAuth) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.contains("Hello, World"), "val="+val); }}); }
@Test public void testPollsAndParsesHttpPostResponse() throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .method("post") .period(100) .onSuccess(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .method("post") .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build(); assertSensorEventually(SENSOR_INT, 200, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "{\"foo\":\"myfoo\"}", TIMEOUT_MS); }
protected void runPreemptiveBasicAuth(String username, String password) throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .credentials(username, password) .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); RecordedRequest req = server.takeRequest(); String headerVal = req.getHeader("Authorization"); String expectedVal = getBasicAuthHeaderVal(username, password); assertEquals(headerVal, expectedVal); }
@Test(groups = {"Integration"}) public void testPollWithInvalidCredentialsFails() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+")) .basicAuthentication("brooklyn", "hunter2") .start(); feed = HttpFeed.builder() .entity(entity) .baseUri(httpService.getUrl()) .credentials("brooklyn", "9876543210") .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onException(Functions.constant("Failed!"))) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 401); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.equals("Failed!"), "val=" + val); } }); } }
@Test public void testUsesFailureHandlerOn4xx() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onFailure(Functions.constant("Failed"))) .build(); assertSensorEventually(SENSOR_INT, 401, TIMEOUT_MS); assertSensorEventually(SENSOR_STRING, "Failed", TIMEOUT_MS); server.shutdown(); }
@Override public void init() { super.init(); addFeed(HttpFeed.builder() .entity(this) .baseUrl(getConfig(BASE_URL)) .poll(HttpPollConfig.forSensor(SENSOR_INT) .period(POLL_PERIOD) .onSuccess(HttpValueFunctions.responseCode())) .poll(HttpPollConfig.forSensor(SENSOR_STRING) .period(POLL_PERIOD) .onSuccess(HttpValueFunctions.stringContentsFunction())) .build()); } }
@Test public void testSetsConnectionTimeoutWhenServerDisconnects() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); } server.play(); baseUrl = server.getUrl("/"); feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); }
@Test public void testUsesExceptionHandlerOn4xxAndNoFailureHandler() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); server.shutdown(); }
.poll(new HttpPollConfig<Integer>(SENSOR_INT) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build();