Browse Source

Add more fromXContent() testing with shuffled field order

Christoph Büscher 9 years ago
parent
commit
7254eac840

+ 1 - 1
core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

@@ -41,12 +41,12 @@ import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryParseContext;
 import org.elasticsearch.index.query.QueryShardContext;
+import org.elasticsearch.index.query.support.InnerHitsBuilder;
 import org.elasticsearch.script.Script;
 import org.elasticsearch.search.aggregations.AggregatorBuilder;
 import org.elasticsearch.search.aggregations.AggregatorFactories;
 import org.elasticsearch.search.aggregations.AggregatorParsers;
 import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder;
-import org.elasticsearch.index.query.support.InnerHitsBuilder;
 import org.elasticsearch.search.fetch.source.FetchSourceContext;
 import org.elasticsearch.search.highlight.HighlightBuilder;
 import org.elasticsearch.search.internal.SearchContext;

+ 2 - 16
core/src/main/java/org/elasticsearch/search/rescore/RescoreBuilder.java

@@ -19,15 +19,13 @@
 
 package org.elasticsearch.search.rescore;
 
-import org.elasticsearch.ExceptionsHelper;
+import org.elasticsearch.action.support.ToXContentToBytes;
 import org.elasticsearch.common.ParseField;
 import org.elasticsearch.common.ParsingException;
 import org.elasticsearch.common.io.stream.NamedWriteable;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.xcontent.ToXContent;
 import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentFactory;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryParseContext;
@@ -40,7 +38,7 @@ import java.util.Objects;
 /**
  * The abstract base builder for instances of {@link RescoreBuilder}.
  */
-public abstract class RescoreBuilder<RB extends RescoreBuilder<RB>> implements ToXContent, NamedWriteable<RB> {
+public abstract class RescoreBuilder<RB extends RescoreBuilder<RB>> extends ToXContentToBytes implements NamedWriteable<RB> {
 
     protected Integer windowSize;
 
@@ -148,16 +146,4 @@ public abstract class RescoreBuilder<RB extends RescoreBuilder<RB>> implements T
         RescoreBuilder other = (RescoreBuilder) obj;
         return Objects.equals(windowSize, other.windowSize);
     }
-
-    @Override
-    public final String toString() {
-        try {
-            XContentBuilder builder = XContentFactory.jsonBuilder();
-            builder.prettyPrint();
-            toXContent(builder, EMPTY_PARAMS);
-            return builder.string();
-        } catch (Exception e) {
-            return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
-        }
-    }
 }

+ 3 - 1
core/src/test/java/org/elasticsearch/common/geo/builders/AbstractShapeBuilderTestCase.java

@@ -34,6 +34,7 @@ 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;
@@ -80,7 +81,8 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
                 contentBuilder.prettyPrint();
             }
             XContentBuilder builder = testShape.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
-            XContentParser shapeParser = XContentHelper.createParser(builder.bytes());
+            XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
+            XContentParser shapeParser = XContentHelper.createParser(shuffled.bytes());
             shapeParser.nextToken();
             ShapeBuilder parsedShape = ShapeBuilder.parse(shapeParser);
             assertNotSame(testShape, parsedShape);

+ 4 - 3
core/src/test/java/org/elasticsearch/ingest/IngestMetadataTests.java

@@ -31,6 +31,7 @@ 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;
 
@@ -51,13 +52,13 @@ public class IngestMetadataTests extends ESTestCase {
         map.put(pipeline.getId(), pipeline);
         map.put(pipeline2.getId(), pipeline2);
         IngestMetadata ingestMetadata = new IngestMetadata(map);
-        XContentBuilder builder = XContentFactory.jsonBuilder();
+        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
         builder.prettyPrint();
         builder.startObject();
         ingestMetadata.toXContent(builder, ToXContent.EMPTY_PARAMS);
         builder.endObject();
-        String string = builder.string();
-        final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(string);
+        XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
+        final XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
         MetaData.Custom custom = ingestMetadata.fromXContent(parser);
         assertTrue(custom instanceof IngestMetadata);
         IngestMetadata m = (IngestMetadata) custom;

+ 4 - 2
core/src/test/java/org/elasticsearch/search/highlight/HighlightBuilderTests.java

@@ -59,6 +59,7 @@ 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;
@@ -151,8 +152,9 @@ public class HighlightBuilderTests extends ESTestCase {
                 builder.prettyPrint();
             }
             highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
+            XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
 
-            XContentParser parser = XContentHelper.createParser(builder.bytes());
+            XContentParser parser = XContentHelper.createParser(shuffled.bytes());
             context.reset(parser);
             parser.nextToken();
             HighlightBuilder secondHighlightBuilder;
@@ -242,7 +244,7 @@ public class HighlightBuilderTests extends ESTestCase {
                     "        \"bad_fieldname\" : { \"field\" : \"value\" }\n" +
                     "     }\n" +
                     "   }\n" +
-                    "}\n"); 
+                    "}\n");
             assertEquals("[highlight] failed to parse field [fields]", e.getMessage());
             assertEquals("[fields] failed to parse field [body]", e.getCause().getMessage());
             assertEquals("[highlight_field] unknown field [bad_fieldname], parser not found", e.getCause().getCause().getMessage());

