2424import java .util .ArrayList ;
2525import java .util .List ;
2626import java .util .Properties ;
27+ import java .util .concurrent .atomic .AtomicInteger ;
2728import java .util .regex .Pattern ;
2829import java .util .stream .IntStream ;
2930
4243import com .mongodb .kafka .connect .mongodb .MongoKafkaTestCase ;
4344import com .mongodb .kafka .connect .source .MongoSourceConfig ;
4445
46+
4547public class MongoSourceConnectorTest extends MongoKafkaTestCase {
4648
49+ private static final AtomicInteger POSTFIX = new AtomicInteger ();
50+
4751 @ BeforeEach
4852 void setUp () {
4953 assumeTrue (isReplicaSetOrSharded ());
@@ -63,10 +67,13 @@ void tearDown() {
6367 void testSourceLoadsDataFromMongoClient () {
6468 addSourceConnector ();
6569
66- MongoCollection <Document > coll1 = getDatabase ("1" ).getCollection ("coll" );
67- MongoCollection <Document > coll2 = getDatabase ("2" ).getCollection ("coll" );
68- MongoCollection <Document > coll3 = getDatabase ("3" ).getCollection ("coll" );
69- MongoCollection <Document > coll4 = getDatabase ("1" ).getCollection ("db1Coll2" );
70+ MongoDatabase db1 = getDatabaseWithPostfix ();
71+ MongoDatabase db2 = getDatabaseWithPostfix ();
72+ MongoDatabase db3 = getDatabaseWithPostfix ();
73+ MongoCollection <Document > coll1 = db1 .getCollection ("coll" );
74+ MongoCollection <Document > coll2 = db2 .getCollection ("coll" );
75+ MongoCollection <Document > coll3 = db3 .getCollection ("coll" );
76+ MongoCollection <Document > coll4 = db1 .getCollection ("db1Coll2" );
7077
7178 insertMany (rangeClosed (1 , 50 ), coll1 , coll2 );
7279
@@ -76,7 +83,7 @@ void testSourceLoadsDataFromMongoClient() {
7683 () -> assertProduced (0 , coll3 ));
7784
7885
79- getDatabase ( "1" ) .drop ();
86+ db1 .drop ();
8087 insertMany (rangeClosed (51 , 60 ), coll2 , coll4 );
8188 insertMany (rangeClosed (1 , 70 ), coll3 );
8289
@@ -95,7 +102,7 @@ void testSourceLoadsDataFromDatabase() {
95102 Pattern pattern = Pattern .compile (format ("^%s.*" , getDatabaseName ()));
96103 consumer .subscribe (pattern );
97104
98- MongoDatabase db = getDatabase ( "4" );
105+ MongoDatabase db = getDatabaseWithPostfix ( );
99106
100107 Properties sourceProperties = new Properties ();
101108 sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , db .getName ());
@@ -134,10 +141,39 @@ void testSourceLoadsDataFromDatabase() {
134141 }
135142 }
136143
144+ @ Test
145+ @ DisplayName ("Ensure source can handle non existent database and survive dropping" )
146+ void testSourceCanHandleNonExistentDatabaseAndSurviveDropping () throws InterruptedException {
147+ try (KafkaConsumer <?, ?> consumer = createConsumer ()) {
148+ Pattern pattern = Pattern .compile (format ("^%s.*" , getDatabaseName ()));
149+ consumer .subscribe (pattern );
150+
151+ MongoDatabase db = getDatabaseWithPostfix ();
152+ MongoCollection <Document > coll = db .getCollection ("coll" );
153+
154+ Properties sourceProperties = new Properties ();
155+ sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , db .getName ());
156+ addSourceConnector (sourceProperties );
157+
158+ Thread .sleep (5000 );
159+ assertProduced (0 , coll );
160+
161+ insertMany (rangeClosed (1 , 100 ), coll );
162+ assertProduced (100 , coll );
163+
164+ db .drop ();
165+ assertProduced (101 , coll );
166+
167+ Thread .sleep (5000 );
168+ insertMany (rangeClosed (1 , 100 ), coll );
169+ assertProduced (201 , coll );
170+ }
171+ }
172+
137173 @ Test
138174 @ DisplayName ("Ensure source loads data from collection" )
139175 void testSourceLoadsDataFromCollection () {
140- MongoCollection <Document > coll = getDatabase ( "5" ).getCollection ("coll" );
176+ MongoCollection <Document > coll = getDatabaseWithPostfix ( ).getCollection ("coll" );
141177
142178 Properties sourceProperties = new Properties ();
143179 sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , coll .getNamespace ().getDatabaseName ());
@@ -148,13 +184,36 @@ void testSourceLoadsDataFromCollection() {
148184 assertProduced (100 , coll );
149185
150186 coll .drop ();
187+ assertProduced (101 , coll );
188+ }
189+
190+ @ Test
191+ @ DisplayName ("Ensure source can handle non existent collection and survive dropping" )
192+ void testSourceCanHandleNonExistentCollectionAndSurviveDropping () throws InterruptedException {
193+ MongoCollection <Document > coll = getDatabaseWithPostfix ().getCollection ("coll" );
194+
195+ Properties sourceProperties = new Properties ();
196+ sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , coll .getNamespace ().getDatabaseName ());
197+ sourceProperties .put (MongoSourceConfig .COLLECTION_CONFIG , coll .getNamespace ().getCollectionName ());
198+ addSourceConnector (sourceProperties );
199+
200+ Thread .sleep (5000 );
201+ assertProduced (0 , coll );
202+
203+ insertMany (rangeClosed (1 , 100 ), coll );
151204 assertProduced (100 , coll );
205+
206+ coll .drop ();
207+ assertProduced (101 , coll );
208+
209+ insertMany (rangeClosed (1 , 100 ), coll );
210+ assertProduced (201 , coll );
152211 }
153212
154213 @ Test
155214 @ DisplayName ("Ensure source loads data from collection and outputs documents only" )
156215 void testSourceLoadsDataFromCollectionDocumentOnly () {
157- MongoCollection <Document > coll = getDatabase ( "6" ).getCollection ("coll" );
216+ MongoCollection <Document > coll = getDatabaseWithPostfix ( ).getCollection ("coll" );
158217
159218 Properties sourceProperties = new Properties ();
160219 sourceProperties .put (MongoSourceConfig .DATABASE_CONFIG , coll .getNamespace ().getDatabaseName ());
@@ -169,8 +228,8 @@ void testSourceLoadsDataFromCollectionDocumentOnly() {
169228 assertProduced (docs , coll );
170229 }
171230
172- private MongoDatabase getDatabase ( final String postfix ) {
173- return getMongoClient ().getDatabase (format ("%s%s" , getDatabaseName (), postfix ));
231+ private MongoDatabase getDatabaseWithPostfix ( ) {
232+ return getMongoClient ().getDatabase (format ("%s%s" , getDatabaseName (), POSTFIX . incrementAndGet () ));
174233 }
175234
176235 private List <Document > insertMany (final IntStream stream , final MongoCollection <?>... collections ) {
0 commit comments