@Test public void testPollsAndParsesHttpErrorResponseLocal() throws Exception { int unboundPort = Networking.nextAvailablePort(10000); feed = HttpFeed.builder() .entity(entity) .baseUri("http://localhost:" + unboundPort + "/path/should/not/exist") .poll(new HttpPollConfig<String>(SENSOR_STRING) .onSuccess(Functions.constant("success")) .onFailure(Functions.constant("failure")) .onException(Functions.constant("error"))) .build(); assertSensorEventually(SENSOR_STRING, "error", TIMEOUT_MS); }
.onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(Boolean.class))) .onFailure(HttpValueFunctions.responseCodeEquals(404)) .setOnException(false)) .poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE)
@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(); }
@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); } }); } }