Skip to content

Commit ef54a9c

Browse files
HoholChristoph Büscher
authored and
Christoph Büscher
committed
Add tests for IntervalsSourceProvider.Wildcard and Prefix (elastic#50306)
This PR adds unit tests for wire and xContent serialization of `IntervalsSourceProvider.Wildcard` and `IntervalsSourceProvider.Prefix`. Relates elastic#50150
1 parent bf63f24 commit ef54a9c

File tree

3 files changed

+181
-5
lines changed

3 files changed

+181
-5
lines changed

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static IntervalsSourceProvider fromXContent(XContentParser parser) throws
8787
return Wildcard.fromXContent(parser);
8888
}
8989
throw new ParsingException(parser.getTokenLocation(),
90-
"Unknown interval type [" + parser.currentName() + "], expecting one of [match, any_of, all_of, prefix]");
90+
"Unknown interval type [" + parser.currentName() + "], expecting one of [match, any_of, all_of, prefix, wildcard]");
9191
}
9292

9393
private static IntervalsSourceProvider parseInnerIntervals(XContentParser parser) throws IOException {
@@ -548,6 +548,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
548548
public static Prefix fromXContent(XContentParser parser) throws IOException {
549549
return PARSER.parse(parser, null);
550550
}
551+
552+
String getPrefix() {
553+
return prefix;
554+
}
555+
556+
String getAnalyzer() {
557+
return analyzer;
558+
}
559+
560+
String getUseField() {
561+
return useField;
562+
}
551563
}
552564

553565
public static class Wildcard extends IntervalsSourceProvider {
@@ -613,10 +625,10 @@ public void extractFields(Set<String> fields) {
613625
public boolean equals(Object o) {
614626
if (this == o) return true;
615627
if (o == null || getClass() != o.getClass()) return false;
616-
Prefix prefix = (Prefix) o;
617-
return Objects.equals(pattern, prefix.prefix) &&
618-
Objects.equals(analyzer, prefix.analyzer) &&
619-
Objects.equals(useField, prefix.useField);
628+
Wildcard wildcard = (Wildcard) o;
629+
return Objects.equals(pattern, wildcard.pattern) &&
630+
Objects.equals(analyzer, wildcard.analyzer) &&
631+
Objects.equals(useField, wildcard.useField);
620632
}
621633

622634
@Override
@@ -665,6 +677,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
665677
public static Wildcard fromXContent(XContentParser parser) throws IOException {
666678
return PARSER.parse(parser, null);
667679
}
680+
681+
String getPattern() {
682+
return pattern;
683+
}
684+
685+
String getAnalyzer() {
686+
return analyzer;
687+
}
688+
689+
String getUseField() {
690+
return useField;
691+
}
668692
}
669693

670694
static class ScriptFilterSource extends FilteredIntervalsSource {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.Writeable;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.test.AbstractSerializingTestCase;
25+
26+
import java.io.IOException;
27+
28+
import static org.elasticsearch.index.query.IntervalsSourceProvider.Prefix;
29+
30+
public class PrefixIntervalsSourceProviderTests extends AbstractSerializingTestCase<Prefix> {
31+
32+
@Override
33+
protected Prefix createTestInstance() {
34+
return new Prefix(
35+
randomAlphaOfLength(10),
36+
randomBoolean() ? randomAlphaOfLength(10) : null,
37+
randomBoolean() ? randomAlphaOfLength(10) : null
38+
);
39+
}
40+
41+
@Override
42+
protected Prefix mutateInstance(Prefix instance) throws IOException {
43+
String prefix = instance.getPrefix();
44+
String analyzer = instance.getAnalyzer();
45+
String useField = instance.getUseField();
46+
switch (between(0, 2)) {
47+
case 0:
48+
prefix += "a";
49+
break;
50+
case 1:
51+
analyzer = randomAlphaOfLength(5);
52+
break;
53+
case 2:
54+
useField = useField == null ? randomAlphaOfLength(5) : null;
55+
break;
56+
default:
57+
throw new AssertionError("Illegal randomisation branch");
58+
}
59+
return new Prefix(prefix, analyzer, useField);
60+
}
61+
62+
@Override
63+
protected Writeable.Reader<Prefix> instanceReader() {
64+
return Prefix::new;
65+
}
66+
67+
@Override
68+
protected Prefix doParseInstance(XContentParser parser) throws IOException {
69+
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
70+
parser.nextToken();
71+
}
72+
Prefix prefix = (Prefix) IntervalsSourceProvider.fromXContent(parser);
73+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
74+
return prefix;
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.Writeable;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.test.AbstractSerializingTestCase;
25+
26+
import java.io.IOException;
27+
28+
import static org.elasticsearch.index.query.IntervalsSourceProvider.Wildcard;
29+
30+
public class WildcardIntervalsSourceProviderTests extends AbstractSerializingTestCase<Wildcard> {
31+
32+
@Override
33+
protected Wildcard createTestInstance() {
34+
return new Wildcard(
35+
randomAlphaOfLength(10),
36+
randomBoolean() ? randomAlphaOfLength(10) : null,
37+
randomBoolean() ? randomAlphaOfLength(10) : null
38+
);
39+
}
40+
41+
@Override
42+
protected Wildcard mutateInstance(Wildcard instance) throws IOException {
43+
String wildcard = instance.getPattern();
44+
String analyzer = instance.getAnalyzer();
45+
String useField = instance.getUseField();
46+
switch (between(0, 2)) {
47+
case 0:
48+
wildcard += "a";
49+
break;
50+
case 1:
51+
analyzer = randomAlphaOfLength(5);
52+
break;
53+
case 2:
54+
useField = useField == null ? randomAlphaOfLength(5) : null;
55+
break;
56+
default:
57+
throw new AssertionError("Illegal randomisation branch");
58+
}
59+
return new Wildcard(wildcard, analyzer, useField);
60+
}
61+
62+
@Override
63+
protected Writeable.Reader<Wildcard> instanceReader() {
64+
return Wildcard::new;
65+
}
66+
67+
@Override
68+
protected Wildcard doParseInstance(XContentParser parser) throws IOException {
69+
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
70+
parser.nextToken();
71+
}
72+
Wildcard wildcard = (Wildcard) IntervalsSourceProvider.fromXContent(parser);
73+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
74+
return wildcard;
75+
}
76+
}

0 commit comments

Comments
 (0)