29
29
import org .apache .pulsar .client .api .PulsarClient ;
30
30
import org .apache .pulsar .client .api .PulsarClientException ;
31
31
import org .apache .pulsar .client .api .Schema ;
32
+ import org .junit .jupiter .api .AfterEach ;
32
33
import org .junit .jupiter .api .BeforeEach ;
33
34
import org .junit .jupiter .api .Test ;
35
+ import org .mockito .MockedStatic ;
36
+ import org .mockito .Mockito ;
34
37
38
+ import org .springframework .pulsar .PulsarException ;
35
39
import org .springframework .pulsar .core .DefaultPulsarConsumerFactory ;
36
40
import org .springframework .pulsar .core .DefaultPulsarProducerFactory ;
37
41
import org .springframework .pulsar .core .PulsarConsumerFactory ;
41
45
* Tests for {@link PulsarConsumerTestUtil}.
42
46
*
43
47
* @author Jonas Geiregat
48
+ * @author Kartik Shrivastava
49
+ * @author Chris Bono
44
50
*/
45
51
class PulsarConsumerTestUtilTests implements PulsarTestContainerSupport {
46
52
@@ -61,9 +67,16 @@ void prepareForTest() throws PulsarClientException {
61
67
this .pulsarTemplate = new PulsarTemplate <>(new DefaultPulsarProducerFactory <>(pulsarClient ));
62
68
}
63
69
70
+ @ AfterEach
71
+ void cleanupFromTest () throws PulsarClientException {
72
+ if (this .pulsarClient != null ) {
73
+ this .pulsarClient .close ();
74
+ }
75
+ }
76
+
64
77
@ Test
65
- void whenConditionIsSpecifiedMessagesAreConsumedUntilConditionIsMet () {
66
- var topic = testTopic ("a " );
78
+ void whenConditionIsSpecifiedThenMessagesConsumedUntilConditionMet () {
79
+ var topic = testTopic ("cond " );
67
80
IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
68
81
var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
69
82
.fromTopic (topic )
@@ -75,8 +88,8 @@ void whenConditionIsSpecifiedMessagesAreConsumedUntilConditionIsMet() {
75
88
}
76
89
77
90
@ Test
78
- void whenConditionIsNotSpecifiedMessagesAreConsumedUntilAwaitDuration () {
79
- var topic = testTopic ("b " );
91
+ void whenConditionIsNotSpecifiedThenMessagesAreConsumedUntilAwaitDuration () {
92
+ var topic = testTopic ("no-cond " );
80
93
IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
81
94
var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
82
95
.fromTopic (topic )
@@ -89,10 +102,25 @@ void whenConditionIsNotSpecifiedMessagesAreConsumedUntilAwaitDuration() {
89
102
}
90
103
91
104
@ Test
92
- void exceptionIsThrownWhenConditionNotMetWithinAwaitDuration () {
105
+ void whenChainedConditionsAreSpecifiedThenMessagesConsumedUntilAllConditionsMet () {
106
+ var topic = testTopic ("chained-cond" );
107
+ IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
108
+ ConsumedMessagesCondition <String > condition1 = ConsumedMessagesConditions .desiredMessageCount (5 );
109
+ ConsumedMessagesCondition <String > condition2 = ConsumedMessagesConditions .atLeastOneMessageMatches ("message-1" );
110
+ var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
111
+ .fromTopic (topic )
112
+ .withSchema (Schema .STRING )
113
+ .awaitAtMost (Duration .ofSeconds (5 ))
114
+ .until (condition1 .and (condition2 ))
115
+ .get ();
116
+ assertThat (msgs ).hasSize (5 );
117
+ }
118
+
119
+ @ Test
120
+ void whenConditionNotMetWithinAwaitDurationThenExceptionIsThrown () {
93
121
assertThatExceptionOfType (ConditionTimeoutException .class )
94
122
.isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
95
- .fromTopic (testTopic ("c " ))
123
+ .fromTopic (testTopic ("cond-not-met " ))
96
124
.withSchema (Schema .STRING )
97
125
.awaitAtMost (Duration .ofSeconds (5 ))
98
126
.until (ConsumedMessagesConditions .desiredMessageCount (3 ))
@@ -101,9 +129,8 @@ void exceptionIsThrownWhenConditionNotMetWithinAwaitDuration() {
101
129
}
102
130
103
131
@ Test
104
- void messagesAreConsumedWhenContainerIsRunningAndConsumeMessagesIsCalledWithoutArguments () {
105
- // depends upon pulsarClient created in prepareForTest
106
- var topic = testTopic ("e1" );
132
+ void consumeMessagesWithNoArgsUsesPulsarContainerIfAvailable () {
133
+ var topic = testTopic ("no-arg" );
107
134
IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
108
135
var msgs = PulsarConsumerTestUtil .<String >consumeMessages ()
109
136
.fromTopic (topic )
@@ -115,15 +142,24 @@ void messagesAreConsumedWhenContainerIsRunningAndConsumeMessagesIsCalledWithoutA
115
142
}
116
143
117
144
@ Test
118
- void messagesAreConsumedWhenContainerIsStoppedAndConsumeMessagesIsCalledWithoutArguments () {
119
- PulsarTestContainerSupport .stopContainer ();
120
- PulsarConsumerTestUtil .<String >consumeMessages ();
121
- // TODO: Complete this test
145
+ void consumeMessagesWithNoArgsUsesDefaultUrlWhenPulsarContainerNotAvailable () {
146
+ try (MockedStatic <PulsarTestContainerSupport > containerSupport = Mockito
147
+ .mockStatic (PulsarTestContainerSupport .class )) {
148
+ containerSupport .when (PulsarTestContainerSupport ::isContainerStarted ).thenReturn (false );
149
+ var topic = testTopic ("no-arg-dft-url" );
150
+ assertThatExceptionOfType (PulsarException .class )
151
+ .isThrownBy (() -> PulsarConsumerTestUtil .<String >consumeMessages ()
152
+ .fromTopic (topic )
153
+ .withSchema (Schema .STRING )
154
+ .awaitAtMost (Duration .ofSeconds (2 ))
155
+ .get ())
156
+ .withStackTraceContaining ("Connection refused: localhost" );
157
+ }
122
158
}
123
159
124
160
@ Test
125
- void messagesAreConsumedWhenConsumeMessagesIsCalledWithBrokerUrl () {
126
- var topic = testTopic ("e2 " );
161
+ void consumeMessagesWithBrokerUrl () {
162
+ var topic = testTopic ("url-arg " );
127
163
IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
128
164
var msgs = PulsarConsumerTestUtil .<String >consumeMessages (PulsarTestContainerSupport .getPulsarBrokerUrl ())
129
165
.fromTopic (topic )
@@ -135,15 +171,8 @@ void messagesAreConsumedWhenConsumeMessagesIsCalledWithBrokerUrl() {
135
171
}
136
172
137
173
@ Test
138
- void exceptionIsThrownWhenConsumeMessagesIsCalledWithNullBrokerUrl () {
139
- String url = null ;
140
- assertThatIllegalArgumentException ().isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (url ))
141
- .withMessage ("url must not be null" );
142
- }
143
-
144
- @ Test
145
- void messagesAreConsumedWhenConsumeMessagesIsCalledWithPulsarClient () {
146
- var topic = testTopic ("e3" );
174
+ void consumeMessagesWithPulsarClient () {
175
+ var topic = testTopic ("client-arg" );
147
176
IntStream .range (0 , 2 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
148
177
var msgs = PulsarConsumerTestUtil .<String >consumeMessages (this .pulsarClient )
149
178
.fromTopic (topic )
@@ -155,31 +184,8 @@ void messagesAreConsumedWhenConsumeMessagesIsCalledWithPulsarClient() {
155
184
}
156
185
157
186
@ Test
158
- void exceptionIsThrownWhenConsumeMessagesIsCalledWithNullPulsarClient () {
159
- PulsarClient localPulsarClient = null ;
160
- assertThatIllegalArgumentException ().isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (localPulsarClient ))
161
- .withMessage ("pulsarClient must not be null" );
162
- }
163
-
164
- @ Test
165
- void whenChainedConditionAreSpecifiedMessagesAreConsumedUntilTheyAreMet () {
166
- var topic = testTopic ("d" );
167
- IntStream .range (0 , 5 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
168
- ConsumedMessagesCondition <String > condition1 = ConsumedMessagesConditions .desiredMessageCount (5 );
169
- ConsumedMessagesCondition <String > condition2 = ConsumedMessagesConditions .atLeastOneMessageMatches ("message-1" );
170
- var msgs = PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
171
- .fromTopic (topic )
172
- .withSchema (Schema .STRING )
173
- .awaitAtMost (Duration .ofSeconds (5 ))
174
- .until (condition1 .and (condition2 ))
175
- .get ();
176
- assertThat (msgs ).hasSize (5 );
177
- }
178
-
179
- @ Test
180
- void exceptionIsThrownWhenUntilIsCalledMultipleTimes () {
181
- var topic = testTopic ("e" );
182
- IntStream .range (0 , 1 ).forEach (i -> pulsarTemplate .send (topic , "message-" + i ));
187
+ void untilCannotBeCalledMultipleTimes () {
188
+ var topic = testTopic ("until-multi" );
183
189
assertThatExceptionOfType (IllegalStateException .class )
184
190
.isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (pulsarConsumerFactory )
185
191
.fromTopic (topic )
@@ -191,6 +197,20 @@ void exceptionIsThrownWhenUntilIsCalledMultipleTimes() {
191
197
.withMessage ("Multiple calls to 'until' are not allowed. Use 'and' to combine conditions." );
192
198
}
193
199
200
+ @ Test
201
+ void brokerUrlCannotBeNull () {
202
+ String url = null ;
203
+ assertThatIllegalArgumentException ().isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (url ))
204
+ .withMessage ("url must not be null" );
205
+ }
206
+
207
+ @ Test
208
+ void pulsarClientCannotBeNull () {
209
+ PulsarClient localPulsarClient = null ;
210
+ assertThatIllegalArgumentException ().isThrownBy (() -> PulsarConsumerTestUtil .consumeMessages (localPulsarClient ))
211
+ .withMessage ("pulsarClient must not be null" );
212
+ }
213
+
194
214
@ Test
195
215
void consumerFactoryCannotBeNull () {
196
216
PulsarConsumerFactory <String > consumerFactory = null ;
0 commit comments