|
| 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.targetsystem.sql; |
| 17 | + |
| 18 | +import com.zaxxer.hikari.HikariConfig; |
| 19 | +import com.zaxxer.hikari.HikariDataSource; |
| 20 | +import io.flamingock.internal.common.sql.SqlDialect; |
| 21 | +import io.flamingock.internal.core.targets.mark.TargetSystemAuditMark; |
| 22 | +import io.flamingock.internal.core.transaction.TransactionManager; |
| 23 | +import org.junit.jupiter.api.*; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.EnumSource; |
| 26 | +import org.testcontainers.containers.JdbcDatabaseContainer; |
| 27 | +import org.testcontainers.containers.MySQLContainer; |
| 28 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 29 | +import org.testcontainers.containers.MSSQLServerContainer; |
| 30 | +import org.testcontainers.oracle.OracleContainer; |
| 31 | +import org.testcontainers.utility.DockerImageName; |
| 32 | + |
| 33 | +import javax.sql.DataSource; |
| 34 | +import java.sql.*; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +@TestInstance(TestInstance.Lifecycle.PER_METHOD) |
| 38 | +public class SqlAuditMarkerDialectHelperTest { |
| 39 | + |
| 40 | + private static final String ONGOING_TASKS_TABLE = "FLAMINGOCK_ONGOING_TASKS"; |
| 41 | + |
| 42 | + private DataSource dataSource; |
| 43 | + private SqlTargetSystemAuditMarker sqlTargetSystemAuditMarker; |
| 44 | + private TransactionManager<Connection> txManager; |
| 45 | + |
| 46 | + private JdbcDatabaseContainer<?> createContainerForDialect(SqlDialect dialect) { |
| 47 | + switch (dialect) { |
| 48 | + case MYSQL: |
| 49 | + case MARIADB: |
| 50 | + return new MySQLContainer<>(DockerImageName.parse("mysql:8.0")) |
| 51 | + .withDatabaseName("testdb") |
| 52 | + .withUsername("testuser") |
| 53 | + .withPassword("testpass"); |
| 54 | + case POSTGRESQL: |
| 55 | + return new PostgreSQLContainer<>(DockerImageName.parse("postgres:14")) |
| 56 | + .withDatabaseName("testdb") |
| 57 | + .withUsername("testuser") |
| 58 | + .withPassword("testpass"); |
| 59 | + case SQLSERVER: |
| 60 | + case SYBASE: |
| 61 | + return new MSSQLServerContainer<>(DockerImageName.parse("mcr.microsoft.com/mssql/server:2019-latest")) |
| 62 | + .withPassword("YourStrong!Passw0rd") |
| 63 | + .acceptLicense(); |
| 64 | + case ORACLE: |
| 65 | + return new OracleContainer("gvenzl/oracle-free:slim-faststart") |
| 66 | + .withDatabaseName("testdb") |
| 67 | + .withUsername("testuser") |
| 68 | + .withPassword("testpass"); |
| 69 | + default: |
| 70 | + return null; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void initForDialect(SqlDialect dialect, JdbcDatabaseContainer<?> container) { |
| 75 | + if (container != null) { |
| 76 | + container.start(); |
| 77 | + HikariConfig config = new HikariConfig(); |
| 78 | + config.setJdbcUrl(container.getJdbcUrl()); |
| 79 | + config.setUsername(container.getUsername()); |
| 80 | + config.setPassword(container.getPassword()); |
| 81 | + try { |
| 82 | + config.setDriverClassName(container.getDriverClassName()); |
| 83 | + } catch (Exception ignored) { |
| 84 | + } |
| 85 | + dataSource = new HikariDataSource(config); |
| 86 | + } else { |
| 87 | + HikariConfig config = new HikariConfig(); |
| 88 | + switch (dialect) { |
| 89 | + case H2: |
| 90 | + config.setJdbcUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"); |
| 91 | + config.setUsername("testuser"); |
| 92 | + config.setPassword(""); |
| 93 | + config.setDriverClassName("org.h2.Driver"); |
| 94 | + break; |
| 95 | + case SQLITE: |
| 96 | + config.setJdbcUrl("jdbc:sqlite:file:memdb?mode=memory&cache=shared"); |
| 97 | + config.setUsername(""); |
| 98 | + config.setPassword(""); |
| 99 | + config.setDriverClassName("org.sqlite.JDBC"); |
| 100 | + break; |
| 101 | + case HSQLDB: |
| 102 | + config.setJdbcUrl("jdbc:hsqldb:mem:testdb"); |
| 103 | + config.setUsername("testuser"); |
| 104 | + config.setPassword(""); |
| 105 | + config.setDriverClassName("org.hsqldb.jdbc.JDBCDriver"); |
| 106 | + break; |
| 107 | + case DB2: |
| 108 | + config.setJdbcUrl("jdbc:h2:mem:testdb;MODE=DB2;DB_CLOSE_DELAY=-1"); |
| 109 | + config.setUsername("testuser"); |
| 110 | + config.setPassword(""); |
| 111 | + config.setDriverClassName("org.h2.Driver"); |
| 112 | + break; |
| 113 | + default: |
| 114 | + dataSource = null; |
| 115 | + break; |
| 116 | + } |
| 117 | + if (dataSource == null && config.getJdbcUrl() != null) { |
| 118 | + dataSource = new HikariDataSource(config); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + txManager = new TransactionManager<>(() -> { |
| 123 | + try { |
| 124 | + return dataSource.getConnection(); |
| 125 | + } catch (SQLException e) { |
| 126 | + throw new RuntimeException(e); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + sqlTargetSystemAuditMarker = SqlTargetSystemAuditMarker.builder(dataSource, txManager) |
| 131 | + .withTableName(ONGOING_TASKS_TABLE) |
| 132 | + .build(); |
| 133 | + } |
| 134 | + |
| 135 | + private void dropTable() throws SQLException { |
| 136 | + try (Connection c = dataSource.getConnection(); Statement s = c.createStatement()) { |
| 137 | + s.execute("DROP TABLE IF EXISTS " + ONGOING_TASKS_TABLE); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @ParameterizedTest(name = "[{index}] dialect={0} - Should add and list two marks successfully") |
| 142 | + @EnumSource(SqlDialect.class) |
| 143 | + void addOngoingTaskMark(SqlDialect dialect) { |
| 144 | + JdbcDatabaseContainer<?> container = createContainerForDialect(dialect); |
| 145 | + Assumptions.assumeTrue(container != null || |
| 146 | + (dialect != SqlDialect.FIREBIRD && dialect != SqlDialect.INFORMIX), |
| 147 | + "No Test support for " + dialect.name()); |
| 148 | + |
| 149 | + try { |
| 150 | + initForDialect(dialect, container); |
| 151 | + |
| 152 | + // GIVEN |
| 153 | + String taskId1 = "test-task-id1"; |
| 154 | + String taskId2 = "test-task-id2"; |
| 155 | + io.flamingock.internal.common.cloud.vo.TargetSystemAuditMarkType operation = io.flamingock.internal.common.cloud.vo.TargetSystemAuditMarkType.ROLLBACK; |
| 156 | + |
| 157 | + TargetSystemAuditMark mark1 = new TargetSystemAuditMark(taskId1, operation); |
| 158 | + TargetSystemAuditMark mark2 = new TargetSystemAuditMark(taskId2, operation); |
| 159 | + |
| 160 | + // WHEN |
| 161 | + txManager.startSession(taskId1); |
| 162 | + sqlTargetSystemAuditMarker.mark(mark1); |
| 163 | + txManager.closeSession(taskId1); |
| 164 | + txManager.startSession(taskId2); |
| 165 | + sqlTargetSystemAuditMarker.mark(mark2); |
| 166 | + txManager.closeSession(taskId2); |
| 167 | + Set<TargetSystemAuditMark> marks = sqlTargetSystemAuditMarker.listAll(); |
| 168 | + |
| 169 | + // THEN |
| 170 | + Assertions.assertEquals(2, marks.size()); |
| 171 | + } finally { |
| 172 | + if (dataSource != null) { |
| 173 | + try { |
| 174 | + dropTable(); |
| 175 | + } catch (SQLException ignored) { |
| 176 | + } |
| 177 | + if (dataSource instanceof HikariDataSource) { |
| 178 | + ((HikariDataSource) dataSource).close(); |
| 179 | + } |
| 180 | + } |
| 181 | + if (container != null && container.isRunning()) { |
| 182 | + container.stop(); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @ParameterizedTest(name = "[{index}] dialect={0} - Should remove all marks successfully") |
| 188 | + @EnumSource(SqlDialect.class) |
| 189 | + void removeOngoingTaskMark(SqlDialect dialect) { |
| 190 | + JdbcDatabaseContainer<?> container = createContainerForDialect(dialect); |
| 191 | + Assumptions.assumeTrue(container != null || |
| 192 | + (dialect != SqlDialect.FIREBIRD && dialect != SqlDialect.INFORMIX), |
| 193 | + "No Test support for " + dialect.name()); |
| 194 | + |
| 195 | + try { |
| 196 | + initForDialect(dialect, container); |
| 197 | + |
| 198 | + // GIVEN |
| 199 | + String taskId1 = "test-task-id1"; |
| 200 | + String taskId2 = "test-task-id2"; |
| 201 | + io.flamingock.internal.common.cloud.vo.TargetSystemAuditMarkType operation = io.flamingock.internal.common.cloud.vo.TargetSystemAuditMarkType.ROLLBACK; |
| 202 | + |
| 203 | + TargetSystemAuditMark mark1 = new TargetSystemAuditMark(taskId1, operation); |
| 204 | + TargetSystemAuditMark mark2 = new TargetSystemAuditMark(taskId2, operation); |
| 205 | + txManager.startSession(taskId1); |
| 206 | + sqlTargetSystemAuditMarker.mark(mark1); |
| 207 | + txManager.closeSession(taskId1); |
| 208 | + txManager.startSession(taskId2); |
| 209 | + sqlTargetSystemAuditMarker.mark(mark2); |
| 210 | + txManager.closeSession(taskId2); |
| 211 | + |
| 212 | + Set<TargetSystemAuditMark> marks = sqlTargetSystemAuditMarker.listAll(); |
| 213 | + |
| 214 | + // WHEN |
| 215 | + for (TargetSystemAuditMark mark : marks) { |
| 216 | + sqlTargetSystemAuditMarker.clearMark(mark.getTaskId()); |
| 217 | + } |
| 218 | + |
| 219 | + // THEN |
| 220 | + Assertions.assertEquals(0, sqlTargetSystemAuditMarker.listAll().size()); |
| 221 | + } finally { |
| 222 | + if (dataSource != null) { |
| 223 | + try { |
| 224 | + dropTable(); |
| 225 | + } catch (SQLException ignored) { |
| 226 | + } |
| 227 | + if (dataSource instanceof HikariDataSource) { |
| 228 | + ((HikariDataSource) dataSource).close(); |
| 229 | + } |
| 230 | + } |
| 231 | + if (container != null && container.isRunning()) { |
| 232 | + container.stop(); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments