Skip to content

Commit

Permalink
Fixes after review (2)
Browse files Browse the repository at this point in the history
-- Started using RandomObjects instead of RandomDocumentPicks
-- Removed changes to ElasticsearchAssertions
  • Loading branch information
sohaibiftikhar committed Jun 19, 2018
1 parent 1de794e commit d08dcb5
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,45 +62,38 @@ public class SimulatePipelineResponse extends ActionResponse implements ToXConte
static {
PARSER.declareObjectArray(
constructorArg(),
(p, c) -> {
ensureExpectedToken(Token.START_OBJECT, p.currentToken(), p::getTokenLocation);
boolean isVerbose = false;
boolean isFirst = true;
(parser, context) -> {
ensureExpectedToken(Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
SimulateDocumentResult result = null;
while (p.nextToken().equals(Token.FIELD_NAME)) {
switch (p.currentName()) {
while (parser.nextToken() != Token.END_OBJECT) {
ensureExpectedToken(parser.currentToken(), Token.FIELD_NAME, parser::getTokenLocation);
String fieldName = parser.currentName();
parser.nextToken();
switch(fieldName) {
case SimulateDocumentVerboseResult.PROCESSOR_RESULT_FIELD:
assert isFirst || isVerbose;
isVerbose = true;
ensureExpectedToken(Token.START_ARRAY, p.nextToken(), p::getTokenLocation);
ensureExpectedToken(Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
List<SimulateProcessorResult> results = new ArrayList<>();
while (p.nextToken().equals(Token.START_OBJECT)) {
results.add(SimulateProcessorResult.fromXContent(p));
while (parser.nextToken().equals(Token.START_OBJECT)) {
results.add(SimulateProcessorResult.fromXContent(parser));
}
ensureExpectedToken(Token.END_ARRAY, p.currentToken(), p::getTokenLocation);
ensureExpectedToken(Token.END_ARRAY, parser.currentToken(), parser::getTokenLocation);
result = new SimulateDocumentVerboseResult(results);
break;
case WriteableIngestDocument.DOC_FIELD:
case "error":
assert !isVerbose;
if (p.currentName().equals("error")) {
p.nextToken();
result = new SimulateDocumentBaseResult(ElasticsearchException.fromXContent(p));
if (fieldName.equals("error")) {
result = new SimulateDocumentBaseResult(ElasticsearchException.fromXContent(parser));
} else {
result = new SimulateDocumentBaseResult(
WriteableIngestDocument.INGEST_DOC_PARSER.apply(p, null).getIngestDocument()
WriteableIngestDocument.INGEST_DOC_PARSER.apply(parser, null).getIngestDocument()
);
}
ensureExpectedToken(Token.END_OBJECT, p.currentToken(), p::getTokenLocation);
ensureExpectedToken(Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation);
break;
default:
p.nextToken();
p.skipChildren();
break;
parser.skipChildren();
}
isFirst = false;
}
ensureExpectedToken(Token.END_OBJECT, p.currentToken(), p::getTokenLocation);
assert result != null;
return result;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
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.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.elasticsearch.action.ingest.WriteableIngestDocumentTests.assertEqualIngestDocs;
import static org.elasticsearch.action.ingest.WriteableIngestDocumentTests.createRandomIngestDoc;

public class SimulateDocumentBaseResultTests extends AbstractXContentTestCase<SimulateDocumentBaseResult> {

Expand All @@ -55,20 +57,24 @@ public void testSerialization() throws IOException {
}
}

protected SimulateDocumentBaseResult createTestInstance(boolean isFailure) {
static SimulateDocumentBaseResult createTestInstance(boolean isFailure) {
SimulateDocumentBaseResult simulateDocumentBaseResult;
if (isFailure) {
simulateDocumentBaseResult = new SimulateDocumentBaseResult(new IllegalArgumentException("test"));
} else {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
IngestDocument ingestDocument = createRandomIngestDoc();
simulateDocumentBaseResult = new SimulateDocumentBaseResult(ingestDocument);
}
return simulateDocumentBaseResult;
}

private static SimulateDocumentBaseResult createTestInstanceWithFailures() {
return createTestInstance(randomBoolean());
}

@Override
protected SimulateDocumentBaseResult createTestInstance() {
return createTestInstance(randomBoolean());
return createTestInstance(false);
}

@Override
Expand All @@ -78,11 +84,27 @@ protected SimulateDocumentBaseResult doParseInstance(XContentParser parser) {

@Override
protected boolean supportsUnknownFields() {
return false;
return true;
}

@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()
);
}

public static void assertEqualDocs(SimulateDocumentBaseResult response, SimulateDocumentBaseResult parsedResponse) {
assertEqualIngestDocs(response.getIngestDocument(), parsedResponse.getIngestDocument());
assertEquals(response.getIngestDocument(), parsedResponse.getIngestDocument());
if (response.getFailure() != null) {
assertNotNull(parsedResponse.getFailure());
assertThat(
Expand All @@ -101,6 +123,21 @@ public void assertEqualInstances(SimulateDocumentBaseResult response, SimulateDo

@Override
protected boolean assertToXContentEquivalence() {
return false;
return true;
}

/**
* Test parsing {@link SimulateDocumentBaseResult} 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<SimulateDocumentBaseResult> instanceSupplier = SimulateDocumentBaseResultTests::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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,49 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class SimulateDocumentVerboseResultTests extends AbstractXContentTestCase<SimulateDocumentVerboseResult> {

@Override
protected SimulateDocumentVerboseResult createTestInstance() {
static SimulateDocumentVerboseResult createTestInstance(boolean withFailures) {
int numDocs = randomIntBetween(0, 10);
List<SimulateProcessorResult> results = new ArrayList<>();
for (int i = 0; i<numDocs; i++) {
boolean isSuccessful = !(withFailures && randomBoolean());
boolean isIgnoredError = withFailures && randomBoolean();
results.add(
SimulateProcessorResultTests.createTestInstance(randomBoolean(), randomBoolean())
SimulateProcessorResultTests
.createTestInstance(isSuccessful, isIgnoredError)
);
}
return new SimulateDocumentVerboseResult(results);
}

private static SimulateDocumentVerboseResult createTestInstanceWithFailures() {
return createTestInstance(true);
}

@Override
protected SimulateDocumentVerboseResult createTestInstance() {
return createTestInstance(false);
}

@Override
protected SimulateDocumentVerboseResult doParseInstance(XContentParser parser) {
return SimulateDocumentVerboseResult.fromXContent(parser);
}

@Override
protected boolean supportsUnknownFields() {
return false;
return true;
}

protected static void assertEqualDocs(SimulateDocumentVerboseResult response,
static void assertEqualDocs(SimulateDocumentVerboseResult response,
SimulateDocumentVerboseResult parsedResponse) {
assertEquals(response.getProcessorResults().size(), parsedResponse.getProcessorResults().size());
for (int i=0; i < response.getProcessorResults().size(); i++) {
Expand All @@ -67,6 +82,37 @@ protected void assertEqualInstances(SimulateDocumentVerboseResult response,

@Override
protected boolean assertToXContentEquivalence() {
return false;
return true;
}

@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 SimulateDocumentVerboseResult} 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<SimulateDocumentVerboseResult> instanceSupplier = SimulateDocumentVerboseResultTests::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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.RandomDocumentPicks;
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;

Expand Down Expand Up @@ -94,35 +93,18 @@ public void testSerialization() throws IOException {
}
}

public static SimulatePipelineResponse createInstance(String pipelineId, boolean isVerbose, boolean withFailure) {
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++) {
boolean isFailure = withFailure && 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));
results.add(
SimulateDocumentVerboseResultTests.createTestInstance(withFailure)
);
} else {
results.add(new SimulateDocumentBaseResult(ingestDocument));
SimulateDocumentBaseResult simulateDocumentBaseResult;
if (isFailure) {
simulateDocumentBaseResult = new SimulateDocumentBaseResult(new IllegalArgumentException("test"));
} else {
simulateDocumentBaseResult = new SimulateDocumentBaseResult(ingestDocument);
}
results.add(simulateDocumentBaseResult);
results.add(
SimulateDocumentBaseResultTests.createTestInstance(withFailure && randomBoolean())
);
}
}
return new SimulatePipelineResponse(pipelineId, isVerbose, results);
Expand Down Expand Up @@ -159,14 +141,14 @@ protected void assertEqualInstances(SimulatePipelineResponse response,
assertEquals(response.getResults().size(), parsedResponse.getResults().size());
for (int i=0; i < response.getResults().size(); i++) {
if (response.isVerbose()) {
assert response.getResults().get(i) instanceof SimulateDocumentVerboseResult;
assert parsedResponse.getResults().get(i) instanceof SimulateDocumentVerboseResult;
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 {
assert response.getResults().get(i) instanceof SimulateDocumentBaseResult;
assert parsedResponse.getResults().get(i) instanceof SimulateDocumentBaseResult;
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);
Expand All @@ -176,8 +158,18 @@ protected void assertEqualInstances(SimulatePipelineResponse response,

@Override
protected Predicate<String> getRandomFieldsExcludeFilter() {
// We cannot have random fields in the _source field
return field -> field.contains("doc._source") || field.contains("doc._ingest");
// 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()
);
}

/**
Expand Down
Loading

0 comments on commit d08dcb5

Please sign in to comment.