|
| 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