22
22
import org .elasticsearch .ingest .IngestDocument ;
23
23
import org .elasticsearch .ingest .Processor ;
24
24
import org .elasticsearch .ingest .RandomDocumentPicks ;
25
+ import org .elasticsearch .ingest .TestTemplateService ;
25
26
import org .elasticsearch .test .ESTestCase ;
26
27
27
28
import java .util .ArrayList ;
@@ -45,7 +46,7 @@ public void testRename() throws Exception {
45
46
do {
46
47
newFieldName = RandomDocumentPicks .randomFieldName (random ());
47
48
} 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 );
49
50
processor .execute (ingestDocument );
50
51
assertThat (ingestDocument .getFieldValue (newFieldName , Object .class ), equalTo (fieldValue ));
51
52
}
@@ -63,7 +64,7 @@ public void testRenameArrayElement() throws Exception {
63
64
document .put ("one" , one );
64
65
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
65
66
66
- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list.0" , "item" , false );
67
+ Processor processor = createRenameProcessor ( "list.0" , "item" , false );
67
68
processor .execute (ingestDocument );
68
69
Object actualObject = ingestDocument .getSourceAndMetadata ().get ("list" );
69
70
assertThat (actualObject , instanceOf (List .class ));
@@ -76,7 +77,7 @@ public void testRenameArrayElement() throws Exception {
76
77
assertThat (actualObject , instanceOf (String .class ));
77
78
assertThat (actualObject , equalTo ("item1" ));
78
79
79
- processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), "list.0" , "list.3" , false );
80
+ processor = createRenameProcessor ( "list.0" , "list.3" , false );
80
81
try {
81
82
processor .execute (ingestDocument );
82
83
fail ("processor execute should have failed" );
@@ -91,7 +92,7 @@ public void testRenameArrayElement() throws Exception {
91
92
public void testRenameNonExistingField () throws Exception {
92
93
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
93
94
String fieldName = RandomDocumentPicks .randomFieldName (random ());
94
- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName ,
95
+ Processor processor = createRenameProcessor ( fieldName ,
95
96
RandomDocumentPicks .randomFieldName (random ()), false );
96
97
try {
97
98
processor .execute (ingestDocument );
@@ -105,7 +106,7 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception {
105
106
IngestDocument originalIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
106
107
IngestDocument ingestDocument = new IngestDocument (originalIngestDocument );
107
108
String fieldName = RandomDocumentPicks .randomFieldName (random ());
108
- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), fieldName ,
109
+ Processor processor = createRenameProcessor ( fieldName ,
109
110
RandomDocumentPicks .randomFieldName (random ()), true );
110
111
processor .execute (ingestDocument );
111
112
assertIngestDocument (originalIngestDocument , ingestDocument );
@@ -114,7 +115,7 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception {
114
115
public void testRenameNewFieldAlreadyExists () throws Exception {
115
116
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
116
117
String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
117
- Processor processor = new RenameProcessor ( randomAlphaOfLength ( 10 ), RandomDocumentPicks .randomExistingFieldName (
118
+ Processor processor = createRenameProcessor ( RandomDocumentPicks .randomExistingFieldName (
118
119
random (), ingestDocument ), fieldName , false );
119
120
try {
120
121
processor .execute (ingestDocument );
@@ -129,7 +130,7 @@ public void testRenameExistingFieldNullValue() throws Exception {
129
130
String fieldName = RandomDocumentPicks .randomFieldName (random ());
130
131
ingestDocument .setFieldValue (fieldName , null );
131
132
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 );
133
134
processor .execute (ingestDocument );
134
135
assertThat (ingestDocument .hasField (fieldName ), equalTo (false ));
135
136
assertThat (ingestDocument .hasField (newFieldName ), equalTo (true ));
@@ -149,7 +150,7 @@ public Object put(String key, Object value) {
149
150
source .put ("list" , Collections .singletonList ("item" ));
150
151
151
152
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 );
153
154
try {
154
155
processor .execute (ingestDocument );
155
156
fail ("processor execute should have failed" );
@@ -173,7 +174,7 @@ public Object remove(Object key) {
173
174
source .put ("list" , Collections .singletonList ("item" ));
174
175
175
176
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 );
177
178
try {
178
179
processor .execute (ingestDocument );
179
180
fail ("processor execute should have failed" );
@@ -188,22 +189,26 @@ public void testRenameLeafIntoBranch() throws Exception {
188
189
Map <String , Object > source = new HashMap <>();
189
190
source .put ("foo" , "bar" );
190
191
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 );
192
193
processor1 .execute (ingestDocument );
193
194
assertThat (ingestDocument .getFieldValue ("foo" , Map .class ), equalTo (Collections .singletonMap ("bar" , "bar" )));
194
195
assertThat (ingestDocument .getFieldValue ("foo.bar" , String .class ), equalTo ("bar" ));
195
196
196
- Processor processor2 = new RenameProcessor ( randomAlphaOfLength ( 10 ), "foo.bar" , "foo.bar.baz" , false );
197
+ Processor processor2 = createRenameProcessor ( "foo.bar" , "foo.bar.baz" , false );
197
198
processor2 .execute (ingestDocument );
198
199
assertThat (ingestDocument .getFieldValue ("foo" , Map .class ), equalTo (Collections .singletonMap ("bar" ,
199
200
Collections .singletonMap ("baz" , "bar" ))));
200
201
assertThat (ingestDocument .getFieldValue ("foo.bar" , Map .class ), equalTo (Collections .singletonMap ("baz" , "bar" )));
201
202
assertThat (ingestDocument .getFieldValue ("foo.bar.baz" , String .class ), equalTo ("bar" ));
202
203
203
204
// 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 );
205
206
Exception e = expectThrows (IllegalArgumentException .class , () -> processor3 .execute (ingestDocument ));
206
207
assertThat (e .getMessage (), equalTo ("field [foo] already exists" ));
207
208
}
208
209
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
+ }
209
214
}
0 commit comments