Skip to content

Commit 80d506a

Browse files
committed
test: added test to validate Mongock ChangeUnits execution if pending
1 parent d816f12 commit 80d506a

11 files changed

Lines changed: 299 additions & 33 deletions

File tree

legacy/mongock-importer-couchbase/src/test/java/io/flamingock/importer/mongock/couchbase/CouchbaseImporterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.flamingock.api.annotations.EnableFlamingock;
2525
import io.flamingock.api.annotations.Stage;
2626
import io.flamingock.community.couchbase.driver.CouchbaseAuditStore;
27+
import io.flamingock.internal.common.core.error.FlamingockException;
2728
import io.flamingock.internal.common.couchbase.CouchbaseCollectionHelper;
2829
import io.flamingock.internal.core.builder.FlamingockFactory;
2930
import io.flamingock.internal.core.runner.Runner;
@@ -49,6 +50,7 @@
4950
import static org.junit.jupiter.api.Assertions.assertEquals;
5051
import static org.junit.jupiter.api.Assertions.assertFalse;
5152
import static org.junit.jupiter.api.Assertions.assertNotNull;
53+
import static org.junit.jupiter.api.Assertions.assertThrows;
5254
import static org.junit.jupiter.api.Assertions.assertTrue;
5355

5456
@Testcontainers
@@ -152,6 +154,26 @@ void GIVEN_someChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_TH
152154
flamingock.run();
153155

154156
validateFlamingockAuditOutput();
157+
}
158+
159+
@Test
160+
@DisplayName("GIVEN mongock audit history empty " +
161+
"WHEN migrating to Flamingock Community" +
162+
"THEN should throw exception")
163+
void GIVEN_mongockAuditHistoryEmpty_WHEN_migratingToFlamingockCommunity_THEN_shouldThrowException() {
164+
// Setup Mongock entries
165+
166+
CouchbaseTargetSystem targetSystem = new CouchbaseTargetSystem("couchbase-target-system", cluster, FLAMINGOCK_BUCKET_NAME);
167+
168+
Runner flamingock = FlamingockFactory.getCommunityBuilder()
169+
.setAuditStore(new CouchbaseAuditStore(cluster, FLAMINGOCK_BUCKET_NAME)
170+
.withScopeName(FLAMINGOCK_SCOPE_NAME)
171+
.withAuditRepositoryName(FLAMINGOCK_COLLECTION_NAME))
172+
.addTargetSystem(targetSystem)
173+
.build();
174+
175+
FlamingockException ex = assertThrows(FlamingockException.class, flamingock::run);
176+
assertEquals("No audit entries found when importing from 'couchbase-target-system'.", ex.getMessage());
155177

156178
}
157179

legacy/mongock-importer-dynamodb/src/test/java/io/flamingock/importer/mongock/dynamodb/DynamoDBImporterTest.java

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.junit.jupiter.api.Assertions;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Disabled;
33+
import org.junit.jupiter.api.DisplayName;
3334
import org.junit.jupiter.api.Test;
3435
import org.testcontainers.containers.GenericContainer;
3536
import org.testcontainers.junit.jupiter.Container;
@@ -48,6 +49,7 @@
4849
import static io.flamingock.core.kit.audit.AuditEntryExpectation.STARTED;
4950
import static io.flamingock.internal.common.core.metadata.Constants.DEFAULT_MONGOCK_ORIGIN;
5051
import static org.junit.jupiter.api.Assertions.assertEquals;
52+
import static org.junit.jupiter.api.Assertions.assertThrows;
5153
import static org.junit.jupiter.api.Assertions.assertTrue;
5254

5355
@Testcontainers
@@ -81,25 +83,30 @@ void setUp() {
8183

8284
// Create required tables using DynamoDBTableFactory
8385
DynamoDBTableFactory.createMongockTable(client, DEFAULT_MONGOCK_ORIGIN);
84-
// DynamoDBTableFactory.createAuditTable(client, DEFAULT_AUDIT_STORE_NAME);
85-
// DynamoDBTableFactory.createLockTable(client, DEFAULT_LOCK_STORE_NAME);
8686

8787
mongockTestHelper = new DynamoDBMongockTestHelper(client, DEFAULT_MONGOCK_ORIGIN);
8888

8989
// Initialize TestKit for unified testing
9090
testKit = DynamoDBTestKit.create(client, new DynamoDBAuditStore(client));
9191
auditHelper = testKit.getAuditHelper();
92+
9293
}
9394

9495
@AfterEach
9596
void tearDown() {
9697
// DynamoDB local doesn't need explicit cleanup between tests
9798
// Tables are automatically cleaned by Testcontainers on restart
99+
mongockTestHelper.reset();
100+
testKit.cleanUp();
98101
client.close();
99102
}
100103

