|
@@ -17,99 +17,35 @@
|
|
|
* under the License.
|
|
|
*/
|
|
|
|
|
|
-package org.elasticsearch.common.xcontent.support.filtering;
|
|
|
+package org.elasticsearch.common.xcontent.support;
|
|
|
|
|
|
+import org.elasticsearch.common.CheckedFunction;
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.util.set.Sets;
|
|
|
import org.elasticsearch.common.xcontent.XContent;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
-import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
-import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
-import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Set;
|
|
|
-import java.util.function.Function;
|
|
|
|
|
|
import static java.util.Collections.emptySet;
|
|
|
import static java.util.Collections.singleton;
|
|
|
-import static org.hamcrest.CoreMatchers.is;
|
|
|
-import static org.hamcrest.Matchers.equalTo;
|
|
|
-import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
-public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase {
|
|
|
-
|
|
|
- protected abstract XContentType getXContentType();
|
|
|
-
|
|
|
- protected abstract void assertXContentBuilder(XContentBuilder expected, XContentBuilder builder);
|
|
|
-
|
|
|
- protected void assertString(XContentBuilder expected, XContentBuilder builder) {
|
|
|
- assertNotNull(builder);
|
|
|
- assertNotNull(expected);
|
|
|
-
|
|
|
- // Verify that the result is equal to the expected string
|
|
|
- assertThat(builder.bytes().utf8ToString(), is(expected.bytes().utf8ToString()));
|
|
|
- }
|
|
|
-
|
|
|
- protected void assertBinary(XContentBuilder expected, XContentBuilder builder) {
|
|
|
- assertNotNull(builder);
|
|
|
- assertNotNull(expected);
|
|
|
-
|
|
|
- try {
|
|
|
- XContent xContent = XContentFactory.xContent(builder.contentType());
|
|
|
- XContentParser jsonParser = createParser(xContent, expected.bytes());
|
|
|
- XContentParser testParser = createParser(xContent, builder.bytes());
|
|
|
-
|
|
|
- while (true) {
|
|
|
- XContentParser.Token token1 = jsonParser.nextToken();
|
|
|
- XContentParser.Token token2 = testParser.nextToken();
|
|
|
- if (token1 == null) {
|
|
|
- assertThat(token2, nullValue());
|
|
|
- return;
|
|
|
- }
|
|
|
- assertThat(token1, equalTo(token2));
|
|
|
- switch (token1) {
|
|
|
- case FIELD_NAME:
|
|
|
- assertThat(jsonParser.currentName(), equalTo(testParser.currentName()));
|
|
|
- break;
|
|
|
- case VALUE_STRING:
|
|
|
- assertThat(jsonParser.text(), equalTo(testParser.text()));
|
|
|
- break;
|
|
|
- case VALUE_NUMBER:
|
|
|
- assertThat(jsonParser.numberType(), equalTo(testParser.numberType()));
|
|
|
- assertThat(jsonParser.numberValue(), equalTo(testParser.numberValue()));
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- fail("Fail to verify the result of the XContentBuilder: " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private XContentBuilder newXContentBuilder() throws IOException {
|
|
|
- return XContentBuilder.builder(getXContentType().xContent());
|
|
|
- }
|
|
|
+/**
|
|
|
+ * Tests for {@link XContent} filtering.
|
|
|
+ */
|
|
|
+public abstract class AbstractFilteringTestCase extends ESTestCase {
|
|
|
|
|
|
- private XContentBuilder newXContentBuilderWithIncludes(String filter) throws IOException {
|
|
|
- return newXContentBuilder(singleton(filter), emptySet());
|
|
|
+ @FunctionalInterface
|
|
|
+ protected interface Builder extends CheckedFunction<XContentBuilder, XContentBuilder, IOException> {
|
|
|
}
|
|
|
|
|
|
- private XContentBuilder newXContentBuilderWithExcludes(String filter) throws IOException {
|
|
|
- return newXContentBuilder(emptySet(), singleton(filter));
|
|
|
- }
|
|
|
+ protected abstract void testFilter(Builder expected, Builder actual, Set<String> includes, Set<String> excludes) throws IOException;
|
|
|
|
|
|
- private XContentBuilder newXContentBuilder(Set<String> includes, Set<String> excludes) throws IOException {
|
|
|
- return XContentBuilder.builder(getXContentType().xContent(), includes, excludes);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Build a sample using a given XContentBuilder
|
|
|
- */
|
|
|
- private XContentBuilder sample(XContentBuilder builder) throws IOException {
|
|
|
- assertNotNull(builder);
|
|
|
- builder.startObject()
|
|
|
- .field("title", "My awesome book")
|
|
|
+ /** Sample test case **/
|
|
|
+ private static final Builder SAMPLE = builder -> builder.startObject()
|
|
|
+ .field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
|
.field("timestamp", 1428582942867L)
|
|
@@ -179,54 +115,32 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
- return builder;
|
|
|
- }
|
|
|
-
|
|
|
- /** Create a new {@link XContentBuilder} and use it to build the sample using the given inclusive filter **/
|
|
|
- private XContentBuilder sampleWithIncludes(String filter) throws IOException {
|
|
|
- return sample(newXContentBuilderWithIncludes(filter));
|
|
|
- }
|
|
|
-
|
|
|
- /** Create a new {@link XContentBuilder} and use it to build the sample using the given exclusive filter **/
|
|
|
- private XContentBuilder sampleWithExcludes(String filter) throws IOException {
|
|
|
- return sample(newXContentBuilderWithExcludes(filter));
|
|
|
- }
|
|
|
-
|
|
|
- /** Create a new {@link XContentBuilder} and use it to build the sample using the given includes and exclusive filters **/
|
|
|
- private XContentBuilder sampleWithFilters(Set<String> includes, Set<String> excludes) throws IOException {
|
|
|
- return sample(newXContentBuilder(includes, excludes));
|
|
|
- }
|
|
|
-
|
|
|
- /** Create a new {@link XContentBuilder} and use it to build the sample **/
|
|
|
- private XContentBuilder sample() throws IOException {
|
|
|
- return sample(newXContentBuilder());
|
|
|
- }
|
|
|
|
|
|
public void testNoFiltering() throws Exception {
|
|
|
- XContentBuilder expected = sample();
|
|
|
+ final Builder expected = SAMPLE;
|
|
|
|
|
|
- assertXContentBuilder(expected, sample());
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("*"));
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("**"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("xyz"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, singleton("*"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, singleton("**"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("xyz"));
|
|
|
}
|
|
|
|
|
|
public void testNoMatch() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject().endObject();
|
|
|
+ final Builder expected = builder -> builder.startObject().endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("xyz"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("*"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("**"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("xyz"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("*"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("**"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleFieldInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject().field("title", "My awesome book").endObject();
|
|
|
+ final Builder expected = builder -> builder.startObject().field("title", "My awesome book").endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("title"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("title"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testSimpleFieldExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
|
.field("timestamp", 1428582942867L)
|
|
@@ -297,11 +211,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("title"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("title"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleFieldWithWildcardInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("price", 27.99)
|
|
|
.startObject("properties")
|
|
|
.field("weight", 0.8d)
|
|
@@ -353,11 +267,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("pr*"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("pr*"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testSimpleFieldWithWildcardExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("timestamp", 1428582942867L)
|
|
@@ -380,20 +294,20 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("pr*"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("pr*"));
|
|
|
}
|
|
|
|
|
|
public void testMultipleFieldsInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(Sets.newHashSet("title", "pages"), emptySet()));
|
|
|
+ testFilter(expected, SAMPLE, Sets.newHashSet("title", "pages"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testMultipleFieldsExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("price", 27.99)
|
|
|
.field("timestamp", 1428582942867L)
|
|
|
.nullField("default")
|
|
@@ -463,22 +377,22 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sample(newXContentBuilder(emptySet(), Sets.newHashSet("title", "pages"))));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), Sets.newHashSet("title", "pages"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startArray("tags")
|
|
|
.value("elasticsearch")
|
|
|
.value("java")
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("tags"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("tags"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -546,11 +460,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("tags"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("tags"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayOfObjectsInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startArray("authors")
|
|
|
.startObject()
|
|
|
.field("name", "John Doe")
|
|
@@ -565,13 +479,13 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("authors"));
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("authors.*"));
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("authors.*name"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("authors"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, singleton("authors.*"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, singleton("authors.*name"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayOfObjectsExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -631,13 +545,13 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("authors"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("authors.*"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("authors.*name"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("authors"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("authors.*"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("authors.*name"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayOfObjectsPropertyInclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startArray("authors")
|
|
|
.startObject()
|
|
|
.field("lastname", "John")
|
|
@@ -648,12 +562,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("authors.lastname"));
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("authors.l*"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("authors.lastname"), emptySet());
|
|
|
+ testFilter(expected, SAMPLE, singleton("authors.l*"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testSimpleArrayOfObjectsPropertyExclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -723,12 +637,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("authors.lastname"));
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("authors.l*"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("authors.lastname"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("authors.l*"));
|
|
|
}
|
|
|
|
|
|
public void testRecurseField1Inclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startArray("authors")
|
|
|
.startObject()
|
|
|
.field("name", "John Doe")
|
|
@@ -776,11 +690,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("**.name"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("**.name"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testRecurseField1Exclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -839,11 +753,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("**.name"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("**.name"));
|
|
|
}
|
|
|
|
|
|
public void testRecurseField2Inclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startObject("properties")
|
|
|
.startObject("language")
|
|
|
.startObject("en")
|
|
@@ -883,11 +797,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("properties.**.name"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("properties.**.name"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testRecurseField2Exclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -948,11 +862,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("properties.**.name"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("properties.**.name"));
|
|
|
}
|
|
|
|
|
|
public void testRecurseField3Inclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startObject("properties")
|
|
|
.startObject("language")
|
|
|
.startObject("en")
|
|
@@ -977,11 +891,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("properties.*.en.**.name"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("properties.*.en.**.name"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testRecurseField3Exclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -1047,11 +961,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("properties.*.en.**.name"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("properties.*.en.**.name"));
|
|
|
}
|
|
|
|
|
|
public void testRecurseField4Inclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.startObject("properties")
|
|
|
.startObject("language")
|
|
|
.startObject("en")
|
|
@@ -1078,11 +992,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithIncludes("properties.**.distributors.name"));
|
|
|
+ testFilter(expected, SAMPLE, singleton("properties.**.distributors.name"), emptySet());
|
|
|
}
|
|
|
|
|
|
public void testRecurseField4Exclusive() throws Exception {
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject()
|
|
|
+ final Builder expected = builder -> builder.startObject()
|
|
|
.field("title", "My awesome book")
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
@@ -1146,145 +1060,144 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithExcludes("properties.**.distributors.name"));
|
|
|
+ testFilter(expected, SAMPLE, emptySet(), singleton("properties.**.distributors.name"));
|
|
|
}
|
|
|
|
|
|
public void testRawField() throws Exception {
|
|
|
- XContentBuilder expectedRawField = newXContentBuilder().startObject().field("foo", 0).startObject("raw")
|
|
|
- .field("content", "hello world!").endObject().endObject();
|
|
|
- XContentBuilder expectedRawFieldFiltered = newXContentBuilder().startObject().field("foo", 0).endObject();
|
|
|
- XContentBuilder expectedRawFieldNotFiltered = newXContentBuilder().startObject().startObject("raw").field("content", "hello world!")
|
|
|
- .endObject().endObject();
|
|
|
+ final Builder expectedRawField = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("foo", 0)
|
|
|
+ .startObject("raw")
|
|
|
+ .field("content", "hello world!")
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ final Builder expectedRawFieldFiltered = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("foo", 0)
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ final Builder expectedRawFieldNotFiltered = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startObject("raw")
|
|
|
+ .field("content", "hello world!")
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
|
|
|
- BytesReference raw = newXContentBuilder().startObject().field("content", "hello world!").endObject().bytes();
|
|
|
+ Builder sampleWithRaw = builder -> {
|
|
|
+ BytesReference raw = XContentBuilder.builder(builder.contentType().xContent())
|
|
|
+ .startObject()
|
|
|
+ .field("content", "hello world!")
|
|
|
+ .endObject()
|
|
|
+ .bytes();
|
|
|
+ return builder.startObject().field("foo", 0).rawField("raw", raw).endObject();
|
|
|
+ };
|
|
|
|
|
|
// Test method: rawField(String fieldName, BytesReference content)
|
|
|
- assertXContentBuilder(expectedRawField, newXContentBuilder().startObject().field("foo", 0).rawField("raw", raw).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldFiltered,
|
|
|
- newXContentBuilderWithIncludes("f*").startObject().field("foo", 0).rawField("raw", raw).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldFiltered,
|
|
|
- newXContentBuilderWithExcludes("r*").startObject().field("foo", 0).rawField("raw", raw).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldNotFiltered,
|
|
|
- newXContentBuilderWithIncludes("r*").startObject().field("foo", 0).rawField("raw", raw).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldNotFiltered,
|
|
|
- newXContentBuilderWithExcludes("f*").startObject().field("foo", 0).rawField("raw", raw).endObject());
|
|
|
+ testFilter(expectedRawField, sampleWithRaw, emptySet(), emptySet());
|
|
|
+ testFilter(expectedRawFieldFiltered, sampleWithRaw, singleton("f*"), emptySet());
|
|
|
+ testFilter(expectedRawFieldFiltered, sampleWithRaw, emptySet(), singleton("r*"));
|
|
|
+ testFilter(expectedRawFieldNotFiltered, sampleWithRaw, singleton("r*"), emptySet());
|
|
|
+ testFilter(expectedRawFieldNotFiltered, sampleWithRaw, emptySet(), singleton("f*"));
|
|
|
+
|
|
|
+ sampleWithRaw = builder -> {
|
|
|
+ BytesReference raw = XContentBuilder.builder(builder.contentType().xContent())
|
|
|
+ .startObject()
|
|
|
+ . field("content", "hello world!")
|
|
|
+ .endObject()
|
|
|
+ .bytes();
|
|
|
+ return builder.startObject().field("foo", 0).rawField("raw", raw.streamInput()).endObject();
|
|
|
+ };
|
|
|
|
|
|
// Test method: rawField(String fieldName, InputStream content)
|
|
|
- assertXContentBuilder(expectedRawField,
|
|
|
- newXContentBuilder().startObject().field("foo", 0).rawField("raw", raw.streamInput()).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilderWithIncludes("f*").startObject().field("foo", 0)
|
|
|
- .rawField("raw", raw.streamInput()).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilderWithExcludes("r*").startObject().field("foo", 0)
|
|
|
- .rawField("raw", raw.streamInput()).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilderWithIncludes("r*").startObject().field("foo", 0)
|
|
|
- .rawField("raw", raw.streamInput()).endObject());
|
|
|
- assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilderWithExcludes("f*").startObject().field("foo", 0)
|
|
|
- .rawField("raw", raw.streamInput()).endObject());
|
|
|
+ testFilter(expectedRawField, sampleWithRaw, emptySet(), emptySet());
|
|
|
+ testFilter(expectedRawFieldFiltered, sampleWithRaw, singleton("f*"), emptySet());
|
|
|
+ testFilter(expectedRawFieldFiltered, sampleWithRaw, emptySet(), singleton("r*"));
|
|
|
+ testFilter(expectedRawFieldNotFiltered, sampleWithRaw, singleton("r*"), emptySet());
|
|
|
+ testFilter(expectedRawFieldNotFiltered, sampleWithRaw, emptySet(), singleton("f*"));
|
|
|
}
|
|
|
|
|
|
public void testArrays() throws Exception {
|
|
|
// Test: Array of values (no filtering)
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject().startArray("tags").value("lorem").value("ipsum").value("dolor")
|
|
|
- .endArray().endObject();
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithIncludes("t*").startObject().startArray("tags").value("lorem").value("ipsum")
|
|
|
- .value("dolor").endArray().endObject());
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithIncludes("tags").startObject().startArray("tags").value("lorem")
|
|
|
- .value("ipsum").value("dolor").endArray().endObject());
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithExcludes("a").startObject().startArray("tags").value("lorem").value("ipsum")
|
|
|
- .value("dolor").endArray().endObject());
|
|
|
+ final Builder sampleArrayOfValues = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startArray("tags")
|
|
|
+ .value("lorem").value("ipsum").value("dolor")
|
|
|
+ .endArray()
|
|
|
+ .endObject();
|
|
|
+ testFilter(sampleArrayOfValues, sampleArrayOfValues, singleton("t*"), emptySet());
|
|
|
+ testFilter(sampleArrayOfValues, sampleArrayOfValues, singleton("tags"), emptySet());
|
|
|
+ testFilter(sampleArrayOfValues, sampleArrayOfValues, emptySet(), singleton("a"));
|
|
|
|
|
|
// Test: Array of values (with filtering)
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilderWithIncludes("foo").startObject()
|
|
|
- .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject());
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilderWithExcludes("t*").startObject()
|
|
|
- .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject());
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilderWithExcludes("tags").startObject()
|
|
|
- .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject());
|
|
|
+ Builder expected = builder -> builder.startObject().endObject();
|
|
|
+ testFilter(expected, sampleArrayOfValues, singleton("foo"), emptySet());
|
|
|
+ testFilter(expected, sampleArrayOfValues, emptySet(), singleton("t*"));
|
|
|
+ testFilter(expected, sampleArrayOfValues, emptySet(), singleton("tags"));
|
|
|
|
|
|
// Test: Array of objects (no filtering)
|
|
|
- expected = newXContentBuilder().startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject()
|
|
|
- .field("firstname", "ipsum").endObject().endArray().endObject();
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithIncludes("t*").startObject().startArray("tags").startObject()
|
|
|
- .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithIncludes("tags").startObject().startArray("tags").startObject()
|
|
|
- .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithExcludes("a").startObject().startArray("tags").startObject()
|
|
|
- .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
+ final Builder sampleArrayOfObjects = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startArray("tags")
|
|
|
+ .startObject()
|
|
|
+ .field("lastname", "lorem")
|
|
|
+ .endObject()
|
|
|
+ .startObject()
|
|
|
+ .field("firstname", "ipsum")
|
|
|
+ .endObject()
|
|
|
+ .endArray()
|
|
|
+ .endObject();
|
|
|
+ testFilter(sampleArrayOfObjects, sampleArrayOfObjects, singleton("t*"), emptySet());
|
|
|
+ testFilter(sampleArrayOfObjects, sampleArrayOfObjects, singleton("tags"), emptySet());
|
|
|
+ testFilter(sampleArrayOfObjects, sampleArrayOfObjects, emptySet(), singleton("a"));
|
|
|
|
|
|
// Test: Array of objects (with filtering)
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(),
|
|
|
- newXContentBuilderWithIncludes("foo").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject()
|
|
|
- .startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(),
|
|
|
- newXContentBuilderWithExcludes("t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject()
|
|
|
- .startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
- assertXContentBuilder(newXContentBuilder().startObject().endObject(),
|
|
|
- newXContentBuilderWithExcludes("tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject()
|
|
|
- .startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
+ testFilter(expected, sampleArrayOfObjects, singleton("foo"), emptySet());
|
|
|
+ testFilter(expected, sampleArrayOfObjects, emptySet(), singleton("t*"));
|
|
|
+ testFilter(expected, sampleArrayOfObjects, emptySet(), singleton("tags"));
|
|
|
|
|
|
// Test: Array of objects (with partial filtering)
|
|
|
- expected = newXContentBuilder().startObject().startArray("tags").startObject().field("firstname", "ipsum").endObject().endArray()
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startArray("tags")
|
|
|
+ .startObject()
|
|
|
+ .field("firstname", "ipsum")
|
|
|
+ .endObject()
|
|
|
+ .endArray()
|
|
|
.endObject();
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithIncludes("t*.firstname").startObject().startArray("tags").startObject()
|
|
|
- .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
- assertXContentBuilder(expected, newXContentBuilderWithExcludes("t*.lastname").startObject().startArray("tags").startObject()
|
|
|
- .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject());
|
|
|
+ testFilter(expected, sampleArrayOfObjects, singleton("t*.firstname"), emptySet());
|
|
|
+ testFilter(expected, sampleArrayOfObjects, emptySet(), singleton("t*.lastname"));
|
|
|
}
|
|
|
|
|
|
public void testEmptyObject() throws IOException {
|
|
|
- final Function<XContentBuilder, XContentBuilder> build = builder -> {
|
|
|
- try {
|
|
|
- return builder.startObject().startObject("foo").endObject().endObject();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- XContentBuilder expected = build.apply(newXContentBuilder());
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("baz"))));
|
|
|
-
|
|
|
- expected = newXContentBuilder().startObject().endObject();
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("foo"))));
|
|
|
- }
|
|
|
+ final Builder sample = builder -> builder.startObject().startObject("foo").endObject().endObject();
|
|
|
|
|
|
- public void testSingleFieldObject() throws IOException {
|
|
|
- final Function<XContentBuilder, XContentBuilder> build = builder -> {
|
|
|
- try {
|
|
|
- return builder.startObject().startObject("foo").field("bar", "test").endObject().endObject();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- XContentBuilder expected = build.apply(newXContentBuilder());
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo.bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo.baz")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("foo"), singleton("foo.baz"))));
|
|
|
+ Builder expected = builder -> builder.startObject().startObject("foo").endObject().endObject();
|
|
|
+ testFilter(expected, sample, singleton("foo"), emptySet());
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("bar"));
|
|
|
+ testFilter(expected, sample, singleton("f*"), singleton("baz"));
|
|
|
|
|
|
- expected = newXContentBuilder().startObject().endObject();
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo.bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("foo"), singleton("foo.b*"))));
|
|
|
+ expected = builder -> builder.startObject().endObject();
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("foo"));
|
|
|
+ testFilter(expected, sample, singleton("bar"), emptySet());
|
|
|
+ testFilter(expected, sample, singleton("f*"), singleton("foo"));
|
|
|
}
|
|
|
|
|
|
public void testSingleFieldWithBothExcludesIncludes() throws IOException {
|
|
|
- XContentBuilder expected = newXContentBuilder()
|
|
|
+ final Builder expected = builder -> builder
|
|
|
.startObject()
|
|
|
.field("pages", 456)
|
|
|
.field("price", 27.99)
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(singleton("p*"), singleton("properties")));
|
|
|
+ testFilter(expected, SAMPLE, singleton("p*"), singleton("properties"));
|
|
|
}
|
|
|
|
|
|
public void testObjectsInArrayWithBothExcludesIncludes() throws IOException {
|
|
|
Set<String> includes = Sets.newHashSet("tags", "authors");
|
|
|
Set<String> excludes = singleton("authors.name");
|
|
|
|
|
|
- XContentBuilder expected = newXContentBuilder()
|
|
|
+ final Builder expected = builder -> builder
|
|
|
.startObject()
|
|
|
.startArray("tags")
|
|
|
.value("elasticsearch")
|
|
@@ -1302,14 +1215,14 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endArray()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(includes, excludes));
|
|
|
+ testFilter(expected, SAMPLE, includes, excludes);
|
|
|
}
|
|
|
|
|
|
public void testRecursiveObjectsInArrayWithBothExcludesIncludes() throws IOException {
|
|
|
Set<String> includes = Sets.newHashSet("**.language", "properties.weight");
|
|
|
Set<String> excludes = singleton("**.distributors");
|
|
|
|
|
|
- XContentBuilder expected = newXContentBuilder()
|
|
|
+ final Builder expected = builder -> builder
|
|
|
.startObject()
|
|
|
.startObject("properties")
|
|
|
.field("weight", 0.8d)
|
|
@@ -1326,22 +1239,22 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(includes, excludes));
|
|
|
+ testFilter(expected, SAMPLE, includes, excludes);
|
|
|
}
|
|
|
|
|
|
public void testRecursiveSameObjectWithBothExcludesIncludes() throws IOException {
|
|
|
Set<String> includes = singleton("**.distributors");
|
|
|
Set<String> excludes = singleton("**.distributors");
|
|
|
|
|
|
- XContentBuilder expected = newXContentBuilder().startObject().endObject();
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(includes, excludes));
|
|
|
+ final Builder expected = builder -> builder.startObject().endObject();
|
|
|
+ testFilter(expected, SAMPLE, includes, excludes);
|
|
|
}
|
|
|
|
|
|
public void testRecursiveObjectsPropertiesWithBothExcludesIncludes() throws IOException {
|
|
|
Set<String> includes = singleton("**.en.*");
|
|
|
Set<String> excludes = Sets.newHashSet("**.distributors.*.name", "**.street");
|
|
|
|
|
|
- XContentBuilder expected = newXContentBuilder()
|
|
|
+ final Builder expected = builder -> builder
|
|
|
.startObject()
|
|
|
.startObject("properties")
|
|
|
.startObject("language")
|
|
@@ -1369,26 +1282,149 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase
|
|
|
.endObject()
|
|
|
.endObject();
|
|
|
|
|
|
- assertXContentBuilder(expected, sampleWithFilters(includes, excludes));
|
|
|
+ testFilter(expected, SAMPLE, includes, excludes);
|
|
|
}
|
|
|
|
|
|
public void testWithLfAtEnd() throws IOException {
|
|
|
- final Function<XContentBuilder, XContentBuilder> build = builder -> {
|
|
|
- try {
|
|
|
- return builder.startObject().startObject("foo").field("bar", "baz").endObject().endObject().prettyPrint().lfAtEnd();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- };
|
|
|
+ final Builder sample = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startObject("foo")
|
|
|
+ .field("bar", "baz")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .prettyPrint()
|
|
|
+ .lfAtEnd();
|
|
|
+
|
|
|
+ testFilter(sample, sample, singleton("foo"), emptySet());
|
|
|
+ testFilter(sample, sample, emptySet(), singleton("bar"));
|
|
|
+ testFilter(sample, sample, singleton("f*"), singleton("baz"));
|
|
|
+
|
|
|
+ final Builder expected = builder -> builder.startObject().endObject().prettyPrint().lfAtEnd();
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("foo"));
|
|
|
+ testFilter(expected, sample, singleton("bar"), emptySet());
|
|
|
+ testFilter(expected, sample, singleton("f*"), singleton("foo"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testBasics() throws Exception {
|
|
|
+ final Builder sample = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("test1", "value1")
|
|
|
+ .field("test2", "value2")
|
|
|
+ .field("something_else", "value3")
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ Builder expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("test1", "value1")
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, singleton("test1"), emptySet());
|
|
|
+
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("test1", "value1")
|
|
|
+ .field("test2", "value2")
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, singleton("test*"), emptySet());
|
|
|
+
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("test2", "value2")
|
|
|
+ .field("something_else", "value3")
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("test1"));
|
|
|
+
|
|
|
+ // more complex object...
|
|
|
+ final Builder complex = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startObject("path1")
|
|
|
+ .startArray("path2")
|
|
|
+ .startObject().field("test", "value1").endObject()
|
|
|
+ .startObject().field("test", "value2").endObject()
|
|
|
+ .endArray()
|
|
|
+ .endObject()
|
|
|
+ .field("test1", "value1")
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .startObject("path1")
|
|
|
+ .startArray("path2")
|
|
|
+ .startObject().field("test", "value1").endObject()
|
|
|
+ .startObject().field("test", "value2").endObject()
|
|
|
+ .endArray()
|
|
|
+ .endObject()
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, complex, singleton("path1"), emptySet());
|
|
|
+ testFilter(expected, complex, singleton("path1*"), emptySet());
|
|
|
+ testFilter(expected, complex, singleton("path1.path2.*"), emptySet());
|
|
|
+
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("test1", "value1")
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, complex, singleton("test1*"), emptySet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generalization of {@link XContentMapValuesTests#testSupplementaryCharactersInPaths()}
|
|
|
+ */
|
|
|
+ public void testFilterSupplementaryCharactersInPaths() throws IOException {
|
|
|
+ final Builder sample = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("搜索", 2)
|
|
|
+ .field("指数", 3)
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ Builder expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("搜索", 2)
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, singleton("搜索"), emptySet());
|
|
|
|
|
|
- XContentBuilder expected = build.apply(newXContentBuilder());
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("baz"))));
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("指数", 3)
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("搜索"));
|
|
|
+ }
|
|
|
|
|
|
- expected = newXContentBuilder().startObject().endObject().prettyPrint().lfAtEnd();
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("bar")));
|
|
|
- assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("foo"))));
|
|
|
+ /**
|
|
|
+ * Generalization of {@link XContentMapValuesTests#testSharedPrefixes()}
|
|
|
+ */
|
|
|
+ public void testFilterSharedPrefixes() throws IOException {
|
|
|
+ final Builder sample = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("foobar", 2)
|
|
|
+ .field("foobaz", 3)
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ Builder expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("foobar", 2)
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, singleton("foobar"), emptySet());
|
|
|
+
|
|
|
+ expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("foobaz", 3)
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, emptySet(), singleton("foobar"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generalization of {@link XContentMapValuesTests#testPrefix()}
|
|
|
+ */
|
|
|
+ public void testFilterPrefix() throws IOException {
|
|
|
+ final Builder sample = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .array("photos", "foo", "bar")
|
|
|
+ .field("photosCount", 2)
|
|
|
+ .endObject();
|
|
|
+
|
|
|
+ Builder expected = builder -> builder
|
|
|
+ .startObject()
|
|
|
+ .field("photosCount", 2)
|
|
|
+ .endObject();
|
|
|
+ testFilter(expected, sample, singleton("photosCount"), emptySet());
|
|
|
}
|
|
|
}
|