2222import org .elasticsearch .ingest .IngestDocument ;
2323import org .elasticsearch .ingest .Processor ;
2424import org .elasticsearch .ingest .RandomDocumentPicks ;
25+ import org .elasticsearch .ingest .TestTemplateService ;
2526import org .elasticsearch .test .ESTestCase ;
2627
2728import java .util .ArrayList ;
@@ -45,7 +46,7 @@ public void testRename() throws Exception {
4546 do {
4647 newFieldName = RandomDocumentPicks .randomFieldName (random ());
4748 } while (RandomDocumentPicks .canAddField (newFieldName , ingestDocument ) == false || newFieldName .equals (fieldName ));
48- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName , newFieldName , false );
49+ Processor processor = createRenameProcessor ( fieldName , newFieldName , false );
4950 processor .execute (ingestDocument );
5051 assertThat (ingestDocument .getFieldValue (newFieldName , Object .class ), equalTo (fieldValue ));
5152 }
@@ -63,7 +64,7 @@ public void testRenameArrayElement() throws Exception {
6364 document .put ("one" , one );
6465 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
6566
66- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list.0" , "item" , false );
67+ Processor processor = createRenameProcessor ( "list.0" , "item" , false );
6768 processor .execute (ingestDocument );
6869 Object actualObject = ingestDocument .getSourceAndMetadata ().get ("list" );
6970 assertThat (actualObject , instanceOf (List .class ));
@@ -76,7 +77,7 @@ public void testRenameArrayElement() throws Exception {
7677 assertThat (actualObject , instanceOf (String .class ));
7778 assertThat (actualObject , equalTo ("item1" ));
7879
79- processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list.0" , "list.3" , false );
80+ processor = createRenameProcessor ( "list.0" , "list.3" , false );
8081 try {
8182 processor .execute (ingestDocument );
8283 fail ("processor execute should have failed" );
@@ -91,7 +92,7 @@ public void testRenameArrayElement() throws Exception {
9192 public void testRenameNonExistingField () throws Exception {
9293 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
9394 String fieldName = RandomDocumentPicks .randomFieldName (random ());
94- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName ,
95+ Processor processor = createRenameProcessor ( fieldName ,
9596 RandomDocumentPicks .randomFieldName (random ()), false );
9697 try {
9798 processor .execute (ingestDocument );
@@ -105,7 +106,7 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception {
105106 IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
106107 IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
107108 String fieldName = RandomDocumentPicks .randomFieldName (random ());
108- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName ,
109+ Processor processor = createRenameProcessor ( fieldName ,
109110 RandomDocumentPicks .randomFieldName (random ()), true );
110111 processor .execute (ingestDocument );
111112 assertIngestDocument (originalIngestDocument , ingestDocument );
@@ -114,7 +115,7 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception {
114115 public void testRenameNewFieldAlreadyExists () throws Exception {
115116 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
116117 String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
117- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), RandomDocumentPicks .randomExistingFieldName (
118+ Processor processor = createRenameProcessor ( RandomDocumentPicks .randomExistingFieldName (
118119 random (), ingestDocument ), fieldName , false );
119120 try {
120121 processor .execute (ingestDocument );
@@ -129,7 +130,7 @@ public void testRenameExistingFieldNullValue() throws Exception {
129130 String fieldName = RandomDocumentPicks .randomFieldName (random ());
130131 ingestDocument .setFieldValue (fieldName , null );
131132 String newFieldName = randomValueOtherThanMany (ingestDocument ::hasField , () -> RandomDocumentPicks .randomFieldName (random ()));
132- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName , newFieldName , false );
133+ Processor processor = createRenameProcessor ( fieldName , newFieldName , false );
133134 processor .execute (ingestDocument );
134135 assertThat (ingestDocument .hasField (fieldName ), equalTo (false ));
135136 assertThat (ingestDocument .hasField (newFieldName ), equalTo (true ));
@@ -149,7 +150,7 @@ public Object put(String key, Object value) {
149150 source .put ("list" , Collections .singletonList ("item" ));
150151
151152 IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
152- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list" , "new_field" , false );
153+ Processor processor = createRenameProcessor ( "list" , "new_field" , false );
153154 try {
154155 processor .execute (ingestDocument );
155156 fail ("processor execute should have failed" );
@@ -173,7 +174,7 @@ public Object remove(Object key) {
173174 source .put ("list" , Collections .singletonList ("item" ));
174175
175176 IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
176- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list" , "new_field" , false );
177+ Processor processor = createRenameProcessor ( "list" , "new_field" , false );
177178 try {
178179 processor .execute (ingestDocument );
179180 fail ("processor execute should have failed" );
@@ -188,22 +189,26 @@ public void testRenameLeafIntoBranch() throws Exception {
188189 Map <String , Object > source = new HashMap <>();
189190 source .put ("foo" , "bar" );
190191 IngestDocument ingestDocument = new IngestDocument (source , Collections .emptyMap ());
191- Processor processor1 = new RenameProcessor ( randomAlphaOfLength ( 10 ), "foo" , "foo.bar" , false );
192+ Processor processor1 = createRenameProcessor ( "foo" , "foo.bar" , false );
192193 processor1 .execute (ingestDocument );
193194 assertThat (ingestDocument .getFieldValue ("foo" , Map .class ), equalTo (Collections .singletonMap ("bar" , "bar" )));
194195 assertThat (ingestDocument .getFieldValue ("foo.bar" , String .class ), equalTo ("bar" ));
195196
196- Processor processor2 = new RenameProcessor ( randomAlphaOfLength ( 10 ), "foo.bar" , "foo.bar.baz" , false );
197+ Processor processor2 = createRenameProcessor ( "foo.bar" , "foo.bar.baz" , false );
197198 processor2 .execute (ingestDocument );
198199 assertThat (ingestDocument .getFieldValue ("foo" , Map .class ), equalTo (Collections .singletonMap ("bar" ,
199200 Collections .singletonMap ("baz" , "bar" ))));
200201 assertThat (ingestDocument .getFieldValue ("foo.bar" , Map .class ), equalTo (Collections .singletonMap ("baz" , "bar" )));
201202 assertThat (ingestDocument .getFieldValue ("foo.bar.baz" , String .class ), equalTo ("bar" ));
202203
203204 // for fun lets try to restore it (which don't allow today)
204- Processor processor3 = new RenameProcessor ( randomAlphaOfLength ( 10 ), "foo.bar.baz" , "foo" , false );
205+ Processor processor3 = createRenameProcessor ( "foo.bar.baz" , "foo" , false );
205206 Exception e = expectThrows (IllegalArgumentException .class , () -> processor3 .execute (ingestDocument ));
206207 assertThat (e .getMessage (), equalTo ("field [foo] already exists" ));
207208 }
208209
210+ private RenameProcessor createRenameProcessor (String field , String targetField , boolean ignoreMissing ) {
211+ return new RenameProcessor (randomAlphaOfLength (10 ), new TestTemplateService .MockTemplateScript .Factory (field ),
212+ new TestTemplateService .MockTemplateScript .Factory (targetField ), ignoreMissing );
213+ }
209214}
0 commit comments