23
23
import static org .junit .Assert .assertSame ;
24
24
import static org .junit .Assert .assertThat ;
25
25
import static org .junit .Assert .assertTrue ;
26
+ import static org .junit .Assert .fail ;
26
27
import static org .mockito .BDDMockito .given ;
27
28
import static org .mockito .BDDMockito .willAnswer ;
28
29
import static org .mockito .BDDMockito .willReturn ;
50
51
51
52
import org .aopalliance .intercept .MethodInterceptor ;
52
53
import org .apache .commons .logging .Log ;
54
+ import org .eclipse .paho .client .mqttv3 .IMqttAsyncClient ;
53
55
import org .eclipse .paho .client .mqttv3 .IMqttClient ;
54
56
import org .eclipse .paho .client .mqttv3 .IMqttToken ;
55
57
import org .eclipse .paho .client .mqttv3 .MqttAsyncClient ;
77
79
import org .springframework .integration .mqtt .outbound .MqttPahoMessageHandler ;
78
80
import org .springframework .integration .test .util .TestUtils ;
79
81
import org .springframework .messaging .Message ;
82
+ import org .springframework .messaging .MessagingException ;
80
83
import org .springframework .messaging .support .GenericMessage ;
81
84
import org .springframework .scheduling .TaskScheduler ;
82
85
import org .springframework .scheduling .concurrent .ThreadPoolTaskScheduler ;
@@ -100,15 +103,32 @@ public class MqttAdapterTests {
100
103
}
101
104
102
105
@ Test
103
- public void testCloseOnBadConnect () throws Exception {
106
+ public void testCloseOnBadConnectIn () throws Exception {
104
107
final IMqttClient client = mock (IMqttClient .class );
105
108
willThrow (new MqttException (0 )).given (client ).connect (any ());
106
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
109
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
107
110
adapter .start ();
108
111
verify (client ).close ();
109
112
adapter .stop ();
110
113
}
111
114
115
+ @ Test
116
+ public void testCloseOnBadConnectOut () throws Exception {
117
+ final IMqttAsyncClient client = mock (IMqttAsyncClient .class );
118
+ willThrow (new MqttException (0 )).given (client ).connect (any ());
119
+ MqttPahoMessageHandler adapter = buildAdapterOut (client );
120
+ adapter .start ();
121
+ try {
122
+ adapter .handleMessage (new GenericMessage <>("foo" ));
123
+ fail ("exception expected" );
124
+ }
125
+ catch (MessagingException e ) {
126
+ // NOSONAR
127
+ }
128
+ verify (client ).close ();
129
+ adapter .stop ();
130
+ }
131
+
112
132
@ Test
113
133
public void testOutboundOptionsApplied () throws Exception {
114
134
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory ();
@@ -290,7 +310,7 @@ public void testInboundOptionsApplied() throws Exception {
290
310
@ Test
291
311
public void testStopActionDefault () throws Exception {
292
312
final IMqttClient client = mock (IMqttClient .class );
293
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , null , null );
313
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , null , null );
294
314
295
315
adapter .start ();
296
316
adapter .stop ();
@@ -300,7 +320,7 @@ public void testStopActionDefault() throws Exception {
300
320
@ Test
301
321
public void testStopActionDefaultNotClean () throws Exception {
302
322
final IMqttClient client = mock (IMqttClient .class );
303
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , false , null );
323
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , false , null );
304
324
305
325
adapter .start ();
306
326
adapter .stop ();
@@ -310,7 +330,7 @@ public void testStopActionDefaultNotClean() throws Exception {
310
330
@ Test
311
331
public void testStopActionAlways () throws Exception {
312
332
final IMqttClient client = mock (IMqttClient .class );
313
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , false ,
333
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , false ,
314
334
ConsumerStopAction .UNSUBSCRIBE_ALWAYS );
315
335
316
336
adapter .start ();
@@ -328,7 +348,7 @@ public void testStopActionAlways() throws Exception {
328
348
@ Test
329
349
public void testStopActionNever () throws Exception {
330
350
final IMqttClient client = mock (IMqttClient .class );
331
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
351
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
332
352
333
353
adapter .start ();
334
354
adapter .stop ();
@@ -338,7 +358,7 @@ public void testStopActionNever() throws Exception {
338
358
@ Test
339
359
public void testReconnect () throws Exception {
340
360
final IMqttClient client = mock (IMqttClient .class );
341
- MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
361
+ MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn (client , null , ConsumerStopAction .UNSUBSCRIBE_NEVER );
342
362
adapter .setRecoveryInterval (10 );
343
363
Log logger = spy (TestUtils .getPropertyValue (adapter , "logger" , Log .class ));
344
364
new DirectFieldAccessor (adapter ).setPropertyValue ("logger" , logger );
@@ -364,7 +384,7 @@ public void testReconnect() throws Exception {
364
384
taskScheduler .destroy ();
365
385
}
366
386
367
- private MqttPahoMessageDrivenChannelAdapter buildAdapter (final IMqttClient client , Boolean cleanSession ,
387
+ private MqttPahoMessageDrivenChannelAdapter buildAdapterIn (final IMqttClient client , Boolean cleanSession ,
368
388
ConsumerStopAction action ) throws MqttException {
369
389
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory () {
370
390
@@ -392,6 +412,25 @@ public IMqttClient getClientInstance(String uri, String clientId) throws MqttExc
392
412
return adapter ;
393
413
}
394
414
415
+ private MqttPahoMessageHandler buildAdapterOut (final IMqttAsyncClient client ) throws MqttException {
416
+ DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory () {
417
+
418
+ @ Override
419
+ public IMqttAsyncClient getAsyncClientInstance (String uri , String clientId ) throws MqttException {
420
+ return client ;
421
+ }
422
+
423
+ };
424
+ MqttConnectOptions connectOptions = new MqttConnectOptions ();
425
+ connectOptions .setServerURIs (new String [] { "tcp://localhost:1883" });
426
+ factory .setConnectionOptions (connectOptions );
427
+ MqttPahoMessageHandler adapter = new MqttPahoMessageHandler ("client" , factory );
428
+ adapter .setDefaultTopic ("foo" );
429
+ adapter .setApplicationEventPublisher (mock (ApplicationEventPublisher .class ));
430
+ adapter .afterPropertiesSet ();
431
+ return adapter ;
432
+ }
433
+
395
434
private void verifyUnsubscribe (IMqttClient client ) throws Exception {
396
435
verify (client ).connect (any (MqttConnectOptions .class ));
397
436
verify (client ).subscribe (any (String [].class ), any (int [].class ));
0 commit comments