Skip to content

Commit 9f087ac

Browse files
committed
feat: improve sql audit store test with singleton containers
1 parent a48916b commit 9f087ac

File tree

2 files changed

+115
-70
lines changed

2 files changed

+115
-70
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.community.sql;
17+
18+
import org.testcontainers.containers.*;
19+
import org.testcontainers.containers.wait.strategy.Wait;
20+
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
21+
import org.testcontainers.utility.DockerImageName;
22+
23+
import java.time.Duration;
24+
import java.util.concurrent.ConcurrentHashMap;
25+
import javax.sql.DataSource;
26+
import com.zaxxer.hikari.HikariConfig;
27+
import com.zaxxer.hikari.HikariDataSource;
28+
29+
public final class SharedSqlContainers {
30+
31+
private static final ConcurrentHashMap<String, JdbcDatabaseContainer<?>> CONTAINERS = new ConcurrentHashMap<>();
32+
33+
private SharedSqlContainers() { }
34+
35+
public static JdbcDatabaseContainer<?> getContainer(String dialectName) {
36+
boolean isCi = System.getenv("CI") != null || System.getenv("GITHUB_ACTIONS") != null;
37+
return CONTAINERS.computeIfAbsent(dialectName, key -> createContainerInternal(key, isCi));
38+
}
39+
40+
private static JdbcDatabaseContainer<?> createContainerInternal(String dialectName, boolean isCi) {
41+
switch (dialectName) {
42+
case "mysql": {
43+
MySQLContainer<?> c = new MySQLContainer<>("mysql:8.0")
44+
.withDatabaseName("testdb")
45+
.withUsername("testuser")
46+
.withPassword("testpass");
47+
if (!isCi) c.withReuse(true);
48+
return c;
49+
}
50+
case "sqlserver": {
51+
MSSQLServerContainer<?> c = new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04")
52+
.acceptLicense()
53+
.withPassword("TestPass123!");
54+
if (!isCi) c.withReuse(true);
55+
return c;
56+
}
57+
case "oracle": {
58+
OracleContainer c = new OracleContainer(
59+
DockerImageName.parse("gvenzl/oracle-free:23-slim-faststart")
60+
.asCompatibleSubstituteFor("gvenzl/oracle-xe")) {
61+
@Override
62+
public String getDatabaseName() {
63+
return "FREEPDB1";
64+
}
65+
}
66+
.withPassword("oracle123")
67+
.withSharedMemorySize(1073741824L)
68+
.withStartupTimeout(Duration.ofMinutes(20))
69+
.waitingFor(new WaitAllStrategy()
70+
.withStrategy(Wait.forListeningPort())
71+
.withStrategy(Wait.forLogMessage(".*DATABASE IS READY TO USE.*\\n", 1))
72+
)
73+
.withEnv("ORACLE_CHARACTERSET", "AL32UTF8");
74+
if (!isCi) c.withReuse(true);
75+
return c;
76+
}
77+
case "postgresql": {
78+
PostgreSQLContainer<?> c = new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
79+
.withDatabaseName("testdb")
80+
.withUsername("test")
81+
.withPassword("test");
82+
if (!isCi) c.withReuse(true);
83+
return c;
84+
}
85+
case "mariadb": {
86+
MariaDBContainer<?> c = new MariaDBContainer<>("mariadb:11.3.2")
87+
.withDatabaseName("testdb")
88+
.withUsername("testuser")
89+
.withPassword("testpass");
90+
if (!isCi) c.withReuse(true);
91+
return c;
92+
}
93+
default:
94+
throw new IllegalArgumentException("Unsupported dialect: " + dialectName);
95+
}
96+
}
97+
98+
public static void stopAll() {
99+
CONTAINERS.values().forEach(JdbcDatabaseContainer::stop);
100+
CONTAINERS.clear();
101+
}
102+
103+
public static DataSource createDataSource(JdbcDatabaseContainer<?> container) {
104+
HikariConfig config = new HikariConfig();
105+
config.setJdbcUrl(container.getJdbcUrl());
106+
config.setUsername(container.getUsername());
107+
config.setPassword(container.getPassword());
108+
config.setDriverClassName(container.getDriverClassName());
109+
return new HikariDataSource(config);
110+
}
111+
}

community/flamingock-auditstore-sql/src/test/java/io/flamingock/community/sql/SqlAuditTestHelper.java

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,81 +15,24 @@
1515
*/
1616
package io.flamingock.community.sql;
1717

