Skip to content

Move xcontent filtering tests #79298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ private Object[] getTargets(String projectPath) {
return new String[] {
"src/*/java/org/elasticsearch/action/admin/cluster/repositories/**/*.java",
"src/*/java/org/elasticsearch/action/admin/cluster/snapshots/**/*.java",
"src/test/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java",
"src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java",
"src/*/java/org/elasticsearch/index/IndexMode.java",
"src/*/java/org/elasticsearch/index/IndexRouting.java",
Expand All @@ -98,6 +97,10 @@ private Object[] getTargets(String projectPath) {
"src/*/java/org/elasticsearch/repositories/**/*.java",
"src/*/java/org/elasticsearch/search/aggregations/**/*.java",
"src/*/java/org/elasticsearch/snapshots/**/*.java" };
} else if (projectPath.equals(":test:framework")) {
return new String[] {
"src/test/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java",
};
} else {
// Normally this isn"t necessary, but we have Java sources in
// non-standard places
Expand Down Expand Up @@ -203,7 +206,6 @@ private Object[] getTargets(String projectPath) {
":test:fixtures:geoip-fixture",
":test:fixtures:krb5kdc-fixture",
":test:fixtures:old-elasticsearch",
":test:framework",
":test:logger-usage",
":x-pack:docs",
":x-pack:license-tools",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.support.AbstractFilteringTestCase;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.common.xcontent.support.AbstractFilteringTestCase;
import org.elasticsearch.xcontent.support.filtering.FilterPath;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -142,6 +141,8 @@ static void assertXContentBuilderAsBytes(final XContentBuilder expected, final X
assertThat(jsonParser.numberType(), equalTo(testParser.numberType()));
assertThat(jsonParser.numberValue(), equalTo(testParser.numberValue()));
break;
default:
break;
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;

import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.support.filtering.FilterPathBasedFilter;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.support.filtering.FilterPath;

import java.util.Arrays;
import java.util.LinkedHashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

package org.elasticsearch.common.xcontent.support.filtering;
package org.elasticsearch.xcontent.support.filtering;

import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,7 @@ public void testPrefixedNamesFilteringTest() {
public void testNestedFiltering() {
Map<String, Object> map = new HashMap<>();
map.put("field", "value");
map.put("array", Arrays.asList(1, new HashMap<String, Object>() {
{
put("nested", 2);
put("nested_2", 3);
}
}));
map.put("array", Arrays.asList(1, Map.of("nested", 2, "nested_2", 3)));
Map<String, Object> filteredMap = XContentMapValues.filter(map, new String[] { "array.nested" }, Strings.EMPTY_ARRAY);
assertThat(filteredMap.size(), equalTo(1));

Expand All @@ -336,12 +331,7 @@ public void testNestedFiltering() {

map.clear();
map.put("field", "value");
map.put("obj", new HashMap<String, Object>() {
{
put("field", "value");
put("field2", "value2");
}
});
map.put("obj", Map.of("field", "value", "field2", "value2"));
filteredMap = XContentMapValues.filter(map, new String[] { "obj.field" }, Strings.EMPTY_ARRAY);
assertThat(filteredMap.size(), equalTo(1));
assertThat(((Map<String, Object>) filteredMap.get("obj")).size(), equalTo(1));
Expand All @@ -359,18 +349,8 @@ public void testNestedFiltering() {
public void testCompleteObjectFiltering() {
Map<String, Object> map = new HashMap<>();
map.put("field", "value");
map.put("obj", new HashMap<String, Object>() {
{
put("field", "value");
put("field2", "value2");
}
});
map.put("array", Arrays.asList(1, new HashMap<String, Object>() {
{
put("field", "value");
put("field2", "value2");
}
}));
map.put("obj", Map.of("field", "value", "field2", "value2"));
map.put("array", Arrays.asList(1, Map.of("field", "value", "field2", "value2")));

Map<String, Object> filteredMap = XContentMapValues.filter(map, new String[] { "obj" }, Strings.EMPTY_ARRAY);
assertThat(filteredMap.size(), equalTo(1));
Expand Down Expand Up @@ -401,18 +381,8 @@ public void testCompleteObjectFiltering() {
public void testFilterIncludesUsingStarPrefix() {
Map<String, Object> map = new HashMap<>();
map.put("field", "value");
map.put("obj", new HashMap<String, Object>() {
{
put("field", "value");
put("field2", "value2");
}
});
map.put("n_obj", new HashMap<String, Object>() {
{
put("n_field", "value");
put("n_field2", "value2");
}
});
map.put("obj", Map.of("field", "value", "field2", "value2"));
map.put("n_obj", Map.of("n_field", "value", "n_field2", "value2"));

Map<String, Object> filteredMap = XContentMapValues.filter(map, new String[] { "*.field2" }, Strings.EMPTY_ARRAY);
assertThat(filteredMap.size(), equalTo(1));
Expand Down Expand Up @@ -546,6 +516,11 @@ public void testDotsInFieldNames() {
assertEquals(expected, filtered);
}

/**
* Tests that we can extract paths containing non-ascii characters.
* See {@link AbstractFilteringTestCase#testFilterSupplementaryCharactersInPaths()}
* for a similar test but for XContent.
*/
public void testSupplementaryCharactersInPaths() {
Map<String, Object> map = new HashMap<>();
map.put("搜索", 2);
Expand All @@ -555,6 +530,11 @@ public void testSupplementaryCharactersInPaths() {
assertEquals(Collections.singletonMap("指数", 3), XContentMapValues.filter(map, new String[0], new String[] { "搜索" }));
}

/**
* Tests that we can extract paths which share a prefix with other paths.
* See {@link AbstractFilteringTestCase#testFilterSharedPrefixes()}
* for a similar test but for XContent.
*/
public void testSharedPrefixes() {
Map<String, Object> map = new HashMap<>();
map.put("foobar", 2);
Expand Down Expand Up @@ -633,6 +613,11 @@ public void testEmptyObjectsSubFieldsInclusion() {
}
}

/**
* Tests that we can extract paths which have another path as a prefix.
* See {@link AbstractFilteringTestCase#testFilterPrefix()}
* for a similar test but for XContent.
*/
public void testPrefix() {
Map<String, Object> map = new HashMap<>();
map.put("photos", Arrays.asList(new String[] { "foo", "bar" }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.FilterXContentParser;
Expand All @@ -21,15 +20,18 @@
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Set;

import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toSet;
import static org.hamcrest.Matchers.notNullValue;

/**
* Tests for {@link XContent} filtering.
Expand All @@ -46,17 +48,16 @@ protected interface Builder extends CheckedFunction<XContentBuilder, XContentBui

protected static Builder builderFor(String file) {
return builder -> {
try (
XContentParser parser = XContentType.JSON.xContent()
.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
AbstractFilteringTestCase.class.getResourceAsStream(file)
)
) {
// copyCurrentStructure does not property handle filters when it is passed a json parser. So we hide it.
return builder.copyCurrentStructure(new FilterXContentParser(parser) {
});
try (InputStream stream = AbstractFilteringTestCase.class.getResourceAsStream(file)) {
assertThat("Couldn't find [" + file + "]", stream, notNullValue());
try (
XContentParser parser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)
) {
// copyCurrentStructure does not property handle filters when it is passed a json parser. So we hide it.
return builder.copyCurrentStructure(new FilterXContentParser(parser) {
});
}
}
};
}
Expand Down Expand Up @@ -397,7 +398,7 @@ public void testBasics() throws Exception {
}

/**
* Generalization of {@link XContentMapValuesTests#testSupplementaryCharactersInPaths()}
* Tests that we can extract paths containing non-ascii characters.
*/
public void testFilterSupplementaryCharactersInPaths() throws IOException {
Builder sample = builder -> builder.startObject().field("搜索", 2).field("指数", 3).endObject();
Expand All @@ -410,7 +411,7 @@ public void testFilterSupplementaryCharactersInPaths() throws IOException {
}

/**
* Generalization of {@link XContentMapValuesTests#testSharedPrefixes()}
* Tests that we can extract paths which share a prefix with other paths.
*/
public void testFilterSharedPrefixes() throws IOException {
Builder sample = builder -> builder.startObject().field("foobar", 2).field("foobaz", 3).endObject();
Expand All @@ -423,7 +424,7 @@ public void testFilterSharedPrefixes() throws IOException {
}

/**
* Generalization of {@link XContentMapValuesTests#testPrefix()}
* Tests that we can extract paths which have another path as a prefix.
*/
public void testFilterPrefix() throws IOException {
Builder sample = builder -> builder.startObject().array("photos", "foo", "bar").field("photosCount", 2).endObject();
Expand All @@ -447,10 +448,12 @@ public void testManyFilters() throws IOException, URISyntaxException {
.endObject()
.endObject()
.endObject();
Set<String> manyFilters = Files.readAllLines(
PathUtils.get(AbstractFilteringTestCase.class.getResource("many_filters.txt").toURI()),
StandardCharsets.UTF_8
).stream().filter(s -> false == s.startsWith("#")).collect(toSet());
testFilter(deep, deep, manyFilters, emptySet());
try (InputStream stream = AbstractFilteringTestCase.class.getResourceAsStream("many_filters.txt")) {
assertThat("Couldn't find [many_filters.txt]", stream, notNullValue());
Set<String> manyFilters = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines()
.filter(s -> false == s.startsWith("#"))
.collect(toSet());
testFilter(deep, deep, manyFilters, emptySet());
}
}
}