28
28
import org .elasticsearch .test .ESTestCase ;
29
29
30
30
import java .util .ArrayList ;
31
+ import java .util .Collections ;
31
32
import java .util .HashMap ;
32
33
import java .util .List ;
33
34
import java .util .Map ;
34
35
35
36
import static org .hamcrest .CoreMatchers .equalTo ;
37
+ import static org .hamcrest .CoreMatchers .instanceOf ;
36
38
import static org .hamcrest .CoreMatchers .not ;
37
39
import static org .hamcrest .CoreMatchers .sameInstance ;
40
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
38
41
39
42
public class AppendProcessorTests extends ESTestCase {
40
43
@@ -53,13 +56,13 @@ public void testAppendValuesToExistingList() throws Exception {
53
56
if (randomBoolean ()) {
54
57
Object value = scalar .randomValue ();
55
58
values .add (value );
56
- appendProcessor = createAppendProcessor (field , value );
59
+ appendProcessor = createAppendProcessor (field , value , true );
57
60
} else {
58
61
int valuesSize = randomIntBetween (0 , 10 );
59
62
for (int i = 0 ; i < valuesSize ; i ++) {
60
63
values .add (scalar .randomValue ());
61
64
}
62
- appendProcessor = createAppendProcessor (field , values );
65
+ appendProcessor = createAppendProcessor (field , values , true );
63
66
}
64
67
appendProcessor .execute (ingestDocument );
65
68
Object fieldValue = ingestDocument .getFieldValue (field , Object .class );
@@ -82,13 +85,13 @@ public void testAppendValuesToNonExistingList() throws Exception {
82
85
if (randomBoolean ()) {
83
86
Object value = scalar .randomValue ();
84
87
values .add (value );
85
- appendProcessor = createAppendProcessor (field , value );
88
+ appendProcessor = createAppendProcessor (field , value , true );
86
89
} else {
87
90
int valuesSize = randomIntBetween (0 , 10 );
88
91
for (int i = 0 ; i < valuesSize ; i ++) {
89
92
values .add (scalar .randomValue ());
90
93
}
91
- appendProcessor = createAppendProcessor (field , values );
94
+ appendProcessor = createAppendProcessor (field , values , true );
92
95
}
93
96
appendProcessor .execute (ingestDocument );
94
97
List <?> list = ingestDocument .getFieldValue (field , List .class );
@@ -106,13 +109,13 @@ public void testConvertScalarToList() throws Exception {
106
109
if (randomBoolean ()) {
107
110
Object value = scalar .randomValue ();
108
111
values .add (value );
109
- appendProcessor = createAppendProcessor (field , value );
112
+ appendProcessor = createAppendProcessor (field , value , true );
110
113
} else {
111
114
int valuesSize = randomIntBetween (0 , 10 );
112
115
for (int i = 0 ; i < valuesSize ; i ++) {
113
116
values .add (scalar .randomValue ());
114
117
}
115
- appendProcessor = createAppendProcessor (field , values );
118
+ appendProcessor = createAppendProcessor (field , values , true );
116
119
}
117
120
appendProcessor .execute (ingestDocument );
118
121
List <?> fieldValue = ingestDocument .getFieldValue (field , List .class );
@@ -132,13 +135,13 @@ public void testAppendMetadataExceptVersion() throws Exception {
132
135
if (randomBoolean ()) {
133
136
String value = randomAlphaOfLengthBetween (1 , 10 );
134
137
values .add (value );
135
- appendProcessor = createAppendProcessor (randomMetadata .getFieldName (), value );
138
+ appendProcessor = createAppendProcessor (randomMetadata .getFieldName (), value , true );
136
139
} else {
137
140
int valuesSize = randomIntBetween (0 , 10 );
138
141
for (int i = 0 ; i < valuesSize ; i ++) {
139
142
values .add (randomAlphaOfLengthBetween (1 , 10 ));
140
143
}
141
- appendProcessor = createAppendProcessor (randomMetadata .getFieldName (), values );
144
+ appendProcessor = createAppendProcessor (randomMetadata .getFieldName (), values , true );
142
145
}
143
146
144
147
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
@@ -156,10 +159,65 @@ public void testAppendMetadataExceptVersion() throws Exception {
156
159
}
157
160
}
158
161
159
- private static Processor createAppendProcessor (String fieldName , Object fieldValue ) {
162
+ public void testAppendingDuplicateValueToScalarDoesNotModifyDocument () throws Exception {
163
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
164
+ String originalValue = randomAlphaOfLengthBetween (1 , 10 );
165
+ String field = RandomDocumentPicks .addRandomField (random (), ingestDocument , originalValue );
166
+
167
+ List <Object > valuesToAppend = new ArrayList <>();
168
+ valuesToAppend .add (originalValue );
169
+ Processor appendProcessor = createAppendProcessor (field , valuesToAppend , false );
170
+ appendProcessor .execute (ingestDocument );
171
+ Object fieldValue = ingestDocument .getFieldValue (field , Object .class );
172
+ assertThat (fieldValue , not (instanceOf (List .class )));
173
+ assertThat (fieldValue , equalTo (originalValue ));
174
+ }
175
+
176
+ public void testAppendingUniqueValueToScalar () throws Exception {
177
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
178
+ String originalValue = randomAlphaOfLengthBetween (1 , 10 );
179
+ String field = RandomDocumentPicks .addRandomField (random (), ingestDocument , originalValue );
180
+
181
+ List <Object > valuesToAppend = new ArrayList <>();
182
+ String newValue = randomAlphaOfLengthBetween (1 , 10 );
183
+ valuesToAppend .add (newValue );
184
+ Processor appendProcessor = createAppendProcessor (field , valuesToAppend , false );
185
+ appendProcessor .execute (ingestDocument );
186
+ List <?> list = ingestDocument .getFieldValue (field , List .class );
187
+ assertThat (list .size (), equalTo (2 ));
188
+ assertThat (list , equalTo (List .of (originalValue , newValue )));
189
+ }
190
+
191
+ public void testAppendingToListWithDuplicatesDisallowed () throws Exception {
192
+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
193
+ List <String > list = new ArrayList <>();
194
+ int size = randomIntBetween (0 , 10 );
195
+ for (int i = 0 ; i < size ; i ++) {
196
+ list .add (randomAlphaOfLengthBetween (1 , 10 ));
197
+ }
198
+ String originalField = RandomDocumentPicks .addRandomField (random (), ingestDocument , list );
199
+ List <String > expectedValues = new ArrayList <>(list );
200
+ List <String > existingValues = randomSubsetOf (list );
201
+ int uniqueValuesSize = randomIntBetween (0 , 10 );
202
+ List <String > uniqueValues = new ArrayList <>();
203
+ for (int i = 0 ; i < uniqueValuesSize ; i ++) {
204
+ uniqueValues .add (randomAlphaOfLengthBetween (1 , 10 ));
205
+ }
206
+ List <String > valuesToAppend = new ArrayList <>(existingValues );
207
+ valuesToAppend .addAll (uniqueValues );
208
+ expectedValues .addAll (uniqueValues );
209
+ Collections .sort (valuesToAppend );
210
+ Processor appendProcessor = createAppendProcessor (originalField , valuesToAppend , false );
211
+ appendProcessor .execute (ingestDocument );
212
+ List <?> fieldValue = ingestDocument .getFieldValue (originalField , List .class );
213
+ assertThat (fieldValue , sameInstance (list ));
214
+ assertThat (fieldValue , containsInAnyOrder (expectedValues .toArray ()));
215
+ }
216
+
217
+ private static Processor createAppendProcessor (String fieldName , Object fieldValue , boolean allowDuplicates ) {
160
218
return new AppendProcessor (randomAlphaOfLength (10 ),
161
219
null , new TestTemplateService .MockTemplateScript .Factory (fieldName ),
162
- ValueSource .wrap (fieldValue , TestTemplateService .instance ()));
220
+ ValueSource .wrap (fieldValue , TestTemplateService .instance ()), allowDuplicates );
163
221
}
164
222
165
223
private enum Scalar {
0 commit comments