/** as {@link #walk(Iterable))} but working with objects which {@link Maybe} contain {@link JsonElement}, * simply preserving a {@link Maybe#absent()} object if additional walks are requested upon it * (cf jquery) */ public static Function<Maybe<JsonElement>, Maybe<JsonElement>> walkM(final Iterable<String> elements) { return new WalkM(elements); }
/** returns a function which traverses the supplied path of entries in a json object (maps of maps of maps...), * @throws NoSuchElementException if any path is not present as a key in that map */ public static Function<JsonElement, JsonElement> walk(final Iterable<String> elements) { // could do this instead, pointing at Maybe for this, and for walkN, but it's slightly less efficient // return Functionals.chain(MaybeFunctions.<JsonElement>wrap(), walkM(elements), MaybeFunctions.<JsonElement>get()); return new Walk(elements); }
@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); }
@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); }
@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 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); }
public static HttpPollConfig<Void> forMultiple() { return new HttpPollConfig<Void>(PollConfig.NO_SENSOR); }
public static <T> Function<HttpToolResponse, T> jsonContents(String element, Class<T> expected) { return jsonContents(new String[] {element}, expected); }
public Builder period(long millis) { return period(millis, TimeUnit.MILLISECONDS); } public Builder period(long val, TimeUnit units) {
public Builder suspended() { return suspended(true); } public Builder suspended(boolean startsSuspended) {
public void disconnectSensors() { if (httpFeed != null && httpFeed.isActivated()) httpFeed.destroy(); if (scanner != null && scanner.isActivated()) scanner.destroy(); }
@Override public T apply(JsonElement input) { return doCast(input, expected); } };
/** * returns an element from a single json primitive value given a full path {@link com.jayway.jsonpath.JsonPath} */ public static <T> Function<JsonElement,T> getPath(final String path) { return new GetPath<T>(path); }
public static Function<String, JsonElement> asJson() { return new AsJson(); }
public static Function<HttpToolResponse, Integer> responseCode() { return new ResponseCode(); }
/** as {@link #walk(Iterable))} but if any element is not found it simply returns null */ public static Function<JsonElement, JsonElement> walkN(final Iterable<String> elements) { return new WalkN(elements); }
public static Builder builder() { return new Builder(); }
@Override public void disconnectSensors() { if (taskScan != null && taskScan.isActivated()) taskScan.destroy(); taskScan = null; }
@Override public T apply(JsonElement input) { return doCast(input, expected); } };