Search in sources :

Example 11 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class DeutscheBahnTrainHandlerTest method testWithoutBridgeStateUpdatesToOffline.

@Test
public void testWithoutBridgeStateUpdatesToOffline() {
    final Thing thing = mockThing(1);
    final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    final DeutscheBahnTrainHandler handler = createAndInitHandler(callback, thing);
    try {
        verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
        verify(callback, timeout(1000)).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE)));
    } finally {
        handler.dispose();
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) GregorianCalendar(java.util.GregorianCalendar) StringType(org.openhab.core.library.types.StringType) ThingUID(org.openhab.core.thing.ThingUID) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) TripLabel(org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel) DateTimeType(org.openhab.core.library.types.DateTimeType) ZoneId(java.time.ZoneId) TimetableStop(org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Channel(org.openhab.core.thing.Channel) Thing(org.openhab.core.thing.Thing) Configuration(org.openhab.core.config.core.Configuration) Event(org.openhab.binding.deutschebahn.internal.timetable.dto.Event) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) BridgeImpl(org.openhab.core.thing.internal.BridgeImpl) ChannelUID(org.openhab.core.thing.ChannelUID) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test)

Example 12 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class DeutscheBahnTrainHandlerTest method testUpdateChannelsWithEventNotPresent.

@Test
public void testUpdateChannelsWithEventNotPresent() {
    final Thing thing = mockThing(1);
    final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
    ThingUID bridgeId = new ThingUID(DeutscheBahnBindingConstants.TIMETABLE_TYPE, "timetable");
    when(callback.getBridge(bridgeId)).thenReturn(new BridgeImpl(DeutscheBahnBindingConstants.TIMETABLE_TYPE, bridgeId));
    final DeutscheBahnTrainHandler handler = createAndInitHandler(callback, thing);
    try {
        verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
        verify(callback, timeout(1000)).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
        // Provide data that will update the channels
        TimetableStop stop = new TimetableStop();
        Event arrival = new Event();
        arrival.setPt("2108161434");
        arrival.setL("RE60");
        stop.setAr(arrival);
        handler.updateChannels(stop);
        final Date arrivalTime = new GregorianCalendar(2021, 7, 16, 14, 34).getTime();
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "trip#category"), UnDefType.UNDEF);
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "arrival#planned-time"), getDateTime(arrivalTime));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "arrival#line"), new StringType("RE60"));
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "arrival#changed-time"), UnDefType.NULL);
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "departure#planned-time"), UnDefType.UNDEF);
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "departure#planned-platform"), UnDefType.UNDEF);
        verify(callback, timeout(1000)).stateUpdated(new ChannelUID(thing.getUID(), "departure#planned-final-station"), UnDefType.UNDEF);
    } finally {
        handler.dispose();
    }
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) GregorianCalendar(java.util.GregorianCalendar) StringType(org.openhab.core.library.types.StringType) ThingUID(org.openhab.core.thing.ThingUID) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) TripLabel(org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel) DateTimeType(org.openhab.core.library.types.DateTimeType) ZoneId(java.time.ZoneId) TimetableStop(org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Channel(org.openhab.core.thing.Channel) Thing(org.openhab.core.thing.Thing) Configuration(org.openhab.core.config.core.Configuration) Event(org.openhab.binding.deutschebahn.internal.timetable.dto.Event) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) BridgeImpl(org.openhab.core.thing.internal.BridgeImpl) ChannelUID(org.openhab.core.thing.ChannelUID) BridgeImpl(org.openhab.core.thing.internal.BridgeImpl) StringType(org.openhab.core.library.types.StringType) ChannelUID(org.openhab.core.thing.ChannelUID) ThingUID(org.openhab.core.thing.ThingUID) GregorianCalendar(java.util.GregorianCalendar) Event(org.openhab.binding.deutschebahn.internal.timetable.dto.Event) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) TimetableStop(org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 13 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class DmxBridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridge = BridgeBuilder.create(THING_TYPE_TEST_BRIDGE, "testbridge").withLabel("Test Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = Mockito.spy(new DmxBridgeHandlerImpl(bridge));
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class Lib485BridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
    bridge = BridgeBuilder.create(THING_TYPE_LIB485_BRIDGE, "lib485bridge").withLabel("Lib485 Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = new Lib485BridgeHandler(bridge) {

        @Override
        protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
        }
    };
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with ThingHandlerCallback

use of org.openhab.core.thing.binding.ThingHandlerCallback in project openhab-addons by openhab.

the class ArtnetBridgeHandlerTest method setUp.

@BeforeEach
public void setUp() {
    bridgeProperties = new HashMap<>();
    bridgeProperties.put(CONFIG_ADDRESS, TEST_ADDRESS);
    bridgeProperties.put(CONFIG_UNIVERSE, TEST_UNIVERSE);
    bridge = BridgeBuilder.create(THING_TYPE_ARTNET_BRIDGE, "artnetbridge").withLabel("Artnet Bridge").withChannel(ChannelBuilder.create(CHANNEL_UID_MUTE, "Switch").withType(MUTE_CHANNEL_TYPEUID).build()).withConfiguration(new Configuration(bridgeProperties)).build();
    ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
    doAnswer(answer -> {
        ((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
        return null;
    }).when(mockCallback).statusUpdated(any(), any());
    bridgeHandler = new ArtnetBridgeHandler(bridge) {

        @Override
        protected void validateConfigurationParameters(Map<String, Object> configurationParameters) {
        }
    };
    bridgeHandler.getThing().setHandler(bridgeHandler);
    bridgeHandler.setCallback(mockCallback);
    bridgeHandler.initialize();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Thing(org.openhab.core.thing.Thing) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)79 Thing (org.openhab.core.thing.Thing)48 Test (org.junit.jupiter.api.Test)39 Configuration (org.openhab.core.config.core.Configuration)30 Channel (org.openhab.core.thing.Channel)30 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)28 ThingHandler (org.openhab.core.thing.binding.ThingHandler)26 ChannelUID (org.openhab.core.thing.ChannelUID)25 Nullable (org.eclipse.jdt.annotation.Nullable)23 InvocationOnMock (org.mockito.invocation.InvocationOnMock)23 ArrayList (java.util.ArrayList)19 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)19 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)19 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)18 ThingUID (org.openhab.core.thing.ThingUID)18 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)16 Bridge (org.openhab.core.thing.Bridge)12 ThingStatus (org.openhab.core.thing.ThingStatus)11 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)11 List (java.util.List)10