use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.
the class MqttAdapterTests method testStopActionDefault.
@Test
public void testStopActionDefault() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, null);
adapter.start();
adapter.stop();
verifyUnsubscribe(client);
}
use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.
the class MqttAdapterTests method testStopActionAlways.
@Test
public void testStopActionAlways() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, false, ConsumerStopAction.UNSUBSCRIBE_ALWAYS);
adapter.start();
adapter.stop();
verifyUnsubscribe(client);
adapter.connectionLost(new RuntimeException("Intentional"));
TaskScheduler taskScheduler = TestUtils.getPropertyValue(adapter, "taskScheduler", TaskScheduler.class);
verify(taskScheduler, never()).schedule(any(Runnable.class), any(Date.class));
}
use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.
the class MqttAdapterTests method testStopActionNever.
@Test
public void testStopActionNever() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
adapter.start();
adapter.stop();
verifyNotUnsubscribe(client);
}
use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.
the class MqttAdapterTests method testReconnect.
@Test
public void testReconnect() throws Exception {
final IMqttClient client = mock(IMqttClient.class);
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapter(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
adapter.setRecoveryInterval(10);
Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
given(logger.isDebugEnabled()).willReturn(true);
final AtomicInteger attemptingReconnectCount = new AtomicInteger();
willAnswer(i -> {
if (attemptingReconnectCount.getAndIncrement() == 0) {
adapter.connectionLost(new RuntimeException("while schedule running"));
}
i.callRealMethod();
return null;
}).given(logger).debug("Attempting reconnect");
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.start();
adapter.connectionLost(new RuntimeException("initial"));
Thread.sleep(1000);
// the following assertion should be equalTo, but leq to protect against a slow CI server
assertThat(attemptingReconnectCount.get(), lessThanOrEqualTo(2));
adapter.stop();
taskScheduler.destroy();
}
use of org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter in project spring-integration by spring-projects.
the class BackToBackAdapterTests method testJson.
@Test
public void testJson() {
MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
adapter.setDefaultTopic("mqtt-foo");
adapter.setBeanFactory(mock(BeanFactory.class));
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper(JacksonJsonUtils.messagingAwareMapper("org.springframework"));
DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
converter.setBytesMessageMapper(mapper);
adapter.setConverter(converter);
adapter.afterPropertiesSet();
adapter.start();
MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in", "mqtt-foo");
QueueChannel outputChannel = new QueueChannel();
inbound.setOutputChannel(outputChannel);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
inbound.setTaskScheduler(taskScheduler);
inbound.setBeanFactory(mock(BeanFactory.class));
inbound.setConverter(converter);
inbound.afterPropertiesSet();
inbound.start();
adapter.handleMessage(new GenericMessage<Foo>(new Foo("bar"), Collections.singletonMap("baz", "qux")));
Message<?> out = outputChannel.receive(20000);
assertNotNull(out);
adapter.stop();
inbound.stop();
assertEquals(new Foo("bar"), out.getPayload());
assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
assertEquals("qux", out.getHeaders().get("baz"));
}
Aggregations