Skip to content

Commit 3299949

Browse files
authored
refactor!: derive MongoDBSyncAuditStore directly from MongoDBSyncTargetSystem (#746)
1 parent 6a3f766 commit 3299949

File tree

8 files changed

+81
-45
lines changed

8 files changed

+81
-45
lines changed

cli/flamingock-cli/src/main/java/io/flamingock/cli/service/AuditService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.flamingock.internal.core.configuration.community.CommunityConfiguration;
4141
import io.flamingock.internal.core.context.SimpleContext;
4242
import io.flamingock.internal.core.store.AuditStore;
43+
import io.flamingock.targetystem.mongodb.sync.MongoDBSyncTargetSystem;
4344
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
4445

4546
import javax.sql.DataSource;
@@ -158,7 +159,7 @@ private AuditStore<?> createMongoAuditStore(Context context) {
158159
// Create MongoDB clients
159160
MongoClient mongoClient = MongoClientFactory.createMongoClient(mongoConfig);
160161

161-
return new MongoDBSyncAuditStore(mongoClient, mongoConfig.getDatabase())
162+
return MongoDBSyncAuditStore.from(new MongoDBSyncTargetSystem("mongodb", mongoClient, mongoConfig.getDatabase()))
162163
.withAuditRepositoryName(mongoConfig.getCollection());
163164
}
164165

community/flamingock-auditstore-mongodb-sync/src/main/java/io/flamingock/community/mongodb/sync/driver/MongoDBSyncAuditStore.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.flamingock.internal.core.store.CommunityAuditStore;
3131
import io.flamingock.internal.util.TimeService;
3232
import io.flamingock.internal.util.id.RunnerId;
33+
import io.flamingock.targetystem.mongodb.sync.MongoDBSyncTargetSystem;
3334

3435
import static io.flamingock.internal.util.constants.CommunityPersistenceConstants.DEFAULT_AUDIT_STORE_NAME;
3536
import static io.flamingock.internal.util.constants.CommunityPersistenceConstants.DEFAULT_LOCK_STORE_NAME;
@@ -52,11 +53,25 @@ public class MongoDBSyncAuditStore implements CommunityAuditStore {
5253
private boolean autoCreate = true;
5354

5455

55-
public MongoDBSyncAuditStore(MongoClient client, String databaseName) {
56+
private MongoDBSyncAuditStore(MongoClient client, String databaseName) {
5657
this.client = client;
5758
this.databaseName = databaseName;
5859
}
5960

61+
/**
62+
* Creates a {@link MongoDBSyncAuditStore} using the same MongoDB client and
63+
* database configured in the given {@link MongoDBSyncTargetSystem}.
64+
* <p>
65+
* Only the underlying MongoDB instance (client + database name) is reused.
66+
* No additional target-system configuration is carried over.
67+
*
68+
* @param targetSystem the target system from which to derive the client and database
69+
* @return a new audit store bound to the same MongoDB instance as the target system
70+
*/
71+
public static MongoDBSyncAuditStore from(MongoDBSyncTargetSystem targetSystem) {
72+
return new MongoDBSyncAuditStore(targetSystem.getClient(), targetSystem.getDatabaseName());
73+
}
74+
6075
public MongoDBSyncAuditStore withAuditRepositoryName(String auditRepositoryName) {
6176
this.auditRepositoryName = auditRepositoryName;
6277
return this;

community/flamingock-auditstore-mongodb-sync/src/test/java/io/flamingock/community/mongodb/sync/MongoSyncAuditPersistenceE2ETest.java renamed to community/flamingock-auditstore-mongodb-sync/src/test/java/io/flamingock/community/mongodb/sync/MongoDBSyncAuditPersistenceE2ETest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void setUp() {
7171
separateMongoClient = MongoClients.create(mongoDBContainer.getConnectionString());
7272

7373
// Initialize test kit with MongoDB persistence
74-
testKit = MongoDBSyncTestKit.create(new MongoDBSyncAuditStore(sharedMongoClient, "test"), sharedMongoClient, database);
74+
testKit = MongoDBSyncTestKit.create(MongoDBSyncAuditStore.from(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test")), sharedMongoClient, database);
7575
auditHelper = testKit.getAuditHelper();
7676
}
7777

@@ -87,6 +87,7 @@ void testCompleteAuditEntryPersistenceInMongoDB() {
8787
String changeId = "non-tx-transactional-false";
8888
LocalDateTime testStart = LocalDateTime.now();
8989
LocalDateTime testEnd = testStart.plusMinutes(5); // Allow enough time for test execution
90+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
9091

9192
// Given-When-Then - Test MongoDB audit persistence with AuditTestSupport
9293
AuditTestSupport.withTestKit(testKit)
@@ -96,8 +97,8 @@ void testCompleteAuditEntryPersistenceInMongoDB() {
9697
.WHEN(() -> {
9798
assertDoesNotThrow(() -> {
9899
testKit.createBuilder()
99-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
100-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
100+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
101+
.addTargetSystem(mongoDBSyncTargetSystem)
101102
.build()
102103
.run();
103104
});
@@ -124,6 +125,7 @@ void testCompleteAuditEntryPersistenceInMongoDB() {
124125
@Test
125126
@DisplayName("Should persist NON_TX txStrategy for transactional=false scenarios")
126127
void testNonTxScenarios() {
128+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
127129
// Given-When-Then - Test NON_TX scenarios with AuditTestSupport
128130
AuditTestSupport.withTestKit(testKit)
129131
.GIVEN_Changes(
@@ -133,8 +135,8 @@ void testNonTxScenarios() {
133135
.WHEN(() -> {
134136
assertDoesNotThrow(() -> {
135137
testKit.createBuilder()
136-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
137-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
138+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
139+
.addTargetSystem(mongoDBSyncTargetSystem)
138140
.addTargetSystem(new NonTransactionalTargetSystem("non-tx-system")) // Non-transactional target system
139141
.build()
140142
.run();
@@ -171,6 +173,7 @@ void testNonTxScenarios() {
171173
@Test
172174
@DisplayName("Should persist TX_SHARED txStrategy when targetSystem not defined in change")
173175
void testTxSharedScenarios() {
176+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
174177
MongoDBSyncTargetSystem sharedTargetSystem = new MongoDBSyncTargetSystem("tx-shared-system", sharedMongoClient, "test"); // Same MongoClient as audit storage
175178

176179

@@ -182,8 +185,8 @@ void testTxSharedScenarios() {
182185
.WHEN(() -> {
183186
assertDoesNotThrow(() -> {
184187
testKit.createBuilder()
185-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
186-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
188+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
189+
.addTargetSystem(mongoDBSyncTargetSystem)
187190
.addTargetSystem(sharedTargetSystem)
188191
.build()
189192
.run();
@@ -207,6 +210,7 @@ void testTxSharedScenarios() {
207210
@Test
208211
@DisplayName("Should persist TX_SEPARATE_NO_MARKER when targetSystem defined and different from auditStore")
209212
void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
213+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
210214
MongoDBSyncTargetSystem sharedTargetSystem = new MongoDBSyncTargetSystem("mongo-system", sharedMongoClient, "test"); // Same MongoClient as audit storage
211215

212216
// Given-When-Then - Test TX_SEPARATE_NO_MARKER scenarios with AuditTestSupport
@@ -217,8 +221,8 @@ void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
217221
.WHEN(() -> {
218222
assertDoesNotThrow(() -> {
219223
testKit.createBuilder()
220-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
221-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
224+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
225+
.addTargetSystem(mongoDBSyncTargetSystem)
222226
.addTargetSystem(sharedTargetSystem)
223227
.build()
224228
.run();
@@ -242,6 +246,7 @@ void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
242246
@Test
243247
@DisplayName("Should persist TX_SEPARATE_NO_MARKER txStrategy for different MongoClient scenario")
244248
void testTxSeparateNoMarkerScenario() {
249+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
245250
MongoDatabase separateDatabase = separateMongoClient.getDatabase("test");
246251
MongoDBSyncTargetSystem separateTargetSystem = new MongoDBSyncTargetSystem("tx-separate-system", separateMongoClient, "test"); // Different MongoClient from audit storage
247252

@@ -253,8 +258,8 @@ void testTxSeparateNoMarkerScenario() {
253258
.WHEN(() -> {
254259
assertDoesNotThrow(() -> {
255260
testKit.createBuilder()
256-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
257-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
261+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
262+
.addTargetSystem(mongoDBSyncTargetSystem)
258263
.addTargetSystem(separateTargetSystem)
259264
.build()
260265
.run();
@@ -278,6 +283,7 @@ void testTxSeparateNoMarkerScenario() {
278283
@Test
279284
@DisplayName("Should persist correct targetSystemId for different target system configurations")
280285
void testTargetSystemIdVariations() {
286+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
281287
MongoDatabase separateDatabase = separateMongoClient.getDatabase("test");
282288

283289
// Given-When-Then - Test multiple target system configurations with AuditTestSupport
@@ -290,8 +296,8 @@ void testTargetSystemIdVariations() {
290296
.WHEN(() -> {
291297
assertDoesNotThrow(() -> {
292298
testKit.createBuilder()
293-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
294-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
299+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
300+
.addTargetSystem(mongoDBSyncTargetSystem)
295301
.addTargetSystem(new NonTransactionalTargetSystem("non-tx-system"))
296302
.addTargetSystem(new MongoDBSyncTargetSystem("tx-separate-system", separateMongoClient, "test"))
297303
.build()
@@ -317,6 +323,7 @@ void testTargetSystemIdVariations() {
317323
@Test
318324
@DisplayName("Should persist multiple changes with different txStrategy configurations correctly")
319325
void testMultipleChangesWithDifferentConfigurations() {
326+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test");
320327

321328

322329
AuditTestSupport.withTestKit(testKit)
@@ -327,8 +334,8 @@ void testMultipleChangesWithDifferentConfigurations() {
327334
).WHEN(() -> assertDoesNotThrow(() -> {
328335
MongoDatabase separateDatabase = separateMongoClient.getDatabase("test");
329336
testKit.createBuilder()
330-
.setAuditStore(new MongoDBSyncAuditStore(sharedMongoClient, "test"))
331-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", sharedMongoClient, "test"))
337+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
338+
.addTargetSystem(mongoDBSyncTargetSystem)
332339
.addTargetSystem(new MongoDBSyncTargetSystem("tx-separate-system", separateMongoClient, "test"))
333340
.build()
334341
.run();

community/flamingock-auditstore-mongodb-sync/src/test/java/io/flamingock/community/mongodb/sync/MongoSyncAuditStoreTest.java renamed to community/flamingock-auditstore-mongodb-sync/src/test/java/io/flamingock/community/mongodb/sync/MongoDBSyncAuditStoreTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class MongoDBSyncAuditStoreTest {
8686
void setupEach() {
8787
mongoClient = MongoClients.create(mongoDBContainer.getConnectionString());
8888
database = mongoClient.getDatabase("test");
89-
testKit = MongoDBSyncTestKit.create(new MongoDBSyncAuditStore(mongoClient, "test"), mongoClient, database);
89+
testKit = MongoDBSyncTestKit.create(MongoDBSyncAuditStore.from(new MongoDBSyncTargetSystem("mongodb", mongoClient, "test")), mongoClient, database);
9090
auditHelper = testKit.getAuditHelper();
9191

9292
mongoDBTestHelper = new MongoDBTestHelper(database);
@@ -102,6 +102,7 @@ void tearDown() {
102102
@Test
103103
@DisplayName("When standalone runs the driver with DEFAULT repository names related collections should exists")
104104
void happyPathWithDefaultRepositoryNames() {
105+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", mongoClient, "test");
105106
//Given-When-Then
106107
AuditTestSupport.withTestKit(testKit)
107108
.GIVEN_Changes(
@@ -110,8 +111,8 @@ void happyPathWithDefaultRepositoryNames() {
110111
new CodeChangeTestDefinition(_003__insert_jorge_happy_transactional.class, Arrays.asList(MongoDatabase.class, ClientSession.class))
111112
)
112113
.WHEN(() -> testKit.createBuilder()
113-
.setAuditStore(new MongoDBSyncAuditStore(mongoClient, "test"))
114-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", mongoClient, "test"))
114+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
115+
.addTargetSystem(mongoDBSyncTargetSystem)
115116
.build()
116117
.run())
117118
.THEN_VerifyAuditSequenceStrict(
@@ -133,6 +134,7 @@ void happyPathWithDefaultRepositoryNames() {
133134
@Test
134135
@DisplayName("When standalone runs the driver with transactions enabled should persist the audit logs and the user's collection updated")
135136
void happyPathWithTransaction() {
137+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", mongoClient, "test");
136138
//Given-When-Then
137139
AuditTestSupport.withTestKit(testKit)
138140
.GIVEN_Changes(
@@ -141,8 +143,8 @@ void happyPathWithTransaction() {
141143
new CodeChangeTestDefinition(_003__insert_jorge_happy_transactional.class, Arrays.asList(MongoDatabase.class, ClientSession.class))
142144
)
143145
.WHEN(() -> testKit.createBuilder()
144-
.setAuditStore(new MongoDBSyncAuditStore(mongoClient, "test"))
145-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", mongoClient, "test"))
146+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
147+
.addTargetSystem(mongoDBSyncTargetSystem)
146148
.build()
147149
.run())
148150
.THEN_VerifyAuditSequenceStrict(
@@ -168,6 +170,7 @@ void happyPathWithTransaction() {
168170
@Test
169171
@DisplayName("When standalone runs the driver with transactions enabled and execution fails should persist only the applied audit logs")
170172
void failedWithTransaction() {
173+
MongoDBSyncTargetSystem mongoDBSyncTargetSystem = new MongoDBSyncTargetSystem("mongodb", mongoClient, "test");
171174
//Given-When-Then
172175
AuditTestSupport.withTestKit(testKit)
173176
.GIVEN_Changes(
@@ -177,8 +180,8 @@ void failedWithTransaction() {
177180
)
178181
.WHEN(() -> assertThrows(PipelineExecutionException.class, () -> {
179182
testKit.createBuilder()
180-
.setAuditStore(new MongoDBSyncAuditStore(mongoClient, "test"))
181-
.addTargetSystem(new MongoDBSyncTargetSystem("mongodb", mongoClient, "test"))
183+
.setAuditStore(MongoDBSyncAuditStore.from(mongoDBSyncTargetSystem))
184+
.addTargetSystem(mongoDBSyncTargetSystem)
182185
.build()
183186
.run();
184187
}))

0 commit comments

Comments
 (0)