14
14
15
15
package org .eclipse .edc .sql .testfixtures ;
16
16
17
- import org .eclipse .edc .spi .system .configuration .Config ;
18
- import org .eclipse .edc .spi .system .configuration .ConfigFactory ;
19
17
import org .eclipse .edc .sql .DriverManagerConnectionFactory ;
20
18
import org .eclipse .edc .sql .QueryExecutor ;
21
19
import org .eclipse .edc .sql .SqlQueryExecutor ;
36
34
import java .sql .Connection ;
37
35
import java .sql .SQLException ;
38
36
import java .util .List ;
39
- import java .util .Map ;
40
37
import java .util .Properties ;
41
38
import java .util .UUID ;
42
39
43
- import static java .lang .String .format ;
44
- import static org .eclipse .edc .util .io .Ports .getFreePort ;
45
-
46
40
/**
47
- * Extension for running PG SQL store implementation. It automatically creates a test database and provided all the base data structure
48
- * for a SQL store to run such as {@link DataSourceRegistry}, {@link TransactionContext} and data source name which is automatically generated
41
+ * Extension for running PostgreSQL store implementation tests. It starts a database container and provides all the base
42
+ * data structure for a SQL store to run such as {@link DataSourceRegistry}, {@link TransactionContext} and data source
43
+ * name which is automatically generated.
49
44
*/
50
45
public class PostgresqlStoreSetupExtension implements BeforeEachCallback , BeforeAllCallback , AfterAllCallback , ParameterResolver {
51
46
52
- public static final String POSTGRES_IMAGE_NAME = "postgres:16.1" ;
53
- private final PostgreSQLContainer <?> postgreSqlContainer = new PostgreSQLContainer <>(POSTGRES_IMAGE_NAME )
54
- .withExposedPorts (5432 )
55
- .withUsername ("postgres" )
56
- .withPassword ("password" )
57
- .withDatabaseName ("itest" );
58
- private final PostgresqlLocalInstance postgres ;
47
+ private static final String DEFAULT_IMAGE = "postgres:17.3" ;
48
+
49
+ private final PostgreSQLContainer <?> postgres ;
59
50
private final QueryExecutor queryExecutor = new SqlQueryExecutor ();
60
51
private final TransactionContext transactionContext = new NoopTransactionContext ();
61
52
private final DataSourceRegistry dataSourceRegistry = new DefaultDataSourceRegistry ();
62
53
private final String datasourceName = UUID .randomUUID ().toString ();
63
- private final String jdbcUrlPrefix ;
64
54
65
55
public PostgresqlStoreSetupExtension () {
66
- var exposedPort = getFreePort ();
67
- postgreSqlContainer .setPortBindings (List .of ("%s:5432" .formatted (exposedPort )));
68
- jdbcUrlPrefix = format ("jdbc:postgresql://%s:%s/" , postgreSqlContainer .getHost (), exposedPort );
69
- postgres = new PostgresqlLocalInstance (postgreSqlContainer .getUsername (), postgreSqlContainer .getPassword (), jdbcUrlPrefix );
70
- }
71
-
72
- public String getDatasourceName () {
73
- return datasourceName ;
56
+ this (DEFAULT_IMAGE );
74
57
}
75
58
76
- public Connection getConnection () {
77
- try {
78
- return dataSourceRegistry .resolve (datasourceName ).getConnection ();
79
- } catch (SQLException e ) {
80
- throw new RuntimeException (e );
81
- }
82
- }
83
-
84
- public int runQuery (String query ) {
85
- return transactionContext .execute (() -> queryExecutor .execute (getConnection (), query ));
86
- }
87
-
88
- public TransactionContext getTransactionContext () {
89
- return transactionContext ;
90
- }
91
-
92
- public DataSourceRegistry getDataSourceRegistry () {
93
- return dataSourceRegistry ;
59
+ public PostgresqlStoreSetupExtension (String dockerImageName ) {
60
+ postgres = new PostgreSQLContainer <>(dockerImageName );
94
61
}
95
62
96
63
@ Override
97
64
public void beforeAll (ExtensionContext context ) {
98
- postgreSqlContainer .start ();
99
- postgres .createDatabase (postgreSqlContainer .getDatabaseName ());
65
+ postgres .start ();
100
66
}
101
67
102
68
@ Override
103
69
public void beforeEach (ExtensionContext context ) {
104
70
var properties = new Properties ();
105
- properties .put ("user" , postgreSqlContainer .getUsername ());
106
- properties .put ("password" , postgreSqlContainer .getPassword ());
71
+ properties .put ("user" , postgres .getUsername ());
72
+ properties .put ("password" , postgres .getPassword ());
107
73
var connectionFactory = new DriverManagerConnectionFactory ();
108
- var jdbcUrl = jdbcUrlPrefix + postgreSqlContainer .getDatabaseName ();
74
+ var jdbcUrl = postgres . getJdbcUrl () + postgres .getDatabaseName ();
109
75
var dataSource = new ConnectionFactoryDataSource (connectionFactory , jdbcUrl , properties );
110
76
dataSourceRegistry .register (datasourceName , dataSource );
111
77
}
112
78
113
79
@ Override
114
80
public void afterAll (ExtensionContext context ) {
115
- postgreSqlContainer .stop ();
116
- postgreSqlContainer .close ();
81
+ postgres .stop ();
82
+ postgres .close ();
117
83
}
118
84
119
85
@ Override
120
86
public boolean supportsParameter (ParameterContext parameterContext , ExtensionContext extensionContext ) throws ParameterResolutionException {
121
87
var type = parameterContext .getParameter ().getParameterizedType ();
122
- return List .of (PostgresqlStoreSetupExtension .class , Connection .class , QueryExecutor .class , PostgresqlLocalInstance . class ).contains (type );
88
+ return List .of (PostgresqlStoreSetupExtension .class , Connection .class , QueryExecutor .class ).contains (type );
123
89
}
124
90
125
91
@ Override
@@ -131,17 +97,32 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
131
97
return getConnection ();
132
98
} else if (type .equals (QueryExecutor .class )) {
133
99
return queryExecutor ;
134
- } else if (type .equals (PostgresqlLocalInstance .class )) {
135
- return postgres ;
136
100
}
137
101
return null ;
138
102
}
139
103
140
- public Config getDatasourceConfig () {
141
- return ConfigFactory .fromMap (getDatasourceConfiguration ());
104
+ public String getDatasourceName () {
105
+ return datasourceName ;
106
+ }
107
+
108
+ public Connection getConnection () {
109
+ try {
110
+ return dataSourceRegistry .resolve (datasourceName ).getConnection ();
111
+ } catch (SQLException e ) {
112
+ throw new RuntimeException (e );
113
+ }
114
+ }
115
+
116
+ public int runQuery (String query ) {
117
+ return transactionContext .execute (() -> queryExecutor .execute (getConnection (), query ));
142
118
}
143
119
144
- public Map < String , String > getDatasourceConfiguration () {
145
- return postgres . createDefaultDatasourceConfiguration ( postgreSqlContainer . getDatabaseName ()) ;
120
+ public TransactionContext getTransactionContext () {
121
+ return transactionContext ;
146
122
}
123
+
124
+ public DataSourceRegistry getDataSourceRegistry () {
125
+ return dataSourceRegistry ;
126
+ }
127
+
147
128
}
0 commit comments