Skip to content

Commit

Permalink
[Test] Use a common testing class for all XContent filtering tests (e…
Browse files Browse the repository at this point in the history
…lastic#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.
  • Loading branch information
tlrx authored Jul 3, 2017
1 parent a9ea742 commit 0e2cfc6
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 425 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -193,4 +196,84 @@ public void testReadBooleans() throws IOException {
assertTrue(parser.booleanValue());
}
}

public void testEmptyList() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.startArray("some_array")
.endArray().endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
if (random().nextBoolean()) {
// sometimes read the start array token, sometimes not
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
}
assertEquals(Collections.emptyList(), parser.list());
}
}

public void testSimpleList() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.startArray("some_array")
.value(1)
.value(3)
.value(0)
.endArray().endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
if (random().nextBoolean()) {
// sometimes read the start array token, sometimes not
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
}
assertEquals(Arrays.asList(1, 3, 0), parser.list());
}
}

public void testNestedList() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.startArray("some_array")
.startArray().endArray()
.startArray().value(1).value(3).endArray()
.startArray().value(2).endArray()
.endArray().endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
if (random().nextBoolean()) {
// sometimes read the start array token, sometimes not
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
}
assertEquals(
Arrays.asList(Collections.<Integer>emptyList(), Arrays.asList(1, 3), Arrays.asList(2)),
parser.list());
}
}

public void testNestedMapInList() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.startArray("some_array")
.startObject().field("foo", "bar").endObject()
.startObject().endObject()
.endArray().endObject();

try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("some_array", parser.currentName());
if (random().nextBoolean()) {
// sometimes read the start array token, sometimes not
assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken());
}
assertEquals(
Arrays.asList(singletonMap("foo", "bar"), emptyMap()),
parser.list());
}
}
}
Loading

0 comments on commit 0e2cfc6

Please sign in to comment.