Skip to content

Commit 44ea58f

Browse files
authored
[7.x] Move xcontent filtering tests (#79298) (#79389)
* Move xcontent filtering tests (#79298) * Move xcontent filtering tests Moves the xcontent filtering tests to the xcontent project because its testing code *in* the xcontent project. * More clear * Spotless * Fixup
1 parent 5050c97 commit 44ea58f

32 files changed

+60
-70
lines changed

build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/precommit/FormattingPrecommitPlugin.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ private Object[] getTargets(String projectPath) {
8989
return new String[] {
9090
"src/*/java/org/elasticsearch/action/admin/cluster/repositories/**/*.java",
9191
"src/*/java/org/elasticsearch/action/admin/cluster/snapshots/**/*.java",
92-
"src/test/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java",
9392
"src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java",
9493
"src/*/java/org/elasticsearch/index/snapshots/**/*.java",
9594
"src/*/java/org/elasticsearch/repositories/**/*.java",
9695
"src/*/java/org/elasticsearch/search/aggregations/**/*.java",
9796
"src/*/java/org/elasticsearch/snapshots/**/*.java" };
97+
} else if (projectPath.equals(":test:framework")) {
98+
return new String[] {
99+
"src/test/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java",
100+
};
98101
} else {
99102
// Normally this isn"t necessary, but we have Java sources in
100103
// non-standard places
@@ -195,7 +198,6 @@ private Object[] getTargets(String projectPath) {
195198
":test:fixtures:geoip-fixture",
196199
":test:fixtures:krb5kdc-fixture",
197200
":test:fixtures:old-elasticsearch",
198-
":test:framework",
199201
":test:logger-usage",
200202
":x-pack:docs",
201203
":x-pack:license-tools",
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.common.bytes.BytesReference;
13+
import org.elasticsearch.common.xcontent.support.AbstractFilteringTestCase;
1314
import org.elasticsearch.xcontent.DeprecationHandler;
1415
import org.elasticsearch.xcontent.NamedXContentRegistry;
1516
import org.elasticsearch.xcontent.XContent;
1617
import org.elasticsearch.xcontent.XContentBuilder;
1718
import org.elasticsearch.xcontent.XContentFactory;
1819
import org.elasticsearch.xcontent.XContentParser;
1920
import org.elasticsearch.xcontent.XContentType;
20-
import org.elasticsearch.common.xcontent.support.AbstractFilteringTestCase;
21-
import org.elasticsearch.xcontent.support.filtering.FilterPath;
2221

2322
import java.io.IOException;
2423
import java.util.Arrays;
@@ -142,6 +141,8 @@ static void assertXContentBuilderAsBytes(final XContentBuilder expected, final X
142141
assertThat(jsonParser.numberType(), equalTo(testParser.numberType()));
143142
assertThat(jsonParser.numberValue(), equalTo(testParser.numberValue()));
144143
break;
144+
default:
145+
break;
145146
}
146147
}
147148
} catch (Exception e) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.xcontent.XContentBuilder;
1212
import org.elasticsearch.xcontent.XContentType;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import com.fasterxml.jackson.core.JsonFactory;
1212
import com.fasterxml.jackson.core.JsonParser;
1313
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
14+
1415
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1516
import org.elasticsearch.test.ESTestCase;
16-
import org.elasticsearch.xcontent.support.filtering.FilterPathBasedFilter;
1717

1818
import java.util.Collections;
1919

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.test.ESTestCase;
12-
import org.elasticsearch.xcontent.support.filtering.FilterPath;
1312

1413
import java.util.Arrays;
1514
import java.util.LinkedHashSet;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.xcontent.XContentBuilder;
1212
import org.elasticsearch.xcontent.XContentType;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.xcontent.XContentBuilder;
1212
import org.elasticsearch.xcontent.XContentType;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.common.xcontent.support.filtering;
9+
package org.elasticsearch.xcontent.support.filtering;
1010

1111
import org.elasticsearch.xcontent.XContentBuilder;
1212
import org.elasticsearch.xcontent.XContentType;

server/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,7 @@ public void testPrefixedNamesFilteringTest() {
316316
public void testNestedFiltering() {
317317
Map<String, Object> map = new HashMap<>();
318318
map.put("field", "value");
319-
map.put("array", Arrays.asList(1, new HashMap<String, Object>() {
320-
{
321-
put("nested", 2);
322-
put("nested_2", 3);
323-
}
324-
}));
319+
map.put("array", Arrays.asList(1, map("nested", 2, "nested_2", 3)));
325320
Map<String, Object> filteredMap = XContentMapValues.filter(map, new String[] { "array.nested" }, Strings.EMPTY_ARRAY);
326321
assertThat(filteredMap.size(), equalTo(1));
327322

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

337332
map.clear();
338333
map.put("field", "value");
339-
map.put("obj", new HashMap<String, Object>() {
340-
{
341-
put("field", "value");
342-
put("field2", "value2");
343-
}
344-
});
334+
map.put("obj", map("field", "value", "field2", "value2"));
345335
filteredMap = XContentMapValues.filter(map, new String[] { "obj.field" }, Strings.EMPTY_ARRAY);
346336
assertThat(filteredMap.size(), equalTo(1));
347337
assertThat(((Map<String, Object>) filteredMap.get("obj")).size(), equalTo(1));
@@ -359,18 +349,8 @@ public void testNestedFiltering() {
359349
public void testCompleteObjectFiltering() {
360350
Map<String, Object> map = new HashMap<>();
361351
map.put("field", "value");
362-
map.put("obj", new HashMap<String, Object>() {
363-
{
364-
put("field", "value");
365-
put("field2", "value2");
366-
}
367-
});
368-
map.put("array", Arrays.asList(1, new HashMap<String, Object>() {
369-
{
370-
put("field", "value");
371-
put("field2", "value2");
372-
}
373-
}));
352+
map.put("obj", map("field", "value", "field2", "value2"));
353+
map.put("array", Arrays.asList(1, map("field", "value", "field2", "value2")));
374354

375355
Map<String, Object> filteredMap = XContentMapValues.filter(map, new String[] { "obj" }, Strings.EMPTY_ARRAY);
376356
assertThat(filteredMap.size(), equalTo(1));
@@ -401,18 +381,8 @@ public void testCompleteObjectFiltering() {
401381
public void testFilterIncludesUsingStarPrefix() {
402382
Map<String, Object> map = new HashMap<>();
403383
map.put("field", "value");
404-
map.put("obj", new HashMap<String, Object>() {
405-
{
406-
put("field", "value");
407-
put("field2", "value2");
408-
}
409-
});
410-
map.put("n_obj", new HashMap<String, Object>() {
411-
{
412-
put("n_field", "value");
413-
put("n_field2", "value2");
414-
}
415-
});
384+
map.put("obj", map("field", "value", "field2", "value2"));
385+
map.put("n_obj", map("n_field", "value", "n_field2", "value2"));
416386

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

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

533+
/**
534+
* Tests that we can extract paths which share a prefix with other paths.
535+
* See {@link AbstractFilteringTestCase#testFilterSharedPrefixes()}
536+
* for a similar test but for XContent.
537+
*/
558538
public void testSharedPrefixes() {
559539
Map<String, Object> map = new HashMap<>();
560540
map.put("foobar", 2);
@@ -633,6 +613,11 @@ public void testEmptyObjectsSubFieldsInclusion() {
633613
}
634614
}
635615

616+
/**
617+
* Tests that we can extract paths which have another path as a prefix.
618+
* See {@link AbstractFilteringTestCase#testFilterPrefix()}
619+
* for a similar test but for XContent.
620+
*/
636621
public void testPrefix() {
637622
Map<String, Object> map = new HashMap<>();
638623
map.put("photos", Arrays.asList(new String[] { "foo", "bar" }));

server/src/test/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java renamed to test/framework/src/main/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.common.bytes.BytesReference;
1212
import org.elasticsearch.common.util.set.Sets;
1313
import org.elasticsearch.core.CheckedFunction;
14-
import org.elasticsearch.core.PathUtils;
1514
import org.elasticsearch.test.ESTestCase;
1615
import org.elasticsearch.xcontent.DeprecationHandler;
1716
import org.elasticsearch.xcontent.FilterXContentParser;
@@ -21,15 +20,18 @@
2120
import org.elasticsearch.xcontent.XContentParser;
2221
import org.elasticsearch.xcontent.XContentType;
2322

23+
import java.io.BufferedReader;
2424
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.InputStreamReader;
2527
import java.net.URISyntaxException;
2628
import java.nio.charset.StandardCharsets;
27-
import java.nio.file.Files;
2829
import java.util.Set;
2930

3031
import static java.util.Collections.emptySet;
3132
import static java.util.Collections.singleton;
3233
import static java.util.stream.Collectors.toSet;
34+
import static org.hamcrest.Matchers.notNullValue;
3335

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

4749
protected static Builder builderFor(String file) {
4850
return builder -> {
49-
try (
50-
XContentParser parser = XContentType.JSON.xContent()
51-
.createParser(
52-
NamedXContentRegistry.EMPTY,
53-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
54-
AbstractFilteringTestCase.class.getResourceAsStream(file)
55-
)
56-
) {
57-
// copyCurrentStructure does not property handle filters when it is passed a json parser. So we hide it.
58-
return builder.copyCurrentStructure(new FilterXContentParser(parser) {
59-
});
51+
try (InputStream stream = AbstractFilteringTestCase.class.getResourceAsStream(file)) {
52+
assertThat("Couldn't find [" + file + "]", stream, notNullValue());
53+
try (
54+
XContentParser parser = XContentType.JSON.xContent()
55+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)
56+
) {
57+
// copyCurrentStructure does not property handle filters when it is passed a json parser. So we hide it.
58+
return builder.copyCurrentStructure(new FilterXContentParser(parser) {
59+
});
60+
}
6061
}
6162
};
6263
}
@@ -397,7 +398,7 @@ public void testBasics() throws Exception {
397398
}
398399

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

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

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

0 commit comments

Comments
 (0)