101104
@Test
102-
void testImportDynamoDBChangeLogs() {
105+
@DisplayName("GIVEN all Mongock changeUnits already executed" +
106+
"WHEN migrating to Flamingock Community " +
107+
"THEN should import the entire history " +
108+
"AND execute the pending flamingock changes")
109+
void GIVEN_allMongockChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory() {
103110
// Setup Mongock entries
104111
mongockTestHelper.setupBasicScenario();
105112

@@ -117,9 +124,9 @@ void testImportDynamoDBChangeLogs() {
117124
// Legacy imports from Mongock (APPLIED only - no STARTED for imported changes)
118125
APPLIED("system-change-00001_before"),
119126
APPLIED("system-change-00001"),
120-
APPLIED("client-initializer_before"),
121-
APPLIED("client-initializer"),
122-
APPLIED("client-updater"),
127+
APPLIED("mongock-change-1_before"),
128+
APPLIED("mongock-change-1"),
129+
APPLIED("mongock-change-2"),
123130

124131
// System stage - actual system importer change
125132
STARTED("migration-mongock-to-flamingock-community"),
@@ -142,11 +149,72 @@ void testImportDynamoDBChangeLogs() {
142149
}
143150

144151
@Test
145-
@Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done")
146-
void failIfEmptyOrigin() {
152+
@DisplayName("GIVEN some Mongock changeUnits already executed " +
153+
"AND some other Mongock changeUnits pending for execution" +
154+
"WHEN migrating to Flamingock Community" +
155+
"THEN migrates the history with the executed changeUnits " +
156+
"AND executes the pending Mongock changeUnits " +
157+
"AND executes the pending Flamingock changes")
158+
void GIVEN_someChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory() {
159+
// Setup Mongock entries
160+
mongockTestHelper.setupWithOnlyOneChange();
161+
162+
DynamoDBTargetSystem dynamodbTargetSystem = new DynamoDBTargetSystem("dynamodb-target-system", client);
163+
147164
Runner flamingock = testKit.createBuilder()
165+
.addTargetSystem(dynamodbTargetSystem)
148166
.build();
149167

150-
Assertions.assertThrows(FlamingockException.class, flamingock::run);
168+
flamingock.run();
169+
170+
// Verify audit sequence: 9 total entries
171+
// Legacy imports only show APPLIED (imported from Mongock), new changes show STARTED+APPLIED
172+
auditHelper.verifyAuditSequenceStrict(
173+
// Legacy imports from Mongock (APPLIED only - no STARTED for imported changes)
174+
APPLIED("system-change-00001_before"),
175+
APPLIED("system-change-00001"),
176+
APPLIED("mongock-change-1_before"),
177+
APPLIED("mongock-change-1"),
178+
179+
// System stage - actual system importer change
180+
STARTED("migration-mongock-to-flamingock-community"),
181+
APPLIED("migration-mongock-to-flamingock-community"),
182+
183+
184+
STARTED("mongock-change-2"),
185+
APPLIED("mongock-change-2"),
186+
// Application stage - new change created by code
187+
STARTED("create-users-table"),
188+
APPLIED("create-users-table")
189+
);
190+
191+
// Validate actual table creation
192+
assertTrue(client.listTables().tableNames().contains("users"), "Users table should exist");
193+
194+
// Verify table structure
195+
DescribeTableResponse tableDescription = client.describeTable(
196+
DescribeTableRequest.builder().tableName("users").build()
197+
);
198+
assertEquals("email", tableDescription.table().keySchema().get(0).attributeName());
199+
assertEquals(KeyType.HASH, tableDescription.table().keySchema().get(0).keyType());
151200
}
201+
202+
@Test
203+
@DisplayName("GIVEN mongock audit history empty " +
204+
"WHEN migrating to Flamingock Community" +
205+
"THEN should throw exception")
206+
void GIVEN_mongockAuditHistoryEmpty_WHEN_migratingToFlamingockCommunity_THEN_shouldThrowException() {
207+
// Setup Mongock entries
208+
209+
DynamoDBTargetSystem dynamodbTargetSystem = new DynamoDBTargetSystem("dynamodb-target-system", client);
210+
211+
Runner flamingock = testKit.createBuilder()
212+
.addTargetSystem(dynamodbTargetSystem)
213+
.build();
214+
215+
FlamingockException ex = assertThrows(FlamingockException.class, flamingock::run);
216+
assertEquals("No audit entries found when importing from 'dynamodb-target-system'.", ex.getMessage());
217+
218+
}
219+
152220
}

legacy/mongock-importer-dynamodb/src/test/java/io/flamingock/importer/mongock/dynamodb/DynamoDBMongockTestHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,26 @@ public DynamoDBMongockTestHelper(DynamoDbClient client, String tableName) {
3535
this.table = enhancedClient.table(tableName, TableSchema.fromBean(MongockDynamoDBAuditEntry.class));
3636
}
3737

38+
@Override
3839
public void write(MongockChangeEntry entry) {
3940
table.putItem(convertToMongockDynamoDBAuditEntry(entry));
4041
}
4142

43+
@Override
4244
public int writeAll(List<MongockChangeEntry> entries) {
4345
for (MongockChangeEntry entry : entries) {
4446
table.putItem(convertToMongockDynamoDBAuditEntry(entry));
4547
}
4648
return entries.size();
4749
}
4850

51+
@Override
52+
public void reset() {
53+
table.deleteTable();
54+
}
55+
56+
57+
4958
private MongockDynamoDBAuditEntry convertToMongockDynamoDBAuditEntry(MongockChangeEntry entry) {
5059
return new MongockDynamoDBAuditEntry(
5160
entry.getExecutionId(),

legacy/mongock-importer-dynamodb/src/test/java/io/flamingock/importer/mongock/dynamodb/changes/MongockChange1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.mongock.api.annotations.ChangeUnit;
1919
import io.mongock.api.annotations.Execution;
2020

21-
@ChangeUnit(id = "client-initializer", order = "1", author = "flamingock-team")
21+
@ChangeUnit(id = "mongock-change-1", order = "1", author = "flamingock-team")
2222
public class MongockChange1 {
2323

2424
@Execution

legacy/mongock-importer-dynamodb/src/test/java/io/flamingock/importer/mongock/dynamodb/changes/MongockChange2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.mongock.api.annotations.ChangeUnit;
1919
import io.mongock.api.annotations.Execution;
2020

21-
@ChangeUnit(id = "client-updater", order = "2", author = "flamingock-team")
21+
@ChangeUnit(id = "mongock-change-2", order = "2", author = "flamingock-team")
2222
public class MongockChange2 {
2323

2424
@Execution

legacy/mongock-importer-mongodb/src/test/java/io/flamingock/importer/mongock/mongodb/MongoDBImporterTest.java

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.junit.jupiter.api.Assertions;
3636
import org.junit.jupiter.api.BeforeEach;
3737
import org.junit.jupiter.api.Disabled;
38+
import org.junit.jupiter.api.DisplayName;
3839
import org.junit.jupiter.api.Test;
3940
import org.testcontainers.containers.MongoDBContainer;
4041
import org.testcontainers.junit.jupiter.Container;
@@ -48,6 +49,7 @@
4849
import static io.flamingock.core.kit.audit.AuditEntryExpectation.STARTED;
4950
import static io.flamingock.internal.common.core.metadata.Constants.DEFAULT_MONGOCK_ORIGIN;
5051
import static org.junit.jupiter.api.Assertions.assertEquals;
52+
import static org.junit.jupiter.api.Assertions.assertThrows;
5153

5254
@Testcontainers
5355
@MongockSupport(targetSystem = "mongodb-target-system")
@@ -88,8 +90,12 @@ void tearDown() {
8890
}
8991

9092
@Test
91-
void testImportMongockChangeLogs() {
92-
//adds the Mongock
93+
@DisplayName("GIVEN all Mongock changeUnits already executed" +
94+
"WHEN migrating to Flamingock Community " +
95+
"THEN should import the entire history " +
96+
"AND execute the pending flamingock changes")
97+
void GIVEN_allMongockChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory() {
98+
// Setup Mongock entries
9399
mongockTestHelper.setupBasicScenario();
94100

95101

@@ -107,9 +113,9 @@ void testImportMongockChangeLogs() {
107113
// Legacy imports from Mongock (APPLIED only - no STARTED for imported changes)
108114
APPLIED("system-change-00001_before"),
109115
APPLIED("system-change-00001"),
110-
APPLIED("client-initializer_before"),
111-
APPLIED("client-initializer"),
112-
APPLIED("client-updater"),
116+
APPLIED("mongock-change-1_before"),
117+
APPLIED("mongock-change-1"),
118+
APPLIED("mongock-change-2"),
113119

114120
// System stage - actual system importer change
115121
STARTED("migration-mongock-to-flamingock-community"),
@@ -144,15 +150,83 @@ void testImportMongockChangeLogs() {
144150

145151

146152
@Test
147-
@Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done")
148-
void failIfEmptyOrigin() {
149-
//adds the Mongock
153+
@DisplayName("GIVEN some Mongock changeUnits already executed " +
154+
"AND some other Mongock changeUnits pending for execution" +
155+
"WHEN migrating to Flamingock Community" +
156+
"THEN migrates the history with the executed changeUnits " +
157+
"AND executes the pending Mongock changeUnits " +
158+
"AND executes the pending Flamingock changes")
159+
void GIVEN_someChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommunity_THEN_shouldImportEntireHistory() {
160+
// Setup Mongock entries
161+
mongockTestHelper.setupWithOnlyOneChange();
162+
163+
164+
MongoDBSyncTargetSystem mongodbTargetSystem = new MongoDBSyncTargetSystem("mongodb-target-system", mongoClient, DATABASE_NAME);
150165

151166
Runner flamingock = testKit.createBuilder()
167+
.addTargetSystem(mongodbTargetSystem)
168+
.build();
169+
170+
flamingock.run();
171+
172+
// Verify audit sequence: 11 total entries as shown in actual execution
173+
// Legacy imports only show APPLIED (imported from Mongock), new changes show STARTED+APPLIED
174+
auditHelper.verifyAuditSequenceStrict(
175+
// Legacy imports from Mongock (APPLIED only - no STARTED for imported changes)
176+
APPLIED("system-change-00001_before"),
177+
APPLIED("system-change-00001"),
178+
APPLIED("mongock-change-1_before"),
179+
APPLIED("mongock-change-1"),
180+
181+
// System stage - actual system importer change
182+
STARTED("migration-mongock-to-flamingock-community"),
183+
APPLIED("migration-mongock-to-flamingock-community"),
184+
185+
STARTED("mongock-change-2"),
186+
APPLIED("mongock-change-2"),
187+
188+
// Application stage - new changes created by templates
189+
STARTED("create-users-collection-with-index"),
190+
APPLIED("create-users-collection-with-index"),
191+
STARTED("seed-users"),
192+
APPLIED("seed-users")
193+
);
194+
195+
196+
197+
198+
199+
200+
// Validate actual change
201+
List<Document> users = database.getCollection("users")
202+
.find()
203+
.into(new ArrayList<>());
204+
205+
assertEquals(2, users.size());
206+
Assertions.assertEquals("Admin", users.get(0).getString("name"));
207+
Assertions.assertEquals("admin@company.com", users.get(0).getString("email"));
208+
Assertions.assertEquals("superuser", users.get(0).getList("roles", String.class).get(0));
209+
210+
Assertions.assertEquals("Backup", users.get(1).getString("name"));
211+
Assertions.assertEquals("backup@company.com", users.get(1).getString("email"));
212+
Assertions.assertEquals("readonly", users.get(1).getList("roles", String.class).get(0));
213+
}
214+
215+
@Test
216+
@DisplayName("GIVEN mongock audit history empty " +
217+
"WHEN migrating to Flamingock Community" +
218+
"THEN should throw exception")
219+
void GIVEN_mongockAuditHistoryEmpty_WHEN_migratingToFlamingockCommunity_THEN_shouldThrowException() {
220+
// Setup Mongock entries
221+
222+
MongoDBSyncTargetSystem mongodbTargetSystem = new MongoDBSyncTargetSystem("mongodb-target-system", mongoClient, DATABASE_NAME);
223+
224+
Runner flamingock = testKit.createBuilder()
225+
.addTargetSystem(mongodbTargetSystem)
152226
.build();
153227

154-
//TODO should check error message, but currently it return the summary text
155-
Assertions.assertThrows(FlamingockException.class, flamingock::run);
228+
FlamingockException ex = assertThrows(FlamingockException.class, flamingock::run);
229+
assertEquals("No audit entries found when importing from 'mongodb-target-system'.", ex.getMessage());
156230

157231
}
158232

legacy/mongock-importer-mongodb/src/test/java/io/flamingock/importer/mongock/mongodb/MongoDBMongockTestHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public MongoDBMongockTestHelper(MongoCollection<Document> changeLogCollection) {
3131
this.changeLogCollection = changeLogCollection;
3232
}
3333

34+
@Override
35+
public void reset() {
36+
changeLogCollection.drop();
37+
}
38+
3439
public void write(MongockChangeEntry entry) {
3540
changeLogCollection.insertOne(convertToDocument(entry));
3641
}

legacy/mongock-importer-mongodb/src/test/java/io/flamingock/importer/mongock/mongodb/changes/MongockChange1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.mongock.api.annotations.ChangeUnit;
1919
import io.mongock.api.annotations.Execution;
2020

21-
@ChangeUnit(id = "client-initializer", order = "1", author = "flamingock-team")
21+
@ChangeUnit(id = "mongock-change-1", order = "1", author = "flamingock-team")
2222
public class MongockChange1 {
2323

2424
@Execution

legacy/mongock-importer-mongodb/src/test/java/io/flamingock/importer/mongock/mongodb/changes/MongockChange2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.mongock.api.annotations.ChangeUnit;
1919
import io.mongock.api.annotations.Execution;
2020

21-
@ChangeUnit(id = "client-updater", order = "2", author = "flamingock-team")
21+
@ChangeUnit(id = "mongock-change-2", order = "2", author = "flamingock-team")
2222
public class MongockChange2 {
2323

2424
@Execution

0 commit comments

Comments
 (0)