Skip to content

Commit 0e2cfc6

Browse files
authored
[Test] Use a common testing class for all XContent filtering tests (#25491)
We have two ways to filter XContent: - The first method is to parse the XContent as a map and use XContentMapValues.filter(). This method filters the content of the map using an automaton. It is used for source filtering, both at search and indexing time. It performs well but can generate a lot of objects and garbage collections when large XContent are filtered. It also returns empty objects (see f2710c1) when all the sub fields have been filtered out and handle dots in field names as if they were sub fields. - The second method is to parse the XContent and copy the XContentParser structure to a XContentBuilder initialized with includes/excludes filters. This method uses the Jackson streaming filter feature. It is used by the Response Filtering ('filter_path') feature. It does not generate a lot of objects, and does not return empty objects and also does not handle dots in field names explicitely. Both methods have similar goals but different tests. This commit changes the current XContentBuilder test class so that it becomes a more generic testing class and we can now ensure that filtering methods generate the same results. It also removes some tests from the XContentMapValuesTests class that should be in XContentParserTests.
1 parent a9ea742 commit 0e2cfc6

File tree

8 files changed

+550
-425
lines changed

8 files changed

+550
-425
lines changed

core/src/test/java/org/elasticsearch/common/xcontent/XContentParserTests.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626

2727
import java.io.IOException;
2828
import java.util.Arrays;
29+
import java.util.Collections;
2930
import java.util.List;
3031
import java.util.Map;
3132

33+
import static java.util.Collections.emptyMap;
34+
import static java.util.Collections.singletonMap;
3235
import static org.hamcrest.Matchers.contains;
3336
import static org.hamcrest.Matchers.containsString;
3437
import static org.hamcrest.Matchers.equalTo;
@@ -193,4 +196,84 @@ public void testReadBooleans() throws IOException {
193196
assertTrue(parser.booleanValue());
194197
}
195198
}
199+
200+
public void testEmptyList() throws IOException {
201+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
202+
.startArray("some_array")
203+
.endArray().endObject();
204+
205+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
206+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
207+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
208+
assertEquals("some_array", parser.currentName());
209+
if (random().nextBoolean()) {
210+
// sometimes read the start array token, sometimes not
211+
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
212+
}
213+
assertEquals(Collections.emptyList(), parser.list());
214+
}
215+
}
216+
217+
public void testSimpleList() throws IOException {
218+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
219+
.startArray("some_array")
220+
.value(1)
221+
.value(3)
222+
.value(0)
223+
.endArray().endObject();
224+
225+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
226+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
227+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
228+
assertEquals("some_array", parser.currentName());
229+
if (random().nextBoolean()) {
230+
// sometimes read the start array token, sometimes not
231+
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
232+
}
233+
assertEquals(Arrays.asList(1, 3, 0), parser.list());
234+
}
235+
}
236+
237+
public void testNestedList() throws IOException {
238+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
239+
.startArray("some_array")
240+
.startArray().endArray()
241+
.startArray().value(1).value(3).endArray()
242+
.startArray().value(2).endArray()
243+
.endArray().endObject();
244+
245+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
246+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
247+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
248+
assertEquals("some_array", parser.currentName());
249+
if (random().nextBoolean()) {
250+
// sometimes read the start array token, sometimes not
251+
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
252+
}
253+
assertEquals(
254+
Arrays.asList(Collections.<Integer>emptyList(), Arrays.asList(1, 3), Arrays.asList(2)),
255+
parser.list());
256+
}
257+
}
258+
259+
public void testNestedMapInList() throws IOException {
260+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
261+
.startArray("some_array")
262+
.startObject().field("foo", "bar").endObject()
263+
.startObject().endObject()
264+
.endArray().endObject();
265+
266+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
267+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
268+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
269+
assertEquals("some_array", parser.currentName());
270+
if (random().nextBoolean()) {
271+
// sometimes read the start array token, sometimes not
272+
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
273+
}
274+
assertEquals(
275+
Arrays.asList(singletonMap("foo", "bar"), emptyMap()),
276+
parser.list());
277+
}
278+
}
196279
}

0 commit comments

Comments
 (0)