@@ -53,7 +53,7 @@ public void setup() {
53
53
separator = randomFrom (SEPARATORS );
54
54
}
55
55
56
- public void testExactNumberOfFields () throws Exception {
56
+ public void testExactNumberOfFields () {
57
57
int numItems = randomIntBetween (2 , 10 );
58
58
Map <String , String > items = new LinkedHashMap <>();
59
59
for (int i = 0 ; i < numItems ; i ++) {
@@ -67,7 +67,67 @@ public void testExactNumberOfFields() throws Exception {
67
67
items .forEach ((key , value ) -> assertEquals (value , ingestDocument .getFieldValue (key , String .class )));
68
68
}
69
69
70
- public void testLessFieldsThanHeaders () throws Exception {
70
+ public void testEmptyValues () {
71
+ int numItems = randomIntBetween (5 , 10 );
72
+ Map <String , String > items = new LinkedHashMap <>();
73
+ for (int i = 0 ; i < 3 ; i ++) {
74
+ items .put (randomAlphaOfLengthBetween (5 , 10 ), randomAlphaOfLengthBetween (5 , 10 ));
75
+ }
76
+ String emptyKey = randomAlphaOfLengthBetween (5 , 10 );
77
+ items .put (emptyKey , "" );
78
+ for (int i = 0 ; i < numItems - 4 ; i ++) {
79
+ items .put (randomAlphaOfLengthBetween (5 , 10 ), randomAlphaOfLengthBetween (5 , 10 ));
80
+ }
81
+ String [] headers = items .keySet ().toArray (new String [numItems ]);
82
+ String csv = items .values ().stream ().map (v -> quote + v + quote ).collect (Collectors .joining (separator + "" ));
83
+
84
+ IngestDocument ingestDocument = processDocument (headers , csv );
85
+
86
+ items .forEach ((key , value ) -> {
87
+ if (emptyKey .equals (key )) {
88
+ assertFalse (ingestDocument .hasField (key ));
89
+ } else {
90
+ assertEquals (value , ingestDocument .getFieldValue (key , String .class ));
91
+ }
92
+ });
93
+ }
94
+
95
+ public void testEmptyValuesReplace () {
96
+ int numItems = randomIntBetween (5 , 10 );
97
+ Map <String , String > items = new LinkedHashMap <>();
98
+ for (int i = 0 ; i < 3 ; i ++) {
99
+ items .put (randomAlphaOfLengthBetween (5 , 10 ), randomAlphaOfLengthBetween (5 , 10 ));
100
+ }
101
+ String emptyKey = randomAlphaOfLengthBetween (5 , 10 );
102
+ items .put (emptyKey , "" );
103
+ for (int i = 0 ; i < numItems - 4 ; i ++) {
104
+ items .put (randomAlphaOfLengthBetween (5 , 10 ), randomAlphaOfLengthBetween (5 , 10 ));
105
+ }
106
+ String [] headers = items .keySet ().toArray (new String [numItems ]);
107
+ String csv = items .values ().stream ().map (v -> quote + v + quote ).collect (Collectors .joining (separator + "" ));
108
+
109
+ IngestDocument ingestDocument = processDocument (headers , csv , true , "" );
110
+
111
+ items .forEach ((key , value ) -> {
112
+ if (emptyKey .equals (key )) {
113
+ assertEquals ("" , ingestDocument .getFieldValue (key , String .class ));
114
+ } else {
115
+ assertEquals (value , ingestDocument .getFieldValue (key , String .class ));
116
+ }
117
+ });
118
+
119
+ IngestDocument ingestDocument2 = processDocument (headers , csv , true , 0 );
120
+
121
+ items .forEach ((key , value ) -> {
122
+ if (emptyKey .equals (key )) {
123
+ assertEquals (0 , (int ) ingestDocument2 .getFieldValue (key , Integer .class ));
124
+ } else {
125
+ assertEquals (value , ingestDocument2 .getFieldValue (key , String .class ));
126
+ }
127
+ });
128
+ }
129
+
130
+ public void testLessFieldsThanHeaders () {
71
131
int numItems = randomIntBetween (4 , 10 );
72
132
Map <String , String > items = new LinkedHashMap <>();
73
133
for (int i = 0 ; i < numItems ; i ++) {
@@ -82,7 +142,7 @@ public void testLessFieldsThanHeaders() throws Exception {
82
142
items .entrySet ().stream ().limit (3 ).forEach (e -> assertEquals (e .getValue (), ingestDocument .getFieldValue (e .getKey (), String .class )));
83
143
}
84
144
85
- public void testLessHeadersThanFields () throws Exception {
145
+ public void testLessHeadersThanFields () {
86
146
int numItems = randomIntBetween (5 , 10 );
87
147
Map <String , String > items = new LinkedHashMap <>();
88
148
for (int i = 0 ; i < numItems ; i ++) {
@@ -96,7 +156,7 @@ public void testLessHeadersThanFields() throws Exception {
96
156
items .entrySet ().stream ().limit (3 ).forEach (e -> assertEquals (e .getValue (), ingestDocument .getFieldValue (e .getKey (), String .class )));
97
157
}
98
158
99
- public void testSingleField () throws Exception {
159
+ public void testSingleField () {
100
160
String [] headers = new String []{randomAlphaOfLengthBetween (5 , 10 )};
101
161
String value = randomAlphaOfLengthBetween (5 , 10 );
102
162
String csv = quote + value + quote ;
@@ -106,7 +166,7 @@ public void testSingleField() throws Exception {
106
166
assertEquals (value , ingestDocument .getFieldValue (headers [0 ], String .class ));
107
167
}
108
168
109
- public void testEscapedQuote () throws Exception {
169
+ public void testEscapedQuote () {
110
170
int numItems = randomIntBetween (2 , 10 );
111
171
Map <String , String > items = new LinkedHashMap <>();
112
172
for (int i = 0 ; i < numItems ; i ++) {
@@ -121,7 +181,7 @@ public void testEscapedQuote() throws Exception {
121
181
items .forEach ((key , value ) -> assertEquals (value .replace (quote + quote , quote ), ingestDocument .getFieldValue (key , String .class )));
122
182
}
123
183
124
- public void testQuotedStrings () throws Exception {
184
+ public void testQuotedStrings () {
125
185
assumeFalse ("quote needed" , quote .isEmpty ());
126
186
int numItems = randomIntBetween (2 , 10 );
127
187
Map <String , String > items = new LinkedHashMap <>();
@@ -138,7 +198,7 @@ public void testQuotedStrings() throws Exception {
138
198
String .class )));
139
199
}
140
200
141
- public void testEmptyFields () throws Exception {
201
+ public void testEmptyFields () {
142
202
int numItems = randomIntBetween (5 , 10 );
143
203
Map <String , String > items = new LinkedHashMap <>();
144
204
for (int i = 0 ; i < numItems ; i ++) {
@@ -167,7 +227,7 @@ public void testWrongStrings() throws Exception {
167
227
expectThrows (IllegalArgumentException .class , () -> processDocument (new String []{"a" }, "abc\r abc" ));
168
228
}
169
229
170
- public void testQuotedWhitespaces () throws Exception {
230
+ public void testQuotedWhitespaces () {
171
231
assumeFalse ("quote needed" , quote .isEmpty ());
172
232
IngestDocument document = processDocument (new String []{"a" , "b" , "c" , "d" },
173
233
" abc " + separator + " def" + separator + "ghi " + separator + " " + quote + " ooo " + quote );
@@ -177,7 +237,7 @@ public void testQuotedWhitespaces() throws Exception {
177
237
assertEquals (" ooo " , document .getFieldValue ("d" , String .class ));
178
238
}
179
239
180
- public void testUntrimmed () throws Exception {
240
+ public void testUntrimmed () {
181
241
assumeFalse ("quote needed" , quote .isEmpty ());
182
242
IngestDocument document = processDocument (new String []{"a" , "b" , "c" , "d" , "e" , "f" },
183
243
" abc " + separator + " def" + separator + "ghi " + separator + " "
@@ -197,9 +257,9 @@ public void testIgnoreMissing() {
197
257
if (ingestDocument .hasField (fieldName )) {
198
258
ingestDocument .removeField (fieldName );
199
259
}
200
- CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String []{"a" }, false , ',' , '"' , true );
260
+ CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String []{"a" }, false , ',' , '"' , true , null );
201
261
processor .execute (ingestDocument );
202
- CsvProcessor processor2 = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String []{"a" }, false , ',' , '"' , false );
262
+ CsvProcessor processor2 = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String []{"a" }, false , ',' , '"' , false , null );
203
263
expectThrows (IllegalArgumentException .class , () -> processor2 .execute (ingestDocument ));
204
264
}
205
265
@@ -209,24 +269,29 @@ public void testEmptyHeaders() throws Exception {
209
269
String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , "abc,abc" );
210
270
HashMap <String , Object > metadata = new HashMap <>(ingestDocument .getSourceAndMetadata ());
211
271
212
- CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String [0 ], false , ',' , '"' , false );
272
+ CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , new String [0 ], false , ',' , '"' , false , null );
213
273
214
274
processor .execute (ingestDocument );
215
275
216
276
assertEquals (metadata , ingestDocument .getSourceAndMetadata ());
217
277
}
218
278
219
- private IngestDocument processDocument (String [] headers , String csv ) throws Exception {
279
+ private IngestDocument processDocument (String [] headers , String csv ) {
220
280
return processDocument (headers , csv , true );
221
281
}
222
282
223
- private IngestDocument processDocument (String [] headers , String csv , boolean trim ) throws Exception {
283
+ private IngestDocument processDocument (String [] headers , String csv , boolean trim ) {
284
+ return processDocument (headers , csv , trim , null );
285
+ }
286
+
287
+ private IngestDocument processDocument (String [] headers , String csv , boolean trim , Object emptyValue ) {
224
288
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
225
289
Arrays .stream (headers ).filter (ingestDocument ::hasField ).forEach (ingestDocument ::removeField );
226
290
227
291
String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , csv );
228
292
char quoteChar = quote .isEmpty () ? '"' : quote .charAt (0 );
229
- CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , headers , trim , separator , quoteChar , false );
293
+ CsvProcessor processor = new CsvProcessor (randomAlphaOfLength (5 ), fieldName , headers , trim , separator , quoteChar , false ,
294
+ emptyValue );
230
295
231
296
processor .execute (ingestDocument );
232
297
0 commit comments