1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 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.
17
17
package org .springframework .integration .mqtt ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
21
import static org .assertj .core .api .Assertions .fail ;
21
22
import static org .mockito .ArgumentMatchers .any ;
22
23
import static org .mockito .ArgumentMatchers .anyLong ;
23
24
import static org .mockito .ArgumentMatchers .anyString ;
25
+ import static org .mockito .ArgumentMatchers .isNull ;
24
26
import static org .mockito .BDDMockito .given ;
25
27
import static org .mockito .BDDMockito .willAnswer ;
26
28
import static org .mockito .BDDMockito .willReturn ;
48
50
49
51
import org .aopalliance .intercept .MethodInterceptor ;
50
52
import org .apache .commons .logging .Log ;
53
+ import org .assertj .core .api .Condition ;
51
54
import org .eclipse .paho .client .mqttv3 .IMqttAsyncClient ;
52
55
import org .eclipse .paho .client .mqttv3 .IMqttClient ;
56
+ import org .eclipse .paho .client .mqttv3 .IMqttMessageListener ;
53
57
import org .eclipse .paho .client .mqttv3 .IMqttToken ;
54
58
import org .eclipse .paho .client .mqttv3 .MqttAsyncClient ;
55
59
import org .eclipse .paho .client .mqttv3 .MqttCallback ;
@@ -435,11 +439,13 @@ public void testSubscribeFailure() throws Exception {
435
439
new DirectFieldAccessor (client ).setPropertyValue ("aClient" , aClient );
436
440
willAnswer (new CallsRealMethods ()).given (client ).connect (any (MqttConnectOptions .class ));
437
441
willAnswer (new CallsRealMethods ()).given (client ).subscribe (any (String [].class ), any (int [].class ));
442
+ willAnswer (new CallsRealMethods ()).given (client ).subscribe (any (String [].class ), any (int [].class ),
443
+ (IMqttMessageListener []) isNull ());
438
444
willReturn (alwaysComplete ).given (aClient ).connect (any (MqttConnectOptions .class ), any (), any ());
439
445
440
446
IMqttToken token = mock (IMqttToken .class );
441
447
given (token .getGrantedQos ()).willReturn (new int [] { 0x80 });
442
- willReturn (token ).given (aClient ).subscribe (any (String [].class ), any (int [].class ), any (), any ());
448
+ willReturn (token ).given (aClient ).subscribe (any (String [].class ), any (int [].class ), isNull (), isNull (), any ());
443
449
444
450
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter ("foo" , "bar" , factory ,
445
451
"baz" , "fix" );
@@ -449,15 +455,12 @@ public void testSubscribeFailure() throws Exception {
449
455
method .set (m );
450
456
}, m -> m .getName ().equals ("connectAndSubscribe" ));
451
457
assertThat (method .get ()).isNotNull ();
452
- try {
453
- method .get ().invoke (adapter );
454
- fail ("Expected InvocationTargetException" );
455
- }
456
- catch (InvocationTargetException e ) {
457
- assertThat (e .getCause ()).isInstanceOf (MqttException .class );
458
- assertThat (((MqttException ) e .getCause ()).getReasonCode ())
459
- .isEqualTo ((int ) MqttException .REASON_CODE_SUBSCRIBE_FAILED );
460
- }
458
+ Condition <InvocationTargetException > subscribeFailed = new Condition <>(ex ->
459
+ ((MqttException ) ex .getCause ()).getReasonCode () == MqttException .REASON_CODE_SUBSCRIBE_FAILED ,
460
+ "expected the reason code to be REASON_CODE_SUBSCRIBE_FAILED" );
461
+ assertThatExceptionOfType (InvocationTargetException .class ).isThrownBy (() -> method .get ().invoke (adapter ))
462
+ .withCauseInstanceOf (MqttException .class )
463
+ .is (subscribeFailed );
461
464
}
462
465
463
466
@ Test
@@ -485,11 +488,13 @@ public void testDifferentQos() throws Exception {
485
488
new DirectFieldAccessor (client ).setPropertyValue ("aClient" , aClient );
486
489
willAnswer (new CallsRealMethods ()).given (client ).connect (any (MqttConnectOptions .class ));
487
490
willAnswer (new CallsRealMethods ()).given (client ).subscribe (any (String [].class ), any (int [].class ));
491
+ willAnswer (new CallsRealMethods ()).given (client ).subscribe (any (String [].class ), any (int [].class ),
492
+ (IMqttMessageListener []) isNull ());
488
493
willReturn (alwaysComplete ).given (aClient ).connect (any (MqttConnectOptions .class ), any (), any ());
489
494
490
495
IMqttToken token = mock (IMqttToken .class );
491
496
given (token .getGrantedQos ()).willReturn (new int [] { 2 , 0 });
492
- willReturn (token ).given (aClient ).subscribe (any (String [].class ), any (int [].class ), any (), any ());
497
+ willReturn (token ).given (aClient ).subscribe (any (String [].class ), any (int [].class ), isNull (), isNull (), any ());
493
498
494
499
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter ("foo" , "bar" , factory ,
495
500
"baz" , "fix" );
@@ -506,6 +511,10 @@ public void testDifferentQos() throws Exception {
506
511
verify (logger , atLeastOnce ())
507
512
.warn ("Granted QOS different to Requested QOS; topics: [baz, fix] requested: [1, 1] granted: [2, 0]" );
508
513
verify (client ).setTimeToWait (30_000L );
514
+
515
+ new DirectFieldAccessor (adapter ).setPropertyValue ("running" , Boolean .TRUE );
516
+ adapter .stop ();
517
+ verify (client ).disconnectForcibly (5_000L );
509
518
}
510
519
511
520
private MqttPahoMessageDrivenChannelAdapter buildAdapterIn (final IMqttClient client , Boolean cleanSession ,
0 commit comments