Skip to content

Commit dfbc8a0

Browse files
authored
refactor!: derive DynamoDBAuditStore directly from DynamoDBTargetSystem (#747)
1 parent 3299949 commit dfbc8a0

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
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.targetsystem.dynamodb.DynamoDBTargetSystem;
4344
import io.flamingock.targetystem.mongodb.sync.MongoDBSyncTargetSystem;
4445
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
4546

@@ -169,7 +170,7 @@ private AuditStore<?> createDynamoAuditStore(Context context) {
169170
// Create DynamoDB client
170171
DynamoDbClient dynamoClient = DynamoDBClientFactory.createDynamoDBClient(dynamoConfig);
171172

172-
return new DynamoDBAuditStore(dynamoClient)
173+
return DynamoDBAuditStore.from(new DynamoDBTargetSystem("dynamo", dynamoClient))
173174
.withAuditRepositoryName(dynamoConfig.getTable());
174175
}
175176

community/flamingock-auditstore-dynamodb/src/main/java/io/flamingock/community/dynamodb/driver/DynamoDBAuditStore.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.flamingock.internal.core.store.CommunityAuditStore;
2727
import io.flamingock.internal.common.core.context.ContextResolver;
2828
import io.flamingock.community.dynamodb.internal.DynamoDBAuditPersistence;
29+
import io.flamingock.targetsystem.dynamodb.DynamoDBTargetSystem;
2930
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
3031

3132
public class DynamoDBAuditStore implements CommunityAuditStore {
@@ -41,10 +42,24 @@ public class DynamoDBAuditStore implements CommunityAuditStore {
4142
private long writeCapacityUnits = 5L;
4243
private boolean autoCreate = true;
4344

44-
public DynamoDBAuditStore(DynamoDbClient client) {
45+
private DynamoDBAuditStore(DynamoDbClient client) {
4546
this.client = client;
4647
}
4748

49+
/**
50+
* Creates a {@link DynamoDBAuditStore} using the same DynamoDB client
51+
* configured in the given {@link DynamoDBTargetSystem}.
52+
* <p>
53+
* Only the underlying DynamoDB instance (client) is reused.
54+
* No additional target-system configuration is carried over.
55+
*
56+
* @param targetSystem the target system from which to derive the client
57+
* @return a new audit store bound to the same DynamoDB instance as the target system
58+
*/
59+
public static DynamoDBAuditStore from(DynamoDBTargetSystem targetSystem) {
60+
return new DynamoDBAuditStore(targetSystem.getClient());
61+
}
62+
4863
public DynamoDBAuditStore withAuditRepositoryName(String auditRepositoryName) {
4964
this.auditRepositoryName = auditRepositoryName;
5065
return this;

community/flamingock-auditstore-dynamodb/src/test/java/io/flamingock/community/dynamodb/DynamoDBAuditPersistenceE2ETest.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void setUp() {
7272

7373

7474
// Initialize test kit with DynamoDB persistence using the same client as the driver
75-
testKit = DynamoDBTestKit.create(sharedDynamoDBClient, new DynamoDBAuditStore(sharedDynamoDBClient));
75+
testKit = DynamoDBTestKit.create(sharedDynamoDBClient, DynamoDBAuditStore.from(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient)));
7676
auditTestHelper = testKit.getAuditHelper();
7777

7878
}
@@ -89,6 +89,7 @@ void testCompleteAuditEntryPersistenceInDynamoDB() {
8989
String changeId = "non-tx-transactional-false";
9090
LocalDateTime testStart = LocalDateTime.now();
9191
LocalDateTime testEnd = testStart.plusMinutes(5); // Allow enough time for test execution
92+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
9293

9394
// When-Then - Complete audit verification within AuditTestSupport framework
9495
AuditTestSupport.withTestKit(testKit)
@@ -99,8 +100,8 @@ void testCompleteAuditEntryPersistenceInDynamoDB() {
99100
.WHEN(() -> {
100101
assertDoesNotThrow(() -> {
101102
FlamingockFactory.getCommunityBuilder()
102-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
103-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
103+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
104+
.addTargetSystem(dynamoDBTargetSystem)
104105
.build()
105106
.run();
106107
});
@@ -132,6 +133,7 @@ void testCompleteAuditEntryPersistenceInDynamoDB() {
132133
@DisplayName("Should persist NON_TX txStrategy for transactional=false scenarios")
133134
void testNonTxScenarios() {
134135
// Given-When-Then - Test NON_TX scenarios
136+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
135137
AuditTestSupport.withTestKit(testKit)
136138

137139
.GIVEN_Changes(
@@ -141,8 +143,8 @@ void testNonTxScenarios() {
141143
.WHEN(() -> {
142144
assertDoesNotThrow(() -> {
143145
FlamingockFactory.getCommunityBuilder()
144-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
145-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
146+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
147+
.addTargetSystem(dynamoDBTargetSystem)
146148
.addTargetSystem(new NonTransactionalTargetSystem("non-tx-system")) // Non-transactional target system
147149
.addDependency(sharedDynamoDBClient)
148150
.build()
@@ -181,6 +183,7 @@ void testNonTxScenarios() {
181183
@DisplayName("Should persist NON_TX txStrategy for transactional=false scenarios without dependency injection")
182184
void testNonTxScenariosWithoutDependencies() {
183185
// Given-When-Then - Test NON_TX scenarios
186+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
184187
AuditTestSupport.withTestKit(testKit)
185188

186189
.GIVEN_Changes(
@@ -190,8 +193,8 @@ void testNonTxScenariosWithoutDependencies() {
190193
.WHEN(() -> {
191194
assertDoesNotThrow(() -> {
192195
FlamingockFactory.getCommunityBuilder()
193-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
194-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
196+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
197+
.addTargetSystem(dynamoDBTargetSystem)
195198
.addTargetSystem(new NonTransactionalTargetSystem("non-tx-system")) // Non-transactional target system
196199
.build()
197200
.run();
@@ -229,6 +232,7 @@ void testNonTxScenariosWithoutDependencies() {
229232
@DisplayName("Should persist TX_SHARED txStrategy for default and explicit same DynamoDBClient scenarios")
230233
void testTxSharedScenarios() {
231234
// Given-When-Then - Test TX_SHARED scenarios
235+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
232236
AuditTestSupport.withTestKit(testKit)
233237

234238
.GIVEN_Changes(
@@ -239,8 +243,8 @@ void testTxSharedScenarios() {
239243
DynamoDBTargetSystem sharedTargetSystem = new DynamoDBTargetSystem("tx-shared-system", sharedDynamoDBClient);
240244

241245
FlamingockFactory.getCommunityBuilder()
242-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
243-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
246+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
247+
.addTargetSystem(dynamoDBTargetSystem)
244248
.addTargetSystem(sharedTargetSystem)
245249
.build()
246250
.run();
@@ -266,6 +270,7 @@ void testTxSharedScenarios() {
266270
@DisplayName("Should persist TX_SEPARATE_NO_MARKER when targetSystem defined and different from auditStore")
267271
void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
268272
// Given-When-Then - Test TX_SEPARATE_NO_MARKER scenarios with AuditTestSupport
273+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
269274
AuditTestSupport.withTestKit(testKit)
270275

271276
.GIVEN_Changes(
@@ -274,8 +279,8 @@ void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
274279
.WHEN(() -> {
275280
assertDoesNotThrow(() -> {
276281
FlamingockFactory.getCommunityBuilder()
277-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
278-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
282+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
283+
.addTargetSystem(dynamoDBTargetSystem)
279284
.addTargetSystem(new DynamoDBTargetSystem("mongo-system", separateDynamoDBClient))
280285
.build()
281286
.run();
@@ -300,6 +305,7 @@ void testTxNoMarkerWhenSameMongoClientButDifferentTargetSystem() {
300305
@Test
301306
@DisplayName("Should persist TX_SEPARATE_NO_MARKER txStrategy for different DynamoDBClient scenario")
302307
void testTxSeparateNoMarkerScenario() {
308+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
303309
DynamoDBTargetSystem separateTargetSystem = new DynamoDBTargetSystem("mongo-system", separateDynamoDBClient); // Different DynamoDbClient from audit storage
304310

305311
// Given-When-Then - Test TX_SEPARATE_NO_MARKER scenarios with AuditTestSupport
@@ -311,8 +317,8 @@ void testTxSeparateNoMarkerScenario() {
311317
.WHEN(() -> {
312318
assertDoesNotThrow(() -> {
313319
FlamingockFactory.getCommunityBuilder()
314-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
315-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
320+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
321+
.addTargetSystem(dynamoDBTargetSystem)
316322
.addTargetSystem(separateTargetSystem)
317323
.build()
318324
.run();
@@ -337,6 +343,7 @@ void testTxSeparateNoMarkerScenario() {
337343
@DisplayName("Should persist correct targetSystemId for different target system configurations")
338344
void testTargetSystemIdVariations() {
339345
// Given-When-Then - Test multiple target system configurations with AuditTestSupport
346+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
340347
AuditTestSupport.withTestKit(testKit)
341348

342349
.GIVEN_Changes(
@@ -347,8 +354,8 @@ void testTargetSystemIdVariations() {
347354
.WHEN(() -> {
348355
assertDoesNotThrow(() -> {
349356
FlamingockFactory.getCommunityBuilder()
350-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
351-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
357+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
358+
.addTargetSystem(dynamoDBTargetSystem)
352359
.addTargetSystem(new NonTransactionalTargetSystem("non-tx-system"))
353360
.addTargetSystem(new DynamoDBTargetSystem("mongo-system", separateDynamoDBClient))
354361
.addDependency(sharedDynamoDBClient)
@@ -377,6 +384,7 @@ void testTargetSystemIdVariations() {
377384
@DisplayName("Should persist multiple changes with different txStrategy configurations correctly")
378385
void testMultipleChangesWithDifferentConfigurations() {
379386
// Given-When-Then - Test comprehensive txStrategy scenarios with AuditTestSupport
387+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient);
380388
AuditTestSupport.withTestKit(testKit)
381389

382390
.GIVEN_Changes(
@@ -387,8 +395,8 @@ void testMultipleChangesWithDifferentConfigurations() {
387395
.WHEN(() -> {
388396
assertDoesNotThrow(() -> {
389397
FlamingockFactory.getCommunityBuilder()
390-
.setAuditStore(new DynamoDBAuditStore(sharedDynamoDBClient))
391-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", sharedDynamoDBClient))
398+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
399+
.addTargetSystem(dynamoDBTargetSystem)
392400
.addTargetSystem(new DynamoDBTargetSystem("mongo-system",separateDynamoDBClient))
393401
.build()
394402
.run();

community/flamingock-auditstore-dynamodb/src/test/java/io/flamingock/community/dynamodb/DynamoDBAuditStoreTest.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void beforeEach() {
8282
client = DynamoDBTestContainer.createClient(dynamoDBContainer);
8383

8484
// Initialize test kit with DynamoDB persistence using the same client as the driver
85-
testKit = DynamoDBTestKit.create(client, new DynamoDBAuditStore(client));
85+
testKit = DynamoDBTestKit.create(client, DynamoDBAuditStore.from(new DynamoDBTargetSystem("dynamodb", client)));
8686
auditHelper = testKit.getAuditHelper();
8787
}
8888

@@ -96,15 +96,16 @@ void afterEach() {
9696
@DisplayName("When standalone runs the driver with DEFAULT repository names related tables should exists")
9797
void happyPathWithDefaultRepositoryNames() {
9898
// Given-When
99+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", client);
99100
AuditTestSupport.withTestKit(testKit)
100101
.GIVEN_Changes(
101102
new CodeChangeTestDefinition(_001__create_client_collection_happy.class, Collections.singletonList(DynamoDbClient.class)),
102103
new CodeChangeTestDefinition(_002__insert_federico_happy_transactional.class, Arrays.asList(DynamoDbClient.class, TransactWriteItemsEnhancedRequest.Builder.class)),
103104
new CodeChangeTestDefinition(_004__insert_jorge_happy_transactional.class, Arrays.asList(DynamoDbClient.class, TransactWriteItemsEnhancedRequest.Builder.class)))
104105
.WHEN(() -> {
105106
FlamingockFactory.getCommunityBuilder()
106-
.setAuditStore(new DynamoDBAuditStore(client))
107-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", client))
107+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
108+
.addTargetSystem(dynamoDBTargetSystem)
108109
.build()
109110
.run();
110111
})
@@ -122,6 +123,7 @@ void happyPathWithDefaultRepositoryNames() {
122123
@DisplayName("When standalone runs the driver with transactions enabled should persist the audit logs and the user's table updated")
123124
void happyPathWithTransaction() {
124125
// Given-When-Then
126+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", client);
125127
AuditTestSupport.withTestKit(testKit)
126128
.GIVEN_Changes(
127129
new CodeChangeTestDefinition(_001__create_client_collection_happy.class, Collections.singletonList(DynamoDbClient.class)),
@@ -130,14 +132,14 @@ void happyPathWithTransaction() {
130132
.WHEN(() -> {
131133
// Run pipeline twice to verify repeated execution
132134
FlamingockFactory.getCommunityBuilder()
133-
.setAuditStore(new DynamoDBAuditStore(client))
134-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", client))
135+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
136+
.addTargetSystem(dynamoDBTargetSystem)
135137
.build()
136138
.run();
137139

138140
FlamingockFactory.getCommunityBuilder()
139-
.setAuditStore(new DynamoDBAuditStore(client))
140-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", client))
141+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
142+
.addTargetSystem(dynamoDBTargetSystem)
141143
.build()
142144
.run();
143145
})
@@ -168,6 +170,7 @@ void happyPathWithTransaction() {
168170
@DisplayName("When standalone runs the driver and execution fails should persist only the applied audit logs")
169171
void failedWithTransaction() {
170172
// Given-When-Then - Test failure scenario with audit verification
173+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", client);
171174
AuditTestSupport.withTestKit(testKit)
172175
.GIVEN_Changes(
173176
new CodeChangeTestDefinition(_001__create_client_collection_happy.class, Collections.singletonList(DynamoDbClient.class)),
@@ -176,8 +179,8 @@ void failedWithTransaction() {
176179
.WHEN(() -> {
177180
assertThrows(PipelineExecutionException.class, () -> {
178181
FlamingockFactory.getCommunityBuilder()
179-
.setAuditStore(new DynamoDBAuditStore(client))
180-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", client))
182+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem))
183+
.addTargetSystem(dynamoDBTargetSystem)
181184
.build()
182185
.run();
183186
});
@@ -232,18 +235,19 @@ void auditStoreWithAutoCreateFalseDoesNotCreateTables() {
232235
Collections.emptyList()
233236
);
234237

238+
DynamoDBTargetSystem dynamoDBTargetSystem = new DynamoDBTargetSystem("dynamodb", client);
235239
AuditTestSupport.withTestKit(testKit)
236240
.GIVEN_Changes(
237241
new CodeChangeTestDefinition(_001__create_client_collection_happy.class, Collections.singletonList(DynamoDbClient.class)),
238242
new CodeChangeTestDefinition(_002__insert_federico_happy_transactional.class, Arrays.asList(DynamoDbClient.class, TransactWriteItemsEnhancedRequest.Builder.class)),
239243
new CodeChangeTestDefinition(_004__insert_jorge_happy_transactional.class, Arrays.asList(DynamoDbClient.class, TransactWriteItemsEnhancedRequest.Builder.class)))
240244
.WHEN(() -> {
241245
FlamingockFactory.getCommunityBuilder()
242-
.setAuditStore(new DynamoDBAuditStore(client)
246+
.setAuditStore(DynamoDBAuditStore.from(dynamoDBTargetSystem)
243247
.withAutoCreate(false)
244248
.withAuditRepositoryName(CUSTOM_AUDIT_REPOSITORY_NAME)
245249
.withLockRepositoryName(CUSTOM_LOCK_REPOSITORY_NAME))
246-
.addTargetSystem(new DynamoDBTargetSystem("dynamodb", client))
250+
.addTargetSystem(dynamoDBTargetSystem)
247251
.build()
248252
.run();
249253
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void setUp() {
8787
mongockTestHelper = new DynamoDBMongockTestHelper(client, DEFAULT_MONGOCK_ORIGIN);
8888

8989
// Initialize TestKit for unified testing
90-
testKit = DynamoDBTestKit.create(client, new DynamoDBAuditStore(client));
90+
testKit = DynamoDBTestKit.create(client, DynamoDBAuditStore.from(new DynamoDBTargetSystem("dynamodb", client)));
9191
auditHelper = testKit.getAuditHelper();
9292

9393
}

0 commit comments

Comments
 (0)