|
@@ -21,57 +21,29 @@ package org.elasticsearch.action.ingest;
|
|
|
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
-import org.elasticsearch.ingest.IngestDocument;
|
|
|
-import org.elasticsearch.ingest.RandomDocumentPicks;
|
|
|
-import org.elasticsearch.test.ESTestCase;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
+import org.elasticsearch.test.AbstractXContentTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
+import java.util.StringJoiner;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.function.Supplier;
|
|
|
|
|
|
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
|
|
import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
|
|
import static org.hamcrest.CoreMatchers.nullValue;
|
|
|
|
|
|
-public class SimulatePipelineResponseTests extends ESTestCase {
|
|
|
+public class SimulatePipelineResponseTests extends AbstractXContentTestCase<SimulatePipelineResponse> {
|
|
|
|
|
|
public void testSerialization() throws IOException {
|
|
|
boolean isVerbose = randomBoolean();
|
|
|
String id = randomBoolean() ? randomAlphaOfLengthBetween(1, 10) : null;
|
|
|
- int numResults = randomIntBetween(1, 10);
|
|
|
- List<SimulateDocumentResult> results = new ArrayList<>(numResults);
|
|
|
- for (int i = 0; i < numResults; i++) {
|
|
|
- boolean isFailure = randomBoolean();
|
|
|
- IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
- if (isVerbose) {
|
|
|
- int numProcessors = randomIntBetween(1, 10);
|
|
|
- List<SimulateProcessorResult> processorResults = new ArrayList<>(numProcessors);
|
|
|
- for (int j = 0; j < numProcessors; j++) {
|
|
|
- String processorTag = randomAlphaOfLengthBetween(1, 10);
|
|
|
- SimulateProcessorResult processorResult;
|
|
|
- if (isFailure) {
|
|
|
- processorResult = new SimulateProcessorResult(processorTag, new IllegalArgumentException("test"));
|
|
|
- } else {
|
|
|
- processorResult = new SimulateProcessorResult(processorTag, ingestDocument);
|
|
|
- }
|
|
|
- processorResults.add(processorResult);
|
|
|
- }
|
|
|
- results.add(new SimulateDocumentVerboseResult(processorResults));
|
|
|
- } else {
|
|
|
- results.add(new SimulateDocumentBaseResult(ingestDocument));
|
|
|
- SimulateDocumentBaseResult simulateDocumentBaseResult;
|
|
|
- if (isFailure) {
|
|
|
- simulateDocumentBaseResult = new SimulateDocumentBaseResult(new IllegalArgumentException("test"));
|
|
|
- } else {
|
|
|
- simulateDocumentBaseResult = new SimulateDocumentBaseResult(ingestDocument);
|
|
|
- }
|
|
|
- results.add(simulateDocumentBaseResult);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- SimulatePipelineResponse response = new SimulatePipelineResponse(id, isVerbose, results);
|
|
|
+ SimulatePipelineResponse response = createInstance(id, isVerbose, true);
|
|
|
BytesStreamOutput out = new BytesStreamOutput();
|
|
|
response.writeTo(out);
|
|
|
StreamInput streamInput = out.bytes().streamInput();
|
|
@@ -120,4 +92,97 @@ public class SimulatePipelineResponseTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ static SimulatePipelineResponse createInstance(String pipelineId, boolean isVerbose, boolean withFailure) {
|
|
|
+ int numResults = randomIntBetween(1, 10);
|
|
|
+ List<SimulateDocumentResult> results = new ArrayList<>(numResults);
|
|
|
+ for (int i = 0; i < numResults; i++) {
|
|
|
+ if (isVerbose) {
|
|
|
+ results.add(
|
|
|
+ SimulateDocumentVerboseResultTests.createTestInstance(withFailure)
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ results.add(
|
|
|
+ SimulateDocumentBaseResultTests.createTestInstance(withFailure && randomBoolean())
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new SimulatePipelineResponse(pipelineId, isVerbose, results);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SimulatePipelineResponse createTestInstanceWithFailures() {
|
|
|
+ boolean isVerbose = randomBoolean();
|
|
|
+ return createInstance(null, isVerbose, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected SimulatePipelineResponse createTestInstance() {
|
|
|
+ boolean isVerbose = randomBoolean();
|
|
|
+ // since the pipeline id is not serialized with XContent we set it to null for equality tests.
|
|
|
+ // we test failures separately since comparing XContent is not possible with failures
|
|
|
+ return createInstance(null, isVerbose, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected SimulatePipelineResponse doParseInstance(XContentParser parser) {
|
|
|
+ return SimulatePipelineResponse.fromXContent(parser);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected boolean supportsUnknownFields() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void assertEqualInstances(SimulatePipelineResponse response,
|
|
|
+ SimulatePipelineResponse parsedResponse) {
|
|
|
+ assertEquals(response.getPipelineId(), parsedResponse.getPipelineId());
|
|
|
+ assertEquals(response.isVerbose(), parsedResponse.isVerbose());
|
|
|
+ assertEquals(response.getResults().size(), parsedResponse.getResults().size());
|
|
|
+ for (int i=0; i < response.getResults().size(); i++) {
|
|
|
+ if (response.isVerbose()) {
|
|
|
+ assertThat(response.getResults().get(i), instanceOf(SimulateDocumentVerboseResult.class));
|
|
|
+ assertThat(parsedResponse.getResults().get(i), instanceOf(SimulateDocumentVerboseResult.class));
|
|
|
+ SimulateDocumentVerboseResult responseResult = (SimulateDocumentVerboseResult)response.getResults().get(i);
|
|
|
+ SimulateDocumentVerboseResult parsedResult = (SimulateDocumentVerboseResult)parsedResponse.getResults().get(i);
|
|
|
+ SimulateDocumentVerboseResultTests.assertEqualDocs(responseResult, parsedResult);
|
|
|
+ } else {
|
|
|
+ assertThat(response.getResults().get(i), instanceOf(SimulateDocumentBaseResult.class));
|
|
|
+ assertThat(parsedResponse.getResults().get(i), instanceOf(SimulateDocumentBaseResult.class));
|
|
|
+ SimulateDocumentBaseResult responseResult = (SimulateDocumentBaseResult)response.getResults().get(i);
|
|
|
+ SimulateDocumentBaseResult parsedResult = (SimulateDocumentBaseResult)parsedResponse.getResults().get(i);
|
|
|
+ SimulateDocumentBaseResultTests.assertEqualDocs(responseResult, parsedResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Predicate<String> getRandomFieldsExcludeFilter() {
|
|
|
+ // We cannot have random fields in the _source field and _ingest field
|
|
|
+ return field ->
|
|
|
+ field.contains(
|
|
|
+ new StringJoiner(".")
|
|
|
+ .add(WriteableIngestDocument.DOC_FIELD)
|
|
|
+ .add(WriteableIngestDocument.SOURCE_FIELD).toString()
|
|
|
+ ) ||
|
|
|
+ field.contains(
|
|
|
+ new StringJoiner(".")
|
|
|
+ .add(WriteableIngestDocument.DOC_FIELD)
|
|
|
+ .add(WriteableIngestDocument.INGEST_FIELD).toString()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test parsing {@link SimulatePipelineResponse} with inner failures as they don't support asserting on xcontent equivalence, given that
|
|
|
+ * exceptions are not parsed back as the same original class. We run the usual {@link AbstractXContentTestCase#testFromXContent()}
|
|
|
+ * without failures, and this other test with failures where we disable asserting on xcontent equivalence at the end.
|
|
|
+ */
|
|
|
+ public void testFromXContentWithFailures() throws IOException {
|
|
|
+ Supplier<SimulatePipelineResponse> instanceSupplier = SimulatePipelineResponseTests::createTestInstanceWithFailures;
|
|
|
+ //exceptions are not of the same type whenever parsed back
|
|
|
+ boolean assertToXContentEquivalence = false;
|
|
|
+ AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, instanceSupplier, supportsUnknownFields(), getShuffleFieldsExceptions(),
|
|
|
+ getRandomFieldsExcludeFilter(), this::createParser, this::doParseInstance,
|
|
|
+ this::assertEqualInstances, assertToXContentEquivalence, getToXContentParams());
|
|
|
+ }
|
|
|
}
|