|
46 | 46 |
|
47 | 47 | import java.time.Instant;
|
48 | 48 | import java.util.ArrayList;
|
| 49 | +import java.util.Arrays; |
49 | 50 | import java.util.HashMap;
|
50 | 51 | import java.util.List;
|
51 | 52 | import java.util.Locale;
|
@@ -1053,6 +1054,144 @@ void testFullDocumentBeforeChange() {
|
1053 | 1054 | }
|
1054 | 1055 | }
|
1055 | 1056 |
|
| 1057 | + @Test |
| 1058 | + @DisplayName("Ensure disambiguatedPaths exist when showExpandedEvents is true") |
| 1059 | + void testDisambiguatedPathsExistWhenShowExpandedEventsIsTrue() { |
| 1060 | + assumeTrue(isAtLeastSevenDotZero()); |
| 1061 | + MongoDatabase db = getDatabaseWithPostfix(); |
| 1062 | + try (AutoCloseableSourceTask task = createSourceTask()) { |
| 1063 | + MongoCollection<Document> coll = db.getCollection("coll"); |
| 1064 | + coll.drop(); |
| 1065 | + db.createCollection(coll.getNamespace().getCollectionName(), new CreateCollectionOptions()); |
| 1066 | + HashMap<String, String> cfg = new HashMap<>(); |
| 1067 | + cfg.put( |
| 1068 | + MongoSourceConfig.OUTPUT_FORMAT_VALUE_CONFIG, |
| 1069 | + OutputFormat.SCHEMA.name().toLowerCase(Locale.ROOT)); |
| 1070 | + cfg.put(MongoSourceConfig.SHOW_EXPANDED_EVENTS_CONFIG, "true"); |
| 1071 | + task.start(cfg); |
| 1072 | + int id = 0; |
| 1073 | + Document expected = new Document("_id", id); |
| 1074 | + coll.insertOne(expected); |
| 1075 | + coll.updateOne(Filters.eq(id), Document.parse("{ $set: { foo: 1 } }")); |
| 1076 | + coll.deleteOne(Filters.eq(id)); |
| 1077 | + List<SourceRecord> records = getNextResults(task); |
| 1078 | + assertEquals(3, records.size()); |
| 1079 | + Struct update = (Struct) records.get(1).value(); |
| 1080 | + assertEquals(OperationType.UPDATE.getValue(), update.getString("operationType")); |
| 1081 | + Struct updateDescription = (Struct) update.get("updateDescription"); |
| 1082 | + assertEquals("{}", updateDescription.getString("disambiguatedPaths")); |
| 1083 | + } finally { |
| 1084 | + db.drop(); |
| 1085 | + } |
| 1086 | + } |
| 1087 | + |
| 1088 | + @Test |
| 1089 | + @DisplayName("Ensure disambiguatedPaths don't exist when showExpandedEvents is false") |
| 1090 | + void testDisambiguatedPathsDontExistWhenShowExpandedEventsIsTrue() { |
| 1091 | + assumeTrue(isAtLeastSevenDotZero()); |
| 1092 | + MongoDatabase db = getDatabaseWithPostfix(); |
| 1093 | + try (AutoCloseableSourceTask task = createSourceTask()) { |
| 1094 | + MongoCollection<Document> coll = db.getCollection("coll"); |
| 1095 | + coll.drop(); |
| 1096 | + db.createCollection(coll.getNamespace().getCollectionName(), new CreateCollectionOptions()); |
| 1097 | + HashMap<String, String> cfg = new HashMap<>(); |
| 1098 | + cfg.put( |
| 1099 | + MongoSourceConfig.OUTPUT_FORMAT_VALUE_CONFIG, |
| 1100 | + OutputFormat.SCHEMA.name().toLowerCase(Locale.ROOT)); |
| 1101 | + cfg.put(MongoSourceConfig.SHOW_EXPANDED_EVENTS_CONFIG, "false"); |
| 1102 | + task.start(cfg); |
| 1103 | + int id = 0; |
| 1104 | + Document expected = new Document("_id", id); |
| 1105 | + coll.insertOne(expected); |
| 1106 | + coll.updateOne(Filters.eq(id), Document.parse("{ $set: { foo: 1 } }")); |
| 1107 | + coll.deleteOne(Filters.eq(id)); |
| 1108 | + List<SourceRecord> records = getNextResults(task); |
| 1109 | + assertEquals(3, records.size()); |
| 1110 | + Struct update = (Struct) records.get(1).value(); |
| 1111 | + assertEquals(OperationType.UPDATE.getValue(), update.getString("operationType")); |
| 1112 | + Struct updateDescription = (Struct) update.get("updateDescription"); |
| 1113 | + assertNull(updateDescription.getString("disambiguatedPaths")); |
| 1114 | + } finally { |
| 1115 | + db.drop(); |
| 1116 | + } |
| 1117 | + } |
| 1118 | + |
| 1119 | + @Test |
| 1120 | + @DisplayName("Ensure disambiguatedPaths don't exist by default") |
| 1121 | + void testDisambiguatedPathsDontExistByDefault() { |
| 1122 | + assumeTrue(isAtLeastSevenDotZero()); |
| 1123 | + MongoDatabase db = getDatabaseWithPostfix(); |
| 1124 | + try (AutoCloseableSourceTask task = createSourceTask()) { |
| 1125 | + MongoCollection<Document> coll = db.getCollection("coll"); |
| 1126 | + coll.drop(); |
| 1127 | + db.createCollection(coll.getNamespace().getCollectionName(), new CreateCollectionOptions()); |
| 1128 | + HashMap<String, String> cfg = new HashMap<>(); |
| 1129 | + cfg.put( |
| 1130 | + MongoSourceConfig.OUTPUT_FORMAT_VALUE_CONFIG, |
| 1131 | + OutputFormat.SCHEMA.name().toLowerCase(Locale.ROOT)); |
| 1132 | + task.start(cfg); |
| 1133 | + int id = 0; |
| 1134 | + Document expected = new Document("_id", id); |
| 1135 | + coll.insertOne(expected); |
| 1136 | + coll.updateOne(Filters.eq(id), Document.parse("{ $set: { foo: 1 } }")); |
| 1137 | + coll.deleteOne(Filters.eq(id)); |
| 1138 | + List<SourceRecord> records = getNextResults(task); |
| 1139 | + assertEquals(3, records.size()); |
| 1140 | + Struct update = (Struct) records.get(1).value(); |
| 1141 | + assertEquals(OperationType.UPDATE.getValue(), update.getString("operationType")); |
| 1142 | + Struct updateDescription = (Struct) update.get("updateDescription"); |
| 1143 | + assertNull(updateDescription.getString("disambiguatedPaths")); |
| 1144 | + } finally { |
| 1145 | + db.drop(); |
| 1146 | + } |
| 1147 | + } |
| 1148 | + |
| 1149 | + @Test |
| 1150 | + @DisplayName("Ensure truncatedArrays works") |
| 1151 | + void testTruncatedArrays() { |
| 1152 | + assumeTrue(isAtLeastSixDotZero()); |
| 1153 | + MongoDatabase db = getDatabaseWithPostfix(); |
| 1154 | + try (AutoCloseableSourceTask task = createSourceTask()) { |
| 1155 | + MongoCollection<Document> coll = db.getCollection("coll"); |
| 1156 | + coll.drop(); |
| 1157 | + db.createCollection(coll.getNamespace().getCollectionName(), new CreateCollectionOptions()); |
| 1158 | + HashMap<String, String> cfg = new HashMap<>(); |
| 1159 | + cfg.put( |
| 1160 | + MongoSourceConfig.OUTPUT_FORMAT_VALUE_CONFIG, |
| 1161 | + OutputFormat.SCHEMA.name().toLowerCase(Locale.ROOT)); |
| 1162 | + task.start(cfg); |
| 1163 | + int id = 0; |
| 1164 | + Document expected = |
| 1165 | + new Document("_id", id) |
| 1166 | + .append("items", Arrays.asList(2, 30, 5, 10, 11, 100, 200, 250, 300, 5, 600)); |
| 1167 | + coll.insertOne(expected); |
| 1168 | + coll.updateOne( |
| 1169 | + Filters.eq(id), |
| 1170 | + singletonList(Document.parse("{ $set: { items: [2,30,5,10,11,100,200,250,300,5] } }"))); |
| 1171 | + coll.deleteOne(Filters.eq(id)); |
| 1172 | + List<SourceRecord> records = getNextResults(task); |
| 1173 | + assertEquals(3, records.size()); |
| 1174 | + Struct update = (Struct) records.get(1).value(); |
| 1175 | + assertEquals(OperationType.UPDATE.getValue(), update.getString("operationType")); |
| 1176 | + Struct updateDescription = (Struct) update.get("updateDescription"); |
| 1177 | + |
| 1178 | + Schema schema = |
| 1179 | + SchemaBuilder.struct() |
| 1180 | + .name("truncatedArray") |
| 1181 | + .field("field", Schema.STRING_SCHEMA) |
| 1182 | + .field("newSize", Schema.INT32_SCHEMA) |
| 1183 | + .build(); |
| 1184 | + |
| 1185 | + Struct truncatedArrayStruct = new Struct(schema).put("field", "items").put("newSize", 10); |
| 1186 | + |
| 1187 | + List<Struct> expectedTruncatedArray = new ArrayList<>(); |
| 1188 | + expectedTruncatedArray.add(truncatedArrayStruct); |
| 1189 | + assertEquals(expectedTruncatedArray, updateDescription.getArray("truncatedArrays")); |
| 1190 | + } finally { |
| 1191 | + db.drop(); |
| 1192 | + } |
| 1193 | + } |
| 1194 | + |
1056 | 1195 | /**
|
1057 | 1196 | * We insert a document into a collection before starting the {@link MongoSourceTask}, yet we
|
1058 | 1197 | * observe the change due to specifying {@link
|
|
0 commit comments