Skip to content

Commit 8aee497

Browse files
feat: add test to assert dependency injection in rollback (#718)
1 parent 796acd7 commit 8aee497

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717

1818
import io.flamingock.common.test.pipeline.CodeChangeTestDefinition;
1919
import io.flamingock.common.test.pipeline.PipelineTestHelper;
20-
import io.flamingock.core.e2e.changes._006__FailingTransactionalChange;
21-
import io.flamingock.core.e2e.changes._003__MultiTest1NonTransactionalChange;
22-
import io.flamingock.core.e2e.changes._004__MultiTest2TransactionalChange;
23-
import io.flamingock.core.e2e.changes._005__SecondRunNonTransactionalChange;
24-
import io.flamingock.core.e2e.changes._001__SimpleNonTransactionalChange;
25-
import io.flamingock.core.e2e.changes._002__SimpleTransactionalChange;
20+
import io.flamingock.core.e2e.changes.*;
21+
import io.flamingock.core.e2e.helpers.Counter;
2622
import io.flamingock.core.kit.audit.AuditTestHelper;
2723
import io.flamingock.core.kit.inmemory.InMemoryTestKit;
2824
import io.flamingock.core.processor.util.Deserializer;
@@ -42,8 +38,7 @@
4238
import static io.flamingock.core.kit.audit.AuditEntryExpectation.FAILED;
4339
import static io.flamingock.core.kit.audit.AuditEntryExpectation.ROLLED_BACK;
4440
import static io.flamingock.core.kit.audit.AuditEntryExpectation.STARTED;
45-
import static org.junit.jupiter.api.Assertions.assertEquals;
46-
import static org.junit.jupiter.api.Assertions.assertThrows;
41+
import static org.junit.jupiter.api.Assertions.*;
4742

4843

4944
class CoreStrategiesE2ETest {
@@ -230,4 +225,43 @@ void testAlreadyAppliedChangesSkipping() {
230225
APPLIED("test5-second-run-change")
231226
);
232227
}
228+
229+
@Test
230+
@DisplayName("Should inject dependencies in rollback for NON-TX change")
231+
void testDependencyInjectionInRollbackForNonTxChange() {
232+
InMemoryTestKit testKit = InMemoryTestKit.create();
233+
AuditTestHelper auditHelper = testKit.getAuditHelper();
234+
235+
Counter counter = new Counter();
236+
237+
NonTransactionalTargetSystem targetSystem = new NonTransactionalTargetSystem("kafka")
238+
.addDependency(counter);
239+
240+
try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) {
241+
mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn(
242+
PipelineTestHelper.getPreviewPipeline(
243+
new CodeChangeTestDefinition(_007__SimpleNonTransactionalChangeWithError.class, Collections.emptyList())
244+
)
245+
);
246+
247+
PipelineExecutionException exception = assertThrows(PipelineExecutionException.class, () -> {
248+
testKit.createBuilder()
249+
.addTargetSystem(targetSystem)
250+
.build()
251+
.run();
252+
});
253+
254+
assertNotNull(exception);
255+
assertTrue(exception.getMessage().contains("Intentional failure"));
256+
}
257+
258+
assertTrue(counter.isExecuted(), "Counter.executed should be true after execution");
259+
assertTrue(counter.isRollbacked(), "Counter.rollbacked should be true after rollback");
260+
261+
auditHelper.verifyAuditSequenceStrict(
262+
STARTED("test1-non-tx-change"),
263+
FAILED("test1-non-tx-change"),
264+
ROLLED_BACK("test1-non-tx-change")
265+
);
266+
}
233267
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.*;
19+
import io.flamingock.core.e2e.helpers.Counter;
20+
21+
import javax.inject.Named;
22+
23+
/**
24+
* Simple non-transactional change for testing core execution strategies.
25+
* Does not require any external dependencies.
26+
*/
27+
@Change(id = "test1-non-tx-change", transactional = false, author = "aperezdieppa")
28+
@TargetSystem(id = "kafka")
29+
public class _007__SimpleNonTransactionalChangeWithError {
30+
31+
@Apply
32+
public void execution(@NonLockGuarded Counter counter) {
33+
counter.setExecuted(true);
34+
throw new RuntimeException("Intentional failure");
35+
}
36+
37+
@Rollback
38+
public void rollback(@NonLockGuarded Counter counter) {
39+
counter.setRollbacked(true);
40+
System.out.println("Rolling back failing transactional change");
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.helpers;
17+
18+
public class Counter {
19+
private boolean executed = false;
20+
private boolean rollbacked = false;
21+
22+
public boolean isExecuted() {
23+
return executed;
24+
}
25+
26+
public void setExecuted(boolean executed) {
27+
this.executed = executed;
28+
}
29+
30+
public boolean isRollbacked() {
31+
return rollbacked;
32+
}
33+
34+
public void setRollbacked(boolean rollbacked) {
35+
this.rollbacked = rollbacked;
36+
}
37+
}
38+

0 commit comments

Comments
 (0)