5
5
import org .apache .kafka .clients .admin .AdminClient ;
6
6
import org .apache .kafka .clients .admin .AdminClientConfig ;
7
7
import org .apache .kafka .clients .admin .NewTopic ;
8
- import org .apache .kafka .clients .consumer .ConsumerConfig ;
9
- import org .apache .kafka .clients .consumer .ConsumerRecord ;
10
- import org .apache .kafka .clients .consumer .ConsumerRecords ;
11
- import org .apache .kafka .clients .consumer .KafkaConsumer ;
12
- import org .apache .kafka .clients .producer .KafkaProducer ;
13
- import org .apache .kafka .clients .producer .ProducerConfig ;
14
- import org .apache .kafka .clients .producer .ProducerRecord ;
15
8
import org .apache .kafka .common .config .SaslConfigs ;
16
9
import org .apache .kafka .common .errors .SaslAuthenticationException ;
17
10
import org .apache .kafka .common .errors .TopicAuthorizationException ;
18
- import org .apache .kafka .common .serialization .StringDeserializer ;
19
- import org .apache .kafka .common .serialization .StringSerializer ;
20
11
import org .awaitility .Awaitility ;
21
12
import org .junit .Test ;
22
- import org .rnorth . ducttape . unreliables . Unreliables ;
13
+ import org .testcontainers . AbstractKafka ;
23
14
import org .testcontainers .Testcontainers ;
24
15
import org .testcontainers .images .builder .Transferable ;
25
16
import org .testcontainers .utility .DockerImageName ;
26
17
27
- import java .time .Duration ;
28
18
import java .util .Collection ;
29
19
import java .util .Collections ;
30
- import java .util .Properties ;
31
20
import java .util .UUID ;
32
21
import java .util .concurrent .TimeUnit ;
33
22
34
23
import static org .assertj .core .api .Assertions .assertThat ;
35
24
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
36
- import static org .assertj .core .api .Assertions .tuple ;
37
25
38
- public class KafkaContainerTest {
26
+ public class KafkaContainerTest extends AbstractKafka {
39
27
40
28
private static final DockerImageName KAFKA_TEST_IMAGE = DockerImageName .parse ("confluentinc/cp-kafka:6.2.1" );
41
29
@@ -45,15 +33,6 @@ public class KafkaContainerTest {
45
33
"confluentinc/cp-zookeeper:4.0.0"
46
34
);
47
35
48
- private final ImmutableMap <String , String > properties = ImmutableMap .of (
49
- AdminClientConfig .SECURITY_PROTOCOL_CONFIG ,
50
- "SASL_PLAINTEXT" ,
51
- SaslConfigs .SASL_MECHANISM ,
52
- "PLAIN" ,
53
- SaslConfigs .SASL_JAAS_CONFIG ,
54
- "org.apache.kafka.common.security.plain.PlainLoginModule required username=\" admin\" password=\" admin\" ;"
55
- );
56
-
57
36
@ Test
58
37
public void testUsage () throws Exception {
59
38
try (KafkaContainer kafka = new KafkaContainer (KAFKA_TEST_IMAGE )) {
@@ -344,91 +323,4 @@ private static String getJaasConfig() {
344
323
"user_test=\" secret\" ;" ;
345
324
return jaasConfig ;
346
325
}
347
-
348
- private void testKafkaFunctionality (String bootstrapServers ) throws Exception {
349
- testKafkaFunctionality (bootstrapServers , false , 1 , 1 );
350
- }
351
-
352
- private void testSecureKafkaFunctionality (String bootstrapServers ) throws Exception {
353
- testKafkaFunctionality (bootstrapServers , true , 1 , 1 );
354
- }
355
-
356
- private void testKafkaFunctionality (String bootstrapServers , boolean authenticated , int partitions , int rf )
357
- throws Exception {
358
- ImmutableMap <String , String > adminClientDefaultProperties = ImmutableMap .of (
359
- AdminClientConfig .BOOTSTRAP_SERVERS_CONFIG ,
360
- bootstrapServers
361
- );
362
- Properties adminClientProperties = new Properties ();
363
- adminClientProperties .putAll (adminClientDefaultProperties );
364
-
365
- ImmutableMap <String , String > consumerDefaultProperties = ImmutableMap .of (
366
- ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ,
367
- bootstrapServers ,
368
- ConsumerConfig .GROUP_ID_CONFIG ,
369
- "tc-" + UUID .randomUUID (),
370
- ConsumerConfig .AUTO_OFFSET_RESET_CONFIG ,
371
- "earliest"
372
- );
373
- Properties consumerProperties = new Properties ();
374
- consumerProperties .putAll (consumerDefaultProperties );
375
-
376
- ImmutableMap <String , String > producerDefaultProperties = ImmutableMap .of (
377
- ProducerConfig .BOOTSTRAP_SERVERS_CONFIG ,
378
- bootstrapServers ,
379
- ProducerConfig .CLIENT_ID_CONFIG ,
380
- UUID .randomUUID ().toString ()
381
- );
382
- Properties producerProperties = new Properties ();
383
- producerProperties .putAll (producerDefaultProperties );
384
-
385
- if (authenticated ) {
386
- adminClientProperties .putAll (this .properties );
387
- consumerProperties .putAll (this .properties );
388
- producerProperties .putAll (this .properties );
389
- }
390
- try (
391
- AdminClient adminClient = AdminClient .create (adminClientProperties );
392
- KafkaProducer <String , String > producer = new KafkaProducer <>(
393
- producerProperties ,
394
- new StringSerializer (),
395
- new StringSerializer ()
396
- );
397
- KafkaConsumer <String , String > consumer = new KafkaConsumer <>(
398
- consumerProperties ,
399
- new StringDeserializer (),
400
- new StringDeserializer ()
401
- );
402
- ) {
403
- String topicName = "messages-" + UUID .randomUUID ();
404
-
405
- Collection <NewTopic > topics = Collections .singletonList (new NewTopic (topicName , partitions , (short ) rf ));
406
- adminClient .createTopics (topics ).all ().get (30 , TimeUnit .SECONDS );
407
-
408
- consumer .subscribe (Collections .singletonList (topicName ));
409
-
410
- producer .send (new ProducerRecord <>(topicName , "testcontainers" , "rulezzz" )).get ();
411
-
412
- Unreliables .retryUntilTrue (
413
- 10 ,
414
- TimeUnit .SECONDS ,
415
- () -> {
416
- ConsumerRecords <String , String > records = consumer .poll (Duration .ofMillis (100 ));
417
-
418
- if (records .isEmpty ()) {
419
- return false ;
420
- }
421
-
422
- assertThat (records )
423
- .hasSize (1 )
424
- .extracting (ConsumerRecord ::topic , ConsumerRecord ::key , ConsumerRecord ::value )
425
- .containsExactly (tuple (topicName , "testcontainers" , "rulezzz" ));
426
-
427
- return true ;
428
- }
429
- );
430
-
431
- consumer .unsubscribe ();
432
- }
433
- }
434
326
}
0 commit comments