Add xContent shuffling to some more tests

This adds some random shuffling of xContent to some more test cases.

Relates to #5831
This commit is contained in:
Christoph Büscher 2016-05-02 19:30:47 +02:00
parent 4ddf916aab
commit 7d14728960
23 changed files with 48 additions and 59 deletions

View File

@ -113,6 +113,10 @@ public final class XContentBuilder implements BytesStream, Releasable {
return this;
}
public boolean isPrettyPrint() {
return generator.isPrettyPrint();
}
public XContentBuilder lfAtEnd() {
generator.usePrintLineFeedAtEnd();
return this;

View File

@ -20,7 +20,6 @@
package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.bytes.BytesReference;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
@ -34,6 +33,8 @@ public interface XContentGenerator extends Closeable {
void usePrettyPrint();
boolean isPrettyPrint();
void usePrintLineFeedAtEnd();
void writeStartArray() throws IOException;

View File

@ -104,6 +104,11 @@ public class JsonXContentGenerator implements XContentGenerator {
prettyPrint = true;
}
@Override
public boolean isPrettyPrint() {
return this.prettyPrint;
}
@Override
public void usePrintLineFeedAtEnd() {
writeLineFeedAtEnd = true;

View File

@ -34,8 +34,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.io.IOException;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@ -81,7 +79,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
contentBuilder.prettyPrint();
}
XContentBuilder builder = testShape.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser shapeParser = XContentHelper.createParser(shuffled.bytes());
shapeParser.nextToken();
ShapeBuilder parsedShape = ShapeBuilder.parse(shapeParser);

View File

@ -28,7 +28,6 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import static java.util.Collections.emptySet;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.hamcrest.Matchers.instanceOf;
@ -48,7 +47,7 @@ public class ConstructingObjectParserTests extends ESTestCase {
expected.setD(randomBoolean());
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
expected.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder = shuffleXContent(builder, emptySet());
builder = shuffleXContent(builder);
BytesReference bytes = builder.bytes();
XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes);
try {

View File

@ -426,11 +426,11 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
}
/**
* Subclasses can override this method and return a set of fields which should be protected from
* Subclasses can override this method and return an array of fieldnames which should be protected from
* recursive random shuffling in the {@link #testFromXContent()} test case
*/
protected Set<String> shuffleProtectedFields() {
return Collections.emptySet();
protected String[] shuffleProtectedFields() {
return new String[0];
}
protected static XContentBuilder toXContent(QueryBuilder query, XContentType contentType) throws IOException {

View File

@ -24,7 +24,6 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -93,7 +92,7 @@ public class InnerHitBuilderTests extends ESTestCase {
InnerHitBuilder innerHit = randomInnerHits(true, false);
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
innerHit.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
if (randomBoolean()) {
shuffled.prettyPrint();
}

View File

@ -42,15 +42,12 @@ import org.junit.BeforeClass;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
public class PercolateQueryBuilderTests extends AbstractQueryTestCase<PercolateQueryBuilder> {
private static final Set<String> SHUFFLE_PROTECTED_FIELDS =
Collections.singleton(PercolateQueryBuilder.DOCUMENT_FIELD.getPreferredName());
private static final String[] SHUFFLE_PROTECTED_FIELDS = new String[] { PercolateQueryBuilder.DOCUMENT_FIELD.getPreferredName()};
private static String queryField;
private static String docType;
@ -105,7 +102,7 @@ public class PercolateQueryBuilderTests extends AbstractQueryTestCase<PercolateQ
* compare when check for equality of the original and the shuffled builder
*/
@Override
protected Set<String> shuffleProtectedFields() {
protected String[] shuffleProtectedFields() {
return SHUFFLE_PROTECTED_FIELDS;
}

View File

@ -32,7 +32,6 @@ import org.elasticsearch.index.store.StoreFileMetaData;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@ -53,7 +52,7 @@ public class FileInfoTests extends ESTestCase {
BlobStoreIndexShardSnapshot.FileInfo info = new BlobStoreIndexShardSnapshot.FileInfo("_foobar", meta, size);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
BlobStoreIndexShardSnapshot.FileInfo.toXContent(info, builder, ToXContent.EMPTY_PARAMS);
byte[] xcontent = builder.bytes().toBytes();
byte[] xcontent = shuffleXContent(builder).bytes().toBytes();
final BlobStoreIndexShardSnapshot.FileInfo parsedInfo;
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xcontent)) {

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -57,7 +56,7 @@ public class IngestMetadataTests extends ESTestCase {
builder.startObject();
ingestMetadata.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
final XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
MetaData.Custom custom = ingestMetadata.fromXContent(parser);
assertTrue(custom instanceof IngestMetadata);

View File

@ -34,8 +34,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ScriptMetaDataTests extends ESTestCase {
@ -79,7 +77,7 @@ public class ScriptMetaDataTests extends ESTestCase {
xContentBuilder.startObject();
expected.toXContent(xContentBuilder, new ToXContent.MapParams(Collections.emptyMap()));
xContentBuilder.endObject();
xContentBuilder = shuffleXContent(xContentBuilder, Collections.emptySet());
xContentBuilder = shuffleXContent(xContentBuilder);
XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes());
parser.nextToken();

View File

@ -223,7 +223,7 @@ public abstract class BaseAggregationTestCase<AB extends AggregatorBuilder<AB>>
builder.prettyPrint();
}
factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());

View File

@ -225,7 +225,7 @@ public abstract class BasePipelineAggregationTestCase<AF extends PipelineAggrega
builder.prettyPrint();
}
factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
String contentString = factoriesBuilder.toString();

View File

@ -59,7 +59,6 @@ import org.junit.BeforeClass;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -150,7 +149,7 @@ public class HighlightBuilderTests extends ESTestCase {
builder.prettyPrint();
}
highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser parser = XContentHelper.createParser(shuffled.bytes());
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.EMPTY);

View File

@ -54,8 +54,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.io.IOException;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@ -137,7 +135,7 @@ public class QueryRescoreBuilderTests extends ESTestCase {
builder.prettyPrint();
}
rescoreBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser parser = XContentHelper.createParser(shuffled.bytes());

View File

@ -41,7 +41,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
public class SearchAfterBuilderTests extends ESTestCase {
@ -220,8 +219,8 @@ public class SearchAfterBuilderTests extends ESTestCase {
builder.startObject();
searchAfterBuilder.innerToXContent(builder);
builder.endObject();
XContentParser parser = XContentHelper.createParser(builder.bytes());
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT);
XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes());
new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT);
parser.nextToken();
parser.nextToken();
parser.nextToken();

View File

@ -137,7 +137,7 @@ public abstract class AbstractSortTestCase<T extends SortBuilder<T>> extends EST
builder.prettyPrint();
}
testItem.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffled = shuffleXContent(builder);
XContentParser itemParser = XContentHelper.createParser(shuffled.bytes());
itemParser.nextToken();

View File

@ -39,9 +39,6 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@ -173,8 +170,8 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui
* Subclasses can override this method and return a set of fields which should be protected from
* recursive random shuffling in the {@link #testFromXContent()} test case
*/
protected Set<String> shuffleProtectedFields() {
return Collections.emptySet();
protected String[] shuffleProtectedFields() {
return new String[0];
}
private SB mutate(SB firstBuilder) throws IOException {

View File

@ -37,14 +37,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.Matchers.containsString;
public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTestCase<CompletionSuggestionBuilder> {
private static final Set<String> SHUFFLE_PROTECTED_FIELDS =
Collections.singleton(CompletionSuggestionBuilder.CONTEXTS_FIELD.getPreferredName());
private static final String[] SHUFFLE_PROTECTED_FIELDS = new String[] {CompletionSuggestionBuilder.CONTEXTS_FIELD.getPreferredName()};
@Override
protected CompletionSuggestionBuilder randomSuggestionBuilder() {
@ -113,7 +110,7 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe
* the equals() test will fail because their {@link BytesReference} representation isn't the same
*/
@Override
protected Set<String> shuffleProtectedFields() {
protected String[] shuffleProtectedFields() {
return SHUFFLE_PROTECTED_FIELDS;
}

View File

@ -35,7 +35,6 @@ import org.elasticsearch.search.suggest.phrase.PhraseSuggestionContext.DirectCan
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
public class DirectCandidateGeneratorTests extends ESTestCase{
@ -117,8 +116,7 @@ public class DirectCandidateGeneratorTests extends ESTestCase{
builder.prettyPrint();
}
generator.toXContent(builder, ToXContent.EMPTY_PARAMS);
XContentParser parser = XContentHelper.createParser(builder.bytes());
XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes());
QueryParseContext context = new QueryParseContext(mockRegistry, parser, ParseFieldMatcher.STRICT);
parser.nextToken();
DirectCandidateGeneratorBuilder secondGenerator = DirectCandidateGeneratorBuilder.fromXContent(context);

View File

@ -100,7 +100,7 @@ public abstract class SmoothingModelTestCase extends ESTestCase {
contentBuilder.startObject();
testModel.innerToXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
XContentParser parser = XContentHelper.createParser(contentBuilder.bytes());
XContentParser parser = XContentHelper.createParser(shuffleXContent(contentBuilder).bytes());
QueryParseContext context = new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT);
parser.nextToken(); // go to start token, real parsing would do that in the outer element parser
SmoothingModel parsedModel = fromXContent(context);

View File

@ -414,7 +414,7 @@ public abstract class ESTestCase extends LuceneTestCase {
if (input != null) {
return randomValueOtherThanMany(input::equals, randomSupplier);
}
return(randomSupplier.get());
}
@ -634,25 +634,29 @@ public abstract class ESTestCase extends LuceneTestCase {
* recursive shuffling behavior can be made by passing in the names of fields which
* internally should stay untouched.
*/
public static XContentBuilder shuffleXContent(XContentBuilder builder, Set<String> exceptFieldNames) throws IOException {
public static XContentBuilder shuffleXContent(XContentBuilder builder, String... exceptFieldNames) throws IOException {
BytesReference bytes = builder.bytes();
XContentParser parser = XContentFactory.xContent(bytes).createParser(bytes);
// use ordered maps for reproducibility
Map<String, Object> shuffledMap = shuffleMap(parser.mapOrdered(), exceptFieldNames);
XContentBuilder jsonBuilder = XContentFactory.contentBuilder(builder.contentType());
return jsonBuilder.map(shuffledMap);
Map<String, Object> shuffledMap = shuffleMap(parser.mapOrdered(), new HashSet<>(Arrays.asList(exceptFieldNames)));
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(builder.contentType());
if (builder.isPrettyPrint()) {
xContentBuilder.prettyPrint();
}
return xContentBuilder.map(shuffledMap);
}
private static Map<String, Object> shuffleMap(Map<String, Object> map, Set<String> exceptFieldNames) {
private static Map<String, Object> shuffleMap(Map<String, Object> map, Set<String> exceptFields) {
List<String> keys = new ArrayList<>(map.keySet());
// even though we shuffle later, we need this to make tests reproduce on different jvms
Collections.sort(keys);
Map<String, Object> targetMap = new TreeMap<>();
Collections.shuffle(keys, random());
for (String key : keys) {
Object value = map.get(key);
if (value instanceof Map && exceptFieldNames.contains(key) == false) {
targetMap.put(key, shuffleMap((Map) value, exceptFieldNames));
if (value instanceof Map && exceptFields.contains(key) == false) {
targetMap.put(key, shuffleMap((Map) value, exceptFields));
} else {
targetMap.put(key, value);
}

View File

@ -29,11 +29,9 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static org.hamcrest.Matchers.greaterThan;
@ -71,7 +69,7 @@ public class ESTestCaseTests extends ESTestCase {
Map<String, Object> randomStringObjectMap = randomStringObjectMap(5);
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.map(randomStringObjectMap);
XContentBuilder shuffleXContent = shuffleXContent(builder, Collections.emptySet());
XContentBuilder shuffleXContent = shuffleXContent(builder);
XContentParser parser = XContentFactory.xContent(shuffleXContent.bytes()).createParser(shuffleXContent.bytes());
Map<String, Object> resultMap = parser.map();
assertEquals("both maps should contain the same mappings", randomStringObjectMap, resultMap);