Search in sources :

Example 26 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerIgnoresRestoringOfThingStatusFromRemoving.

@Test
public void thingManagerIgnoresRestoringOfThingStatusFromRemoving() {
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    managedThingProvider.add(thing);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    final ThingStatusInfo removingNone = ThingStatusInfoBuilder.create(ThingStatus.REMOVING, ThingStatusDetail.NONE).build();
    final ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
    thing.setStatusInfo(removingNone);
    state.callback.statusUpdated(thing, onlineNone);
    assertThat(thing.getStatusInfo(), is(removingNone));
}
Also used : ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 27 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerHandlesThingUpdatesCorrectly.

@Test
@SuppressWarnings({ "null", "unchecked" })
public void thingManagerHandlesThingUpdatesCorrectly() {
    String itemName = "name";
    managedThingProvider.add(thing);
    managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    RegistryChangeListener<Thing> registryChangeListener = mock(RegistryChangeListener.class);
    try {
        thingRegistry.addRegistryChangeListener(registryChangeListener);
        state.callback.thingUpdated(thing);
        verify(registryChangeListener, times(1)).updated(any(Thing.class), any(Thing.class));
    } finally {
        thingRegistry.removeRegistryChangeListener(registryChangeListener);
    }
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingRegistry(org.openhab.core.thing.ThingRegistry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 28 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerCallsUnregisterHandlerForRemovedThing.

@Test
public void thingManagerCallsUnregisterHandlerForRemovedThing() {
    ThingHandler thingHandler = mock(ThingHandler.class);
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    managedThingProvider.add(thing);
    managedThingProvider.remove(thing.getUID());
    waitForAssert(() -> verify(thingHandlerFactory, times(1)).removeThing(thing.getUID()));
    waitForAssert(() -> verify(thingHandlerFactory, times(1)).unregisterHandler(thing));
}
Also used : ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 29 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerHandlesThingHandlerLifecycleCorrectly.

@Test
public void thingManagerHandlesThingHandlerLifecycleCorrectly() {
    class ThingHandlerState {

        @Nullable
        ThingHandlerCallback callback = null;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
            return null;
        }
    }).when(thingHandler).initialize();
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    final ThingStatusInfo uninitializedNone = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
    assertThat(thing.getStatusInfo(), is(uninitializedNone));
    // add thing - provokes handler registration & initialization
    managedThingProvider.add(thing);
    waitForAssert(() -> verify(thingHandlerFactory, times(1)).registerHandler(thing));
    waitForAssert(() -> verify(thingHandler, times(1)).initialize());
    final ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(onlineNone)));
    // remove handler factory - provokes handler deregistration & disposal
    unregisterService(thingHandlerFactory);
    waitForAssert(() -> verify(thingHandlerFactory, times(1)).unregisterHandler(thing));
    waitForAssert(() -> verify(thingHandler, times(1)).dispose());
    final ThingStatusInfo uninitializedHandlerMissing = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_MISSING_ERROR).build();
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(uninitializedHandlerMissing)));
    // add handler factory - provokes handler registration & initialization
    registerService(thingHandlerFactory);
    waitForAssert(() -> verify(thingHandlerFactory, times(2)).registerHandler(thing));
    waitForAssert(() -> verify(thingHandler, times(2)).initialize());
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(onlineNone)));
    // remove thing - provokes handler deregistration & disposal
    managedThingProvider.remove(thing.getUID());
    waitForAssert(() -> verify(thingHandlerFactory, times(2)).unregisterHandler(thing));
    waitForAssert(() -> verify(thingHandler, times(2)).dispose());
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(uninitializedHandlerMissing)));
}
Also used : ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 30 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerHandlesThingStatusUpdatesUninitializedAndInitializingCorrectly.

@Test
public void thingManagerHandlesThingStatusUpdatesUninitializedAndInitializingCorrectly() {
    registerThingTypeProvider();
    ThingHandler thingHandler = mock(ThingHandler.class);
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    final ThingStatusInfo uninitializedNone = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
    assertThat(thing.getStatusInfo(), is(uninitializedNone));
    managedThingProvider.add(thing);
    final ThingStatusInfo initializingNone = ThingStatusInfoBuilder.create(ThingStatus.INITIALIZING, ThingStatusDetail.NONE).build();
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(initializingNone)));
    unregisterService(thingHandlerFactory);
    final ThingStatusInfo uninitializedHandlerMissing = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_MISSING_ERROR).build();
    waitForAssert(() -> assertThat(thing.getStatusInfo(), is(uninitializedHandlerMissing)));
}
Also used : ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)40 Thing (org.openhab.core.thing.Thing)32 ThingHandler (org.openhab.core.thing.binding.ThingHandler)31 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)30 Test (org.junit.jupiter.api.Test)27 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)26 Nullable (org.eclipse.jdt.annotation.Nullable)25 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)20 InvocationOnMock (org.mockito.invocation.InvocationOnMock)19 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)15 ArrayList (java.util.ArrayList)8 ThingUID (org.openhab.core.thing.ThingUID)8 Configuration (org.openhab.core.config.core.Configuration)6 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)5 Item (org.openhab.core.items.Item)5 Bridge (org.openhab.core.thing.Bridge)5 ThingRegistry (org.openhab.core.thing.ThingRegistry)5 BaseThingHandlerFactory (org.openhab.core.thing.binding.BaseThingHandlerFactory)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 StringItem (org.openhab.core.library.items.StringItem)4