20
20
21
21
import java .util .ArrayList ;
22
22
import java .util .Collections ;
23
+ import java .util .Date ;
23
24
import java .util .HashMap ;
24
25
import java .util .HashSet ;
25
26
import java .util .List ;
@@ -51,13 +52,13 @@ public void testAppendValuesToExistingList() throws Exception {
51
52
if (randomBoolean ()) {
52
53
Object value = scalar .randomValue ();
53
54
values .add (value );
54
- appendProcessor = createAppendProcessor (field , value , true );
55
+ appendProcessor = createAppendProcessor (field , value , null , true );
55
56
} else {
56
57
int valuesSize = randomIntBetween (0 , 10 );
57
58
for (int i = 0 ; i < valuesSize ; i ++) {
58
59
values .add (scalar .randomValue ());
59
60
}
60
- appendProcessor = createAppendProcessor (field , values , true );
61
+ appendProcessor = createAppendProcessor (field , values , null , true );
61
62
}
62
63
appendProcessor .execute (ingestDocument );
63
64
Object fieldValue = ingestDocument .getFieldValue (field , Object .class );
@@ -80,13 +81,13 @@ public void testAppendValuesToNonExistingList() throws Exception {
80
81
if (randomBoolean ()) {
81
82
Object value = scalar .randomValue ();
82
83
values .add (value );
83
- appendProcessor = createAppendProcessor (field , value , true );
84
+ appendProcessor = createAppendProcessor (field , value , null , true );
84
85
} else {
85
86
int valuesSize = randomIntBetween (0 , 10 );
86
87
for (int i = 0 ; i < valuesSize ; i ++) {
87
88
values .add (scalar .randomValue ());
88
89
}
89
- appendProcessor = createAppendProcessor (field , values , true );
90
+ appendProcessor = createAppendProcessor (field , values , null , true );
90
91
}
91
92
appendProcessor .execute (ingestDocument );
92
93
List <?> list = ingestDocument .getFieldValue (field , List .class );
@@ -104,13 +105,13 @@ public void testConvertScalarToList() throws Exception {
104
105
if (randomBoolean ()) {
105
106
Object value = scalar .randomValue ();
106
107
values .add (value );
107
- appendProcessor = createAppendProcessor (field , value , true );
108
+ appendProcessor = createAppendProcessor (field , value , null , true );
108
109
} else {
109
110
int valuesSize = randomIntBetween (0 , 10 );
110
111
for (int i = 0 ; i < valuesSize ; i ++) {
111
112
values .add (scalar .randomValue ());
112
113
}
113
- appendProcessor = createAppendProcessor (field , values , true );
114
+ appendProcessor = createAppendProcessor (field , values , null , true );
114
115
}
115
116
appendProcessor .execute (ingestDocument );
116
117
List <?> fieldValue = ingestDocument .getFieldValue (field , List .class );
@@ -128,7 +129,7 @@ public void testAppendingDuplicateValueToScalarDoesNotModifyDocument() throws Ex
128
129
129
130
List <Object > valuesToAppend = new ArrayList <>();
130
131
valuesToAppend .add (originalValue );
131
- Processor appendProcessor = createAppendProcessor (field , valuesToAppend , false );
132
+ Processor appendProcessor = createAppendProcessor (field , valuesToAppend , null , false );
132
133
appendProcessor .execute (ingestDocument );
133
134
Object fieldValue = ingestDocument .getFieldValue (field , Object .class );
134
135
assertThat (fieldValue , not (instanceOf (List .class )));
@@ -143,7 +144,7 @@ public void testAppendingUniqueValueToScalar() throws Exception {
143
144
List <Object > valuesToAppend = new ArrayList <>();
144
145
String newValue = randomValueOtherThan (originalValue , () -> randomAlphaOfLengthBetween (1 , 10 ));
145
146
valuesToAppend .add (newValue );
146
- Processor appendProcessor = createAppendProcessor (field , valuesToAppend , false );
147
+ Processor appendProcessor = createAppendProcessor (field , valuesToAppend , null , false );
147
148
appendProcessor .execute (ingestDocument );
148
149
List <?> list = ingestDocument .getFieldValue (field , List .class );
149
150
assertThat (list .size (), equalTo (2 ));
@@ -172,19 +173,149 @@ public void testAppendingToListWithDuplicatesDisallowed() throws Exception {
172
173
Collections .sort (valuesToAppend );
173
174
174
175
// attempt to append both new and existing values
175
- Processor appendProcessor = createAppendProcessor (originalField , valuesToAppend , false );
176
+ Processor appendProcessor = createAppendProcessor (originalField , valuesToAppend , null , false );
176
177
appendProcessor .execute (ingestDocument );
177
178
List <?> fieldValue = ingestDocument .getFieldValue (originalField , List .class );
178
179
assertThat (fieldValue , sameInstance (list ));
179
180
assertThat (fieldValue , containsInAnyOrder (expectedValues .toArray ()));
180
181
}
181
182
182
- private static Processor createAppendProcessor (String fieldName , Object fieldValue , boolean allowDuplicates ) {
183
+ public void testCopyFromOtherField () throws Exception {
184
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
185
+
186
+ // generate values, add some to a target field, the rest to a source field
187
+ int size = randomIntBetween (0 , 10 );
188
+ List <String > allValues = Stream .generate (() -> randomAlphaOfLengthBetween (1 , 10 )).limit (size ).collect (Collectors .toList ());
189
+ List <String > originalValues = randomSubsetOf (allValues );
190
+ List <String > additionalValues = new ArrayList <>(Sets .difference (new HashSet <>(allValues ), new HashSet <>(originalValues )));
191
+ List <String > targetFieldValue = new ArrayList <>(originalValues );
192
+ String targetField = RandomDocumentPicks .addRandomField (random (), ingestDocument , targetFieldValue );
193
+ String sourceField = RandomDocumentPicks .addRandomField (random (), ingestDocument , additionalValues );
194
+
195
+ Processor appendProcessor = createAppendProcessor (targetField , null , sourceField , false );
196
+ appendProcessor .execute (ingestDocument );
197
+ List <?> fieldValue = ingestDocument .getFieldValue (targetField , List .class );
198
+ assertThat (fieldValue , sameInstance (targetFieldValue ));
199
+ assertThat (fieldValue , containsInAnyOrder (allValues .toArray ()));
200
+ }
201
+
202
+ public void testCopyFromCopiesNonPrimitiveMutableTypes () throws Exception {
203
+ final String sourceField = "sourceField" ;
204
+ final String targetField = "targetField" ;
205
+ Processor processor = createAppendProcessor (targetField , null , sourceField , false );
206
+
207
+ // map types
208
+ Map <String , Object > document = new HashMap <>();
209
+ Map <String , Object > sourceMap = new HashMap <>();
210
+ sourceMap .put ("foo" , "bar" );
211
+ document .put (sourceField , sourceMap );
212
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
213
+ IngestDocument output = processor .execute (ingestDocument );
214
+ sourceMap .put ("foo" , "not-bar" );
215
+ Map <?, ?> outputMap = (Map <?, ?>) output .getFieldValue (targetField , List .class ).getFirst ();
216
+ assertThat (outputMap .get ("foo" ), equalTo ("bar" ));
217
+
218
+ // set types
219
+ document = new HashMap <>();
220
+ Set <String > sourceSet = randomUnique (() -> randomAlphaOfLength (5 ), 5 );
221
+ Set <String > preservedSet = new HashSet <>(sourceSet );
222
+ document .put (sourceField , sourceSet );
223
+ ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
224
+ processor .execute (ingestDocument );
225
+ sourceSet .add (randomValueOtherThanMany (sourceSet ::contains , () -> randomAlphaOfLength (5 )));
226
+ Set <?> outputSet = (Set <?>) ingestDocument .getFieldValue (targetField , List .class ).getFirst ();
227
+ assertThat (outputSet , equalTo (preservedSet ));
228
+
229
+ // list types (the outer list isn't used, but an inner list should be copied)
230
+ document = new HashMap <>();
231
+ List <String > sourceList = randomList (1 , 5 , () -> randomAlphaOfLength (5 ));
232
+ List <String > preservedList = new ArrayList <>(sourceList );
233
+ List <List <String >> wrappedSourceList = List .of (sourceList );
234
+ document .put (sourceField , wrappedSourceList );
235
+ ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
236
+ processor .execute (ingestDocument );
237
+ sourceList .add (randomValueOtherThanMany (sourceList ::contains , () -> randomAlphaOfLength (5 )));
238
+ List <?> unwrappedOutputList = (List <?>) ingestDocument .getFieldValue (targetField , List .class ).getFirst ();
239
+ assertThat (unwrappedOutputList , equalTo (preservedList ));
240
+
241
+ // byte[] types
242
+ document = new HashMap <>();
243
+ byte [] sourceBytes = randomByteArrayOfLength (10 );
244
+ byte [] preservedBytes = new byte [sourceBytes .length ];
245
+ System .arraycopy (sourceBytes , 0 , preservedBytes , 0 , sourceBytes .length );
246
+ document .put (sourceField , sourceBytes );
247
+ ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
248
+ processor .execute (ingestDocument );
249
+ sourceBytes [0 ] = sourceBytes [0 ] == 0 ? (byte ) 1 : (byte ) 0 ;
250
+ byte [] outputBytes = (byte []) ingestDocument .getFieldValue (targetField , List .class ).getFirst ();
251
+ assertThat (outputBytes , equalTo (preservedBytes ));
252
+
253
+ // Date types
254
+ document = new HashMap <>();
255
+ Date sourceDate = new Date ();
256
+ Date preservedDate = new Date (sourceDate .getTime ());
257
+ document .put (sourceField , sourceDate );
258
+ ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
259
+ processor .execute (ingestDocument );
260
+ sourceDate .setTime (sourceDate .getTime () + 1 );
261
+ Date outputDate = (Date ) ingestDocument .getFieldValue (targetField , List .class ).getFirst ();
262
+ assertThat (outputDate , equalTo (preservedDate ));
263
+ }
264
+
265
+ public void testCopyFromDeepCopiesNonPrimitiveMutableTypes () throws Exception {
266
+ final String sourceField = "sourceField" ;
267
+ final String targetField = "targetField" ;
268
+ Processor processor = createAppendProcessor (targetField , null , sourceField , false );
269
+ Map <String , Object > document = new HashMap <>();
270
+
271
+ // a root map with values of map, set, list, bytes, date
272
+ Map <String , Object > sourceMap = new HashMap <>();
273
+ sourceMap .put ("foo" , "bar" );
274
+ Set <String > sourceSet = randomUnique (() -> randomAlphaOfLength (5 ), 5 );
275
+ List <String > sourceList = randomList (1 , 5 , () -> randomAlphaOfLength (5 ));
276
+ byte [] sourceBytes = randomByteArrayOfLength (10 );
277
+ Date sourceDate = new Date ();
278
+ Map <String , Object > root = new HashMap <>();
279
+ root .put ("foo" , "bar" );
280
+ root .put ("map" , sourceMap );
281
+ root .put ("set" , sourceSet );
282
+ root .put ("list" , sourceList );
283
+ root .put ("bytes" , sourceBytes );
284
+ root .put ("date" , sourceDate );
285
+
286
+ Set <String > preservedSet = new HashSet <>(sourceSet );
287
+ List <String > preservedList = new ArrayList <>(sourceList );
288
+ byte [] preservedBytes = new byte [sourceBytes .length ];
289
+ System .arraycopy (sourceBytes , 0 , preservedBytes , 0 , sourceBytes .length );
290
+ Date preservedDate = new Date (sourceDate .getTime ());
291
+
292
+ document .put (sourceField , root );
293
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
294
+ IngestDocument output = processor .execute (ingestDocument );
295
+ Map <?, ?> outputRoot = (Map <?, ?>) output .getFieldValue (targetField , List .class ).getFirst ();
296
+
297
+ root .put ("foo" , "not-bar" );
298
+ sourceMap .put ("foo" , "not-bar" );
299
+ sourceSet .add (randomValueOtherThanMany (sourceSet ::contains , () -> randomAlphaOfLength (5 )));
300
+ sourceList .add (randomValueOtherThanMany (sourceList ::contains , () -> randomAlphaOfLength (5 )));
301
+ sourceBytes [0 ] = sourceBytes [0 ] == 0 ? (byte ) 1 : (byte ) 0 ;
302
+ sourceDate .setTime (sourceDate .getTime () + 1 );
303
+
304
+ assertThat (outputRoot .get ("foo" ), equalTo ("bar" ));
305
+ assertThat (((Map <?, ?>) outputRoot .get ("map" )).get ("foo" ), equalTo ("bar" ));
306
+ assertThat (((Set <?>) outputRoot .get ("set" )), equalTo (preservedSet ));
307
+ assertThat (((List <?>) outputRoot .get ("list" )), equalTo (preservedList ));
308
+ assertThat (((byte []) outputRoot .get ("bytes" )), equalTo (preservedBytes ));
309
+ assertThat (((Date ) outputRoot .get ("date" )), equalTo (preservedDate ));
310
+ }
311
+
312
+ private static Processor createAppendProcessor (String fieldName , Object fieldValue , String copyFrom , boolean allowDuplicates ) {
183
313
return new AppendProcessor (
184
314
randomAlphaOfLength (10 ),
185
315
null ,
186
316
new TestTemplateService .MockTemplateScript .Factory (fieldName ),
187
317
ValueSource .wrap (fieldValue , TestTemplateService .instance ()),
318
+ copyFrom ,
188
319
allowDuplicates
189
320
);
190
321
}
0 commit comments