forked from confluentinc/kafka-connect-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataConverterTest.java
413 lines (352 loc) · 17.2 KB
/
DataConverterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2018 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.confluent.connect.elasticsearch;
import io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.BehaviorOnNullValues;
import org.apache.kafka.connect.data.*;
import org.apache.kafka.connect.errors.DataException;
import org.apache.kafka.connect.sink.SinkRecord;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.junit.Before;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import static io.confluent.connect.elasticsearch.DataConverter.MAP_KEY;
import static io.confluent.connect.elasticsearch.DataConverter.MAP_VALUE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
public class DataConverterTest {
private DataConverter converter;
private Map<String, String> props;
private String key;
private String topic;
private int partition;
private long offset;
private String index;
private Schema schema;
@Before
public void setUp() {
props = ElasticsearchSinkConnectorConfigTest.addNecessaryProps(new HashMap<>());
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
key = "key";
topic = "topic";
partition = 0;
offset = 0;
index = "index";
schema = SchemaBuilder
.struct()
.name("struct")
.field("string", Schema.STRING_SCHEMA)
.build();
}
@Test
public void primitives() {
assertIdenticalAfterPreProcess(Schema.INT8_SCHEMA);
assertIdenticalAfterPreProcess(Schema.INT16_SCHEMA);
assertIdenticalAfterPreProcess(Schema.INT32_SCHEMA);
assertIdenticalAfterPreProcess(Schema.INT64_SCHEMA);
assertIdenticalAfterPreProcess(Schema.FLOAT32_SCHEMA);
assertIdenticalAfterPreProcess(Schema.FLOAT64_SCHEMA);
assertIdenticalAfterPreProcess(Schema.BOOLEAN_SCHEMA);
assertIdenticalAfterPreProcess(Schema.STRING_SCHEMA);
assertIdenticalAfterPreProcess(Schema.BYTES_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_INT16_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_INT32_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_INT64_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_FLOAT32_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_FLOAT64_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_BOOLEAN_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_STRING_SCHEMA);
assertIdenticalAfterPreProcess(Schema.OPTIONAL_BYTES_SCHEMA);
assertIdenticalAfterPreProcess(SchemaBuilder.int8().defaultValue((byte) 42).build());
assertIdenticalAfterPreProcess(SchemaBuilder.int16().defaultValue((short) 42).build());
assertIdenticalAfterPreProcess(SchemaBuilder.int32().defaultValue(42).build());
assertIdenticalAfterPreProcess(SchemaBuilder.int64().defaultValue(42L).build());
assertIdenticalAfterPreProcess(SchemaBuilder.float32().defaultValue(42.0f).build());
assertIdenticalAfterPreProcess(SchemaBuilder.float64().defaultValue(42.0d).build());
assertIdenticalAfterPreProcess(SchemaBuilder.bool().defaultValue(true).build());
assertIdenticalAfterPreProcess(SchemaBuilder.string().defaultValue("foo").build());
assertIdenticalAfterPreProcess(SchemaBuilder.bytes().defaultValue(new byte[0]).build());
}
private void assertIdenticalAfterPreProcess(Schema schema) {
assertEquals(schema, converter.preProcessSchema(schema));
}
@Test
public void decimal() {
Schema origSchema = Decimal.schema(2);
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(Schema.FLOAT64_SCHEMA, preProcessedSchema);
assertEquals(0.02, converter.preProcessValue(new BigDecimal("0.02"), origSchema, preProcessedSchema));
// optional
assertEquals(
Schema.OPTIONAL_FLOAT64_SCHEMA,
converter.preProcessSchema(Decimal.builder(2).optional().build())
);
// defval
assertEquals(
SchemaBuilder.float64().defaultValue(0.00).build(),
converter.preProcessSchema(Decimal.builder(2).defaultValue(new BigDecimal("0.00")).build())
);
}
@Test
public void array() {
Schema origSchema = SchemaBuilder.array(Decimal.schema(2)).schema();
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(SchemaBuilder.array(Schema.FLOAT64_SCHEMA).build(), preProcessedSchema);
assertEquals(
Arrays.asList(0.02, 0.42),
converter.preProcessValue(Arrays.asList(new BigDecimal("0.02"), new BigDecimal("0.42")), origSchema, preProcessedSchema)
);
// optional
assertEquals(
SchemaBuilder.array(preProcessedSchema.valueSchema()).optional().build(),
converter.preProcessSchema(SchemaBuilder.array(Decimal.schema(2)).optional().build())
);
// defval
assertEquals(
SchemaBuilder.array(preProcessedSchema.valueSchema()).defaultValue(Collections.emptyList()).build(),
converter.preProcessSchema(SchemaBuilder.array(Decimal.schema(2)).defaultValue(Collections.emptyList()).build())
);
}
@Test
public void map() {
Schema origSchema = SchemaBuilder.map(Schema.INT32_SCHEMA, Decimal.schema(2)).build();
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(
SchemaBuilder.array(
SchemaBuilder.struct().name(Schema.INT32_SCHEMA.type().name() + "-" + Decimal.LOGICAL_NAME)
.field(MAP_KEY, Schema.INT32_SCHEMA)
.field(MAP_VALUE, Schema.FLOAT64_SCHEMA)
.build()
).build(),
preProcessedSchema
);
Map<Object, Object> origValue = new HashMap<>();
origValue.put(1, new BigDecimal("0.02"));
origValue.put(2, new BigDecimal("0.42"));
assertEquals(
new HashSet<>(Arrays.asList(
new Struct(preProcessedSchema.valueSchema())
.put(MAP_KEY, 1)
.put(MAP_VALUE, 0.02),
new Struct(preProcessedSchema.valueSchema())
.put(MAP_KEY, 2)
.put(MAP_VALUE, 0.42)
)),
new HashSet<>((List<?>) converter.preProcessValue(origValue, origSchema, preProcessedSchema))
);
// optional
assertEquals(
SchemaBuilder.array(preProcessedSchema.valueSchema()).optional().build(),
converter.preProcessSchema(SchemaBuilder.map(Schema.INT32_SCHEMA, Decimal.schema(2)).optional().build())
);
// defval
assertEquals(
SchemaBuilder.array(preProcessedSchema.valueSchema()).defaultValue(Collections.emptyList()).build(),
converter.preProcessSchema(SchemaBuilder.map(Schema.INT32_SCHEMA, Decimal.schema(2)).defaultValue(Collections.emptyMap()).build())
);
}
@Test
public void stringKeyedMapNonCompactFormat() {
Schema origSchema = SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build();
Map<Object, Object> origValue = new HashMap<>();
origValue.put("field1", 1);
origValue.put("field2", 2);
// Use the older non-compact format for map entries with string keys
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "false");
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(
SchemaBuilder.array(
SchemaBuilder.struct().name(Schema.STRING_SCHEMA.type().name() + "-" + Schema.INT32_SCHEMA.type().name())
.field(MAP_KEY, Schema.STRING_SCHEMA)
.field(MAP_VALUE, Schema.INT32_SCHEMA)
.build()
).build(),
preProcessedSchema
);
assertEquals(
new HashSet<>(Arrays.asList(
new Struct(preProcessedSchema.valueSchema())
.put(MAP_KEY, "field1")
.put(MAP_VALUE, 1),
new Struct(preProcessedSchema.valueSchema())
.put(MAP_KEY, "field2")
.put(MAP_VALUE, 2)
)),
new HashSet<>((List<?>) converter.preProcessValue(origValue, origSchema, preProcessedSchema))
);
}
@Test
public void stringKeyedMapCompactFormat() {
Schema origSchema = SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build();
Map<Object, Object> origValue = new HashMap<>();
origValue.put("field1", 1);
origValue.put("field2", 2);
// Use the newer compact format for map entries with string keys
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(
SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.INT32_SCHEMA).build(),
preProcessedSchema
);
HashMap<?, ?> newValue = (HashMap<?, ?>) converter.preProcessValue(origValue, origSchema, preProcessedSchema);
assertEquals(origValue, newValue);
}
@Test
public void struct() {
Schema origSchema = SchemaBuilder.struct().name("struct").field("decimal", Decimal.schema(2)).build();
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
assertEquals(
SchemaBuilder.struct().name("struct").field("decimal", Schema.FLOAT64_SCHEMA).build(),
preProcessedSchema
);
assertEquals(
new Struct(preProcessedSchema).put("decimal", 0.02),
converter.preProcessValue(new Struct(origSchema).put("decimal", new BigDecimal("0.02")), origSchema, preProcessedSchema)
);
// optional
assertEquals(
SchemaBuilder.struct().name("struct").field("decimal", Schema.FLOAT64_SCHEMA).optional().build(),
converter.preProcessSchema(SchemaBuilder.struct().name("struct").field("decimal", Decimal.schema(2)).optional().build())
);
}
@Test
public void optionalFieldsWithoutDefaults() {
// One primitive type should be enough
testOptionalFieldWithoutDefault(SchemaBuilder.bool());
// Logical types
testOptionalFieldWithoutDefault(Decimal.builder(2));
testOptionalFieldWithoutDefault(Time.builder());
testOptionalFieldWithoutDefault(Timestamp.builder());
// Complex types
testOptionalFieldWithoutDefault(SchemaBuilder.array(Schema.BOOLEAN_SCHEMA));
testOptionalFieldWithoutDefault(SchemaBuilder.struct().field("innerField", Schema.BOOLEAN_SCHEMA));
testOptionalFieldWithoutDefault(SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.BOOLEAN_SCHEMA));
// Have to test maps with useCompactMapEntries set to true and set to false
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "false");
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
testOptionalFieldWithoutDefault(SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.BOOLEAN_SCHEMA));
}
private void testOptionalFieldWithoutDefault(
SchemaBuilder optionalFieldSchema
) {
Schema origSchema = SchemaBuilder.struct().name("struct").field(
"optionalField", optionalFieldSchema.optional().build()
).build();
Schema preProcessedSchema = converter.preProcessSchema(origSchema);
Object preProcessedValue = converter.preProcessValue(
new Struct(origSchema).put("optionalField", null), origSchema, preProcessedSchema
);
assertEquals(new Struct(preProcessedSchema).put("optionalField", null), preProcessedValue);
}
@Test
public void ignoreOnNullValue() {
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.IGNORE.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
SinkRecord sinkRecord = createSinkRecordWithValue(null);
assertNull(converter.convertRecord(sinkRecord, index));
}
@Test
public void deleteOnNullValue() {
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.DELETE.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
SinkRecord sinkRecord = createSinkRecordWithValue(null);
DeleteRequest actualRecord = (DeleteRequest) converter.convertRecord(sinkRecord, index);
assertEquals(key, actualRecord.id());
assertEquals(index, actualRecord.index());
assertEquals(sinkRecord.kafkaOffset(), actualRecord.version());
}
@Test
public void externalVersionHeaderOnDelete() {
String externalVersionHeader = "version";
long expectedExternalVersion = 123l;
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.EXTERNAL_VERSION_HEADER_CONFIG, externalVersionHeader);
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.DELETE.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
SinkRecord sinkRecord = createSinkRecordWithValue(null);
sinkRecord.headers().addLong(externalVersionHeader, expectedExternalVersion);
DeleteRequest actualRecord = (DeleteRequest) converter.convertRecord(sinkRecord, index);
assertEquals(key, actualRecord.id());
assertEquals(index, actualRecord.index());
assertEquals(expectedExternalVersion, actualRecord.version());
}
@Test
public void externalVersionHeaderOnIndex() {
String externalVersionHeader = "version";
long expectedExternalVersion = 123l;
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.EXTERNAL_VERSION_HEADER_CONFIG, externalVersionHeader);
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.DELETE.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
Schema preProcessedSchema = converter.preProcessSchema(schema);
Struct struct = new Struct(preProcessedSchema).put("string", "myValue");
SinkRecord sinkRecord = createSinkRecordWithValue(struct);
sinkRecord.headers().addLong(externalVersionHeader, expectedExternalVersion);
IndexRequest actualRecord = (IndexRequest) converter.convertRecord(sinkRecord, index);
assertEquals(key, actualRecord.id());
assertEquals(index, actualRecord.index());
assertEquals(expectedExternalVersion, actualRecord.version());
}
@Test
public void ignoreDeleteOnNullValueWithNullKey() {
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.DELETE.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
key = null;
SinkRecord sinkRecord = createSinkRecordWithValue(null);
assertNull(converter.convertRecord(sinkRecord, index));
}
@Test
public void failOnNullValue() {
props.put(ElasticsearchSinkConnectorConfig.COMPACT_MAP_ENTRIES_CONFIG, "true");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG, "false");
props.put(ElasticsearchSinkConnectorConfig.BEHAVIOR_ON_NULL_VALUES_CONFIG, BehaviorOnNullValues.FAIL.name());
converter = new DataConverter(new ElasticsearchSinkConnectorConfig(props));
SinkRecord sinkRecord = createSinkRecordWithValue(null);
try {
converter.convertRecord(sinkRecord, index);
fail("should fail on null-valued record with behaviorOnNullValues = FAIL");
} catch (DataException e) {
// expected
}
}
public SinkRecord createSinkRecordWithValue(Object value) {
return new SinkRecord(topic, partition, Schema.STRING_SCHEMA, key, schema, value, offset);
}
}