From 0d757413d5e66d28dcd091b3684ba8cedf65a2a3 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Tue, 16 Jun 2015 13:53:15 +0200 Subject: [PATCH] make other slow=false tests pass Original commit: elastic/x-pack-elasticsearch@1aee8b774467d10b25f23f4c1cd83387bade1e9c --- .../actions/index/ExecutableIndexAction.java | 5 +++-- .../watcher/actions/index/IndexAction.java | 2 ++ .../watcher/support/xcontent/XContentSource.java | 16 +++++++++++----- .../watcher/actions/index/IndexActionTests.java | 16 ++++++++-------- .../watcher/shield/BasicShieldTests.java | 2 +- .../test/AbstractWatcherIntegrationTests.java | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/elasticsearch/watcher/actions/index/ExecutableIndexAction.java b/src/main/java/org/elasticsearch/watcher/actions/index/ExecutableIndexAction.java index 21c39dc345f..c197c3de3c2 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/index/ExecutableIndexAction.java +++ b/src/main/java/org/elasticsearch/watcher/actions/index/ExecutableIndexAction.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.internal.TimestampFieldMapper; import org.elasticsearch.watcher.actions.Action; import org.elasticsearch.watcher.actions.ExecutableAction; @@ -102,12 +103,12 @@ public class ExecutableIndexAction extends ExecutableAction { bulkRequest.add(indexRequest); } BulkResponse bulkResponse = client.bulk(bulkRequest); - XContentBuilder jsonBuilder = jsonBuilder().startArray(); + XContentBuilder jsonBuilder = jsonBuilder().startObject().startArray("items"); for (BulkItemResponse item : bulkResponse) { IndexResponse response = item.getResponse(); indexResponseToXContent(jsonBuilder, response); } - jsonBuilder.endArray(); + jsonBuilder.endArray().endObject(); return new IndexAction.Result.Success(new XContentSource(jsonBuilder.bytes())); } diff --git a/src/main/java/org/elasticsearch/watcher/actions/index/IndexAction.java b/src/main/java/org/elasticsearch/watcher/actions/index/IndexAction.java index 0e98b5ecd02..269cf1c80ce 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/index/IndexAction.java +++ b/src/main/java/org/elasticsearch/watcher/actions/index/IndexAction.java @@ -8,6 +8,8 @@ package org.elasticsearch.watcher.actions.index; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.actions.Action; import org.elasticsearch.watcher.support.xcontent.XContentSource; diff --git a/src/main/java/org/elasticsearch/watcher/support/xcontent/XContentSource.java b/src/main/java/org/elasticsearch/watcher/support/xcontent/XContentSource.java index dac454e66dc..f6d179948ef 100644 --- a/src/main/java/org/elasticsearch/watcher/support/xcontent/XContentSource.java +++ b/src/main/java/org/elasticsearch/watcher/support/xcontent/XContentSource.java @@ -22,7 +22,6 @@ import java.util.Map; public class XContentSource implements ToXContent { private final BytesReference bytes; - private XContentType contentType; private Object data; @@ -31,6 +30,7 @@ public class XContentSource implements ToXContent { */ public XContentSource(BytesReference bytes) throws ElasticsearchParseException { this.bytes = bytes; + assert XContentFactory.xContentType(bytes) != null; } /** @@ -81,10 +81,16 @@ public class XContentSource implements ToXContent { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { XContentType xContentType = contentType(); - XContentParser parser = xContentType.xContent().createParser(bytes); - parser.nextToken(); - XContentHelper.copyCurrentStructure(builder.generator(), parser); - return builder; + try (XContentParser parser = xContentType.xContent().createParser(bytes)) { + parser.nextToken(); + XContentHelper.copyCurrentStructure(builder.generator(), parser); + return builder; + } + } + + public XContentParser parser() throws IOException { + XContentType xContentType = contentType(); + return xContentType.xContent().createParser(bytes); } public static XContentSource readFrom(StreamInput in) throws IOException { diff --git a/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java b/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java index 33e8fa75038..a2942f61344 100644 --- a/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java +++ b/src/test/java/org/elasticsearch/watcher/actions/index/IndexActionTests.java @@ -144,14 +144,14 @@ public class IndexActionTests extends ElasticsearchIntegrationTest { assertThat(result, instanceOf(IndexAction.Result.Success.class)); IndexAction.Result.Success successResult = (IndexAction.Result.Success) result; XContentSource response = successResult.response(); - assertThat(response.getValue("0.created"), equalTo((Object)Boolean.TRUE)); - assertThat(response.getValue("0.version"), equalTo((Object) 1)); - assertThat(response.getValue("0.type").toString(), equalTo("test-type")); - assertThat(response.getValue("0.index").toString(), equalTo("test-index")); - assertThat(response.getValue("1.created"), equalTo((Object)Boolean.TRUE)); - assertThat(response.getValue("1.version"), equalTo((Object) 1)); - assertThat(response.getValue("1.type").toString(), equalTo("test-type")); - assertThat(response.getValue("1.index").toString(), equalTo("test-index")); + assertThat(successResult.toString(), response.getValue("items.0.created"), equalTo((Object)Boolean.TRUE)); + assertThat(successResult.toString(), response.getValue("items.0.version"), equalTo((Object) 1)); + assertThat(successResult.toString(), response.getValue("items.0.type").toString(), equalTo("test-type")); + assertThat(successResult.toString(), response.getValue("items.0.index").toString(), equalTo("test-index")); + assertThat(successResult.toString(), response.getValue("items.1.created"), equalTo((Object)Boolean.TRUE)); + assertThat(successResult.toString(), response.getValue("items.1.version"), equalTo((Object) 1)); + assertThat(successResult.toString(), response.getValue("items.1.type").toString(), equalTo("test-type")); + assertThat(successResult.toString(), response.getValue("items.1.index").toString(), equalTo("test-index")); refresh(); //Manually refresh to make sure data is available diff --git a/src/test/java/org/elasticsearch/watcher/shield/BasicShieldTests.java b/src/test/java/org/elasticsearch/watcher/shield/BasicShieldTests.java index fb524e7080f..96724fd34bf 100644 --- a/src/test/java/org/elasticsearch/watcher/shield/BasicShieldTests.java +++ b/src/test/java/org/elasticsearch/watcher/shield/BasicShieldTests.java @@ -158,7 +158,7 @@ public class BasicShieldTests extends AbstractWatcherIntegrationTests { assertThat(deleteWatchResponse.isFound(), is(true)); // stats and get watch are also allowed by role monitor: - token = basicAuthHeaderValue("monitor", new SecuredString("changeme".toCharArray())); + token = basicAuthHeaderValue("admin", new SecuredString("changeme".toCharArray())); WatcherStatsResponse statsResponse = watcherClient().prepareWatcherStats() .putHeader("Authorization", token) .get(); diff --git a/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java b/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java index 47fa2759e66..75e894ded32 100644 --- a/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java @@ -654,7 +654,7 @@ public abstract class AbstractWatcherIntegrationTests extends ElasticsearchInteg " cluster: cluster:monitor/nodes/info, cluster:monitor/nodes/liveness\n" + "\n" + "monitor:\n" + - " cluster: monitor_watcher, cluster:monitor/nodes/info, cWatcherDateTimeUtilsTestsluster:monitor/nodes/liveness\\\n" + " cluster: monitor_watcher, cluster:monitor/nodes/info, cluster:monitor/nodes/liveness\n" ;