From 5533e1172c4ac4f5e510109ebb1f5047452896bf Mon Sep 17 00:00:00 2001 From: Nikita Glashenko Date: Mon, 6 Jan 2020 15:46:08 +0400 Subject: [PATCH] Add tests for remaining IntervalsSourceProvider implementations (#50326) This PR adds unit tests for wire and xContent serialization of remaining IntervalsSourceProvider implementations. Closes #50150 --- .../index/query/IntervalsSourceProvider.java | 95 +++++++++++++++- .../elasticsearch/search/SearchModule.java | 31 +++--- .../CombineIntervalsSourceProviderTests.java | 88 +++++++++++++++ ...sjunctionIntervalsSourceProviderTests.java | 75 +++++++++++++ .../FilterIntervalsSourceProviderTests.java | 87 +++++++++++++++ .../FuzzyIntervalsSourceProviderTests.java | 101 ++++++++++++++++++ .../query/IntervalQueryBuilderTests.java | 60 +++++++---- .../MatchIntervalsSourceProviderTests.java | 93 ++++++++++++++++ .../WildcardIntervalsSourceProviderTests.java | 4 + 9 files changed, 595 insertions(+), 39 deletions(-) create mode 100644 server/src/test/java/org/elasticsearch/index/query/CombineIntervalsSourceProviderTests.java create mode 100644 server/src/test/java/org/elasticsearch/index/query/DisjunctionIntervalsSourceProviderTests.java create mode 100644 server/src/test/java/org/elasticsearch/index/query/FilterIntervalsSourceProviderTests.java create mode 100644 server/src/test/java/org/elasticsearch/index/query/FuzzyIntervalsSourceProviderTests.java create mode 100644 server/src/test/java/org/elasticsearch/index/query/MatchIntervalsSourceProviderTests.java diff --git a/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java b/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java index dbd8f339ca6..1f015af9b7f 100644 --- a/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java +++ b/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java @@ -246,6 +246,30 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont public static Match fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } + + String getQuery() { + return query; + } + + int getMaxGaps() { + return maxGaps; + } + + boolean isOrdered() { + return ordered; + } + + String getAnalyzer() { + return analyzer; + } + + IntervalFilter getFilter() { + return filter; + } + + String getUseField() { + return useField; + } } public static class Disjunction extends IntervalsSourceProvider { @@ -290,12 +314,13 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Disjunction that = (Disjunction) o; - return Objects.equals(subSources, that.subSources); + return Objects.equals(subSources, that.subSources) && + Objects.equals(filter, that.filter); } @Override public int hashCode() { - return Objects.hash(subSources); + return Objects.hash(subSources, filter); } @Override @@ -342,6 +367,14 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont public static Disjunction fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } + + List getSubSources() { + return subSources; + } + + IntervalFilter getFilter() { + return filter; + } } public static class Combine extends IntervalsSourceProvider { @@ -393,12 +426,14 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont if (o == null || getClass() != o.getClass()) return false; Combine combine = (Combine) o; return Objects.equals(subSources, combine.subSources) && - ordered == combine.ordered && maxGaps == combine.maxGaps; + ordered == combine.ordered && + maxGaps == combine.maxGaps && + Objects.equals(filter, combine.filter); } @Override public int hashCode() { - return Objects.hash(subSources, ordered, maxGaps); + return Objects.hash(subSources, ordered, maxGaps, filter); } @Override @@ -452,6 +487,22 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont public static Combine fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } + + List getSubSources() { + return subSources; + } + + boolean isOrdered() { + return ordered; + } + + int getMaxGaps() { + return maxGaps; + } + + IntervalFilter getFilter() { + return filter; + } } public static class Prefix extends IntervalsSourceProvider { @@ -838,6 +889,30 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont public static Fuzzy fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } + + String getTerm() { + return term; + } + + int getPrefixLength() { + return prefixLength; + } + + boolean isTranspositions() { + return transpositions; + } + + Fuzziness getFuzziness() { + return fuzziness; + } + + String getAnalyzer() { + return analyzer; + } + + String getUseField() { + return useField; + } } static class ScriptFilterSource extends FilteredIntervalsSource { @@ -985,6 +1060,18 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont } return new IntervalFilter(intervals, type); } + + String getType() { + return type; + } + + IntervalsSourceProvider getFilter() { + return filter; + } + + Script getScript() { + return script; + } } diff --git a/server/src/main/java/org/elasticsearch/search/SearchModule.java b/server/src/main/java/org/elasticsearch/search/SearchModule.java index 18e7c06f88c..52f4ff9a8ae 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/server/src/main/java/org/elasticsearch/search/SearchModule.java @@ -277,6 +277,7 @@ import org.elasticsearch.search.suggest.term.TermSuggestion; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -286,6 +287,7 @@ import static java.util.Collections.unmodifiableMap; import static java.util.Objects.requireNonNull; import static org.elasticsearch.index.query.CommonTermsQueryBuilder.COMMON_TERMS_QUERY_DEPRECATION_MSG; import static org.elasticsearch.index.query.SpanNearQueryBuilder.SpanGapQueryBuilder; +import static java.util.Collections.unmodifiableList; /** * Sets up things that can be done at search time like queries, aggregations, and suggesters. @@ -851,18 +853,23 @@ public class SearchModule { } private void registerIntervalsSourceProviders() { - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Match.NAME, IntervalsSourceProvider.Match::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Combine.NAME, IntervalsSourceProvider.Combine::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Disjunction.NAME, IntervalsSourceProvider.Disjunction::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Prefix.NAME, IntervalsSourceProvider.Prefix::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Wildcard.NAME, IntervalsSourceProvider.Wildcard::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, - IntervalsSourceProvider.Fuzzy.NAME, IntervalsSourceProvider.Fuzzy::new)); + namedWriteables.addAll(getIntervalsSourceProviderNamedWritables()); + } + + public static List getIntervalsSourceProviderNamedWritables() { + return unmodifiableList(Arrays.asList( + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Match.NAME, + IntervalsSourceProvider.Match::new), + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Combine.NAME, + IntervalsSourceProvider.Combine::new), + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Disjunction.NAME, + IntervalsSourceProvider.Disjunction::new), + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Prefix.NAME, + IntervalsSourceProvider.Prefix::new), + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Wildcard.NAME, + IntervalsSourceProvider.Wildcard::new), + new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Fuzzy.NAME, + IntervalsSourceProvider.Fuzzy::new))); } private void registerQuery(QuerySpec spec) { diff --git a/server/src/test/java/org/elasticsearch/index/query/CombineIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/CombineIntervalsSourceProviderTests.java new file mode 100644 index 00000000000..f65a08d2b2b --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/CombineIntervalsSourceProviderTests.java @@ -0,0 +1,88 @@ +/* + * 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.index.query; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.index.query.IntervalsSourceProvider.Combine; + +public class CombineIntervalsSourceProviderTests extends AbstractSerializingTestCase { + + @Override + protected Combine createTestInstance() { + return IntervalQueryBuilderTests.createRandomCombine(0, randomBoolean()); + } + + @Override + protected Combine mutateInstance(Combine instance) throws IOException { + List subSources = instance.getSubSources(); + boolean ordered = instance.isOrdered(); + int maxGaps = instance.getMaxGaps(); + IntervalsSourceProvider.IntervalFilter filter = instance.getFilter(); + switch (between(0, 3)) { + case 0: + subSources = subSources == null ? + IntervalQueryBuilderTests.createRandomSourceList(0, randomBoolean(), randomInt(5) + 1) : + null; + break; + case 1: + ordered = !ordered; + break; + case 2: + maxGaps++; + break; + case 3: + filter = filter == null ? + IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()) : + FilterIntervalsSourceProviderTests.mutateFilter(filter); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new Combine(subSources, ordered, maxGaps, filter); + } + + @Override + protected Writeable.Reader instanceReader() { + return Combine::new; + } + + @Override + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables()); + } + + @Override + protected Combine doParseInstance(XContentParser parser) throws IOException { + if (parser.nextToken() == XContentParser.Token.START_OBJECT) { + parser.nextToken(); + } + Combine combine = (Combine) IntervalsSourceProvider.fromXContent(parser); + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); + return combine; + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/DisjunctionIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/DisjunctionIntervalsSourceProviderTests.java new file mode 100644 index 00000000000..0daf48df07b --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/DisjunctionIntervalsSourceProviderTests.java @@ -0,0 +1,75 @@ +/* + * 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.index.query; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.index.query.IntervalsSourceProvider.Disjunction; + +public class DisjunctionIntervalsSourceProviderTests extends AbstractSerializingTestCase { + + @Override + protected Disjunction createTestInstance() { + return IntervalQueryBuilderTests.createRandomDisjunction(0, randomBoolean()); + } + + @Override + protected Disjunction mutateInstance(Disjunction instance) throws IOException { + List subSources = instance.getSubSources(); + IntervalsSourceProvider.IntervalFilter filter = instance.getFilter(); + if (randomBoolean()) { + subSources = subSources == null ? + IntervalQueryBuilderTests.createRandomSourceList(0, randomBoolean(), randomInt(5) + 1) : + null; + } else { + filter = filter == null ? + IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()) : + FilterIntervalsSourceProviderTests.mutateFilter(filter); + } + return new Disjunction(subSources, filter); + } + + @Override + protected Writeable.Reader instanceReader() { + return Disjunction::new; + } + + @Override + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables()); + } + + @Override + protected Disjunction doParseInstance(XContentParser parser) throws IOException { + if (parser.nextToken() == XContentParser.Token.START_OBJECT) { + parser.nextToken(); + } + Disjunction disjunction = (Disjunction) IntervalsSourceProvider.fromXContent(parser); + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); + return disjunction; + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/FilterIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/FilterIntervalsSourceProviderTests.java new file mode 100644 index 00000000000..9e7c5e84a0c --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/FilterIntervalsSourceProviderTests.java @@ -0,0 +1,87 @@ +/* + * 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.index.query; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; + +import java.io.IOException; +import java.util.Collections; + +import static org.elasticsearch.index.query.IntervalsSourceProvider.IntervalFilter; + +public class FilterIntervalsSourceProviderTests extends AbstractSerializingTestCase { + + @Override + protected IntervalFilter createTestInstance() { + return IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()); + } + + @Override + protected IntervalFilter mutateInstance(IntervalFilter instance) throws IOException { + return mutateFilter(instance); + } + + static IntervalFilter mutateFilter(IntervalFilter instance) { + IntervalsSourceProvider filter = instance.getFilter(); + String type = instance.getType(); + Script script = instance.getScript(); + + if (filter != null) { + if (randomBoolean()) { + if (filter instanceof IntervalsSourceProvider.Match) { + filter = WildcardIntervalsSourceProviderTests.createRandomWildcard(); + } else { + filter = IntervalQueryBuilderTests.createRandomMatch(0, randomBoolean()); + } + } else { + if (type.equals("containing")) { + type = "overlapping"; + } else { + type = "containing"; + } + } + return new IntervalFilter(filter, type); + } else { + return new IntervalFilter(new Script(ScriptType.INLINE, "mockscript", script.getIdOrCode() + "foo", Collections.emptyMap())); + } + } + + @Override + protected Writeable.Reader instanceReader() { + return IntervalFilter::new; + } + + @Override + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables()); + } + + @Override + protected IntervalFilter doParseInstance(XContentParser parser) throws IOException { + parser.nextToken(); + return IntervalFilter.fromXContent(parser); + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/FuzzyIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/FuzzyIntervalsSourceProviderTests.java new file mode 100644 index 00000000000..ade07777408 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/FuzzyIntervalsSourceProviderTests.java @@ -0,0 +1,101 @@ +/* + * 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.index.query; + +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.IntervalsSourceProvider.Fuzzy; +import org.elasticsearch.test.AbstractSerializingTestCase; + +import java.io.IOException; + +public class FuzzyIntervalsSourceProviderTests extends AbstractSerializingTestCase { + @Override + protected Fuzzy createTestInstance() { + return new Fuzzy( + randomAlphaOfLength(10), + randomInt(5), + randomBoolean(), + Fuzziness.fromEdits(randomInt(2)), + randomBoolean() ? null : randomAlphaOfLength(10), + randomBoolean() ? null : randomAlphaOfLength(10) + ); + } + + @Override + protected Fuzzy mutateInstance(Fuzzy instance) throws IOException { + String term = instance.getTerm(); + int prefixLength = instance.getPrefixLength(); + boolean isTranspositions = instance.isTranspositions(); + Fuzziness fuzziness = instance.getFuzziness(); + String analyzer = instance.getAnalyzer(); + String useField = instance.getUseField(); + switch (between(0, 5)) { + case 0: + term = randomAlphaOfLength(5); + break; + case 1: + prefixLength++; + break; + case 2: + isTranspositions = !isTranspositions; + break; + case 3: + if (fuzziness.equals(Fuzziness.ZERO)) { + fuzziness = Fuzziness.ONE; + } else { + fuzziness = Fuzziness.ZERO; + } + break; + case 4: + analyzer = analyzer == null ? randomAlphaOfLength(5) : null; + break; + case 5: + useField = useField == null ? randomAlphaOfLength(5) : null; + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new Fuzzy( + term, + prefixLength, + isTranspositions, + fuzziness, + analyzer, + useField + ); + } + + @Override + protected Writeable.Reader instanceReader() { + return Fuzzy::new; + } + + @Override + protected Fuzzy doParseInstance(XContentParser parser) throws IOException { + if (parser.nextToken() == XContentParser.Token.START_OBJECT) { + parser.nextToken(); + } + Fuzzy Fuzzy = (Fuzzy) IntervalsSourceProvider.fromXContent(parser); + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); + return Fuzzy; + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java index ed7caeb0473..4da5ebf4d4e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java @@ -89,47 +89,61 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase 2) { return createRandomMatch(depth + 1, useScripts); } switch (randomInt(20)) { case 0: case 1: - int orCount = randomInt(4) + 1; - List orSources = new ArrayList<>(); - for (int i = 0; i < orCount; i++) { - orSources.add(createRandomSource(depth + 1, useScripts)); - } - return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter(depth + 1, useScripts)); + return createRandomDisjunction(depth, useScripts); case 2: case 3: - int count = randomInt(5) + 1; - List subSources = new ArrayList<>(); - for (int i = 0; i < count; i++) { - subSources.add(createRandomSource(depth + 1, useScripts)); - } - boolean ordered = randomBoolean(); - int maxGaps = randomInt(5) - 1; - IntervalsSourceProvider.IntervalFilter filter = createRandomFilter(depth + 1, useScripts); - return new IntervalsSourceProvider.Combine(subSources, ordered, maxGaps, filter); + return createRandomCombine(depth, useScripts); default: return createRandomMatch(depth + 1, useScripts); } } - private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth, boolean useScripts) { + static IntervalsSourceProvider.Disjunction createRandomDisjunction(int depth, boolean useScripts) { + int orCount = randomInt(4) + 1; + List orSources = createRandomSourceList(depth, useScripts, orCount); + return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter(depth + 1, useScripts)); + } + + static IntervalsSourceProvider.Combine createRandomCombine(int depth, boolean useScripts) { + int count = randomInt(5) + 1; + List subSources = createRandomSourceList(depth, useScripts, count); + boolean ordered = randomBoolean(); + int maxGaps = randomInt(5) - 1; + IntervalsSourceProvider.IntervalFilter filter = createRandomFilter(depth + 1, useScripts); + return new IntervalsSourceProvider.Combine(subSources, ordered, maxGaps, filter); + } + + static List createRandomSourceList(int depth, boolean useScripts, int count) { + List subSources = new ArrayList<>(); + for (int i = 0; i < count; i++) { + subSources.add(createRandomSource(depth + 1, useScripts)); + } + return subSources; + } + + private static IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth, boolean useScripts) { if (depth < 3 && randomInt(20) > 18) { - if (useScripts == false || randomBoolean()) { - return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1, false), randomFrom(filters)); - } - return new IntervalsSourceProvider.IntervalFilter( - new Script(ScriptType.INLINE, "mockscript", "1", Collections.emptyMap())); + return createRandomNonNullFilter(depth, useScripts); } return null; } - private IntervalsSourceProvider createRandomMatch(int depth, boolean useScripts) { + static IntervalsSourceProvider.IntervalFilter createRandomNonNullFilter(int depth, boolean useScripts) { + if (useScripts == false || randomBoolean()) { + return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1, false), randomFrom(filters)); + } + return new IntervalsSourceProvider.IntervalFilter( + new Script(ScriptType.INLINE, "mockscript", "1", Collections.emptyMap())); + } + + static IntervalsSourceProvider.Match createRandomMatch(int depth, boolean useScripts) { String useField = rarely() ? MASKED_FIELD : null; int wordCount = randomInt(4) + 1; List words = new ArrayList<>(); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchIntervalsSourceProviderTests.java new file mode 100644 index 00000000000..be2a2d69a05 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/MatchIntervalsSourceProviderTests.java @@ -0,0 +1,93 @@ +/* + * 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.index.query; + +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; + +import java.io.IOException; + +import static org.elasticsearch.index.query.IntervalsSourceProvider.Match; + +public class MatchIntervalsSourceProviderTests extends AbstractSerializingTestCase { + + @Override + protected Match createTestInstance() { + return IntervalQueryBuilderTests.createRandomMatch(0, randomBoolean()); + } + + @Override + protected Match mutateInstance(Match instance) throws IOException { + String query = instance.getQuery(); + int maxGaps = instance.getMaxGaps(); + boolean isOrdered = instance.isOrdered(); + String analyzer = instance.getAnalyzer(); + IntervalsSourceProvider.IntervalFilter filter = instance.getFilter(); + String useField = instance.getUseField(); + switch (between(0, 5)) { + case 0: + query = randomAlphaOfLength(query.length() + 3); + break; + case 1: + maxGaps++; + break; + case 2: + isOrdered = !isOrdered; + break; + case 3: + analyzer = analyzer == null ? randomAlphaOfLength(5) : null; + break; + case 4: + filter = filter == null ? + IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()) : + FilterIntervalsSourceProviderTests.mutateFilter(filter); + break; + case 5: + useField = useField == null ? randomAlphaOfLength(5) : (useField + "foo"); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + return new Match(query, maxGaps, isOrdered, analyzer, filter, useField); + } + + @Override + protected Writeable.Reader instanceReader() { + return Match::new; + } + + @Override + protected NamedWriteableRegistry getNamedWriteableRegistry() { + return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables()); + } + + @Override + protected Match doParseInstance(XContentParser parser) throws IOException { + if (parser.nextToken() == XContentParser.Token.START_OBJECT) { + parser.nextToken(); + } + Match Match = (Match) IntervalsSourceProvider.fromXContent(parser); + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); + return Match; + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/WildcardIntervalsSourceProviderTests.java b/server/src/test/java/org/elasticsearch/index/query/WildcardIntervalsSourceProviderTests.java index 7bcf3defeea..b67621e8878 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardIntervalsSourceProviderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardIntervalsSourceProviderTests.java @@ -31,6 +31,10 @@ public class WildcardIntervalsSourceProviderTests extends AbstractSerializingTes @Override protected Wildcard createTestInstance() { + return createRandomWildcard(); + } + + static Wildcard createRandomWildcard() { return new Wildcard( randomAlphaOfLength(10), randomBoolean() ? randomAlphaOfLength(10) : null,