4848
4949import static io .flamingock .internal .common .core .metadata .Constants .MONGOCK_IMPORT_EMPTY_ORIGIN_ALLOWED_PROPERTY_KEY ;
5050import static io .flamingock .internal .common .core .metadata .Constants .MONGOCK_IMPORT_ORIGIN_PROPERTY_KEY ;
51+ import static io .flamingock .internal .common .core .metadata .Constants .MONGOCK_IMPORT_SKIP_PROPERTY_KEY ;
5152import static io .flamingock .internal .util .constants .AuditEntryFieldConstants .KEY_CREATED_AT ;
5253import static io .flamingock .internal .util .constants .AuditEntryFieldConstants .KEY_STATE ;
5354import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -66,6 +67,9 @@ public class CouchbaseImporterTest {
6667 public static final String MONGOCK_SCOPE_NAME = CollectionIdentifier .DEFAULT_SCOPE ;
6768 public static final String MONGOCK_COLLECTION_NAME = CollectionIdentifier .DEFAULT_COLLECTION ;
6869
70+ public static final String CUSTOM_MONGOCK_ORIGIN_SCOPE = "mongockCustomScope" ;
71+ public static final String CUSTOM_MONGOCK_ORIGIN_COLLECTION = "mongockCustomOriginCollection" ;
72+
6973
7074 @ Container
7175 static final CouchbaseContainer couchbaseContainer = new CouchbaseContainer ("couchbase/server:7.2.4" )
@@ -101,7 +105,15 @@ static void setupAll() {
101105 @ AfterEach
102106 void cleanUp () {
103107 CouchbaseCollectionHelper .deleteAllDocuments (cluster , FLAMINGOCK_BUCKET_NAME , FLAMINGOCK_SCOPE_NAME , FLAMINGOCK_COLLECTION_NAME );
108+ CouchbaseCollectionHelper .waitUntilEmpty (cluster , FLAMINGOCK_BUCKET_NAME , FLAMINGOCK_SCOPE_NAME , FLAMINGOCK_COLLECTION_NAME , Duration .ofSeconds (10 ));
109+
104110 CouchbaseCollectionHelper .deleteAllDocuments (cluster , MONGOCK_BUCKET_NAME , MONGOCK_SCOPE_NAME , MONGOCK_COLLECTION_NAME );
111+ CouchbaseCollectionHelper .waitUntilEmpty (cluster , MONGOCK_BUCKET_NAME , MONGOCK_SCOPE_NAME , MONGOCK_COLLECTION_NAME , Duration .ofSeconds (10 ));
112+
113+ if (CouchbaseCollectionHelper .collectionExists (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION )) {
114+ CouchbaseCollectionHelper .deleteAllDocuments (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION );
115+ CouchbaseCollectionHelper .waitUntilEmpty (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION , Duration .ofSeconds (10 ));
116+ }
105117 }
106118
107119 @ Test
@@ -300,15 +312,13 @@ void GIVEN_mongockAuditHistoryEmptyAndFailIfEmptyOriginDisabled_WHEN_migratingTo
300312 "AND execute the pending flamingock changes" )
301313 void GIVEN_allMongockChangeUnitsAlreadyExecutedAndCustomOriginProvided_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory () {
302314 // Setup Mongock entries
303- final String customMongockOriginScope = "mongockCustomScope" ;
304- final String customMongockOriginCollection = "mongockCustomOriginCollection" ;
305- final String customMongockOrigin = String .format ("%s.%s" , customMongockOriginScope , customMongockOriginCollection );
315+ final String customMongockOrigin = String .format ("%s.%s" , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION );
306316
307- CouchbaseCollectionHelper .createScopeIfNotExists (cluster , MONGOCK_BUCKET_NAME , customMongockOriginScope );
308- CouchbaseCollectionHelper .createCollectionIfNotExists (cluster , MONGOCK_BUCKET_NAME , customMongockOriginScope , customMongockOriginCollection );
309- CouchbaseCollectionHelper .createPrimaryIndexIfNotExists (cluster , MONGOCK_BUCKET_NAME , customMongockOriginScope , customMongockOriginCollection );
317+ CouchbaseCollectionHelper .createScopeIfNotExists (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE );
318+ CouchbaseCollectionHelper .createCollectionIfNotExists (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION );
319+ CouchbaseCollectionHelper .createPrimaryIndexIfNotExists (cluster , MONGOCK_BUCKET_NAME , CUSTOM_MONGOCK_ORIGIN_SCOPE , CUSTOM_MONGOCK_ORIGIN_COLLECTION );
310320
311- Collection originCollection = cluster .bucket (MONGOCK_BUCKET_NAME ).scope (customMongockOriginScope ).collection (customMongockOriginCollection );
321+ Collection originCollection = cluster .bucket (MONGOCK_BUCKET_NAME ).scope (CUSTOM_MONGOCK_ORIGIN_SCOPE ).collection (CUSTOM_MONGOCK_ORIGIN_COLLECTION );
312322
313323 originCollection .upsert ("mongock-change-1" , createAuditObject ("mongock-change-1" ));
314324
@@ -352,6 +362,183 @@ void GIVEN_allMongockChangeUnitsAlreadyExecutedAndCustomOriginProvided_WHEN_migr
352362 assertEquals ("APPLIED" , auditLog .get (6 ).getString ("state" ));
353363 }
354364
365+ @ Test
366+ @ DisplayName ("GIVEN skip import flag with invalid value " +
367+ "WHEN migrating to Flamingock Community" +
368+ "THEN should throw exception" )
369+ void GIVEN_skipImportFlagWithInvalidValue_WHEN_migratingToFlamingockCommunity_THEN_shouldThrowException () {
370+
371+ final String SKIP_IMPORT_VALUE = "invalid_value" ;
372+
373+ CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem ("couchbase-target-system" , cluster , FLAMINGOCK_BUCKET_NAME );
374+
375+ Runner flamingock = FlamingockFactory .getCommunityBuilder ()
376+ .setAuditStore (CouchbaseAuditStore .from (targetSystem )
377+ .withScopeName (FLAMINGOCK_SCOPE_NAME )
378+ .withAuditRepositoryName (FLAMINGOCK_COLLECTION_NAME ))
379+ .addTargetSystem (targetSystem )
380+ .setProperty (MONGOCK_IMPORT_SKIP_PROPERTY_KEY , SKIP_IMPORT_VALUE ) // only allows empty / true / false
381+ .build ();
382+
383+ FlamingockException ex = assertThrows (FlamingockException .class , flamingock ::run );
384+ assertEquals ("Invalid value for " + MONGOCK_IMPORT_SKIP_PROPERTY_KEY + ": " + SKIP_IMPORT_VALUE
385+ + " (expected \" true\" or \" false\" or empty)" , ex .getMessage ());
386+ }
387+
388+ @ Test
389+ @ DisplayName ("GIVEN all Mongock changeUnits already executed " +
390+ "AND skip import flag enabled " +
391+ "WHEN migrating to Flamingock Community" +
392+ "THEN should not import any audit history entry " +
393+ "AND execute the all mongock and flamingock changes" )
394+ void GIVEN_skipImportFlagEnabled_WHEN_migratingToFlamingockCommunity_THEN_shouldNotMigrateAnyAuditLog () {
395+
396+ Collection originCollection = cluster .bucket (MONGOCK_BUCKET_NAME ).scope (MONGOCK_SCOPE_NAME ).collection (MONGOCK_COLLECTION_NAME );
397+
398+ originCollection .upsert ("mongock-change-1" , createAuditObject ("mongock-change-1" ));
399+ originCollection .upsert ("mongock-change-2" , createAuditObject ("mongock-change-2" ));
400+
401+ final String SKIP_IMPORT_VALUE = "true" ;
402+
403+ CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem ("couchbase-target-system" , cluster , FLAMINGOCK_BUCKET_NAME );
404+
405+ Runner flamingock = FlamingockFactory .getCommunityBuilder ()
406+ .setAuditStore (CouchbaseAuditStore .from (targetSystem )
407+ .withScopeName (FLAMINGOCK_SCOPE_NAME )
408+ .withAuditRepositoryName (FLAMINGOCK_COLLECTION_NAME ))
409+ .addTargetSystem (targetSystem )
410+ .setProperty (MONGOCK_IMPORT_SKIP_PROPERTY_KEY , SKIP_IMPORT_VALUE ) // only allows empty / true / false
411+ .build ();
412+
413+ flamingock .run ();
414+
415+ List <JsonObject > auditLog = getAuditLog ();
416+
417+ assertEquals (8 , auditLog .size ());
418+
419+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (0 ).getString ("changeId" ));
420+ assertEquals ("STARTED" , auditLog .get (0 ).getString ("state" ));
421+
422+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (1 ).getString ("changeId" ));
423+ assertEquals ("APPLIED" , auditLog .get (1 ).getString ("state" ));
424+
425+ assertEquals ("mongock-change-1" , auditLog .get (2 ).getString ("changeId" ));
426+ assertEquals ("STARTED" , auditLog .get (2 ).getString ("state" ));
427+
428+ assertEquals ("mongock-change-1" , auditLog .get (3 ).getString ("changeId" ));
429+ assertEquals ("APPLIED" , auditLog .get (3 ).getString ("state" ));
430+
431+ assertEquals ("mongock-change-2" , auditLog .get (4 ).getString ("changeId" ));
432+ assertEquals ("STARTED" , auditLog .get (4 ).getString ("state" ));
433+
434+ assertEquals ("mongock-change-2" , auditLog .get (5 ).getString ("changeId" ));
435+ assertEquals ("APPLIED" , auditLog .get (5 ).getString ("state" ));
436+
437+ assertEquals ("flamingock-change" , auditLog .get (6 ).getString ("changeId" ));
438+ assertEquals ("STARTED" , auditLog .get (6 ).getString ("state" ));
439+
440+ assertEquals ("flamingock-change" , auditLog .get (7 ).getString ("changeId" ));
441+ assertEquals ("APPLIED" , auditLog .get (7 ).getString ("state" ));
442+ }
443+
444+ @ Test
445+ @ DisplayName ("GIVEN all Mongock changeUnits already executed " +
446+ "AND skip import flag disabled (explicit) " +
447+ "WHEN migrating to Flamingock Community " +
448+ "THEN should import the entire history " +
449+ "AND execute the pending flamingock changes" )
450+ void GIVEN_allMongockChangeUnitsAlreadyExecutedAndSkipImportFlagDisabledExplicit_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory () {
451+ Collection originCollection = cluster .bucket (MONGOCK_BUCKET_NAME ).scope (MONGOCK_SCOPE_NAME ).collection (MONGOCK_COLLECTION_NAME );
452+
453+ originCollection .upsert ("mongock-change-1" , createAuditObject ("mongock-change-1" ));
454+ originCollection .upsert ("mongock-change-2" , createAuditObject ("mongock-change-2" ));
455+
456+ CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem ("couchbase-target-system" , cluster , FLAMINGOCK_BUCKET_NAME );
457+
458+ final String SKIP_IMPORT_VALUE = "false" ;
459+
460+ Runner flamingock = FlamingockFactory .getCommunityBuilder ()
461+ .setAuditStore (CouchbaseAuditStore .from (targetSystem )
462+ .withScopeName (FLAMINGOCK_SCOPE_NAME )
463+ .withAuditRepositoryName (FLAMINGOCK_COLLECTION_NAME ))
464+ .addTargetSystem (targetSystem )
465+ .setProperty (MONGOCK_IMPORT_SKIP_PROPERTY_KEY , SKIP_IMPORT_VALUE ) // only allows empty / true / false
466+ .build ();
467+
468+ flamingock .run ();
469+
470+ List <JsonObject > auditLog = getAuditLog ();
471+
472+ assertEquals (6 , auditLog .size ());
473+
474+ assertEquals ("mongock-change-1" , auditLog .get (0 ).getString ("changeId" ));
475+ assertEquals ("APPLIED" , auditLog .get (0 ).getString ("state" ));
476+
477+ assertEquals ("mongock-change-2" , auditLog .get (1 ).getString ("changeId" ));
478+ assertEquals ("APPLIED" , auditLog .get (1 ).getString ("state" ));
479+
480+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (2 ).getString ("changeId" ));
481+ assertEquals ("STARTED" , auditLog .get (2 ).getString ("state" ));
482+
483+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (3 ).getString ("changeId" ));
484+ assertEquals ("APPLIED" , auditLog .get (3 ).getString ("state" ));
485+
486+ assertEquals ("flamingock-change" , auditLog .get (4 ).getString ("changeId" ));
487+ assertEquals ("STARTED" , auditLog .get (4 ).getString ("state" ));
488+
489+ assertEquals ("flamingock-change" , auditLog .get (5 ).getString ("changeId" ));
490+ assertEquals ("APPLIED" , auditLog .get (5 ).getString ("state" ));
491+ }
492+
493+ @ Test
494+ @ DisplayName ("GIVEN all Mongock changeUnits already executed " +
495+ "AND skip import flag disabled (implicit) " +
496+ "WHEN migrating to Flamingock Community " +
497+ "THEN should import the entire history " +
498+ "AND execute the pending flamingock changes" )
499+ void GIVEN_allMongockChangeUnitsAlreadyExecutedAndSkipImportFlagDisabledImplicit_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory () {
500+ Collection originCollection = cluster .bucket (MONGOCK_BUCKET_NAME ).scope (MONGOCK_SCOPE_NAME ).collection (MONGOCK_COLLECTION_NAME );
501+
502+ originCollection .upsert ("mongock-change-1" , createAuditObject ("mongock-change-1" ));
503+ originCollection .upsert ("mongock-change-2" , createAuditObject ("mongock-change-2" ));
504+
505+ CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem ("couchbase-target-system" , cluster , FLAMINGOCK_BUCKET_NAME );
506+
507+ final String SKIP_IMPORT_VALUE = "" ;
508+
509+ Runner flamingock = FlamingockFactory .getCommunityBuilder ()
510+ .setAuditStore (CouchbaseAuditStore .from (targetSystem )
511+ .withScopeName (FLAMINGOCK_SCOPE_NAME )
512+ .withAuditRepositoryName (FLAMINGOCK_COLLECTION_NAME ))
513+ .addTargetSystem (targetSystem )
514+ .setProperty (MONGOCK_IMPORT_SKIP_PROPERTY_KEY , SKIP_IMPORT_VALUE ) // only allows empty / true / false
515+ .build ();
516+
517+ flamingock .run ();
518+
519+ List <JsonObject > auditLog = getAuditLog ();
520+
521+ assertEquals (6 , auditLog .size ());
522+
523+ assertEquals ("mongock-change-1" , auditLog .get (0 ).getString ("changeId" ));
524+ assertEquals ("APPLIED" , auditLog .get (0 ).getString ("state" ));
525+
526+ assertEquals ("mongock-change-2" , auditLog .get (1 ).getString ("changeId" ));
527+ assertEquals ("APPLIED" , auditLog .get (1 ).getString ("state" ));
528+
529+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (2 ).getString ("changeId" ));
530+ assertEquals ("STARTED" , auditLog .get (2 ).getString ("state" ));
531+
532+ assertEquals ("migration-mongock-to-flamingock-community" , auditLog .get (3 ).getString ("changeId" ));
533+ assertEquals ("APPLIED" , auditLog .get (3 ).getString ("state" ));
534+
535+ assertEquals ("flamingock-change" , auditLog .get (4 ).getString ("changeId" ));
536+ assertEquals ("STARTED" , auditLog .get (4 ).getString ("state" ));
537+
538+ assertEquals ("flamingock-change" , auditLog .get (5 ).getString ("changeId" ));
539+ assertEquals ("APPLIED" , auditLog .get (5 ).getString ("state" ));
540+ }
541+
355542
356543 private List <JsonObject > getAuditLog () {
357544 return CouchbaseCollectionHelper .selectAllDocuments (cluster , FLAMINGOCK_BUCKET_NAME , FLAMINGOCK_SCOPE_NAME , FLAMINGOCK_COLLECTION_NAME )
@@ -379,5 +566,4 @@ private static JsonObject createAuditObject(String value) {
379566 .put ("_doctype" , "mongockChangeEntry" );
380567 return doc ;
381568 }
382-
383569}
0 commit comments