use of org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic.FieldChanged in project smarthome by eclipse.
the class SubscribeFieldToMQTTtopicTests method MessageReceive.
@Test
public void MessageReceive() throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
final FieldChanged changed = (field, value) -> {
try {
field.set(attributes.getFieldsOf(), value);
} catch (IllegalArgumentException | IllegalAccessException e) {
fail(e.getMessage());
}
};
final Field field = Attributes.class.getField("Int");
ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, changed, "homie/device123", false);
CompletableFuture<@Nullable Void> future = subscriber.subscribeAndReceive(connection, 1000);
// Simulate a received MQTT message
subscriber.processMessage("ignored", "10".getBytes());
// No timeout should happen
future.get(50, TimeUnit.MILLISECONDS);
assertThat(attributes.Int, is(10));
}
Aggregations