make the rest pass

Original commit: elastic/x-pack-elasticsearch@3d60f13994
This commit is contained in:
Simon Willnauer 2015-06-17 15:46:52 +02:00
parent 9886ecab5c
commit ef7d216b4e
5 changed files with 33 additions and 31 deletions

View File

@ -103,13 +103,13 @@ public class ExecutableIndexAction extends ExecutableAction<IndexAction> {
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulkRequest);
XContentBuilder jsonBuilder = jsonBuilder().startObject().startArray("items");
XContentBuilder jsonBuilder = jsonBuilder().startArray();
for (BulkItemResponse item : bulkResponse) {
IndexResponse response = item.getResponse();
indexResponseToXContent(jsonBuilder, response);
}
jsonBuilder.endArray().endObject();
return new IndexAction.Result.Success(new XContentSource(jsonBuilder.bytes()));
jsonBuilder.endArray();
return new IndexAction.Result.Success(new XContentSource(jsonBuilder.bytes(), XContentType.JSON));
}
static void indexResponseToXContent(XContentBuilder builder, IndexResponse response) throws IOException {

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.watcher.support.xcontent;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
@ -22,15 +23,22 @@ import java.util.Map;
public class XContentSource implements ToXContent {
private final BytesReference bytes;
private XContentType contentType;
private final XContentType contentType;
private Object data;
/**
* Constructs a new XContentSource out of the given bytes reference.
*/
public XContentSource(BytesReference bytes) throws ElasticsearchParseException {
public XContentSource(BytesReference bytes, XContentType xContentType) throws ElasticsearchParseException {
if (xContentType == null) {
throw new IllegalArgumentException("xContentType must not be null");
}
this.bytes = bytes;
assert XContentFactory.xContentType(bytes) != null;
this.contentType = xContentType;
}
public XContentSource(BytesReference bytes) {
this(bytes, XContentFactory.xContentType(bytes));
}
/**
@ -80,8 +88,7 @@ public class XContentSource implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
XContentType xContentType = contentType();
try (XContentParser parser = xContentType.xContent().createParser(bytes)) {
try (XContentParser parser = parser()) {
parser.nextToken();
XContentHelper.copyCurrentStructure(builder.generator(), parser);
return builder;
@ -89,30 +96,25 @@ public class XContentSource implements ToXContent {
}
public XContentParser parser() throws IOException {
XContentType xContentType = contentType();
return xContentType.xContent().createParser(bytes);
return contentType.xContent().createParser(bytes);
}
public static XContentSource readFrom(StreamInput in) throws IOException {
return new XContentSource(in.readBytesReference());
return new XContentSource(in.readBytesReference(), XContentType.readFrom(in));
}
public static void writeTo(XContentSource source, StreamOutput out) throws IOException {
out.writeBytesReference(source.bytes);
}
private XContentType contentType() {
if (contentType == null) {
contentType = XContentFactory.xContentType(bytes);
}
return contentType;
XContentType.writeTo(source.contentType, out);
}
private Object data() {
if (data == null) {
Tuple<XContentType, Object> tuple = WatcherXContentUtils.convertToObject(bytes);
this.contentType = tuple.v1();
this.data = tuple.v2();
try (XContentParser parser = parser()) {
data = WatcherXContentUtils.readValue(parser, parser.nextToken());
} catch (IOException ex) {
throw new ElasticsearchException("failed to read value", ex);
}
}
return data;
}

View File

@ -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(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"));
assertThat(successResult.toString(), response.getValue("0.created"), equalTo((Object)Boolean.TRUE));
assertThat(successResult.toString(), response.getValue("0.version"), equalTo((Object) 1));
assertThat(successResult.toString(), response.getValue("0.type").toString(), equalTo("test-type"));
assertThat(successResult.toString(), response.getValue("0.index").toString(), equalTo("test-index"));
assertThat(successResult.toString(), response.getValue("1.created"), equalTo((Object)Boolean.TRUE));
assertThat(successResult.toString(), response.getValue("1.version"), equalTo((Object) 1));
assertThat(successResult.toString(), response.getValue("1.type").toString(), equalTo("test-type"));
assertThat(successResult.toString(), response.getValue("1.index").toString(), equalTo("test-index"));
refresh(); //Manually refresh to make sure data is available

View File

@ -98,7 +98,7 @@ public class TriggeredWatchStoreTests extends ElasticsearchTestCase {
state = ShardRoutingState.UNASSIGNED;
}
indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(indexName, 0), false)
.addShard(new ImmutableShardRouting(indexName, 0, "_node_id", null, true, state, 1))
.addShard(new ImmutableShardRouting(indexName, 0, "_node_id", null, null, true, state, 1, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")))
.build());
indexRoutingTableBuilder.addReplica();
}

View File

@ -88,7 +88,7 @@ public class WatchStoreTests extends ElasticsearchTestCase {
metaDateBuilder.put(IndexMetaData.builder(WatchStore.INDEX).settings(settings).numberOfShards(1).numberOfReplicas(1));
IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(WatchStore.INDEX);
indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(WatchStore.INDEX, 0), false)
.addShard(new ImmutableShardRouting(WatchStore.INDEX, 0, "_node_id", null, true, ShardRoutingState.UNASSIGNED, 1))
.addShard(new ImmutableShardRouting(WatchStore.INDEX, 0, "_node_id", null, null, true, ShardRoutingState.UNASSIGNED, 1, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")))
.build());
indexRoutingTableBuilder.addReplica();
routingTableBuilder.add(indexRoutingTableBuilder.build());