Search in sources :

Example 1 with FieldChanged

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));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FIELD(java.lang.annotation.ElementType.FIELD) FieldChanged(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic.FieldChanged) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) Mock(org.mockito.Mock) TimeoutException(java.util.concurrent.TimeoutException) Target(java.lang.annotation.Target) CompletableFuture(java.util.concurrent.CompletableFuture) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.junit.Test) Field(java.lang.reflect.Field) Retention(java.lang.annotation.Retention) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) BigDecimal(java.math.BigDecimal) MockitoAnnotations(org.mockito.MockitoAnnotations) Nullable(org.eclipse.jdt.annotation.Nullable) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assert(org.junit.Assert) Mockito.doReturn(org.mockito.Mockito.doReturn) NonNull(org.eclipse.jdt.annotation.NonNull) RetentionPolicy(java.lang.annotation.RetentionPolicy) Before(org.junit.Before) Field(java.lang.reflect.Field) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) FieldChanged(org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic.FieldChanged) Test(org.junit.Test)

Aggregations

FIELD (java.lang.annotation.ElementType.FIELD)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 Field (java.lang.reflect.Field)1 BigDecimal (java.math.BigDecimal)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 FieldChanged (org.eclipse.smarthome.binding.mqtt.generic.internal.mapping.SubscribeFieldToMQTTtopic.FieldChanged)1 MqttBrokerConnection (org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 Assert (org.junit.Assert)1 Before (org.junit.Before)1 Test (org.junit.Test)1