Skip to content

Commit 5533e11

Browse files
HoholChristoph Büscher
authored and
Christoph Büscher
committed
Add tests for remaining IntervalsSourceProvider implementations (#50326)
This PR adds unit tests for wire and xContent serialization of remaining IntervalsSourceProvider implementations. Closes #50150
1 parent 66c6909 commit 5533e11

9 files changed

+595
-39
lines changed

server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
246246
public static Match fromXContent(XContentParser parser) {
247247
return PARSER.apply(parser, null);
248248
}
249+
250+
String getQuery() {
251+
return query;
252+
}
253+
254+
int getMaxGaps() {
255+
return maxGaps;
256+
}
257+
258+
boolean isOrdered() {
259+
return ordered;
260+
}
261+
262+
String getAnalyzer() {
263+
return analyzer;
264+
}
265+
266+
IntervalFilter getFilter() {
267+
return filter;
268+
}
269+
270+
String getUseField() {
271+
return useField;
272+
}
249273
}
250274

251275
public static class Disjunction extends IntervalsSourceProvider {
@@ -290,12 +314,13 @@ public boolean equals(Object o) {
290314
if (this == o) return true;
291315
if (o == null || getClass() != o.getClass()) return false;
292316
Disjunction that = (Disjunction) o;
293-
return Objects.equals(subSources, that.subSources);
317+
return Objects.equals(subSources, that.subSources) &&
318+
Objects.equals(filter, that.filter);
294319
}
295320

296321
@Override
297322
public int hashCode() {
298-
return Objects.hash(subSources);
323+
return Objects.hash(subSources, filter);
299324
}
300325

301326
@Override
@@ -342,6 +367,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
342367
public static Disjunction fromXContent(XContentParser parser) throws IOException {
343368
return PARSER.parse(parser, null);
344369
}
370+
371+
List<IntervalsSourceProvider> getSubSources() {
372+
return subSources;
373+
}
374+
375+
IntervalFilter getFilter() {
376+
return filter;
377+
}
345378
}
346379

347380
public static class Combine extends IntervalsSourceProvider {
@@ -393,12 +426,14 @@ public boolean equals(Object o) {
393426
if (o == null || getClass() != o.getClass()) return false;
394427
Combine combine = (Combine) o;
395428
return Objects.equals(subSources, combine.subSources) &&
396-
ordered == combine.ordered && maxGaps == combine.maxGaps;
429+
ordered == combine.ordered &&
430+
maxGaps == combine.maxGaps &&
431+
Objects.equals(filter, combine.filter);
397432
}
398433

399434
@Override
400435
public int hashCode() {
401-
return Objects.hash(subSources, ordered, maxGaps);
436+
return Objects.hash(subSources, ordered, maxGaps, filter);
402437
}
403438

404439
@Override
@@ -452,6 +487,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
452487
public static Combine fromXContent(XContentParser parser) {
453488
return PARSER.apply(parser, null);
454489
}
490+
491+
List<IntervalsSourceProvider> getSubSources() {
492+
return subSources;
493+
}
494+
495+
boolean isOrdered() {
496+
return ordered;
497+
}
498+
499+
int getMaxGaps() {
500+
return maxGaps;
501+
}
502+
503+
IntervalFilter getFilter() {
504+
return filter;
505+
}
455506
}
456507

457508
public static class Prefix extends IntervalsSourceProvider {
@@ -838,6 +889,30 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
838889
public static Fuzzy fromXContent(XContentParser parser) throws IOException {
839890
return PARSER.parse(parser, null);
840891
}
892+
893+
String getTerm() {
894+
return term;
895+
}
896+
897+
int getPrefixLength() {
898+
return prefixLength;
899+
}
900+
901+
boolean isTranspositions() {
902+
return transpositions;
903+
}
904+
905+
Fuzziness getFuzziness() {
906+
return fuzziness;
907+
}
908+
909+
String getAnalyzer() {
910+
return analyzer;
911+
}
912+
913+
String getUseField() {
914+
return useField;
915+
}
841916
}
842917

843918
static class ScriptFilterSource extends FilteredIntervalsSource {
@@ -985,6 +1060,18 @@ public static IntervalFilter fromXContent(XContentParser parser) throws IOExcept
9851060
}
9861061
return new IntervalFilter(intervals, type);
9871062
}
1063+
1064+
String getType() {
1065+
return type;
1066+
}
1067+
1068+
IntervalsSourceProvider getFilter() {
1069+
return filter;
1070+
}
1071+
1072+
Script getScript() {
1073+
return script;
1074+
}
9881075
}
9891076

9901077

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
278278

279279
import java.util.ArrayList;
280+
import java.util.Arrays;
280281
import java.util.List;
281282
import java.util.Map;
282283
import java.util.function.Consumer;
@@ -286,6 +287,7 @@
286287
import static java.util.Objects.requireNonNull;
287288
import static org.elasticsearch.index.query.CommonTermsQueryBuilder.COMMON_TERMS_QUERY_DEPRECATION_MSG;
288289
import static org.elasticsearch.index.query.SpanNearQueryBuilder.SpanGapQueryBuilder;
290+
import static java.util.Collections.unmodifiableList;
289291

