2727import java .time .format .DateTimeFormatter ;
2828import java .util .Arrays ;
2929import java .util .HashMap ;
30- import java .util .List ;
30+ import java .util .HashSet ;
3131import java .util .Map ;
32- import java .util .Properties ;
32+ import java .util .Set ;
3333import java .util .UUID ;
3434import java .util .concurrent .ExecutionException ;
3535import java .util .concurrent .Future ;
3636
37- import org .apache .kafka .clients .admin .AdminClient ;
38- import org .apache .kafka .clients .admin .AdminClientConfig ;
39- import org .apache .kafka .clients .admin .NewTopic ;
37+ import io .aiven .commons .kafka .testkit .KafkaIntegrationTestBase ;
38+ import io .aiven .commons .kafka .testkit .KafkaManager ;
4039import org .apache .kafka .clients .producer .KafkaProducer ;
4140import org .apache .kafka .clients .producer .ProducerConfig ;
4241import org .apache .kafka .clients .producer .ProducerRecord ;
4544import io .aiven .kafka .connect .common .config .CompressionType ;
4645import io .aiven .kafka .connect .gcs .testutils .BucketAccessor ;
4746
48- import com .github .dockerjava .api .model .Ulimit ;
4947import com .google .cloud .NoCredentials ;
5048import com .google .cloud .storage .BucketInfo ;
5149import com .google .cloud .storage .Storage ;
5250import com .google .cloud .storage .StorageOptions ;
5351import org .junit .jupiter .api .AfterEach ;
5452import org .junit .jupiter .api .BeforeAll ;
53+ import org .junit .jupiter .api .BeforeEach ;
5554import org .testcontainers .containers .FixedHostPortGenericContainer ;
5655import org .testcontainers .containers .GenericContainer ;
57- import org .testcontainers .containers .KafkaContainer ;
58- import org .testcontainers .containers .Network ;
5956import org .testcontainers .junit .jupiter .Container ;
6057import org .testcontainers .junit .jupiter .Testcontainers ;
6158
6259@ SuppressWarnings ({ "deprecation" , "PMD.TestClassWithoutTestCases" })
6360@ Testcontainers
64- class AbstractIntegrationTest <K , V > {
61+ class AbstractIntegrationTest <K , V > extends KafkaIntegrationTestBase {
6562 protected final String testTopic0 ;
6663 protected final String testTopic1 ;
6764
68- private AdminClient adminClient ;
69- private ConnectRunner connectRunner ;
65+ private KafkaManager kafkaManager ;
66+
7067 private KafkaProducer <K , V > producer ;
7168
7269 protected static final int OFFSET_FLUSH_INTERVAL_MS = 5000 ;
@@ -89,20 +86,14 @@ class AbstractIntegrationTest<K, V> {
8986 protected static String gcsEndpoint ; // NOPMD mutable static state
9087
9188 private static final String FAKE_GCS_SERVER_VERSION = System .getProperty ("fake-gcs-server-version" , "latest" );
89+ private static final Set <String > CONNECTOR_NAMES = new HashSet <>();
9290 @ Container
9391 @ SuppressWarnings ("rawtypes" )
9492 private static final GenericContainer <?> FAKE_GCS_CONTAINER = new FixedHostPortGenericContainer (
9593 String .format ("fsouza/fake-gcs-server:%s" , FAKE_GCS_SERVER_VERSION ))
9694 .withFixedExposedPort (GCS_PORT , GCS_PORT )
9795 .withCommand ("-port" , Integer .toString (GCS_PORT ), "-scheme" , "http" )
9896 .withReuse (true );
99- @ Container
100- protected static final KafkaContainer KAFKA = new KafkaContainer ("5.2.1" )
101- .withEnv ("KAFKA_AUTO_CREATE_TOPICS_ENABLE" , "false" )
102- .withNetwork (Network .newNetwork ())
103- .withExposedPorts (KafkaContainer .KAFKA_PORT , 9092 )
104- .withCreateContainerCmdModifier (
105- cmd -> cmd .getHostConfig ().withUlimits (List .of (new Ulimit ("nofile" , 30_000L , 30_000L ))));
10697
10798 static int getRandomPort () {
10899 try (ServerSocket socket = new ServerSocket (0 )) {
@@ -114,6 +105,7 @@ static int getRandomPort() {
114105 }
115106
116107 protected AbstractIntegrationTest () {
108+ super ();
117109 testTopic0 = "test-topic-0-" + UUID .randomUUID ();
118110 testTopic1 = "test-topic-1-" + UUID .randomUUID ();
119111 }
@@ -162,13 +154,17 @@ static void setUpAll() throws IOException, InterruptedException {
162154 assert process .waitFor () == 0 ;
163155 }
164156
157+ @ BeforeEach
158+ void setupKafka () throws IOException {
159+ kafkaManager = setupKafka (true , GcsSinkConnector .class );
160+ }
161+
165162 @ AfterEach
166163 void tearDown () {
167- connectRunner .stop ();
168- adminClient .close ();
169164 producer .close ();
170165 testBucketAccessor .clear (gcsPrefix );
171- connectRunner .awaitStop ();
166+ CONNECTOR_NAMES .forEach (kafkaManager ::deleteConnector );
167+ CONNECTOR_NAMES .clear ();
172168 }
173169
174170 protected static boolean useFakeGCS () {
@@ -206,27 +202,20 @@ protected Future<RecordMetadata> sendMessageAsync(final String topicName, final
206202 return producer .send (msg );
207203 }
208204
209- protected ConnectRunner getConnectRunner () {
210- return connectRunner ;
211- }
212-
213205 protected void startConnectRunner (final Map <String , Object > testSpecificProducerProperties )
214206 throws ExecutionException , InterruptedException {
215207 testBucketAccessor .clear (gcsPrefix );
216208
217- final Properties adminClientConfig = new Properties ();
218- adminClientConfig .put (AdminClientConfig .BOOTSTRAP_SERVERS_CONFIG , KAFKA .getBootstrapServers ());
219- adminClient = AdminClient .create (adminClientConfig );
209+ kafkaManager .createTopics (Arrays .asList (testTopic0 , testTopic1 ));
220210
221211 final Map <String , Object > producerProps = new HashMap <>(testSpecificProducerProperties );
222- producerProps .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , KAFKA . getBootstrapServers ());
212+ producerProps .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , kafkaManager . bootstrapServers ());
223213 producer = new KafkaProducer <>(producerProps );
224214
225- final NewTopic newTopic0 = new NewTopic (testTopic0 , 4 , (short ) 1 );
226- final NewTopic newTopic1 = new NewTopic (testTopic1 , 4 , (short ) 1 );
227- adminClient .createTopics (Arrays .asList (newTopic0 , newTopic1 )).all ().get ();
215+ }
228216
229- connectRunner = new ConnectRunner (pluginDir , KAFKA .getBootstrapServers (), OFFSET_FLUSH_INTERVAL_MS );
230- connectRunner .start ();
217+ protected void createConnector (final Map <String , String > connectorConfig ) {
218+ CONNECTOR_NAMES .add (connectorConfig .get ("name" ));
219+ kafkaManager .configureConnector (connectorConfig .get ("name" ), connectorConfig );
231220 }
232221}
0 commit comments