16
16
17
17
package io .aiven .kafka .connect .jdbc .postgres ;
18
18
19
- import javax .sql .DataSource ;
20
-
21
- import java .sql .Connection ;
22
19
import java .sql .SQLException ;
23
- import java .sql .Statement ;
24
20
import java .time .Duration ;
25
21
import java .util .ArrayList ;
26
- import java .util .HashMap ;
27
22
import java .util .List ;
28
23
import java .util .Map ;
29
24
import java .util .concurrent .ExecutionException ;
33
28
import org .apache .kafka .clients .producer .RecordMetadata ;
34
29
35
30
import io .aiven .connect .jdbc .JdbcSinkConnector ;
36
- import io .aiven .kafka .connect .jdbc .AbstractIT ;
37
31
38
32
import org .apache .avro .Schema ;
39
33
import org .apache .avro .generic .GenericRecord ;
40
- import org .assertj .core .util .Arrays ;
41
34
import org .assertj .db .type .Table ;
42
35
import org .junit .jupiter .api .Test ;
43
- import org .postgresql .ds .PGSimpleDataSource ;
44
- import org .testcontainers .containers .PostgreSQLContainer ;
45
- import org .testcontainers .junit .jupiter .Container ;
46
- import org .testcontainers .junit .jupiter .Testcontainers ;
47
- import org .testcontainers .utility .DockerImageName ;
48
36
49
37
import static org .apache .avro .generic .GenericData .Record ;
50
38
import static org .assertj .db .api .Assertions .assertThat ;
51
39
import static org .awaitility .Awaitility .await ;
52
40
53
- @ Testcontainers
54
- public class PartitionedTableIntegrationTest extends AbstractIT {
41
+ public class PartitionedTableIntegrationTest extends AbstractPostgresIT {
55
42
56
- public static final String DEFAULT_POSTGRES_TAG = "10.20" ;
57
43
private static final String CONNECTOR_NAME = "test-sink-connector" ;
58
44
private static final int TEST_TOPIC_PARTITIONS = 1 ;
59
45
private static final Schema VALUE_RECORD_SCHEMA =
@@ -82,17 +68,11 @@ public class PartitionedTableIntegrationTest extends AbstractIT {
82
68
private static final String CREATE_PARTITION =
83
69
"create table partition partition of \" " + TEST_TOPIC_NAME
84
70
+ "\" for values from ('2022-03-03') to ('2122-03-03');" ;
85
- private static final DockerImageName DEFAULT_POSTGRES_IMAGE_NAME =
86
- DockerImageName .parse ("postgres" )
87
- .withTag (DEFAULT_POSTGRES_TAG );
88
-
89
- @ Container
90
- private final PostgreSQLContainer <?> postgreSqlContainer = new PostgreSQLContainer <>(DEFAULT_POSTGRES_IMAGE_NAME );
91
71
92
72
@ Test
93
73
final void testBasicDelivery () throws ExecutionException , InterruptedException , SQLException {
94
74
executeUpdate (CREATE_TABLE );
95
- connectRunner .createConnector (basicConnectorConfig ());
75
+ connectRunner .createConnector (basicSinkConnectorConfig ());
96
76
97
77
sendTestData (1000 );
98
78
@@ -104,31 +84,14 @@ final void testBasicDelivery() throws ExecutionException, InterruptedException,
104
84
final void testBasicDeliveryForPartitionedTable () throws ExecutionException , InterruptedException , SQLException {
105
85
executeUpdate (CREATE_TABLE_WITH_PARTITION );
106
86
executeUpdate (CREATE_PARTITION );
107
- connectRunner .createConnector (basicConnectorConfig ());
87
+ connectRunner .createConnector (basicSinkConnectorConfig ());
108
88
109
89
sendTestData (1000 );
110
90
111
91
await ().atMost (Duration .ofSeconds (15 )).pollInterval (Duration .ofMillis (100 ))
112
92
.untilAsserted (() -> assertThat (new Table (getDatasource (), TEST_TOPIC_NAME )).hasNumberOfRows (1000 ));
113
93
}
114
94
115
- private void executeUpdate (final String updateStatement ) throws SQLException {
116
- try (final Connection connection = getDatasource ().getConnection ();
117
- final Statement statement = connection .createStatement ()) {
118
- statement .executeUpdate (updateStatement );
119
- }
120
- }
121
-
122
- public DataSource getDatasource () {
123
- final PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource ();
124
- pgSimpleDataSource .setServerNames (Arrays .array (postgreSqlContainer .getHost ()));
125
- pgSimpleDataSource .setPortNumbers (new int [] {postgreSqlContainer .getMappedPort (5432 )});
126
- pgSimpleDataSource .setDatabaseName (postgreSqlContainer .getDatabaseName ());
127
- pgSimpleDataSource .setUser (postgreSqlContainer .getUsername ());
128
- pgSimpleDataSource .setPassword (postgreSqlContainer .getPassword ());
129
- return pgSimpleDataSource ;
130
- }
131
-
132
95
private void sendTestData (final int numberOfRecords ) throws InterruptedException , ExecutionException {
133
96
final List <Future <RecordMetadata >> sendFutures = new ArrayList <>();
134
97
for (int i = 0 ; i < numberOfRecords ; i ++) {
@@ -155,21 +118,13 @@ private Record createRecord(final String name, final String value) {
155
118
return valueRecord ;
156
119
}
157
120
158
- private Map <String , String > basicConnectorConfig () {
159
- final HashMap <String , String > config = new HashMap <> ();
121
+ private Map <String , String > basicSinkConnectorConfig () {
122
+ final Map <String , String > config = basicConnectorConfig ();
160
123
config .put ("name" , CONNECTOR_NAME );
161
124
config .put ("connector.class" , JdbcSinkConnector .class .getName ());
162
125
config .put ("topics" , TEST_TOPIC_NAME );
163
- config .put ("key.converter" , "io.confluent.connect.avro.AvroConverter" );
164
- config .put ("key.converter.schema.registry.url" , schemaRegistryContainer .getSchemaRegistryUrl ());
165
- config .put ("value.converter" , "io.confluent.connect.avro.AvroConverter" );
166
- config .put ("value.converter.schema.registry.url" , schemaRegistryContainer .getSchemaRegistryUrl ());
167
- config .put ("tasks.max" , "1" );
168
- config .put ("connection.url" , postgreSqlContainer .getJdbcUrl ());
169
- config .put ("connection.user" , postgreSqlContainer .getUsername ());
170
- config .put ("connection.password" , postgreSqlContainer .getPassword ());
171
126
config .put ("insert.mode" , "insert" );
172
- config .put ("dialect.name" , "PostgreSqlDatabaseDialect" );
173
127
return config ;
174
128
}
129
+
175
130
}
0 commit comments