1
1
/*
2
- * Copyright 2019-2022 the original author or authors.
2
+ * Copyright 2019-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
21
22
22
import java .time .Duration ;
23
+ import java .util .List ;
23
24
import java .util .Map ;
24
25
25
26
import org .apache .kafka .clients .admin .AdminClient ;
26
27
import org .apache .kafka .clients .consumer .ConsumerConfig ;
27
28
import org .apache .kafka .clients .consumer .ConsumerRecord ;
28
29
import org .apache .kafka .clients .consumer .ConsumerRecords ;
29
30
import org .apache .kafka .clients .consumer .KafkaConsumer ;
31
+ import org .apache .kafka .clients .consumer .OffsetAndMetadata ;
30
32
import org .apache .kafka .clients .producer .KafkaProducer ;
31
33
import org .apache .kafka .clients .producer .ProducerRecord ;
32
34
import org .apache .kafka .common .TopicPartition ;
35
+ import org .apache .kafka .common .TopicPartitionInfo ;
33
36
import org .junit .jupiter .api .Test ;
34
37
35
38
import org .springframework .kafka .test .EmbeddedKafkaBroker ;
36
39
import org .springframework .kafka .test .context .EmbeddedKafka ;
37
40
38
41
/**
39
42
* @author Gary Russell
43
+ * @author Artem Bilan
40
44
* @since 2.2.7
41
45
*
42
46
*/
43
- @ EmbeddedKafka (topics = { "singleTopic1" , "singleTopic2" , "singleTopic3" , "singleTopic4" , "singleTopic5" ,
44
- "multiTopic1" })
47
+ @ EmbeddedKafka (topics = {"singleTopic1" , "singleTopic2" , "singleTopic3" , "singleTopic4" , "singleTopic5" ,
48
+ "multiTopic1" })
45
49
public class KafkaTestUtilsTests {
46
50
47
51
@ Test
48
- void testGetSingleWithMoreThatOneTopic (EmbeddedKafkaBroker broker ) {
52
+ void testGetSingleWithMoreThanOneTopic (EmbeddedKafkaBroker broker ) {
49
53
Map <String , Object > producerProps = KafkaTestUtils .producerProps (broker );
50
54
KafkaProducer <Integer , String > producer = new KafkaProducer <>(producerProps );
51
55
producer .send (new ProducerRecord <>("singleTopic1" , 0 , 1 , "foo" ));
@@ -64,7 +68,7 @@ void testGetSingleWithMoreThatOneTopic(EmbeddedKafkaBroker broker) {
64
68
}
65
69
66
70
@ Test
67
- void testGetSingleWithMoreThatOneTopicRecordNotThereYet (EmbeddedKafkaBroker broker ) {
71
+ void testGetSingleWithMoreThanOneTopicRecordNotThereYet (EmbeddedKafkaBroker broker ) {
68
72
Map <String , Object > producerProps = KafkaTestUtils .producerProps (broker );
69
73
KafkaProducer <Integer , String > producer = new KafkaProducer <>(producerProps );
70
74
producer .send (new ProducerRecord <>("singleTopic4" , 0 , 1 , "foo" ));
@@ -73,7 +77,7 @@ void testGetSingleWithMoreThatOneTopicRecordNotThereYet(EmbeddedKafkaBroker brok
73
77
broker .consumeFromEmbeddedTopics (consumer , "singleTopic4" , "singleTopic5" );
74
78
long t1 = System .currentTimeMillis ();
75
79
assertThatExceptionOfType (IllegalStateException .class ).isThrownBy (() ->
76
- KafkaTestUtils .getSingleRecord (consumer , "singleTopic5" , Duration .ofSeconds (2 )));
80
+ KafkaTestUtils .getSingleRecord (consumer , "singleTopic5" , Duration .ofSeconds (2 )));
77
81
assertThat (System .currentTimeMillis () - t1 ).isGreaterThanOrEqualTo (2000L );
78
82
producer .send (new ProducerRecord <>("singleTopic5" , 1 , "foo" ));
79
83
producer .close ();
@@ -97,19 +101,19 @@ public void testGetOneRecord(EmbeddedKafkaBroker broker) throws Exception {
97
101
assertThat (oneRecord .value ()).isEqualTo ("foo" );
98
102
assertThat (KafkaTestUtils .getCurrentOffset (broker .getBrokersAsString (), "getOne" , "singleTopic3" , 0 ))
99
103
.isNotNull ()
100
- .extracting (omd -> omd . offset () )
104
+ .extracting (OffsetAndMetadata :: offset )
101
105
.isEqualTo (1L );
102
106
oneRecord = KafkaTestUtils .getOneRecord (broker .getBrokersAsString (), "getOne" ,
103
107
"singleTopic3" , 0 , true , true , Duration .ofSeconds (10 ));
104
108
assertThat (oneRecord .value ()).isEqualTo ("foo" );
105
109
assertThat (KafkaTestUtils .getCurrentOffset (broker .getBrokersAsString (), "getOne" , "singleTopic3" , 0 ))
106
110
.isNotNull ()
107
- .extracting (omd -> omd . offset () )
111
+ .extracting (OffsetAndMetadata :: offset )
108
112
.isEqualTo (1L );
109
113
}
110
114
111
115
@ Test
112
- public void testMultiMinRecords (EmbeddedKafkaBroker broker ) throws Exception {
116
+ public void testMultiMinRecords (EmbeddedKafkaBroker broker ) {
113
117
Map <String , Object > producerProps = KafkaTestUtils .producerProps (broker );
114
118
KafkaProducer <Integer , String > producer = new KafkaProducer <>(producerProps );
115
119
producer .send (new ProducerRecord <>("multiTopic1" , 0 , 1 , "foo" ));
@@ -135,16 +139,36 @@ public void testMultiMinRecords(EmbeddedKafkaBroker broker) throws Exception {
135
139
public void testGetCurrentOffsetWithAdminClient (EmbeddedKafkaBroker broker ) throws Exception {
136
140
Map <String , Object > adminClientProps = Map .of (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG , broker .getBrokersAsString ());
137
141
Map <String , Object > producerProps = KafkaTestUtils .producerProps (broker );
138
- try (AdminClient adminClient = AdminClient .create (adminClientProps ); KafkaProducer < Integer , String > producer = new KafkaProducer <>(producerProps )) {
142
+ try (var adminClient = AdminClient .create (adminClientProps ); var producer = new KafkaProducer <>(producerProps )) {
139
143
producer .send (new ProducerRecord <>("singleTopic3" , 0 , 1 , "foo" ));
140
144
141
145
KafkaTestUtils .getOneRecord (broker .getBrokersAsString (), "testGetCurrentOffsetWithAdminClient" ,
142
146
"singleTopic3" , 0 , false , true , Duration .ofSeconds (10 ));
143
147
assertThat (KafkaTestUtils .getCurrentOffset (adminClient , "testGetCurrentOffsetWithAdminClient" , "singleTopic3" , 0 ))
144
148
.isNotNull ()
145
- .extracting (omd -> omd . offset () )
149
+ .extracting (OffsetAndMetadata :: offset )
146
150
.isEqualTo (1L );
147
151
}
152
+ }
153
+
154
+ @ Test
155
+ public void topicAutomaticallyCreatedWithProperNumberOfPartitions (EmbeddedKafkaBroker broker ) throws Exception {
156
+ Map <String , Object > producerProps = KafkaTestUtils .producerProps (broker );
157
+
158
+ Map <String , Object > adminClientProps =
159
+ Map .of (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG , broker .getBrokersAsString ());
160
+ try (var adminClient = AdminClient .create (adminClientProps ); var producer = new KafkaProducer <>(producerProps )) {
161
+ producer .send (new ProducerRecord <>("auto-topic" , "test data" )).get ();
162
+
163
+ List <TopicPartitionInfo > partitions =
164
+ adminClient .describeTopics (List .of ("auto-topic" ))
165
+ .allTopicNames ()
166
+ .get ()
167
+ .get ("auto-topic" )
168
+ .partitions ();
169
+
170
+ assertThat (partitions ).hasSize (2 );
171
+ }
148
172
149
173
}
150
174
0 commit comments