Skip to content

Commit 4a1e344

Browse files
committed
Added GetTaskResponseTest, tidied endpoint building, removed weird Javadoc
1 parent 6ab1881 commit 4a1e344

File tree

5 files changed

+113
-9
lines changed

5 files changed

+113
-9
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,11 +1020,6 @@ EndpointBuilder addPathPart(String... parts) {
10201020
return this;
10211021
}
10221022

1023-
EndpointBuilder addColonSeparatedPathParts(String... parts) {
1024-
addPathPart(String.join(":", parts));
1025-
return this;
1026-
}
1027-
10281023
EndpointBuilder addCommaSeparatedPathParts(String[] parts) {
10291024
addPathPart(String.join(",", parts));
10301025
return this;

client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static Request listTasks(ListTasksRequest listTaskRequest) {
5959

6060
static Request getTask(GetTaskRequest getTaskRequest) {
6161
String endpoint = new EndpointBuilder().addPathPartAsIs("_tasks")
62-
.addColonSeparatedPathParts(getTaskRequest.getNodeId(), Long.toString(getTaskRequest.getTaskId()))
62+
.addPathPartAsIs(getTaskRequest.getNodeId() + ":" + Long.toString(getTaskRequest.getTaskId()))
6363
.build();
6464
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
6565
RequestConverters.Params params = new RequestConverters.Params(request);

client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public TimeValue getTimeout() {
6767
}
6868

6969
/**
70-
* Timeout to wait for any async actions this request must take. It must take anywhere from 0 to 2.
70+
* Timeout to wait for any async actions this request must take.
7171
*/
7272
public GetTaskRequest setTimeout(TimeValue timeout) {
7373
this.timeout = timeout;

client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
public class GetTaskResponse {
3030
private final boolean completed;
3131
private final TaskInfo taskInfo;
32+
public static final ParseField COMPLETED = new ParseField("completed");
33+
public static final ParseField TASK = new ParseField("task");
3234

3335
public GetTaskResponse(boolean completed, TaskInfo taskInfo) {
3436
this.completed = completed;
@@ -46,8 +48,8 @@ public TaskInfo getTaskInfo() {
4648
private static final ConstructingObjectParser<GetTaskResponse, Void> PARSER = new ConstructingObjectParser<>("get_task",
4749
true, a -> new GetTaskResponse((boolean) a[0], (TaskInfo) a[1]));
4850
static {
49-
PARSER.declareBoolean(constructorArg(), new ParseField("completed"));
50-
PARSER.declareObject(constructorArg(), (p, c) -> TaskInfo.fromXContent(p), new ParseField("task"));
51+
PARSER.declareBoolean(constructorArg(), COMPLETED);
52+
PARSER.declareObject(constructorArg(), (p, c) -> TaskInfo.fromXContent(p), TASK);
5153
}
5254

5355
public static GetTaskResponse fromXContent(XContentParser parser) {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.core.tasks;
21+
22+
import org.elasticsearch.client.Requests;
23+
import org.elasticsearch.client.tasks.GetTaskResponse;
24+
import org.elasticsearch.common.bytes.BytesReference;
25+
import org.elasticsearch.common.xcontent.ToXContent;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
import org.elasticsearch.tasks.RawTaskStatus;
28+
import org.elasticsearch.tasks.Task;
29+
import org.elasticsearch.tasks.TaskId;
30+
import org.elasticsearch.tasks.TaskInfo;
31+
import org.elasticsearch.test.ESTestCase;
32+
33+
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.Map;
36+
37+
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
38+
39+
public class GetTaskResponseTest extends ESTestCase {
40+
41+
public void testFromXContent() throws IOException {
42+
xContentTester(
43+
this::createParser,
44+
this::createTestInstance,
45+
this::toXContent,
46+
GetTaskResponse::fromXContent)
47+
.supportsUnknownFields(true)
48+
.assertEqualsConsumer(this::assertEqualInstances)
49+
.assertToXContentEquivalence(true)
50+
.randomFieldsExcludeFilter(field ->field.endsWith("headers") || field.endsWith("status"))
51+
.test();
52+
}
53+
54+
private GetTaskResponse createTestInstance() {
55+
return new GetTaskResponse(randomBoolean(), randomTaskInfo());
56+
}
57+
58+
private void toXContent(GetTaskResponse response, XContentBuilder builder) throws IOException {
59+
builder.startObject();
60+
{
61+
builder.field(GetTaskResponse.COMPLETED.getPreferredName(), response.isCompleted());
62+
builder.startObject(GetTaskResponse.TASK.getPreferredName());
63+
response.getTaskInfo().toXContent(builder, ToXContent.EMPTY_PARAMS);
64+
builder.endObject();
65+
}
66+
builder.endObject();
67+
}
68+
69+
private void assertEqualInstances(GetTaskResponse expectedInstance, GetTaskResponse newInstance) {
70+
assertEquals(expectedInstance.isCompleted(), newInstance.isCompleted());
71+
assertEquals(expectedInstance.getTaskInfo(), newInstance.getTaskInfo());
72+
}
73+
74+
static TaskInfo randomTaskInfo() {
75+
TaskId taskId = randomTaskId();
76+
String type = randomAlphaOfLength(5);
77+
String action = randomAlphaOfLength(5);
78+
Task.Status status = randomBoolean() ? randomRawTaskStatus() : null;
79+
String description = randomBoolean() ? randomAlphaOfLength(5) : null;
80+
long startTime = randomLong();
81+
long runningTimeNanos = randomLong();
82+
boolean cancellable = randomBoolean();
83+
TaskId parentTaskId = randomBoolean() ? TaskId.EMPTY_TASK_ID : randomTaskId();
84+
Map<String, String> headers = randomBoolean() ?
85+
Collections.emptyMap() :
86+
Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5));
87+
return new TaskInfo(taskId, type, action, description, status, startTime, runningTimeNanos, cancellable, parentTaskId, headers);
88+
}
89+
90+
private static TaskId randomTaskId() {
91+
return new TaskId(randomAlphaOfLength(5), randomLong());
92+
}
93+
94+
private static RawTaskStatus randomRawTaskStatus() {
95+
try (XContentBuilder builder = XContentBuilder.builder(Requests.INDEX_CONTENT_TYPE.xContent())) {
96+
builder.startObject();
97+
int fields = between(0, 10);
98+
for (int f = 0; f < fields; f++) {
99+
builder.field(randomAlphaOfLength(5), randomAlphaOfLength(5));
100+
}
101+
builder.endObject();
102+
return new RawTaskStatus(BytesReference.bytes(builder));
103+
} catch (IOException e) {
104+
throw new IllegalStateException(e);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)