+ 9 - 10
core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java

@@ -54,6 +54,7 @@ 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;
@@ -133,8 +134,15 @@ public class QueryRescoreBuilderTests extends ESTestCase {
         context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY));
         for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
             RescoreBuilder<?> rescoreBuilder = randomRescoreBuilder();
+            XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
+            if (randomBoolean()) {
+                builder.prettyPrint();
+            }
+            rescoreBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
+            XContentBuilder shuffled = shuffleXContent(builder, Collections.emptySet());
+
 
-            XContentParser parser = createParser(rescoreBuilder);
+            XContentParser parser = XContentHelper.createParser(shuffled.bytes());
             context.reset(parser);
             parser.nextToken();
             RescoreBuilder<?> secondRescoreBuilder = RescoreBuilder.parseFromXContent(context);
@@ -144,15 +152,6 @@ public class QueryRescoreBuilderTests extends ESTestCase {
         }
     }
 
-    private static XContentParser createParser(RescoreBuilder<?> rescoreBuilder) throws IOException {
-        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
-        if (randomBoolean()) {
-            builder.prettyPrint();
-        }
-        rescoreBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
-        return XContentHelper.createParser(builder.bytes());
-    }
-
     /**
      * test that build() outputs a {@link RescoreSearchContext} that has the same properties
      * than the test builder

+ 2 - 1
core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java

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

+ 12 - 1
core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java

@@ -39,6 +39,8 @@ 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;
@@ -156,7 +158,8 @@ public abstract class AbstractSuggestionBuilderTestCase<SB extends SuggestionBui
             suggestionBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
             xContentBuilder.endObject();
 
-            XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes());
+            XContentBuilder shuffled = shuffleXContent(xContentBuilder, shuffleProtectedFields());
+            XContentParser parser = XContentHelper.createParser(shuffled.bytes());
             context.reset(parser);
             // we need to skip the start object and the name, those will be parsed by outer SuggestBuilder
             parser.nextToken();
@@ -168,6 +171,14 @@ 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();
+    }
+
     private SB mutate(SB firstBuilder) throws IOException {
         SB mutation = serializedCopy(firstBuilder);
         assertNotSame(mutation, firstBuilder);

+ 14 - 0
core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java

@@ -22,6 +22,7 @@ package org.elasticsearch.search.suggest.completion;
 import com.carrotsearch.randomizedtesting.generators.RandomStrings;
 
 import org.elasticsearch.common.ParsingException;
+import org.elasticsearch.common.bytes.BytesReference;
 import org.elasticsearch.common.unit.Fuzziness;
 import org.elasticsearch.common.xcontent.ToXContent;
 import org.elasticsearch.search.suggest.AbstractSuggestionBuilderTestCase;
@@ -36,11 +37,15 @@ 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());
+
     @Override
     protected CompletionSuggestionBuilder randomSuggestionBuilder() {
         return randomCompletionSuggestionBuilder();
@@ -103,6 +108,15 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe
         return builderAndInfo;
     }
 
+    /**
+     * exclude the "contexts" field from recursive random shuffling in fromXContent tests or else
+     * the equals() test will fail because their {@link BytesReference} representation isn't the same
+     */
+    @Override
+    protected Set<String> shuffleProtectedFields() {
+        return SHUFFLE_PROTECTED_FIELDS;
+    }
+
     @Override
     protected void mutateSpecificParameters(CompletionSuggestionBuilder builder) throws IOException {
         switch (randomIntBetween(0, 5)) {