2424import java .util .ArrayList ;
2525import java .util .List ;
2626import java .util .Properties ;
27- import java .util .concurrent .atomic .AtomicInteger ;
2827import java .util .regex .Pattern ;
2928import java .util .stream .IntStream ;
3029
4342import com .mongodb .kafka .connect .mongodb .MongoKafkaTestCase ;
4443import com .mongodb .kafka .connect .source .MongoSourceConfig ;
4544
46-
4745public class MongoSourceConnectorTest extends MongoKafkaTestCase {
4846
49- private static final AtomicInteger POSTFIX = new AtomicInteger ();
50-
5147 @ BeforeEach
5248 void setUp () {
5349 assumeTrue (isReplicaSetOrSharded ());
@@ -63,7 +59,7 @@ void tearDown() {
6359 }
6460
6561 @ Test
66- @ DisplayName ("Ensure source loads data from MongoDB MongoClient" )
62+ @ DisplayName ("Ensure source loads data from MongoClient" )
6763 void testSourceLoadsDataFromMongoClient () {
6864 addSourceConnector ();
6965
@@ -96,7 +92,41 @@ void testSourceLoadsDataFromMongoClient() {
9692 }
9793
9894 @ Test
99- @ DisplayName ("Ensure source loads data from MongoDB database" )
95+ @ DisplayName ("Ensure source loads data from MongoClient with copy existing data" )
96+ void testSourceLoadsDataFromMongoClientWithCopyExisting () {
97+ MongoDatabase db1 = getDatabaseWithPostfix ();
98+ MongoDatabase db2 = getDatabaseWithPostfix ();
99+ MongoDatabase db3 = getDatabaseWithPostfix ();
100+ MongoCollection <Document > coll1 = db1 .getCollection ("coll" );
101+ MongoCollection <Document > coll2 = db2 .getCollection ("coll" );
102+ MongoCollection <Document > coll3 = db3 .getCollection ("coll" );
103+ MongoCollection <Document > coll4 = db1 .getCollection ("db1Coll2" );
104+
105+ insertMany (rangeClosed (1 , 50 ), coll1 , coll2 );
106+
107+ Properties sourceProperties = new Properties ();
108+ sourceProperties .put (MongoSourceConfig .COPY_EXISTING_CONFIG , "true" );
109+ addSourceConnector (sourceProperties );
110+
111+ assertAll (
112+ () -> assertProduced (50 , coll1 ),
113+ () -> assertProduced (50 , coll2 ),
114+ () -> assertProduced (0 , coll3 ));
115+
116+ db1 .drop ();
117+ insertMany (rangeClosed (51 , 60 ), coll2 , coll4 );
118+ insertMany (rangeClosed (1 , 70 ), coll3 );
119+
120+ assertAll (
121+ () -> assertProduced (51 , coll1 ),
122+ () -> assertProduced (60 , coll2 ),
123+ () -> assertProduced (70 , coll3 ),
124+ () -> assertProduced (10 , coll4 )
125+ );
126+ }
127+
128+ @ Test
129+ @ DisplayName ("Ensure source loads data from database" )
100130 void testSourceLoadsDataFromDatabase () {
101131 try (KafkaConsumer <?, ?> consumer = createConsumer ()) {
102132 Pattern pattern = Pattern .compile (format ("^%s.*" , getDatabaseName ()));
@@ -141,6 +171,53 @@ void testSourceLoadsDataFromDatabase() {
141171 }
142172 }
143173
174+ @ Test
175+ @ DisplayName ("Ensure source loads data from database with copy existing data" )
176+ void testSourceLoadsDataFromDatabaseCopyExisting () {
177+ try (KafkaConsumer <?, ?> consumer = createConsumer ()) {
178+ Pattern pattern = Pattern .compile (format ("^%s.*" , getDatabaseName ()));
179+ consumer .subscribe (pattern );
180+
181+ MongoDatabase db = getDatabaseWithPostfix ();
182+
183+ MongoCollection <Document > coll1 = db .getCollection ("coll1" );
184+ MongoCollection <Document > coll2 = db .getCollection ("coll2" );
185+ MongoCollection <Document > coll3 = db .getCollection ("coll3" );
186+
187+ insertMany (rangeClosed (1 , 50 ), coll1 , coll2 );
188+
189+ Properties sourceProperties = new Properties ();
190+ sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , db .getName ());
191+ sourceProperties .put (MongoSourceConfig .COPY_EXISTING_CONFIG , "true" );
192+ addSourceConnector (sourceProperties );
193+
194+ assertAll (
195+ () -> assertProduced (50 , coll1 ),
196+ () -> assertProduced (50 , coll2 ),
197+ () -> assertProduced (0 , coll3 )
198+ );
199+
200+ // Update some of the collections
201+ coll1 .drop ();
202+ coll2 .drop ();
203+
204+ insertMany (rangeClosed (1 , 20 ), coll3 );
205+
206+ String collName4 = "coll4" ;
207+ coll3 .renameCollection (new MongoNamespace (getDatabaseName (), collName4 ));
208+ MongoCollection <Document > coll4 = db .getCollection (collName4 );
209+
210+ insertMany (rangeClosed (21 , 30 ), coll4 );
211+
212+ assertAll (
213+ () -> assertProduced (51 , coll1 ),
214+ () -> assertProduced (51 , coll2 ),
215+ () -> assertProduced (21 , coll3 ),
216+ () -> assertProduced (10 , coll4 )
217+ );
218+ }
219+ }
220+
144221 @ Test
145222 @ DisplayName ("Ensure source can handle non existent database and survive dropping" )
146223 void testSourceCanHandleNonExistentDatabaseAndSurviveDropping () throws InterruptedException {
@@ -187,6 +264,28 @@ void testSourceLoadsDataFromCollection() {
187264 assertProduced (101 , coll );
188265 }
189266
267+ @ Test
268+ @ DisplayName ("Ensure source loads data from collection with copy existing data" )
269+ void testSourceLoadsDataFromCollectionCopyExisting () {
270+ MongoCollection <Document > coll = getDatabaseWithPostfix ().getCollection ("coll" );
271+
272+ insertMany (rangeClosed (1 , 50 ), coll );
273+
274+ Properties sourceProperties = new Properties ();
275+ sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , coll .getNamespace ().getDatabaseName ());
276+ sourceProperties .put (MongoSourceConfig .COLLECTION_CONFIG , coll .getNamespace ().getCollectionName ());
277+ sourceProperties .put (MongoSourceConfig .COPY_EXISTING_CONFIG , "true" );
278+ addSourceConnector (sourceProperties );
279+
280+ assertProduced (50 , coll );
281+
282+ insertMany (rangeClosed (51 , 100 ), coll );
283+ assertProduced (100 , coll );
284+
285+ coll .drop ();
286+ assertProduced (101 , coll );
287+ }
288+
190289 @ Test
191290 @ DisplayName ("Ensure source can handle non existent collection and survive dropping" )
192291 void testSourceCanHandleNonExistentCollectionAndSurviveDropping () throws InterruptedException {
@@ -215,17 +314,22 @@ void testSourceCanHandleNonExistentCollectionAndSurviveDropping() throws Interru
215314 void testSourceLoadsDataFromCollectionDocumentOnly () {
216315 MongoCollection <Document > coll = getDatabaseWithPostfix ().getCollection ("coll" );
217316
317+ List <Document > docs = insertMany (rangeClosed (1 , 50 ), coll );
318+
218319 Properties sourceProperties = new Properties ();
219320 sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , coll .getNamespace ().getDatabaseName ());
220321 sourceProperties .put (MongoSourceConfig .COLLECTION_CONFIG , coll .getNamespace ().getCollectionName ());
221322 sourceProperties .put (MongoSourceConfig .PUBLISH_FULL_DOCUMENT_ONLY_CONFIG , "true" );
323+ sourceProperties .put (MongoSourceConfig .COPY_EXISTING_CONFIG , "true" );
222324 addSourceConnector (sourceProperties );
223325
224- List <Document > docs = insertMany (rangeClosed (1 , 100 ), coll );
225326 assertProduced (docs , coll );
226327
328+ List <Document > allDocs = new ArrayList <>(docs );
329+ allDocs .addAll (insertMany (rangeClosed (51 , 100 ), coll ));
330+
227331 coll .drop ();
228- assertProduced (docs , coll );
332+ assertProduced (allDocs , coll );
229333 }
230334
231335 private MongoDatabase getDatabaseWithPostfix () {
0 commit comments