290292
/**
291293
* Sets up things that can be done at search time like queries, aggregations, and suggesters.
@@ -851,18 +853,23 @@ private void registerQueryParsers(List<SearchPlugin> plugins) {
851853
}
852854

853855
private void registerIntervalsSourceProviders() {
854-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
855-
IntervalsSourceProvider.Match.NAME, IntervalsSourceProvider.Match::new));
856-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
857-
IntervalsSourceProvider.Combine.NAME, IntervalsSourceProvider.Combine::new));
858-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
859-
IntervalsSourceProvider.Disjunction.NAME, IntervalsSourceProvider.Disjunction::new));
860-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
861-
IntervalsSourceProvider.Prefix.NAME, IntervalsSourceProvider.Prefix::new));
862-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
863-
IntervalsSourceProvider.Wildcard.NAME, IntervalsSourceProvider.Wildcard::new));
864-
namedWriteables.add(new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class,
865-
IntervalsSourceProvider.Fuzzy.NAME, IntervalsSourceProvider.Fuzzy::new));
856+
namedWriteables.addAll(getIntervalsSourceProviderNamedWritables());
857+
}
858+
859+
public static List<NamedWriteableRegistry.Entry> getIntervalsSourceProviderNamedWritables() {
860+
return unmodifiableList(Arrays.asList(
861+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Match.NAME,
862+
IntervalsSourceProvider.Match::new),
863+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Combine.NAME,
864+
IntervalsSourceProvider.Combine::new),
865+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Disjunction.NAME,
866+
IntervalsSourceProvider.Disjunction::new),
867+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Prefix.NAME,
868+
IntervalsSourceProvider.Prefix::new),
869+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Wildcard.NAME,
870+
IntervalsSourceProvider.Wildcard::new),
871+
new NamedWriteableRegistry.Entry(IntervalsSourceProvider.class, IntervalsSourceProvider.Fuzzy.NAME,
872+
IntervalsSourceProvider.Fuzzy::new)));
866873
}
867874

868875
private void registerQuery(QuerySpec<?> spec) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.query;
21+
22+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.search.SearchModule;
26+
import org.elasticsearch.test.AbstractSerializingTestCase;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
31+
import static org.elasticsearch.index.query.IntervalsSourceProvider.Combine;
32+
33+
public class CombineIntervalsSourceProviderTests extends AbstractSerializingTestCase<Combine> {
34+
35+
@Override
36+
protected Combine createTestInstance() {
37+
return IntervalQueryBuilderTests.createRandomCombine(0, randomBoolean());
38+
}
39+
40+
@Override
41+
protected Combine mutateInstance(Combine instance) throws IOException {
42+
List<IntervalsSourceProvider> subSources = instance.getSubSources();
43+
boolean ordered = instance.isOrdered();
44+
int maxGaps = instance.getMaxGaps();
45+
IntervalsSourceProvider.IntervalFilter filter = instance.getFilter();
46+
switch (between(0, 3)) {
47+
case 0:
48+
subSources = subSources == null ?
49+
IntervalQueryBuilderTests.createRandomSourceList(0, randomBoolean(), randomInt(5) + 1) :
50+
null;
51+
break;
52+
case 1:
53+
ordered = !ordered;
54+
break;
55+
case 2:
56+
maxGaps++;
57+
break;
58+
case 3:
59+
filter = filter == null ?
60+
IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()) :
61+
FilterIntervalsSourceProviderTests.mutateFilter(filter);
62+
break;
63+
default:
64+
throw new AssertionError("Illegal randomisation branch");
65+
}
66+
return new Combine(subSources, ordered, maxGaps, filter);
67+
}
68+
69+
@Override
70+
protected Writeable.Reader<Combine> instanceReader() {
71+
return Combine::new;
72+
}
73+
74+
@Override
75+
protected NamedWriteableRegistry getNamedWriteableRegistry() {
76+
return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables());
77+
}
78+
79+
@Override
80+
protected Combine doParseInstance(XContentParser parser) throws IOException {
81+
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
82+
parser.nextToken();
83+
}
84+
Combine combine = (Combine) IntervalsSourceProvider.fromXContent(parser);
85+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
86+
return combine;
87+
}
88+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.query;
21+
22+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.search.SearchModule;
26+
import org.elasticsearch.test.AbstractSerializingTestCase;
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
31+
import static org.elasticsearch.index.query.IntervalsSourceProvider.Disjunction;
32+
33+
public class DisjunctionIntervalsSourceProviderTests extends AbstractSerializingTestCase<Disjunction> {
34+
35+
@Override
36+
protected Disjunction createTestInstance() {
37+
return IntervalQueryBuilderTests.createRandomDisjunction(0, randomBoolean());
38+
}
39+
40+
@Override
41+
protected Disjunction mutateInstance(Disjunction instance) throws IOException {
42+
List<IntervalsSourceProvider> subSources = instance.getSubSources();
43+
IntervalsSourceProvider.IntervalFilter filter = instance.getFilter();
44+
if (randomBoolean()) {
45+
subSources = subSources == null ?
46+
IntervalQueryBuilderTests.createRandomSourceList(0, randomBoolean(), randomInt(5) + 1) :
47+
null;
48+
} else {
49+
filter = filter == null ?
50+
IntervalQueryBuilderTests.createRandomNonNullFilter(0, randomBoolean()) :
51+
FilterIntervalsSourceProviderTests.mutateFilter(filter);
52+
}
53+
return new Disjunction(subSources, filter);
54+
}
55+
56+
@Override
57+
protected Writeable.Reader<Disjunction> instanceReader() {
58+
return Disjunction::new;
59+
}
60+
61+
@Override
62+
protected NamedWriteableRegistry getNamedWriteableRegistry() {
63+
return new NamedWriteableRegistry(SearchModule.getIntervalsSourceProviderNamedWritables());
64+
}
65+
66+
@Override
67+
protected Disjunction doParseInstance(XContentParser parser) throws IOException {
68+
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
69+
parser.nextToken();
70+
}
71+
Disjunction disjunction = (Disjunction) IntervalsSourceProvider.fromXContent(parser);
72+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
73+
return disjunction;
74+
}
75+
}

0 commit comments

Comments
 (0)