Skip to content

Commit 71100ff

Browse files
committed
Additional tests for issue elastic#4047
1 parent 7f27952 commit 71100ff

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,38 @@ public void testThatFilterIncludesObjectsWithSomeExcludedProperties() throws Exc
440440
assertThat(((Map<String, Object>) filteredSource.get("obj")), hasKey("f2"));
441441
}
442442

443+
@Test
444+
public void testThatFilterExcludesObjectWithWildcardPrefix() throws Exception {
445+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
446+
.startObject("obj")
447+
.field("f1", "v1")
448+
.endObject()
449+
.endObject();
450+
451+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
452+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"*.f1"});
453+
454+
assertThat(filteredSource.size(), equalTo(0));
455+
}
456+
457+
@Test
458+
public void testThatFilterExcludesFieldsWithWildcardPrefix() throws Exception {
459+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
460+
.startObject("obj")
461+
.field("f1", "v1")
462+
.field("f2", "v2")
463+
.endObject()
464+
.endObject();
465+
466+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
467+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"*.f1"});
468+
469+
assertThat(filteredSource.size(), equalTo(1));
470+
assertThat(filteredSource, hasKey("obj"));
471+
assertThat(((Map) filteredSource.get("obj")).size(), equalTo(1));
472+
assertThat(((Map<String, Object>) filteredSource.get("obj")), hasKey("f2"));
473+
}
474+
443475
@Test
444476
public void testThatFilterIncludesEmptyObjectWhenUsingIncludes() throws Exception {
445477
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
@@ -528,6 +560,25 @@ public void testFilterExcludesWithNestedObjects() throws Exception {
528560
assertThat(filteredSource.size(), equalTo(0));
529561
}
530562

563+
@Test
564+
public void testFilterExcludesNestedObjectAndKeepsParent() throws Exception {
565+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
566+
.startObject("obj1")
567+
.field("f1", "v1")
568+
.startObject("obj2")
569+
.endObject()
570+
.endObject()
571+
.endObject();
572+
573+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
574+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"*.obj2.*"});
575+
576+
assertThat(filteredSource.size(), equalTo(1));
577+
assertThat(filteredSource, hasKey("obj1"));
578+
assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(1));
579+
assertThat(((Map<String, Object>) filteredSource.get("obj1")), hasKey("f1"));
580+
}
581+
531582
@Test
532583
public void testFilterOmitsObjectWithNestedExcludedObjects() throws Exception {
533584
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()

0 commit comments

Comments
 (0)