@@ -52,6 +52,18 @@ private Map<String, AttributeValue> getAttributes() {
52
52
return attributes ;
53
53
}
54
54
55
+ private Map <String , AttributeValue > getAttributesWithInvalidAvroCharacters () {
56
+ Map <String , AttributeValue > attributes = new HashMap <>();
57
+ attributes .put ("test-1234" , new AttributeValue ().withS ("testKV1Value" ));
58
+ attributes .put ("1-starts-with-number" , new AttributeValue ().withS ("2" ));
59
+ attributes .put ("_starts_with_underscore" , new AttributeValue ().withN ("1" ));
60
+ attributes .put ("test!@£$%^" , new AttributeValue ().withS ("testStringValue" ));
61
+
62
+ return attributes ;
63
+ }
64
+
65
+
66
+
55
67
private SourceInfo getSourceInfo (String table ) {
56
68
SourceInfo sourceInfo = new SourceInfo (table , Clock .fixed (Instant .parse ("2001-01-02T00:00:00Z" ), ZoneId .of ("UTC" )));
57
69
sourceInfo .initSyncStatus = InitSyncStatus .RUNNING ;
@@ -191,6 +203,81 @@ public void recordAttributesAreAddedToValueData() throws Exception {
191
203
((Struct ) record .value ()).getString ("document" ));
192
204
}
193
205
206
+ @ Test
207
+ public void singleItemKeyIsAddedToRecordWhenKeyContainsInvalidCharacters () throws Exception {
208
+ // Arrange
209
+ List <KeySchemaElement > keySchema = new LinkedList <>();
210
+ keySchema .add (new KeySchemaElement ().withKeyType ("S" ).withAttributeName ("test-1234" ));
211
+
212
+ RecordConverter converter = new RecordConverter (getTableDescription (keySchema ), "TestTopicPrefix-" );
213
+
214
+ // Act
215
+ SourceRecord record = converter .toSourceRecord (
216
+ getSourceInfo (table ),
217
+ Envelope .Operation .forCode ("r" ),
218
+ getAttributesWithInvalidAvroCharacters (),
219
+ Instant .parse ("2001-01-02T00:00:00.00Z" ),
220
+ "testShardID1" ,
221
+ "testSequenceNumberID1"
222
+ );
223
+
224
+ // Assert
225
+ assertEquals ("test_1234" , record .keySchema ().fields ().get (0 ).name ());
226
+ assertEquals (SchemaBuilder .string ().build (), record .keySchema ().fields ().get (0 ).schema ());
227
+ assertEquals ("testKV1Value" , ((Struct ) record .key ()).getString ("test_1234" ));
228
+ }
229
+
230
+ @ Test
231
+ public void multiItemKeyIsAddedToRecordWhenKeyContainsInvalidCharacters () throws Exception {
232
+ // Arrange
233
+ List <KeySchemaElement > keySchema = new LinkedList <>();
234
+ keySchema .add (new KeySchemaElement ().withKeyType ("S" ).withAttributeName ("test-1234" ));
235
+ keySchema .add (new KeySchemaElement ().withKeyType ("N" ).withAttributeName ("1-starts-with-number" ));
236
+
237
+ RecordConverter converter = new RecordConverter (getTableDescription (keySchema ), "TestTopicPrefix-" );
238
+
239
+ // Act
240
+ SourceRecord record = converter .toSourceRecord (
241
+ getSourceInfo (table ),
242
+ Envelope .Operation .forCode ("r" ),
243
+ getAttributesWithInvalidAvroCharacters (),
244
+ Instant .parse ("2001-01-02T00:00:00.00Z" ),
245
+ "testShardID1" ,
246
+ "testSequenceNumberID1"
247
+ );
248
+
249
+ // Assert
250
+ assertEquals ("test_1234" , record .keySchema ().fields ().get (0 ).name ());
251
+ assertEquals (SchemaBuilder .string ().build (), record .keySchema ().fields ().get (0 ).schema ());
252
+ assertEquals ("testKV1Value" , ((Struct ) record .key ()).getString ("test_1234" ));
253
+
254
+ assertEquals ("__starts_with_number" , record .keySchema ().fields ().get (1 ).name ());
255
+ assertEquals (SchemaBuilder .string ().build (), record .keySchema ().fields ().get (1 ).schema ());
256
+ assertEquals ("2" , ((Struct ) record .key ()).getString ("__starts_with_number" ));
257
+ }
258
+
259
+ @ Test
260
+ public void recordAttributesAreAddedToValueDataWhenAttributesContainsInvalidCharacters () throws Exception {
261
+ // Arrange
262
+ RecordConverter converter = new RecordConverter (getTableDescription (null ), "TestTopicPrefix-" );
263
+
264
+ // Act
265
+ SourceRecord record = converter .toSourceRecord (
266
+ getSourceInfo (table ),
267
+ Envelope .Operation .forCode ("r" ),
268
+ getAttributesWithInvalidAvroCharacters (),
269
+ Instant .parse ("2001-01-02T00:00:00.00Z" ),
270
+ "testShardID1" ,
271
+ "testSequenceNumberID1"
272
+ );
273
+
274
+ String expected = "{\" test_1234\" :{\" s\" :\" testKV1Value\" },\" _starts_with_underscore\" :{\" n\" :\" 1\" },\" __starts_with_number\" :{\" s\" :\" 2\" },\" test______\" :{\" s\" :\" testStringValue\" }}" ;
275
+
276
+ // Assert
277
+ assertEquals (expected ,
278
+ ((Struct ) record .value ()).getString ("document" ));
279
+ }
280
+
194
281
@ Test
195
282
public void sourceInfoIsAddedToValueData () throws Exception {
196
283
// Arrange
0 commit comments