11/*
2- * Copyright 2002-2019 the original author or authors.
2+ * Copyright 2002-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .integration .mqtt ;
1818
1919import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2021import static org .assertj .core .api .Assertions .fail ;
2122import static org .mockito .ArgumentMatchers .any ;
2223import static org .mockito .ArgumentMatchers .anyLong ;
2324import static org .mockito .ArgumentMatchers .anyString ;
25+ import static org .mockito .ArgumentMatchers .isNull ;
2426import static org .mockito .BDDMockito .given ;
2527import static org .mockito .BDDMockito .willAnswer ;
2628import static org .mockito .BDDMockito .willReturn ;
4850
4951import org .aopalliance .intercept .MethodInterceptor ;
5052import org .apache .commons .logging .Log ;
53+ import org .assertj .core .api .Condition ;
5154import org .eclipse .paho .client .mqttv3 .IMqttAsyncClient ;
5255import org .eclipse .paho .client .mqttv3 .IMqttClient ;
56+ import org .eclipse .paho .client .mqttv3 .IMqttMessageListener ;
5357import org .eclipse .paho .client .mqttv3 .IMqttToken ;
5458import org .eclipse .paho .client .mqttv3 .MqttAsyncClient ;
5559import org .eclipse .paho .client .mqttv3 .MqttCallback ;
@@ -435,11 +439,13 @@ public void testSubscribeFailure() throws Exception {
435439 new DirectFieldAccessor (client ).setPropertyValue ("aClient" , aClient );
436440 willAnswer (new CallsRealMethods ()).given (client ).connect (any (MqttConnectOptions .class ));
437441 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 ());
438444 willReturn (alwaysComplete ).given (aClient ).connect (any (MqttConnectOptions .class ), any (), any ());
439445
440446 IMqttToken token = mock (IMqttToken .class );
441447 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 ());
443449
444450 MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter ("foo" , "bar" , factory ,
445451 "baz" , "fix" );
@@ -449,15 +455,12 @@ public void testSubscribeFailure() throws Exception {
449455 method .set (m );
450456 }, m -> m .getName ().equals ("connectAndSubscribe" ));
451457 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 );
461464 }
462465
463466 @ Test
@@ -485,11 +488,13 @@ public void testDifferentQos() throws Exception {
485488 new DirectFieldAccessor (client ).setPropertyValue ("aClient" , aClient );
486489 willAnswer (new CallsRealMethods ()).given (client ).connect (any (MqttConnectOptions .class ));
487490 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 ());
488493 willReturn (alwaysComplete ).given (aClient ).connect (any (MqttConnectOptions .class ), any (), any ());
489494
490495 IMqttToken token = mock (IMqttToken .class );
491496 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 ());
493498
494499 MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter ("foo" , "bar" , factory ,
495500 "baz" , "fix" );
@@ -506,6 +511,10 @@ public void testDifferentQos() throws Exception {
506511 verify (logger , atLeastOnce ())
507512 .warn ("Granted QOS different to Requested QOS; topics: [baz, fix] requested: [1, 1] granted: [2, 0]" );
508513 verify (client ).setTimeToWait (30_000L );
514+
515+ new DirectFieldAccessor (adapter ).setPropertyValue ("running" , Boolean .TRUE );
516+ adapter .stop ();
517+ verify (client ).disconnectForcibly (5_000L );
509518 }
510519
511520 private MqttPahoMessageDrivenChannelAdapter buildAdapterIn (final IMqttClient client , Boolean cleanSession ,
0 commit comments