26
26
import org .elasticsearch .common .xcontent .XContentHelper ;
27
27
import org .elasticsearch .common .xcontent .XContentType ;
28
28
import org .elasticsearch .test .ElasticsearchTestCase ;
29
+ import org .hamcrest .Matchers ;
29
30
import org .junit .Test ;
30
31
31
32
import java .util .Arrays ;
32
33
import java .util .HashMap ;
33
34
import java .util .List ;
34
35
import java .util .Map ;
35
36
36
- import static org .hamcrest .MatcherAssert .assertThat ;
37
37
import static org .hamcrest .Matchers .*;
38
38
import static org .hamcrest .core .IsEqual .equalTo ;
39
39
@@ -46,6 +46,7 @@ public void testFilter() throws Exception {
46
46
XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
47
47
.field ("test1" , "value1" )
48
48
.field ("test2" , "value2" )
49
+ .field ("something_else" , "value3" )
49
50
.endObject ();
50
51
51
52
Map <String , Object > source = XContentFactory .xContent (XContentType .JSON ).createParser (builder .string ()).mapAndClose ();
@@ -59,8 +60,9 @@ public void testFilter() throws Exception {
59
60
assertThat (filter .get ("test2" ).toString (), equalTo ("value2" ));
60
61
61
62
filter = XContentMapValues .filter (source , Strings .EMPTY_ARRAY , new String []{"test1" });
62
- assertThat (filter .size (), equalTo (1 ));
63
+ assertThat (filter .size (), equalTo (2 ));
63
64
assertThat (filter .get ("test2" ).toString (), equalTo ("value2" ));
65
+ assertThat (filter .get ("something_else" ).toString (), equalTo ("value3" ));
64
66
65
67
// more complex object...
66
68
builder = XContentFactory .jsonBuilder ().startObject ()
@@ -200,20 +202,6 @@ public void testExtractRawValue() throws Exception {
200
202
assertThat (XContentMapValues .extractRawValues ("path1.xxx.path2.yyy.test" , map ).get (0 ).toString (), equalTo ("value" ));
201
203
}
202
204
203
- @ Test
204
- public void testThatFilteringWithNestedArrayAndExclusionWorks () throws Exception {
205
- XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
206
- .startArray ("coordinates" )
207
- .startArray ().value ("foo" ).endArray ()
208
- .endArray ()
209
- .endObject ();
210
-
211
- Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
212
- Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), Strings .EMPTY_ARRAY , new String []{"nonExistingField" });
213
-
214
- assertThat (mapTuple .v2 (), equalTo (filteredSource ));
215
- }
216
-
217
205
@ Test
218
206
public void prefixedNamesFilteringTest () {
219
207
Map <String , Object > map = new HashMap <String , Object >();
@@ -368,4 +356,101 @@ public void filterWithEmptyIncludesExcludes() {
368
356
assertThat (filteredMap .get ("field" ).toString (), equalTo ("value" ));
369
357
370
358
}
359
+
360
+ @ SuppressWarnings ({"unchecked" })
361
+ @ Test
362
+ public void testThatFilterIncludesEmptyObjectWhenUsingIncludes () throws Exception {
363
+ XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
364
+ .startObject ("obj" )
365
+ .endObject ()
366
+ .endObject ();
367
+
368
+ Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
369
+ Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), new String []{"obj" }, Strings .EMPTY_ARRAY );
370
+
371
+ assertThat (mapTuple .v2 (), equalTo (filteredSource ));
372
+ }
373
+
374
+ @ Test
375
+ public void testThatFilterIncludesEmptyObjectWhenUsingExcludes () throws Exception {
376
+ XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
377
+ .startObject ("obj" )
378
+ .endObject ()
379
+ .endObject ();
380
+
381
+ Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
382
+ Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), Strings .EMPTY_ARRAY , new String []{"nonExistingField" });
383
+
384
+ assertThat (mapTuple .v2 (), equalTo (filteredSource ));
385
+ }
386
+
387
+ @ Test
388
+ public void testNotOmittingObjectsWithExcludedProperties () throws Exception {
389
+ XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
390
+ .startObject ("obj" )
391
+ .field ("f1" , "v1" )
392
+ .endObject ()
393
+ .endObject ();
394
+
395
+ Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
396
+ Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), Strings .EMPTY_ARRAY , new String []{"obj.f1" });
397
+
398
+ assertThat (filteredSource .size (), equalTo (1 ));
399
+ assertThat (filteredSource , hasKey ("obj" ));
400
+ assertThat (((Map ) filteredSource .get ("obj" )).size (), equalTo (0 ));
401
+ }
402
+
403
+ @ SuppressWarnings ({"unchecked" })
404
+ @ Test
405
+ public void testNotOmittingObjectWithNestedExcludedObject () throws Exception {
406
+ XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
407
+ .startObject ("obj1" )
408
+ .startObject ("obj2" )
409
+ .startObject ("obj3" )
410
+ .endObject ()
411
+ .endObject ()
412
+ .endObject ()
413
+ .endObject ();
414
+
415
+ // implicit include
416
+ Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
417
+ Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), Strings .EMPTY_ARRAY , new String []{"*.obj2" });
418
+
419
+ assertThat (filteredSource .size (), equalTo (1 ));
420
+ assertThat (filteredSource , hasKey ("obj1" ));
421
+ assertThat (((Map ) filteredSource .get ("obj1" )).size (), Matchers .equalTo (0 ));
422
+
423
+ // explicit include
424
+ filteredSource = XContentMapValues .filter (mapTuple .v2 (), new String []{"obj1" }, new String []{"*.obj2" });
425
+ assertThat (filteredSource .size (), equalTo (1 ));
426
+ assertThat (filteredSource , hasKey ("obj1" ));
427
+ assertThat (((Map ) filteredSource .get ("obj1" )).size (), Matchers .equalTo (0 ));
428
+
429
+ // wild card include
430
+ filteredSource = XContentMapValues .filter (mapTuple .v2 (), new String []{"*.obj2" }, new String []{"*.obj3" });
431
+ assertThat (filteredSource .size (), equalTo (1 ));
432
+ assertThat (filteredSource , hasKey ("obj1" ));
433
+ assertThat (((Map <String , Object >) filteredSource .get ("obj1" )), hasKey ("obj2" ));
434
+ assertThat (((Map ) ((Map ) filteredSource .get ("obj1" )).get ("obj2" )).size (), Matchers .equalTo (0 ));
435
+ }
436
+
437
+ @ SuppressWarnings ({"unchecked" })
438
+ @ Test
439
+ public void testIncludingObjectWithNestedIncludedObject () throws Exception {
440
+ XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ()
441
+ .startObject ("obj1" )
442
+ .startObject ("obj2" )
443
+ .endObject ()
444
+ .endObject ()
445
+ .endObject ();
446
+
447
+ Tuple <XContentType , Map <String , Object >> mapTuple = XContentHelper .convertToMap (builder .bytes (), true );
448
+ Map <String , Object > filteredSource = XContentMapValues .filter (mapTuple .v2 (), new String []{"*.obj2" }, Strings .EMPTY_ARRAY );
449
+
450
+ assertThat (filteredSource .size (), equalTo (1 ));
451
+ assertThat (filteredSource , hasKey ("obj1" ));
452
+ assertThat (((Map ) filteredSource .get ("obj1" )).size (), equalTo (1 ));
453
+ assertThat (((Map <String , Object >) filteredSource .get ("obj1" )), hasKey ("obj2" ));
454
+ assertThat (((Map ) ((Map ) filteredSource .get ("obj1" )).get ("obj2" )).size (), equalTo (0 ));
455
+ }
371
456
}
0 commit comments