-
Notifications
You must be signed in to change notification settings - Fork 24.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REST high-level client: add simulate pipeline API #31158
Changes from 4 commits
e530bd4
3497e0c
1de794e
d08dcb5
755de50
54e7f6f
d8f5da9
64c9c51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,15 @@ | |
import org.elasticsearch.action.ingest.GetPipelineRequest; | ||
import org.elasticsearch.action.ingest.GetPipelineResponse; | ||
import org.elasticsearch.action.ingest.PutPipelineRequest; | ||
import org.elasticsearch.action.ingest.SimulateDocumentBaseResult; | ||
import org.elasticsearch.action.ingest.SimulateDocumentResult; | ||
import org.elasticsearch.action.ingest.SimulateDocumentVerboseResult; | ||
import org.elasticsearch.action.ingest.SimulatePipelineRequest; | ||
import org.elasticsearch.action.ingest.SimulatePipelineResponse; | ||
import org.elasticsearch.action.ingest.WritePipelineResponse; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.ingest.PipelineConfiguration; | ||
|
||
import java.io.IOException; | ||
|
@@ -80,4 +86,76 @@ public void testDeletePipeline() throws IOException { | |
execute(request, highLevelClient().ingest()::deletePipeline, highLevelClient().ingest()::deletePipelineAsync); | ||
assertTrue(response.isAcknowledged()); | ||
} | ||
|
||
public void testSimulatePipeline() throws IOException { | ||
XContentType xContentType = randomFrom(XContentType.values()); | ||
XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()); | ||
boolean isVerbose = randomBoolean(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I have left a comment on this before, can we avoid randomizing values in integration tests and rather have two tests, one for verbose and one without verbose? Same for failures There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That means four tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. I am sorry I missed this comment from before. I separated it now. |
||
builder.startObject(); | ||
{ | ||
builder.field("pipeline"); | ||
buildRandomXContentPipeline(builder); | ||
builder.startArray("docs"); | ||
{ | ||
builder.startObject() | ||
.field("_index", "index") | ||
.field("_type", "doc") | ||
.field("_id", "doc_" + 1) | ||
.startObject("_source").field("foo", "rab_" + 1).field("rank", "1234").endObject() | ||
.endObject(); | ||
builder.startObject() | ||
.field("_index", "index") | ||
.field("_type", "doc") | ||
.field("_id", "doc_" + 2) | ||
.startObject("_source").field("foo", "rab_" + 1).field("rank", "non-int").endObject() | ||
.endObject(); | ||
} | ||
builder.endArray(); | ||
} | ||
builder.endObject(); | ||
|
||
SimulatePipelineRequest request = new SimulatePipelineRequest( | ||
BytesReference.bytes(builder), | ||
builder.contentType() | ||
); | ||
request.setVerbose(isVerbose); | ||
|
||
SimulatePipelineResponse simulatePipelineResponse = | ||
execute(request, highLevelClient().ingest()::simulatePipeline, highLevelClient().ingest()::simulatePipelineAsync); | ||
|
||
SimulateDocumentResult result0 = simulatePipelineResponse.getResults().get(0); | ||
SimulateDocumentResult result1 = simulatePipelineResponse.getResults().get(1); | ||
if (isVerbose) { | ||
assertTrue(result0 instanceof SimulateDocumentVerboseResult); | ||
SimulateDocumentVerboseResult verboseResult = (SimulateDocumentVerboseResult)result0; | ||
SimulateDocumentVerboseResult failedVerboseResult = (SimulateDocumentVerboseResult)result1; | ||
assertTrue(verboseResult.getProcessorResults().size() > 0); | ||
assertEquals( | ||
verboseResult.getProcessorResults().get(0).getIngestDocument() | ||
.getFieldValue("foo", String.class), | ||
"bar" | ||
); | ||
assertEquals( | ||
Integer.valueOf(1234), | ||
verboseResult.getProcessorResults().get(1).getIngestDocument() | ||
.getFieldValue("rank", Integer.class) | ||
); | ||
assertNotNull(failedVerboseResult.getProcessorResults().get(1).getFailure()); | ||
} else { | ||
assertTrue(result0 instanceof SimulateDocumentBaseResult); | ||
SimulateDocumentBaseResult baseResult = (SimulateDocumentBaseResult)result0; | ||
SimulateDocumentBaseResult failedBaseResult = (SimulateDocumentBaseResult)result1; | ||
assertNotNull(baseResult.getIngestDocument()); | ||
assertEquals( | ||
baseResult.getIngestDocument().getFieldValue("foo", String.class), | ||
"bar" | ||
); | ||
assertEquals( | ||
Integer.valueOf(1234), | ||
baseResult.getIngestDocument() | ||
.getFieldValue("rank", Integer.class) | ||
); | ||
assertNotNull(failedBaseResult.getFailure()); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that being this an integration test, we should randomize much less, if at all. We can have a fixed request and check that the response is expected, let's leave the randomizations for the unit tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also the test the case where you get back an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we test the failure separately? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ | |
import org.elasticsearch.action.ingest.DeletePipelineRequest; | ||
import org.elasticsearch.action.ingest.GetPipelineRequest; | ||
import org.elasticsearch.action.ingest.PutPipelineRequest; | ||
import org.elasticsearch.action.ingest.SimulatePipelineRequest; | ||
import org.elasticsearch.action.search.ClearScrollRequest; | ||
import org.elasticsearch.action.search.MultiSearchRequest; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
|
@@ -1531,6 +1532,34 @@ public void testDeletePipeline() { | |
assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testSimulatePipeline() throws IOException { | ||
String pipelineId = randomBoolean() ? "some_pipeline_id" : null; | ||
boolean verbose = randomBoolean(); | ||
String json = "{\"pipeline\":{" + | ||
"\"description\":\"_description\"," + | ||
"\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]}," + | ||
"\"docs\":[{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}"; | ||
SimulatePipelineRequest request = new SimulatePipelineRequest( | ||
new BytesArray(json.getBytes(StandardCharsets.UTF_8)), | ||
XContentType.JSON | ||
); | ||
request.setId(pipelineId); | ||
request.setVerbose(verbose); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
expectedParams.put("verbose", Boolean.toString(verbose)); | ||
|
||
Request expectedRequest = RequestConverters.simulatePipeline(request); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
endpoint.add("_ingest/pipeline"); | ||
if (pipelineId != null && !pipelineId.isEmpty()) | ||
endpoint.add(pipelineId); | ||
endpoint.add("_simulate"); | ||
assertEquals(endpoint.toString(), expectedRequest.getEndpoint()); | ||
assertEquals(HttpPost.METHOD_NAME, expectedRequest.getMethod()); | ||
assertEquals(expectedParams, expectedRequest.getParameters()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you try it with a more realistic request body and assert on it at the end? |
||
assertToXContentBody(request, expectedRequest.getEntity()); | ||
} | ||
|
||
public void testClusterHealth() { | ||
ClusterHealthRequest healthRequest = new ClusterHealthRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also have two separate tests for with and without verbose rather than randomizing it?