22
22
import java .util .Map ;
23
23
24
24
import static org .hamcrest .Matchers .containsString ;
25
- import static org .hamcrest .Matchers .equalTo ;
25
+ import static org .hamcrest .Matchers .is ;
26
26
27
27
public class RemoveProcessorTests extends ESTestCase {
28
28
29
29
public void testRemoveFields () throws Exception {
30
- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
31
- String field = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
30
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random ());
31
+ String field = RandomDocumentPicks .randomExistingFieldName (random (), document );
32
32
Processor processor = new RemoveProcessor (
33
33
randomAlphaOfLength (10 ),
34
34
null ,
35
35
List .of (new TestTemplateService .MockTemplateScript .Factory (field )),
36
36
List .of (),
37
37
false
38
38
);
39
- processor .execute (ingestDocument );
40
- assertThat (ingestDocument .hasField (field ), equalTo (false ));
39
+ processor .execute (document );
40
+ assertThat (document .hasField (field ), is (false ));
41
41
}
42
42
43
43
public void testRemoveNonExistingField () throws Exception {
44
- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
44
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
45
45
String fieldName = RandomDocumentPicks .randomFieldName (random ());
46
46
Map <String , Object > config = new HashMap <>();
47
47
config .put ("field" , fieldName );
48
- String processorTag = randomAlphaOfLength (10 );
49
- Processor processor = new RemoveProcessor .Factory (TestTemplateService .instance ()).create (null , processorTag , null , config , null );
48
+ String tag = randomAlphaOfLength (10 );
49
+ Processor processor = new RemoveProcessor .Factory (TestTemplateService .instance ()).create (null , tag , null , config , null );
50
50
try {
51
- processor .execute (ingestDocument );
51
+ processor .execute (document );
52
52
fail ("remove field should have failed" );
53
53
} catch (IllegalArgumentException e ) {
54
54
assertThat (e .getMessage (), containsString ("not present as part of path [" + fieldName + "]" ));
55
55
}
56
56
}
57
57
58
58
public void testIgnoreMissing () throws Exception {
59
- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
59
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
60
60
String fieldName = RandomDocumentPicks .randomFieldName (random ());
61
61
Map <String , Object > config = new HashMap <>();
62
62
config .put ("field" , fieldName );
63
63
config .put ("ignore_missing" , true );
64
- String processorTag = randomAlphaOfLength (10 );
65
- Processor processor = new RemoveProcessor .Factory (TestTemplateService .instance ()).create (null , processorTag , null , config , null );
66
- processor .execute (ingestDocument );
64
+ String tag = randomAlphaOfLength (10 );
65
+ Processor processor = new RemoveProcessor .Factory (TestTemplateService .instance ()).create (null , tag , null , config , null );
66
+ processor .execute (document );
67
+ }
68
+
69
+ public void testIgnoreMissingAndNullInPath () throws Exception {
70
+ Map <String , Object > source = new HashMap <>();
71
+ Map <String , Object > some = new HashMap <>();
72
+ Map <String , Object > map = new HashMap <>();
73
+ Map <String , Object > path = new HashMap <>();
74
+
75
+ switch (randomIntBetween (0 , 6 )) {
76
+ case 0 -> {
77
+ // empty source
78
+ }
79
+ case 1 -> {
80
+ source .put ("some" , null );
81
+ }
82
+ case 2 -> {
83
+ some .put ("map" , null );
84
+ source .put ("some" , some );
85
+ }
86
+ case 3 -> {
87
+ some .put ("map" , map );
88
+ source .put ("some" , some );
89
+ }
90
+ case 4 -> {
91
+ map .put ("path" , null );
92
+ some .put ("map" , map );
93
+ source .put ("some" , some );
94
+ }
95
+ case 5 -> {
96
+ map .put ("path" , path );
97
+ some .put ("map" , map );
98
+ source .put ("some" , some );
99
+ }
100
+ case 6 -> {
101
+ map .put ("path" , "foobar" );
102
+ some .put ("map" , map );
103
+ source .put ("some" , some );
104
+ }
105
+ }
106
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), source );
107
+ Map <String , Object > config = new HashMap <>();
108
+ config .put ("field" , "some.map.path" );
109
+ config .put ("ignore_missing" , true );
110
+ Processor processor = new RemoveProcessor .Factory (TestTemplateService .instance ()).create (null , null , null , config , null );
111
+ processor .execute (document );
112
+ assertThat (document .hasField ("some.map.path" ), is (false ));
67
113
}
68
114
69
115
public void testKeepFields () throws Exception {
@@ -76,24 +122,24 @@ public void testKeepFields() throws Exception {
76
122
source .put ("age" , 55 );
77
123
source .put ("address" , address );
78
124
79
- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), source );
125
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), source );
80
126
81
127
List <TemplateScript .Factory > fieldsToKeep = List .of (
82
128
new TestTemplateService .MockTemplateScript .Factory ("name" ),
83
129
new TestTemplateService .MockTemplateScript .Factory ("address.street" )
84
130
);
85
131
86
132
Processor processor = new RemoveProcessor (randomAlphaOfLength (10 ), null , new ArrayList <>(), fieldsToKeep , false );
87
- processor .execute (ingestDocument );
88
- assertTrue (ingestDocument .hasField ("name" ));
89
- assertTrue (ingestDocument .hasField ("address" ));
90
- assertTrue (ingestDocument .hasField ("address.street" ));
91
- assertFalse (ingestDocument .hasField ("age" ));
92
- assertFalse (ingestDocument .hasField ("address.number" ));
93
- assertTrue (ingestDocument .hasField ("_index" ));
94
- assertTrue (ingestDocument .hasField ("_version" ));
95
- assertTrue (ingestDocument .hasField ("_id" ));
96
- assertTrue (ingestDocument .hasField ("_version_type" ));
133
+ processor .execute (document );
134
+ assertTrue (document .hasField ("name" ));
135
+ assertTrue (document .hasField ("address" ));
136
+ assertTrue (document .hasField ("address.street" ));
137
+ assertFalse (document .hasField ("age" ));
138
+ assertFalse (document .hasField ("address.number" ));
139
+ assertTrue (document .hasField ("_index" ));
140
+ assertTrue (document .hasField ("_version" ));
141
+ assertTrue (document .hasField ("_id" ));
142
+ assertTrue (document .hasField ("_version_type" ));
97
143
}
98
144
99
145
public void testShouldKeep (String a , String b ) {
@@ -105,55 +151,37 @@ public void testShouldKeep(String a, String b) {
105
151
source .put ("name" , "eric clapton" );
106
152
source .put ("address" , address );
107
153
108
- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), source );
154
+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), source );
109
155
110
- assertTrue (RemoveProcessor .shouldKeep ("name" , List .of (new TestTemplateService .MockTemplateScript .Factory ("name" )), ingestDocument ));
156
+ assertTrue (RemoveProcessor .shouldKeep ("name" , List .of (new TestTemplateService .MockTemplateScript .Factory ("name" )), document ));
111
157
112
- assertTrue (RemoveProcessor .shouldKeep ("age" , List .of (new TestTemplateService .MockTemplateScript .Factory ("age" )), ingestDocument ));
158
+ assertTrue (RemoveProcessor .shouldKeep ("age" , List .of (new TestTemplateService .MockTemplateScript .Factory ("age" )), document ));
113
159
114
- assertFalse (RemoveProcessor .shouldKeep ("name" , List .of (new TestTemplateService .MockTemplateScript .Factory ("age" )), ingestDocument ));
160
+ assertFalse (RemoveProcessor .shouldKeep ("name" , List .of (new TestTemplateService .MockTemplateScript .Factory ("age" )), document ));
115
161
116
162
assertTrue (
117
- RemoveProcessor .shouldKeep (
118
- "address" ,
119
- List .of (new TestTemplateService .MockTemplateScript .Factory ("address.street" )),
120
- ingestDocument
121
- )
163
+ RemoveProcessor .shouldKeep ("address" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address.street" )), document )
122
164
);
123
165
124
166
assertTrue (
125
- RemoveProcessor .shouldKeep (
126
- "address" ,
127
- List .of (new TestTemplateService .MockTemplateScript .Factory ("address.number" )),
128
- ingestDocument
129
- )
167
+ RemoveProcessor .shouldKeep ("address" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address.number" )), document )
130
168
);
131
169
132
170
assertTrue (
133
- RemoveProcessor .shouldKeep (
134
- "address.street" ,
135
- List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )),
136
- ingestDocument
137
- )
171
+ RemoveProcessor .shouldKeep ("address.street" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )), document )
138
172
);
139
173
140
174
assertTrue (
141
- RemoveProcessor .shouldKeep (
142
- "address.number" ,
143
- List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )),
144
- ingestDocument
145
- )
175
+ RemoveProcessor .shouldKeep ("address.number" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )), document )
146
176
);
147
177
148
- assertTrue (
149
- RemoveProcessor .shouldKeep ("address" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )), ingestDocument )
150
- );
178
+ assertTrue (RemoveProcessor .shouldKeep ("address" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )), document ));
151
179
152
180
assertFalse (
153
181
RemoveProcessor .shouldKeep (
154
182
"address.street" ,
155
183
List .of (new TestTemplateService .MockTemplateScript .Factory ("address.number" )),
156
- ingestDocument
184
+ document
157
185
)
158
186
);
159
187
}
0 commit comments