Skip to content

Commit e3629e7

Browse files
committed
adding runtime ClientManger tests
* `testV3ClientManagerRuntime` and `testV5ClientManagerRuntime` adding MessageDrivenAdaptes at runtime using `IntegrationFlowContext`
1 parent d29d208 commit e3629e7

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

spring-integration-mqtt/src/test/java/org/springframework/integration/mqtt/ClientManagerBackToBackTests.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.integration.mqtt;
1818

19+
import java.lang.reflect.Constructor;
1920
import java.nio.charset.StandardCharsets;
21+
import java.util.Arrays;
2022
import java.util.concurrent.CountDownLatch;
2123
import java.util.concurrent.TimeUnit;
2224

@@ -29,8 +31,12 @@
2931
import org.springframework.context.annotation.Configuration;
3032
import org.springframework.context.event.EventListener;
3133
import org.springframework.integration.annotation.ServiceActivator;
34+
import org.springframework.integration.channel.QueueChannel;
3235
import org.springframework.integration.config.EnableIntegration;
3336
import org.springframework.integration.dsl.IntegrationFlow;
37+
import org.springframework.integration.dsl.context.IntegrationFlowContext;
38+
import org.springframework.integration.endpoint.MessageProducerSupport;
39+
import org.springframework.integration.mqtt.core.ClientManager;
3440
import org.springframework.integration.mqtt.core.Mqttv3ClientManager;
3541
import org.springframework.integration.mqtt.core.Mqttv5ClientManager;
3642
import org.springframework.integration.mqtt.event.MqttSubscribedEvent;
@@ -76,6 +82,12 @@ void testV3ClientManagerStarted() throws Exception {
7682
Mqttv3ConfigWithStartedManager.subscribedLatch);
7783
}
7884

