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));
}
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);
}
}
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));
}
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)));
}
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)));
}
Aggregations