1616import org .elasticsearch .script .TemplateScript ;
1717import org .elasticsearch .test .ESTestCase ;
1818
19- import java .util .ArrayList ;
19+ import java .util .Arrays ;
2020import java .util .HashMap ;
2121import java .util .List ;
2222import java .util .Map ;
2323
24+ import static org .elasticsearch .ingest .common .RemoveProcessor .shouldKeep ;
2425import static org .hamcrest .Matchers .containsString ;
2526import static org .hamcrest .Matchers .equalTo ;
2627
@@ -76,24 +77,19 @@ public void testKeepFields() throws Exception {
7677 source .put ("age" , 55 );
7778 source .put ("address" , address );
7879
79- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), source );
80-
81- List <TemplateScript .Factory > fieldsToKeep = List .of (
82- new TestTemplateService .MockTemplateScript .Factory ("name" ),
83- new TestTemplateService .MockTemplateScript .Factory ("address.street" )
84- );
85-
86- 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" ));
80+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), source );
81+
82+ Processor processor = new RemoveProcessor (null , null , List .of (), templates ("name" , "address.street" ), false );
83+ processor .execute (document );
84+ assertTrue (document .hasField ("name" ));
85+ assertTrue (document .hasField ("address" ));
86+ assertTrue (document .hasField ("address.street" ));
87+ assertFalse (document .hasField ("age" ));
88+ assertFalse (document .hasField ("address.number" ));
89+ assertTrue (document .hasField ("_index" ));
90+ assertTrue (document .hasField ("_version" ));
91+ assertTrue (document .hasField ("_id" ));
92+ assertTrue (document .hasField ("_version_type" ));
9793 }
9894
9995 public void testShouldKeep (String a , String b ) {
@@ -105,57 +101,28 @@ public void testShouldKeep(String a, String b) {
105101 source .put ("name" , "eric clapton" );
106102 source .put ("address" , address );
107103
108- IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), source );
104+ IngestDocument document = RandomDocumentPicks .randomIngestDocument (random (), source );
109105
110- assertTrue (RemoveProcessor . shouldKeep ("name" , List . of ( new TestTemplateService . MockTemplateScript . Factory ( "name" )), ingestDocument ));
106+ assertTrue (shouldKeep ("name" , templates ( "name" ), document ));
111107
112- assertTrue (RemoveProcessor . shouldKeep ("age" , List . of ( new TestTemplateService . MockTemplateScript . Factory ( "age" )), ingestDocument ));
108+ assertTrue (shouldKeep ("age" , templates ( "age" ), document ));
113109
114- assertFalse (RemoveProcessor . shouldKeep ("name" , List . of ( new TestTemplateService . MockTemplateScript . Factory ( "age" )), ingestDocument ));
110+ assertFalse (shouldKeep ("name" , templates ( "age" ), document ));
115111
116- assertTrue (
117- RemoveProcessor .shouldKeep (
118- "address" ,
119- List .of (new TestTemplateService .MockTemplateScript .Factory ("address.street" )),
120- ingestDocument
121- )
122- );
112+ assertTrue (shouldKeep ("address" , templates ("address.street" ), document ));
123113
124- assertTrue (
125- RemoveProcessor .shouldKeep (
126- "address" ,
127- List .of (new TestTemplateService .MockTemplateScript .Factory ("address.number" )),
128- ingestDocument
129- )
130- );
114+ assertTrue (shouldKeep ("address" , templates ("address.number" ), document ));
131115
132- assertTrue (
133- RemoveProcessor .shouldKeep (
134- "address.street" ,
135- List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )),
136- ingestDocument
137- )
138- );
116+ assertTrue (shouldKeep ("address.street" , templates ("address" ), document ));
139117
140- assertTrue (
141- RemoveProcessor .shouldKeep (
142- "address.number" ,
143- List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )),
144- ingestDocument
145- )
146- );
118+ assertTrue (shouldKeep ("address.number" , templates ("address" ), document ));
147119
148- assertTrue (
149- RemoveProcessor .shouldKeep ("address" , List .of (new TestTemplateService .MockTemplateScript .Factory ("address" )), ingestDocument )
150- );
120+ assertTrue (shouldKeep ("address" , templates ("address" ), document ));
151121
152- assertFalse (
153- RemoveProcessor .shouldKeep (
154- "address.street" ,
155- List .of (new TestTemplateService .MockTemplateScript .Factory ("address.number" )),
156- ingestDocument
157- )
158- );
122+ assertFalse (shouldKeep ("address.street" , templates ("address.number" ), document ));
159123 }
160124
125+ private static List <TemplateScript .Factory > templates (String ... fields ) {
126+ return Arrays .stream (fields ).map (f -> (TemplateScript .Factory ) new TestTemplateService .MockTemplateScript .Factory (f )).toList ();
127+ }
161128}
0 commit comments