From 374be8b732bec03c904cb62952cfac30b8934064 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 27 Mar 2017 10:22:22 +0200 Subject: [PATCH] Watcher: Support arrays in http response payload (elastic/x-pack-elasticsearch#793) The xcontent parser was only set to read all data to a map which did not work, when the returned data was in form of an array (for example the cat API is doing this, if the response format is set to JSON). relates elastic/x-pack-elasticsearch#351 Original commit: elastic/x-pack-elasticsearch@08ad457bf650c2a7bd4096d0209429ecdc4e7909 --- .../input/http/ExecutableHttpInput.java | 8 +++++- .../watcher/input/http/HttpInputTests.java | 27 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java index eba6e4bcc41..93c8b991740 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java @@ -83,7 +83,13 @@ public class ExecutableHttpInput extends ExecutableInput headers = Collections.singletonMap("Content-Type", new String[]{"application/json"}); + HttpResponse response = new HttpResponse(200, "[ { \"foo\": \"first\" }, { \"foo\": \"second\"}]", headers); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(response); + + HttpRequestTemplate.Builder request = HttpRequestTemplate.builder("localhost", 8080); + HttpInput httpInput = InputBuilders.httpInput(request.build()).build(); + ExecutableHttpInput input = new ExecutableHttpInput(httpInput, logger, httpClient, templateEngine); + + WatchExecutionContext ctx = createWatchExecutionContext(); + HttpInput.Result result = input.execute(ctx, new Payload.Simple()); + assertThat(result.statusCode, is(200)); + assertThat(result.payload().data(), not(hasKey("_value"))); + assertThat(result.payload().data(), hasKey("data")); + assertThat(result.payload().data().get("data"), instanceOf(List.class)); + List> data = (List>) result.payload().data().get("data"); + assertThat(data, hasSize(2)); + assertThat(data.get(0).get("foo"), is("first")); + assertThat(data.get(1).get("foo"), is("second")); + } + + private WatchExecutionContext createWatchExecutionContext() { Watch watch = new Watch("test-watch", new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.MINUTES))),