From 690a878b38cec745eaf9060d8621bfc51d8e3d4e Mon Sep 17 00:00:00 2001 From: olcbean Date: Wed, 23 May 2018 19:17:02 +0200 Subject: [PATCH 1/5] XContentTests : insert random fields at random positions --- .../test/AbstractXContentTestCase.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 983897049c767..465e89acad295 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -25,10 +25,12 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; import java.io.IOException; import java.util.function.BiConsumer; @@ -52,20 +54,21 @@ public static void testFromXContent(int numberOfTestRuns, for (int runs = 0; runs < numberOfTestRuns; runs++) { T testInstance = instanceSupplier.get(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference shuffled = toShuffledXContent(testInstance, xContentType, ToXContent.EMPTY_PARAMS, false, createParserFunction, - shuffleFieldsExceptions); + BytesReference xContent = XContentHelper.toXContent(testInstance, xContentType, ToXContent.EMPTY_PARAMS, false); BytesReference withRandomFields; if (supportsUnknownFields) { // we add a few random fields to check that parser is lenient on new fields - withRandomFields = XContentTestUtils.insertRandomFields(xContentType, shuffled, randomFieldsExcludeFilter, random()); + withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random()); } else { - withRandomFields = shuffled; + withRandomFields = xContent; } - XContentParser parser = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields); + XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields); + BytesReference shuffledContent = BytesReference.bytes(shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions)); + XContentParser parser = createParserFunction.apply(XContentFactory.xContent(xContentType), shuffledContent); T parsed = parseFunction.apply(parser); assertEqualsConsumer.accept(testInstance, parsed); if (assertToXContentEquivalence) { - assertToXContentEquivalent(shuffled, XContentHelper.toXContent(parsed, xContentType, false), xContentType); + assertToXContentEquivalent(xContent, XContentHelper.toXContent(parsed, xContentType, false), xContentType); } } } From 31de42c78dd9046c8ba35cceb58a5c126a45376a Mon Sep 17 00:00:00 2001 From: olcbean Date: Thu, 21 Jun 2018 23:11:54 +0200 Subject: [PATCH 2/5] adding a test that a random field will be inserted at a random position rather than at the last position ( as till now ) --- .../test/AbstractXContentTestCase.java | 36 ++++++++----- .../test/AbstractXContentTestCaseTests.java | 52 +++++++++++++++++++ 2 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 12b99fbeae40a..395992cd09cab 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -54,21 +54,16 @@ public static void testFromXContent(int numberOfTestRuns, for (int runs = 0; runs < numberOfTestRuns; runs++) { T testInstance = instanceSupplier.get(); XContentType xContentType = randomFrom(XContentType.values()); - BytesReference xContent = XContentHelper.toXContent(testInstance, xContentType, ToXContent.EMPTY_PARAMS, false); - BytesReference withRandomFields; - if (supportsUnknownFields) { - // we add a few random fields to check that parser is lenient on new fields - withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random()); - } else { - withRandomFields = xContent; - } - XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields); - BytesReference shuffledContent = BytesReference.bytes(shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions)); + BytesReference shuffledContent = insertRandomFieldsAndShuffle(testInstance, xContentType, supportsUnknownFields, + shuffleFieldsExceptions, randomFieldsExcludeFilter, createParserFunction, toXContentParams); XContentParser parser = createParserFunction.apply(XContentFactory.xContent(xContentType), shuffledContent); T parsed = parseFunction.apply(parser); assertEqualsConsumer.accept(testInstance, parsed); if (assertToXContentEquivalence) { - assertToXContentEquivalent(xContent, XContentHelper.toXContent(parsed, xContentType, false), xContentType); + assertToXContentEquivalent( + XContentHelper.toXContent(testInstance, xContentType, false), + XContentHelper.toXContent(parsed, xContentType, false), + xContentType); } } } @@ -132,9 +127,26 @@ protected String[] getShuffleFieldsExceptions() { } /** - * Params that have to be provided when calling calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)} + * Params that have to be provided when calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)} */ protected ToXContent.Params getToXContentParams() { return ToXContent.EMPTY_PARAMS; } + + public static BytesReference insertRandomFieldsAndShuffle(ToXContent testInstance, XContentType xContentType, + boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate randomFieldsExcludeFilter, + CheckedBiFunction createParserFunction, + ToXContent.Params toXContentParams) throws IOException { + BytesReference xContent = XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false); + BytesReference withRandomFields; + if (supportsUnknownFields) { + // add a few random fields to check that the parser is lenient on new fields + withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random()); + } else { + withRandomFields = xContent; + } + XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields); + return BytesReference.bytes(ESTestCase.shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions)); + } + } diff --git a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java new file mode 100644 index 0000000000000..0771b7a227d49 --- /dev/null +++ b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java @@ -0,0 +1,52 @@ +package org.elasticsearch.test; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; +import java.util.Map; + +import static org.hamcrest.Matchers.equalTo; + +public class AbstractXContentTestCaseTests extends ESTestCase { + + private static final int NUMBER_OF_TEST_RUNS = 10; + + public void testSomethign() throws IOException { + for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { + TestInstance t = new TestInstance(); + boolean atRandomPosition = false; + for (int i = 0; i < 5; i++) { + BytesReference insertRandomFieldsAndShuffle = AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, + true, new String[] {}, null, this::createParser, ToXContent.EMPTY_PARAMS); + try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { + Map mapOrdered = parser.mapOrdered(); + assertThat(mapOrdered.size(), equalTo(2)); + if (false == "field".equals(mapOrdered.keySet().iterator().next())) { + atRandomPosition = true; + break; + } + } + } + assertThat(atRandomPosition, equalTo(true)); + } + } +} + +class TestInstance implements ToXContentObject { + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + { + builder.field("field", 1); + } + builder.endObject(); + return builder; + } + +} From d6d28f5a694f4304739c5bed1d237ce5134be143 Mon Sep 17 00:00:00 2001 From: olcbean Date: Fri, 22 Jun 2018 10:04:36 +0200 Subject: [PATCH 3/5] integrate reviewers comments --- .../test/AbstractXContentTestCase.java | 4 +- .../test/AbstractXContentTestCaseTests.java | 50 +++++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 395992cd09cab..31695e3eb336e 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -61,8 +61,8 @@ public static void testFromXContent(int numberOfTestRuns, assertEqualsConsumer.accept(testInstance, parsed); if (assertToXContentEquivalence) { assertToXContentEquivalent( - XContentHelper.toXContent(testInstance, xContentType, false), - XContentHelper.toXContent(parsed, xContentType, false), + XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false), + XContentHelper.toXContent(parsed, xContentType, toXContentParams, false), xContentType); } } diff --git a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java index 0771b7a227d49..a3d6ef4b2c5aa 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java @@ -14,39 +14,35 @@ public class AbstractXContentTestCaseTests extends ESTestCase { - private static final int NUMBER_OF_TEST_RUNS = 10; - - public void testSomethign() throws IOException { - for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) { - TestInstance t = new TestInstance(); - boolean atRandomPosition = false; - for (int i = 0; i < 5; i++) { - BytesReference insertRandomFieldsAndShuffle = AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, - true, new String[] {}, null, this::createParser, ToXContent.EMPTY_PARAMS); - try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { - Map mapOrdered = parser.mapOrdered(); - assertThat(mapOrdered.size(), equalTo(2)); - if (false == "field".equals(mapOrdered.keySet().iterator().next())) { - atRandomPosition = true; - break; - } + public void testInsertRandomFieldsAndShuffle() throws IOException { + TestInstance t = new TestInstance(); + boolean randomFieldAtFirstPosition = false; + for (int i = 0; i < 5; i++) { + BytesReference insertRandomFieldsAndShuffle = AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, true, + new String[] {}, null, this::createParser, ToXContent.EMPTY_PARAMS); + try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { + Map mapOrdered = parser.mapOrdered(); + assertThat(mapOrdered.size(), equalTo(2)); + if (false == "field".equals(mapOrdered.keySet().iterator().next())) { + randomFieldAtFirstPosition = true; } } - assertThat(atRandomPosition, equalTo(true)); } + assertThat(randomFieldAtFirstPosition, equalTo(true)); } -} -class TestInstance implements ToXContentObject { + private class TestInstance implements ToXContentObject { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - { - builder.field("field", 1); + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + { + builder.field("field", 1); + } + builder.endObject(); + return builder; } - builder.endObject(); - return builder; + } -} +} \ No newline at end of file From 85b9b32ed14849f31d4ddf21d929d82dd7ea8577 Mon Sep 17 00:00:00 2001 From: olcbean Date: Wed, 11 Jul 2018 14:44:04 +0200 Subject: [PATCH 4/5] removing the unreliable loop by using RandomizedContext#runWithPrivateRandomness instead --- .../test/AbstractXContentTestCase.java | 2 +- .../test/AbstractXContentTestCaseTests.java | 24 +++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 31695e3eb336e..c740a65d28a6e 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -133,7 +133,7 @@ protected ToXContent.Params getToXContentParams() { return ToXContent.EMPTY_PARAMS; } - public static BytesReference insertRandomFieldsAndShuffle(ToXContent testInstance, XContentType xContentType, + static BytesReference insertRandomFieldsAndShuffle(ToXContent testInstance, XContentType xContentType, boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate randomFieldsExcludeFilter, CheckedBiFunction createParserFunction, ToXContent.Params toXContentParams) throws IOException { diff --git a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java index a3d6ef4b2c5aa..1c70ded7c021a 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java @@ -1,5 +1,7 @@ package org.elasticsearch.test; +import com.carrotsearch.randomizedtesting.RandomizedContext; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; @@ -11,24 +13,20 @@ import java.util.Map; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; public class AbstractXContentTestCaseTests extends ESTestCase { - public void testInsertRandomFieldsAndShuffle() throws IOException { + public void testInsertRandomFieldsAndShuffle() throws Exception { TestInstance t = new TestInstance(); - boolean randomFieldAtFirstPosition = false; - for (int i = 0; i < 5; i++) { - BytesReference insertRandomFieldsAndShuffle = AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, true, - new String[] {}, null, this::createParser, ToXContent.EMPTY_PARAMS); - try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { - Map mapOrdered = parser.mapOrdered(); - assertThat(mapOrdered.size(), equalTo(2)); - if (false == "field".equals(mapOrdered.keySet().iterator().next())) { - randomFieldAtFirstPosition = true; - } - } + BytesReference insertRandomFieldsAndShuffle = RandomizedContext.current().runWithPrivateRandomness(1, + () -> AbstractXContentTestCase.insertRandomFieldsAndShuffle(t, XContentType.JSON, true, new String[] {}, null, + this::createParser, ToXContent.EMPTY_PARAMS)); + try (XContentParser parser = createParser(XContentType.JSON.xContent(), insertRandomFieldsAndShuffle)) { + Map mapOrdered = parser.mapOrdered(); + assertThat(mapOrdered.size(), equalTo(2)); + assertThat(mapOrdered.keySet().iterator().next(), not(equalTo("field"))); } - assertThat(randomFieldAtFirstPosition, equalTo(true)); } private class TestInstance implements ToXContentObject { From a854b23ad106fcfe1f42ac6c3b9e79dd3dbebe91 Mon Sep 17 00:00:00 2001 From: olcbean Date: Wed, 11 Jul 2018 16:57:48 +0200 Subject: [PATCH 5/5] adding license header --- .../test/AbstractXContentTestCaseTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java index 1c70ded7c021a..165df584c030c 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/AbstractXContentTestCaseTests.java @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.elasticsearch.test; import com.carrotsearch.randomizedtesting.RandomizedContext;