|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.flamingock.springboot.support; |
| 18 | + |
| 19 | +import io.flamingock.common.test.pipeline.CodeChangeTestDefinition; |
| 20 | +import io.flamingock.common.test.pipeline.PipelineTestHelper; |
| 21 | +import io.flamingock.internal.common.core.util.Deserializer; |
| 22 | +import io.flamingock.internal.core.builder.AbstractChangeRunnerBuilder; |
| 23 | +import io.flamingock.internal.core.runner.PipelineExecutionException; |
| 24 | +import io.flamingock.springboot.support.changes.*; |
| 25 | +import io.flamingock.support.FlamingockTestSupport; |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.mockito.MockedStatic; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.core.env.Environment; |
| 32 | +import org.springframework.test.annotation.DirtiesContext; |
| 33 | + |
| 34 | +import java.util.Collections; |
| 35 | + |
| 36 | +import static io.flamingock.support.domain.AuditEntryDefinition.*; |
| 37 | +import static org.junit.jupiter.api.Assertions.*; |
| 38 | + |
| 39 | +@FlamingockIntegrationTest(classes = TestApplication.class) |
| 40 | +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) |
| 41 | +@DisplayName("Spring Boot Integration - FlamingockIntegrationTest Annotation") |
| 42 | +class FlamingockIntegrationTestAnnotationTest { |
| 43 | + |
| 44 | + @Autowired |
| 45 | + private AbstractChangeRunnerBuilder<?, ?> builder; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private Environment environment; |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("Should inject Flamingock builder as Spring bean") |
| 52 | + void shouldInjectFlamingockBuilder() { |
| 53 | + assertNotNull(builder, "Builder should be injected by Spring"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("Should provide test infrastructure via Spring beans") |
| 58 | + void shouldProvideTestInfrastructure() { |
| 59 | + assertNotNull(builder, "Builder bean should be provided by test configuration"); |
| 60 | + assertNotNull(environment, "Environment should be available"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("Should execute non-transactional change") |
| 65 | + void shouldExecuteNonTransactionalChange() { |
| 66 | + try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) { |
| 67 | + mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn( |
| 68 | + PipelineTestHelper.getPreviewPipeline( |
| 69 | + new CodeChangeTestDefinition(_001__SpringBootTestChange.class, Collections.emptyList()) |
| 70 | + ) |
| 71 | + ); |
| 72 | + |
| 73 | + FlamingockTestSupport |
| 74 | + .given(builder) |
| 75 | + .whenRun() |
| 76 | + .thenExpectAuditSequenceStrict( |
| 77 | + APPLIED(_001__SpringBootTestChange.class) |
| 78 | + ) |
| 79 | + .verify(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("Should verify multiple changes execute in correct sequence with complete audit flow") |
| 85 | + void shouldVerifyMultipleChangesInSequence() { |
| 86 | + try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) { |
| 87 | + mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn( |
| 88 | + PipelineTestHelper.getPreviewPipeline( |
| 89 | + new CodeChangeTestDefinition(_001__SpringBootTestChange.class, Collections.emptyList()), |
| 90 | + new CodeChangeTestDefinition(_002__SpringBootTransactionalChange.class, Collections.emptyList()) |
| 91 | + ) |
| 92 | + ); |
| 93 | + |
| 94 | + FlamingockTestSupport |
| 95 | + .given(builder) |
| 96 | + .whenRun() |
| 97 | + .thenExpectAuditSequenceStrict( |
| 98 | + APPLIED(_001__SpringBootTestChange.class), |
| 99 | + APPLIED(_002__SpringBootTransactionalChange.class) |
| 100 | + ) |
| 101 | + .verify(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @DisplayName("Should verify failing transactional change triggers rollback with correct audit trail") |
| 107 | + void shouldVerifyFailingTransactionalChangeTriggersRollback() { |
| 108 | + try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) { |
| 109 | + mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn( |
| 110 | + PipelineTestHelper.getPreviewPipeline( |
| 111 | + new CodeChangeTestDefinition(_003__SpringBootFailingChange.class, Collections.emptyList(), Collections.emptyList()) |
| 112 | + ) |
| 113 | + ); |
| 114 | + |
| 115 | + FlamingockTestSupport |
| 116 | + .given(builder) |
| 117 | + .whenRun() |
| 118 | + .thenExpectException(PipelineExecutionException.class, ex -> |
| 119 | + assertTrue(ex.getMessage().contains("Intentional test failure")) |
| 120 | + ) |
| 121 | + .andExpectAuditSequenceStrict( |
| 122 | + FAILED(_003__SpringBootFailingChange.class), |
| 123 | + ROLLED_BACK(_003__SpringBootFailingChange.class) |
| 124 | + ) |
| 125 | + .verify(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + @DisplayName("Should verify already-applied changes are skipped on subsequent runs") |
| 131 | + void shouldVerifyAlreadyAppliedChangesAreSkipped() { |
| 132 | + try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) { |
| 133 | + mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn( |
| 134 | + PipelineTestHelper.getPreviewPipeline( |
| 135 | + new CodeChangeTestDefinition(_001__SpringBootTestChange.class, Collections.emptyList()) |
| 136 | + ) |
| 137 | + ); |
| 138 | + |
| 139 | + FlamingockTestSupport |
| 140 | + .given(builder) |
| 141 | + .andExistingAudit( |
| 142 | + APPLIED(_001__SpringBootTestChange.class) |
| 143 | + ) |
| 144 | + .whenRun() |
| 145 | + .thenExpectAuditSequenceStrict( |
| 146 | + APPLIED(_001__SpringBootTestChange.class) |
| 147 | + ) |
| 148 | + .verify(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @DisplayName("Should verify transactional change executes successfully with correct audit entries") |
| 154 | + void shouldVerifyTransactionalChangeExecutesSuccessfully() { |
| 155 | + try (MockedStatic<Deserializer> mocked = Mockito.mockStatic(Deserializer.class)) { |
| 156 | + mocked.when(Deserializer::readPreviewPipelineFromFile).thenReturn( |
| 157 | + PipelineTestHelper.getPreviewPipeline( |
| 158 | + new CodeChangeTestDefinition(_002__SpringBootTransactionalChange.class, Collections.emptyList()) |
| 159 | + ) |
| 160 | + ); |
| 161 | + |
| 162 | + FlamingockTestSupport |
| 163 | + .given(builder) |
| 164 | + .whenRun() |
| 165 | + .thenExpectAuditSequenceStrict( |
| 166 | + APPLIED(_002__SpringBootTransactionalChange.class) |
| 167 | + ) |
| 168 | + .verify(); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
0 commit comments