18-
import com.zaxxer.hikari.HikariConfig;
19-
import com.zaxxer.hikari.HikariDataSource;
2018
import io.flamingock.internal.common.sql.SqlDialect;
2119
import org.testcontainers.containers.*;
22-
import org.testcontainers.containers.wait.strategy.Wait;
23-
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
24-
import org.testcontainers.utility.DockerImageName;
25-
2620
import javax.sql.DataSource;
2721
import java.sql.Connection;
2822
import java.sql.PreparedStatement;
2923
import java.sql.ResultSet;
3024
import java.sql.SQLException;
31-
import java.time.Duration;
3225

3326
import static org.junit.jupiter.api.Assertions.assertTrue;
3427

3528
public class SqlAuditTestHelper {
3629

37-
private static OracleContainer oracleInstance;
38-
3930
public static JdbcDatabaseContainer<?> createContainer(String dialectName) {
40-
boolean isCi = System.getenv("CI") != null || System.getenv("GITHUB_ACTIONS") != null;
31+
return SharedSqlContainers.getContainer(dialectName);
32+
}
4133

42-
switch (dialectName) {
43-
case "mysql":
44-
return new MySQLContainer<>("mysql:8.0")
45-
.withDatabaseName("testdb")
46-
.withUsername("testuser")
47-
.withPassword("testpass")
48-
.withReuse(true);
49-
case "sqlserver":
50-
return new MSSQLServerContainer<>("mcr.microsoft.com/mssql/server:2019-CU18-ubuntu-20.04")
51-
.acceptLicense()
52-
.withPassword("TestPass123!")
53-
.withReuse(true);
54-
case "oracle": {
55-
if (oracleInstance == null) {
56-
oracleInstance = new OracleContainer(
57-
DockerImageName.parse("gvenzl/oracle-free:23-slim-faststart")
58-
.asCompatibleSubstituteFor("gvenzl/oracle-xe")) {
59-
@Override
60-
public String getDatabaseName() {
61-
return "FREEPDB1";
62-
}
63-
}
64-
.withPassword("oracle123")
65-
.withSharedMemorySize(1073741824L)
66-
.withStartupTimeout(Duration.ofMinutes(20))
67-
.waitingFor(new WaitAllStrategy()
68-
.withStrategy(Wait.forListeningPort())
69-
.withStrategy(Wait.forLogMessage(".*DATABASE IS READY TO USE.*\\n", 1))
70-
)
71-
.withEnv("ORACLE_CHARACTERSET", "AL32UTF8");
72-
if (!isCi) {
73-
oracleInstance.withReuse(true);
74-
}
75-
}
76-
return oracleInstance;
77-
}
78-
case "postgresql":
79-
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
80-
.withDatabaseName("testdb")
81-
.withUsername("test")
82-
.withPassword("test")
83-
.withReuse(true);
84-
case "mariadb":
85-
return new MariaDBContainer<>("mariadb:11.3.2")
86-
.withDatabaseName("testdb")
87-
.withUsername("testuser")
88-
.withPassword("testpass")
89-
.withReuse(true);
90-
default:
91-
throw new IllegalArgumentException("Unsupported dialect: " + dialectName);
92-
}
34+
public static DataSource createDataSource(JdbcDatabaseContainer<?> container) {
35+
return SharedSqlContainers.createDataSource(container);
9336
}
9437

9538
public static void createTables(DataSource dataSource, SqlDialect dialect) throws SQLException {
@@ -190,15 +133,6 @@ private static String getCreateLockTableSql(SqlDialect dialect) {
190133
}
191134
}
192135

193-
public static DataSource createDataSource(JdbcDatabaseContainer<?> container) {
194-
HikariConfig config = new HikariConfig();
195-
config.setJdbcUrl(container.getJdbcUrl());
196-
config.setUsername(container.getUsername());
197-
config.setPassword(container.getPassword());
198-
config.setDriverClassName(container.getDriverClassName());
199-
return new HikariDataSource(config);
200-
}
201-
202136
public static void verifyPartialDataState(DataSource dataSource) throws SQLException {
203137
try (Connection conn = dataSource.getConnection();
204138
PreparedStatement ps = conn.prepareStatement("SELECT name FROM test_table WHERE id = ?")) {

0 commit comments

Comments
 (0)