Fix broken TaskInfo.toString()

Related to #22387
This commit is contained in:
Igor Motov 2017-01-19 11:57:52 -05:00
parent d86f97c428
commit cfb415de7b
5 changed files with 57 additions and 9 deletions

View File

@ -161,8 +161,9 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContentOb
}
builder.startObject("tasks");
for(TaskInfo task : entry.getValue()) {
builder.field(task.getTaskId().toString());
builder.startObject(task.getTaskId().toString());
task.toXContent(builder, params);
builder.endObject();
}
builder.endObject();
builder.endObject();

View File

@ -81,7 +81,7 @@ public class TaskGroup implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
task.innerToXContent(builder, params);
task.toXContent(builder, params);
if (childTasks.isEmpty() == false) {
builder.startArray("children");
for (TaskGroup taskGroup : childTasks) {

View File

@ -163,12 +163,6 @@ public final class TaskInfo implements Writeable, ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
innerToXContent(builder, params);
return builder.endObject();
}
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("node", taskId.getNodeId());
builder.field("id", taskId.getId());
builder.field("type", type);

View File

@ -160,7 +160,9 @@ public final class TaskResult implements Writeable, ToXContent {
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("completed", completed);
builder.field("task", task);
builder.startObject("task");
task.toXContent(builder, params);
builder.endObject();
if (error != null) {
XContentHelper.writeRawField("error", error, builder, params);
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.admin.cluster.node.tasks;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.tasks.TaskInfo;
import org.elasticsearch.test.ESTestCase;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class TaskTests extends ESTestCase {
public void testTaskInfoToString() {
String nodeId = randomAsciiOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = randomBoolean();
TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type",
"test_action", "test_description", null, startTime, runningTime, cancellable, TaskId.EMPTY_TASK_ID);
String taskInfoString = taskInfo.toString();
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
assertEquals(((Number)map.get("id")).longValue(), taskId);
assertEquals(map.get("type"), "test_type");
assertEquals(map.get("action"), "test_action");
assertEquals(map.get("description"), "test_description");
assertEquals(((Number)map.get("start_time_in_millis")).longValue(), startTime);
assertEquals(((Number)map.get("running_time_in_nanos")).longValue(), runningTime);
assertEquals(map.get("cancellable"), cancellable);
}
}