85+
@Test
86+
void testV3ClientManagerRuntime() throws Exception{
87+
testSubscribeAndPublishRuntime(Mqttv3ConfigRuntime.class, Mqttv3ConfigRuntime.TOPIC_NAME,
88+
Mqttv3ConfigRuntime.subscribedLatch, Mqttv3ConfigRuntime.adapter);
89+
}
90+
7991
@Test
8092
void testV5ClientManagerReconnect() throws Exception {
8193
testSubscribeAndPublish(Mqttv5ConfigWithDisconnect.class, Mqttv5ConfigWithDisconnect.TOPIC_NAME,
@@ -88,6 +100,12 @@ void testV5ClientManagerStarted() throws Exception {
88100
Mqttv5ConfigWithStartedManager.subscribedLatch);
89101
}
90102

103+
@Test
104+
void testV5ClientManagerRuntime() throws Exception{
105+
testSubscribeAndPublishRuntime(Mqttv5ConfigRuntime.class, Mqttv5ConfigRuntime.TOPIC_NAME,
106+
Mqttv5ConfigRuntime.subscribedLatch, Mqttv5ConfigRuntime.adapter);
107+
}
108+
91109
private void testSubscribeAndPublish(Class<?> configClass, String topicName, CountDownLatch subscribedLatch)
92110
throws Exception {
93111

@@ -114,6 +132,40 @@ private void testSubscribeAndPublish(Class<?> configClass, String topicName, Cou
114132
}
115133
}
116134

135+
private void testSubscribeAndPublishRuntime(Class<?> configClass, String topicName, CountDownLatch subscribedLatch, Class<?> adapter)
136+
throws Exception {
137+
138+
try (var ctx = new AnnotationConfigApplicationContext(configClass)) {
139+
// given
140+
var input = ctx.getBean("mqttOutFlow.input", MessageChannel.class);
141+
var flowContext = ctx.getBean(IntegrationFlowContext.class);
142+
var clientManager = ctx.getBean(ClientManager.class);
143+
var output = new QueueChannel();
144+
Class<?>[] parameterTypes = {ClientManager.class, String[].class};
145+
Constructor<?> declaredConstructor = adapter.getConstructor(parameterTypes);
146+
flowContext.registration(IntegrationFlow
147+
.from((MessageProducerSupport) declaredConstructor.newInstance(clientManager,new String[] {topicName}))
148+
.channel(output)
149+
.get()).register();
150+
String testPayload = "foo";
151+
assertThat(subscribedLatch.await(20, TimeUnit.SECONDS)).isTrue();
152+
153+
// when
154+
input.send(MessageBuilder.withPayload(testPayload).setHeader(MqttHeaders.TOPIC, topicName).build());
155+
Message<?> receive = output.receive(20_000);
156+
157+
// then
158+
assertThat(receive).isNotNull();
159+
Object payload = receive.getPayload();
160+
if (payload instanceof String sp) {
161+
assertThat(sp).isEqualTo(testPayload);
162+
}
163+
else {
164+
assertThat(payload).isEqualTo(testPayload.getBytes(StandardCharsets.UTF_8));
165+
}
166+
}
167+
}
168+
117169
@Configuration
118170
@EnableIntegration
119171
public static class Mqttv3Config {
@@ -208,6 +260,7 @@ public Mqttv3ClientManager mqttv3ClientManager() {
208260
connectionOptions.setServerURIs(new String[] {MosquittoContainerTest.mqttUrl()});
209261
connectionOptions.setAutomaticReconnect(true);
210262
Mqttv3ClientManager manager = new Mqttv3ClientManager(connectionOptions, "client-manager-client-id-v3");
263+
manager.start();
211264
return manager;
212265
}
213266

@@ -223,6 +276,35 @@ public IntegrationFlow mqttInFlow(Mqttv3ClientManager mqttv3ClientManager) {
223276
.get();
224277
}
225278

279+
}
280+
@Configuration
281+
@EnableIntegration
282+
public static class Mqttv3ConfigRuntime {
283+
284+
static final String TOPIC_NAME = "test-topic-v3";
285+
286+
static final CountDownLatch subscribedLatch = new CountDownLatch(1);
287+
288+
static final Class<?> adapter = MqttPahoMessageDrivenChannelAdapter.class;
289+
290+
@EventListener
291+
public void onSubscribed(MqttSubscribedEvent e) {
292+
subscribedLatch.countDown();
293+
}
294+
295+
@Bean
296+
public Mqttv3ClientManager mqttv3ClientManager() {
297+
MqttConnectOptions connectionOptions = new MqttConnectOptions();
298+
connectionOptions.setServerURIs(new String[] {MosquittoContainerTest.mqttUrl()});
299+
connectionOptions.setAutomaticReconnect(true);
300+
return new Mqttv3ClientManager(connectionOptions, "client-manager-client-id-v3");
301+
}
302+
303+
@Bean
304+
public IntegrationFlow mqttOutFlow(Mqttv3ClientManager mqttv3ClientManager) {
305+
return f -> f.handle(new MqttPahoMessageHandler(mqttv3ClientManager));
306+
}
307+
226308
}
227309

228310
@Configuration
@@ -328,6 +410,33 @@ public IntegrationFlow mqttInFlow(Mqttv5ClientManager mqttv5ClientManager) {
328410
.get();
329411
}
330412

413+
}
414+
@Configuration
415+
@EnableIntegration
416+
public static class Mqttv5ConfigRuntime {
417+
418+
static final String TOPIC_NAME = "test-topic-v5";
419+
420+
static final CountDownLatch subscribedLatch = new CountDownLatch(1);
421+
422+
static final Class<?> adapter = Mqttv5PahoMessageDrivenChannelAdapter.class;
423+
424+
@EventListener
425+
public void onSubscribed(MqttSubscribedEvent e) {
426+
subscribedLatch.countDown();
427+
}
428+
429+
@Bean
430+
public Mqttv5ClientManager mqttv5ClientManager() {
431+
return new Mqttv5ClientManager(MosquittoContainerTest.mqttUrl(), "client-manager-client-id-v5");
432+
}
433+
434+
@Bean
435+
@ServiceActivator(inputChannel = "mqttOutFlow.input")
436+
public Mqttv5PahoMessageHandler mqttv5PahoMessageHandler(Mqttv5ClientManager mqttv5ClientManager) {
437+
return new Mqttv5PahoMessageHandler(mqttv5ClientManager);
438+
}
439+
331440
}
332441

333442
record ClientV3Disconnector(Mqttv3ClientManager clientManager) {

0 commit comments

Comments
 (0)