Skip to content

Commit 772366d

Browse files
authored
feat: property change.targetSystem.id available as dependency in changes (#737)
1 parent fe22946 commit 772366d

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

core/flamingock-core-commons/src/main/java/io/flamingock/internal/common/core/metadata/FlamingockMetadata.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,40 @@
1818
import io.flamingock.internal.common.core.preview.PreviewPipeline;
1919

2020
public class FlamingockMetadata {
21-
21+
2222
private PreviewPipeline pipeline;
2323
private String setup;
2424
private String configFile;
25-
25+
2626
public FlamingockMetadata() {
2727
}
28-
28+
2929
public FlamingockMetadata(PreviewPipeline pipeline, String setup, String configFile) {
3030
this.pipeline = pipeline;
3131
this.setup = setup;
3232
this.configFile = configFile;
3333
}
34-
34+
3535
public PreviewPipeline getPipeline() {
3636
return pipeline;
3737
}
38-
38+
3939
public void setPipeline(PreviewPipeline pipeline) {
4040
this.pipeline = pipeline;
4141
}
42-
42+
4343
public String getSetup() {
4444
return setup;
4545
}
46-
46+
4747
public void setSetup(String setup) {
4848
this.setup = setup;
4949
}
50-
50+
5151
public String getPipelineFile() {
5252
return configFile;
5353
}
54-
54+
5555
public void setPipelineFile(String configFile) {
5656
this.configFile = configFile;
5757
}

core/flamingock-core/src/main/java/io/flamingock/internal/core/targets/AbstractTargetSystem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ public abstract class AbstractTargetSystem<HOLDER extends AbstractTargetSystem<H
6969
ContextConfigurable<HOLDER> {
7070
private final String id;
7171

72-
protected Context targetSystemContext = new SimpleContext();
72+
protected final Context targetSystemContext = new SimpleContext();
7373

7474
public AbstractTargetSystem(String id) {
7575
this.id = id;
76+
targetSystemContext.setProperty("change.targetSystem.id", id);
77+
7678
}
7779

7880
@Override

e2e/core-e2e/src/test/java/io/flamingock/core/e2e/BuilderE2ETest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.flamingock.common.test.pipeline.CodeChangeTestDefinition;
1919
import io.flamingock.common.test.pipeline.PipelineTestHelper;
2020
import io.flamingock.core.e2e.changes._008__TargetSystemManagerInjectionChange;
21+
import io.flamingock.core.e2e.changes._009__TargetSystemIdInjectionChange;
2122
import io.flamingock.core.e2e.helpers.Counter;
2223
import io.flamingock.core.kit.audit.AuditTestHelper;
2324
import io.flamingock.core.kit.inmemory.InMemoryTestKit;
@@ -30,9 +31,11 @@
3031
import org.mockito.Mockito;
3132

3233
import java.util.Arrays;
34+
import java.util.Collections;
3335

3436
import static io.flamingock.core.kit.audit.AuditEntryExpectation.APPLIED;
3537
import static io.flamingock.core.kit.audit.AuditEntryExpectation.STARTED;
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
3639
import static org.junit.jupiter.api.Assertions.assertTrue;
3740

3841

@@ -76,4 +79,44 @@ void shouldInjectTargetSystemManagerInChange() {
7679
APPLIED("test8-target-system-manager-injection")
7780
);
7881
}
82+
83+
@Test
84+
@DisplayName("Should inject target system ID as dependency in change via @Named annotation")
85+
void shouldInjectTargetSystemIdInChange() {
86+
// Given - Create isolated test kit with domain-separated helpers
87+
InMemoryTestKit testKit = InMemoryTestKit.create();
88+
AuditTestHelper auditHelper = testKit.getAuditHelper();
89+
90+
Counter counter = new Counter();
91+
92+
NonTransactionalTargetSystem targetSystem = new NonTransactionalTargetSystem("kafka")
93+
.addDependency(counter);
94+
95+
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
96+
mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn(
97+
PipelineTestHelper.getPreviewPipeline(
98+
new CodeChangeTestDefinition(
99+
_009__TargetSystemIdInjectionChange.class,
100+
Collections.singletonList(Counter.class)
101+
)
102+
)
103+
);
104+
105+
// When - Execute using test builder
106+
testKit.createBuilder()
107+
.addTargetSystem(targetSystem)
108+
.build()
109+
.run();
110+
}
111+
112+
// Then - Verify that target system ID was successfully injected
113+
assertTrue(counter.isExecuted(), "Counter.executed should be true, indicating target system ID was injected");
114+
assertEquals("kafka", counter.getTargetSystemId(), "Target system ID should match the configured ID");
115+
116+
// Verify complete audit flow
117+
auditHelper.verifyAuditSequenceStrict(
118+
STARTED("test9-target-system-id-injection"),
119+
APPLIED("test9-target-system-id-injection")
120+
);
121+
}
79122
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2025 Flamingock (https://www.flamingock.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.flamingock.core.e2e.changes;
17+
18+
import io.flamingock.api.annotations.Change;
19+
import io.flamingock.api.annotations.Apply;
20+
import io.flamingock.api.annotations.NonLockGuarded;
21+
import io.flamingock.api.annotations.TargetSystem;
22+
import io.flamingock.core.e2e.helpers.Counter;
23+
24+
import javax.inject.Named;
25+
26+
/**
27+
* Change that receives the target system ID as a dependency to verify
28+
* that it can be injected via @Named("change.targetSystem.id").
29+
*/
30+
@Change(id = "test9-target-system-id-injection", transactional = false, author = "aperezdieppa")
31+
@TargetSystem(id = "kafka")
32+
public class _009__TargetSystemIdInjectionChange {
33+
34+
@Apply
35+
public void apply(@Named("change.targetSystem.id") String targetSystemId, @NonLockGuarded Counter counter) {
36+
if (targetSystemId != null && !targetSystemId.isEmpty()) {
37+
counter.setTargetSystemId(targetSystemId);
38+
counter.setExecuted(true);
39+
System.out.println("Target system ID successfully injected: " + targetSystemId);
40+
} else {
41+
throw new RuntimeException("Target system ID was not injected");
42+
}
43+
}
44+
}

e2e/core-e2e/src/test/java/io/flamingock/core/e2e/helpers/Counter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public class Counter {
1919
private boolean executed = false;
2020
private boolean rollbacked = false;
21+
private String targetSystemId;
2122

2223
public boolean isExecuted() {
2324
return executed;
@@ -34,5 +35,13 @@ public boolean isRollbacked() {
3435
public void setRollbacked(boolean rollbacked) {
3536
this.rollbacked = rollbacked;
3637
}
38+
39+
public String getTargetSystemId() {
40+
return targetSystemId;
41+
}
42+
43+
public void setTargetSystemId(String targetSystemId) {
44+
this.targetSystemId = targetSystemId;
45+
}
3746
}
3847

0 commit comments

Comments
 (0)