Search in sources :

Example 1 with SimulateDocumentVerboseResult

use of org.opensearch.action.ingest.SimulateDocumentVerboseResult in project OpenSearch by opensearch-project.

the class IngestClientIT method testSimulatePipeline.

private void testSimulatePipeline(boolean isVerbose, boolean isFailure) throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentBuilder.builder(xContentType.xContent());
    String rankValue = isFailure ? "non-int" : Integer.toString(1234);
    builder.startObject();
    {
        builder.field("pipeline");
        buildRandomXContentPipeline(builder);
        builder.startArray("docs");
        {
            builder.startObject().field("_index", "index").field("_id", "doc_" + 1).startObject("_source").field("foo", "rab_" + 1).field("rank", rankValue).endObject().endObject();
        }
        builder.endArray();
    }
    builder.endObject();
    SimulatePipelineRequest request = new SimulatePipelineRequest(BytesReference.bytes(builder), builder.contentType());
    request.setVerbose(isVerbose);
    SimulatePipelineResponse response = execute(request, highLevelClient().ingest()::simulate, highLevelClient().ingest()::simulateAsync);
    List<SimulateDocumentResult> results = response.getResults();
    assertEquals(1, results.size());
    if (isVerbose) {
        assertThat(results.get(0), instanceOf(SimulateDocumentVerboseResult.class));
        SimulateDocumentVerboseResult verboseResult = (SimulateDocumentVerboseResult) results.get(0);
        assertEquals(2, verboseResult.getProcessorResults().size());
        if (isFailure) {
            assertNotNull(verboseResult.getProcessorResults().get(1).getFailure());
            assertThat(verboseResult.getProcessorResults().get(1).getFailure().getMessage(), containsString("unable to convert [non-int] to integer"));
        } else {
            assertEquals(verboseResult.getProcessorResults().get(0).getIngestDocument().getFieldValue("foo", String.class), "bar");
            assertEquals(Integer.valueOf(1234), verboseResult.getProcessorResults().get(1).getIngestDocument().getFieldValue("rank", Integer.class));
        }
    } else {
        assertThat(results.get(0), instanceOf(SimulateDocumentBaseResult.class));
        SimulateDocumentBaseResult baseResult = (SimulateDocumentBaseResult) results.get(0);
        if (isFailure) {
            assertNotNull(baseResult.getFailure());
            assertThat(baseResult.getFailure().getMessage(), containsString("unable to convert [non-int] to integer"));
        } else {
            assertNotNull(baseResult.getIngestDocument());
            assertEquals(baseResult.getIngestDocument().getFieldValue("foo", String.class), "bar");
            assertEquals(Integer.valueOf(1234), baseResult.getIngestDocument().getFieldValue("rank", Integer.class));
        }
    }
}
Also used : SimulateDocumentResult(org.opensearch.action.ingest.SimulateDocumentResult) XContentType(org.opensearch.common.xcontent.XContentType) SimulatePipelineResponse(org.opensearch.action.ingest.SimulatePipelineResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) SimulateDocumentVerboseResult(org.opensearch.action.ingest.SimulateDocumentVerboseResult) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SimulateDocumentBaseResult(org.opensearch.action.ingest.SimulateDocumentBaseResult)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)1 SimulateDocumentBaseResult (org.opensearch.action.ingest.SimulateDocumentBaseResult)1 SimulateDocumentResult (org.opensearch.action.ingest.SimulateDocumentResult)1 SimulateDocumentVerboseResult (org.opensearch.action.ingest.SimulateDocumentVerboseResult)1 SimulatePipelineRequest (org.opensearch.action.ingest.SimulatePipelineRequest)1 SimulatePipelineResponse (org.opensearch.action.ingest.SimulatePipelineResponse)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 XContentType (org.opensearch.common.xcontent.XContentType)1