Skip to content

Commit e0288f1

Browse files
extended AbstractStreamableXContentTestCase
-- Also fixed strings -- removed getPipelineConfigs method and switched to pipelines() method.
1 parent da82960 commit e0288f1

File tree

5 files changed

+112
-34
lines changed

5 files changed

+112
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void putPipelineAsync(PutPipelineRequest request, ActionListener<PutPipel
9494
* Get an existing pipeline
9595
* <p>
9696
* See
97-
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/put-pipeline-api.html"> Get Pipeline API on elastic.co</a>
97+
* <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/get-pipeline-api.html"> Get Pipeline API on elastic.co</a>
9898
*/
9999
public GetPipelineResponse getPipeline(GetPipelineRequest request, Header... headers) throws IOException {
100100
return restHighLevelClient.performRequestAndParseEntity( request, RequestConverters::getPipeline,

client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void testPutPipeline() throws IOException {
127127
}
128128

129129
public void testGetPipeline() throws IOException {
130-
String id = "get_pipeline_id";
130+
String id = "some_pipeline_id";
131131
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
132132
{
133133
PutPipelineRequest request = new PutPipelineRequest(
@@ -143,9 +143,9 @@ public void testGetPipeline() throws IOException {
143143
GetPipelineResponse response =
144144
execute(request, highLevelClient().cluster()::getPipeline, highLevelClient().cluster()::getPipelineAsync);
145145
assertTrue(response.isFound());
146-
assertEquals(response.getPipelineConfigs().get(0).getId(), id);
146+
assertEquals(response.pipelines().get(0).getId(), id);
147147
PipelineConfiguration expectedConfig =
148148
new PipelineConfiguration(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
149-
assertEquals(expectedConfig.getConfigAsMap(), response.getPipelineConfigs().get(0).getConfigAsMap());
149+
assertEquals(expectedConfig.getConfigAsMap(), response.pipelines().get(0).getConfigAsMap());
150150
}
151151
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.nio.charset.StandardCharsets;
4343
import java.util.HashMap;
4444
import java.util.Map;
45+
import java.util.List;
4546
import java.util.concurrent.CountDownLatch;
4647
import java.util.concurrent.TimeUnit;
4748

@@ -284,7 +285,7 @@ public void testGetPipeline() throws IOException {
284285

285286
// tag::get-pipeline-response
286287
boolean successful = response.isFound(); // <1>
287-
List<PipelineConfiguration> pipelines = response.getPipelineConfigs(); // <2>
288+
List<PipelineConfiguration> pipelines = response.pipelines(); // <2>
288289
for(PipelineConfiguration pipeline: pipelines) {
289290
Map<String, Object> config = pipeline.getConfigAsMap(); // <3>
290291
}

server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.ArrayList;
3535
import java.util.Collections;
3636
import java.util.List;
37+
import java.util.Map;
38+
import java.util.HashMap;
3739

3840
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
3941

@@ -48,8 +50,13 @@ public GetPipelineResponse(List<PipelineConfiguration> pipelines) {
4850
this.pipelines = pipelines;
4951
}
5052

53+
/**
54+
* Get the list of pipelines that were a part of this response.
55+
* The pipeline id can be obtained using getId on the PipelineConfiguration object.
56+
* @return A list of {@link PipelineConfiguration} objects.
57+
*/
5158
public List<PipelineConfiguration> pipelines() {
52-
return pipelines;
59+
return Collections.unmodifiableList(pipelines);
5360
}
5461

5562
@Override
@@ -80,15 +87,6 @@ public RestStatus status() {
8087
return isFound() ? RestStatus.OK : RestStatus.NOT_FOUND;
8188
}
8289

83-
/**
84-
* Get the list of pipelines that were a part of this response
85-
* The pipeline id can be obtained using
86-
* @return A list of PipelineConfiguration objects.
87-
*/
88-
public List<PipelineConfiguration> getPipelineConfigs() {
89-
return Collections.unmodifiableList(pipelines);
90-
}
91-
9290
@Override
9391
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
9492
builder.startObject();
@@ -103,7 +101,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
103101
*
104102
* @param parser the parser for the XContent that contains the serialized GetPipelineResponse.
105103
* @return an instance of GetPipelineResponse read from the parser
106-
* @throws IOException
104+
* @throws IOException If the parsing fails
107105
*/
108106
public static GetPipelineResponse fromXContent(XContentParser parser) throws IOException {
109107
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
@@ -122,4 +120,43 @@ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOE
122120
ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation);
123121
return new GetPipelineResponse(pipelines);
124122
}
123+
124+
@Override
125+
public boolean equals(Object other) {
126+
if (other == null) {
127+
return false;
128+
} else if (other instanceof GetPipelineResponse){
129+
GetPipelineResponse otherResponse = (GetPipelineResponse)other;
130+
if (pipelines == null) {
131+
return otherResponse.pipelines == null;
132+
} else {
133+
// We need a map here because order does not matter for equality
134+
Map<String, PipelineConfiguration> otherPipelineMap = new HashMap<>();
135+
for (PipelineConfiguration pipeline: otherResponse.pipelines) {
136+
otherPipelineMap.put(pipeline.getId(), pipeline);
137+
}
138+
for (PipelineConfiguration pipeline: pipelines) {
139+
PipelineConfiguration otherPipeline = otherPipelineMap.get(pipeline.getId());
140+
if (otherPipeline == null ||
141+
!pipeline.getConfigAsMap().equals(otherPipeline.getConfigAsMap())) {
142+
return false;
143+
}
144+
}
145+
return true;
146+
}
147+
} else {
148+
return false;
149+
}
150+
}
151+
152+
@Override
153+
public int hashCode() {
154+
int result = 1;
155+
for (PipelineConfiguration pipeline: pipelines) {
156+
// We only take the sum here to ensure that the order does not matter.
157+
result += (pipeline == null ? 0 : pipeline.getConfigAsMap().hashCode());
158+
}
159+
return result;
160+
}
161+
125162
}

server/src/test/java/org/elasticsearch/action/ingest/GetPipelineResponseTests.java

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,45 @@
2626
import org.elasticsearch.common.xcontent.XContentParser;
2727
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2828
import org.elasticsearch.ingest.PipelineConfiguration;
29-
import org.elasticsearch.test.ESTestCase;
29+
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
3030

3131
import java.io.IOException;
3232
import java.util.ArrayList;
3333
import java.util.HashMap;
3434
import java.util.List;
3535
import java.util.Map;
3636

37-
public class GetPipelineResponseTests extends ESTestCase {
37+
public class GetPipelineResponseTests extends AbstractStreamableXContentTestCase<GetPipelineResponse> {
3838

3939
private XContentBuilder getRandomXContentBuilder() throws IOException {
4040
XContentType xContentType = randomFrom(XContentType.values());
4141
return XContentBuilder.builder(xContentType.xContent());
4242
}
4343

44+
private PipelineConfiguration createRandomPipeline(String pipelineId) throws IOException {
45+
String field = "field_" + randomInt();
46+
String value = "value_" + randomInt();
47+
XContentBuilder builder = getRandomXContentBuilder();
48+
builder.startObject();
49+
// We only use a single SetProcessor here in each pipeline to test.
50+
// Since the contents are returned as a configMap anyway this does not matter for fromXContent
51+
builder.startObject("set");
52+
builder.field("field", field);
53+
builder.field("value", value);
54+
builder.endObject();
55+
builder.endObject();
56+
return
57+
new PipelineConfiguration(
58+
pipelineId, BytesReference.bytes(builder), builder.contentType()
59+
);
60+
}
61+
4462
private Map<String, PipelineConfiguration> createPipelineConfigMap() throws IOException {
4563
int numPipelines = randomInt(5);
4664
Map<String, PipelineConfiguration> pipelinesMap = new HashMap<>();
4765
for (int i=0; i<numPipelines; i++) {
48-
// We only use a single SetProcessor here in each pipeline to test.
49-
// Since the contents are returned as a configMap anyway this does not matter for fromXContent
5066
String pipelineId = "pipeline_" + i;
51-
String field = "field_" + i;
52-
String value = "value_" + i;
53-
XContentBuilder builder = getRandomXContentBuilder();
54-
builder.startObject();
55-
builder.startObject("set");
56-
builder.field("field", field);
57-
builder.field("value", value);
58-
builder.endObject();
59-
builder.endObject();
60-
PipelineConfiguration pipeline =
61-
new PipelineConfiguration(
62-
pipelineId, BytesReference.bytes(builder), builder.contentType()
63-
);
64-
pipelinesMap.put(pipelineId, pipeline);
67+
pipelinesMap.put(pipelineId, createRandomPipeline(pipelineId));
6568
}
6669
return pipelinesMap;
6770
}
@@ -89,4 +92,41 @@ public void testXContentDeserialization() throws IOException {
8992
assertEquals(pipelinesMap.get(pipeline.getId()).getConfigAsMap(), pipeline.getConfigAsMap());
9093
}
9194
}
95+
96+
@Override
97+
protected GetPipelineResponse doParseInstance(XContentParser parser) throws IOException {
98+
return GetPipelineResponse.fromXContent(parser);
99+
}
100+
101+
@Override
102+
protected GetPipelineResponse createBlankInstance() {
103+
return new GetPipelineResponse();
104+
}
105+
106+
@Override
107+
protected GetPipelineResponse createTestInstance() {
108+
try {
109+
return new GetPipelineResponse(new ArrayList<>(createPipelineConfigMap().values()));
110+
} catch (IOException e) {
111+
// If we fail to construct an instance we return `null` which would make the user of this method
112+
// fail the test.
113+
return null;
114+
}
115+
}
116+
117+
@Override
118+
protected boolean supportsUnknownFields() {
119+
return false;
120+
}
121+
122+
@Override
123+
protected GetPipelineResponse mutateInstance(GetPipelineResponse response) {
124+
try {
125+
ArrayList<PipelineConfiguration> clonePipelines = new ArrayList<>(response.pipelines());
126+
clonePipelines.add(createRandomPipeline("pipeline_" + clonePipelines.size() + 1));
127+
return new GetPipelineResponse(clonePipelines);
128+
} catch (IOException e) {
129+
return null;
130+
}
131+
}
92132
}

0 commit comments

Comments
 (0)