use of org.openhab.core.thing.ThingRegistry 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.ThingRegistry in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerAllowsChangesToUnmanagedThings.
@Test
@SuppressWarnings({ "null", "unchecked" })
public void thingManagerAllowsChangesToUnmanagedThings() throws Exception {
ThingManager thingManager = (ThingManager) getService(ThingTypeMigrationService.class);
assertThat(thingManager, is(notNullValue()));
ThingProvider customThingProvider = mock(ThingProvider.class);
when(customThingProvider.getAll()).thenReturn(List.of(thing));
registerService(customThingProvider);
ThingRegistry thingRegistry = getService(ThingRegistry.class);
RegistryChangeListener<Thing> registryChangeListener = mock(RegistryChangeListener.class);
try {
thingRegistry.addRegistryChangeListener(registryChangeListener);
Field field = thingManager.getClass().getDeclaredField("thingHandlerCallback");
field.setAccessible(true);
ThingHandlerCallback callback = (ThingHandlerCallback) field.get(thingManager);
callback.thingUpdated(thing);
verify(registryChangeListener, times(1)).updated(any(Thing.class), any(Thing.class));
} finally {
thingRegistry.removeRegistryChangeListener(registryChangeListener);
}
}
use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.
the class ThingManagerOSGiTest method thingManagerCallsBridgeStatusChangedOnThingHandlerCorrectly.
@Test
@SuppressWarnings("null")
public void thingManagerCallsBridgeStatusChangedOnThingHandlerCorrectly() {
class BridgeHandlerState {
@Nullable
ThingHandlerCallback callback;
}
final BridgeHandlerState bridgeState = new BridgeHandlerState();
Bridge bridge = BridgeBuilder.create(new ThingTypeUID("binding:test"), new ThingUID("binding:test:someBridgeUID-1")).build();
BridgeHandler bridgeHandler = mock(BridgeHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
bridgeState.callback = (ThingHandlerCallback) invocation.getArgument(0);
return null;
}
}).when(bridgeHandler).setCallback(any(ThingHandlerCallback.class));
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
bridgeState.callback.statusUpdated(bridge, ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build());
return null;
}
}).when(bridgeHandler).initialize();
when(bridgeHandler.getThing()).thenReturn(bridge);
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
bridgeState.callback.statusUpdated(bridge, ThingStatusInfoBuilder.create(ThingStatus.REMOVED, ThingStatusDetail.NONE).build());
return null;
}
}).when(bridgeHandler).handleRemoval();
class ThingHandlerState {
@Nullable
ThingHandlerCallback callback;
}
final ThingHandlerState thingState = new ThingHandlerState();
Thing thing = ThingBuilder.create(new ThingTypeUID("binding:type"), new ThingUID("binding:type:thingUID-1")).withBridge(bridge.getUID()).build();
ThingHandler thingHandler = mock(ThingHandler.class);
doAnswer(new Answer<Void>() {
@Override
@Nullable
public Void answer(InvocationOnMock invocation) throws Throwable {
thingState.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 {
thingState.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build());
return null;
}
}).when(thingHandler).initialize();
when(thingHandler.getThing()).thenReturn(thing);
ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
doAnswer(new Answer<ThingHandler>() {
@Override
@Nullable
public ThingHandler answer(InvocationOnMock invocation) throws Throwable {
Thing thing = (Thing) invocation.getArgument(0);
if (thing instanceof Bridge) {
return bridgeHandler;
} else if (thing instanceof Thing) {
return thingHandler;
}
return null;
}
}).when(thingHandlerFactory).registerHandler(any(Thing.class));
registerService(thingHandlerFactory);
managedThingProvider.add(bridge);
managedThingProvider.add(thing);
waitForAssert(() -> assertThat(bridge.getStatus(), is(ThingStatus.ONLINE)));
waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
// initial bridge initialization is not reported as status change
waitForAssert(() -> verify(thingHandler, never()).bridgeStatusChanged(any(ThingStatusInfo.class)));
// the same status is also not reported, because it's not a change
ThingStatusInfo onlineNone = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
bridgeState.callback.statusUpdated(bridge, onlineNone);
waitForAssert(() -> verify(thingHandler, never()).bridgeStatusChanged(any(ThingStatusInfo.class)));
// report a change to OFFLINE
ThingStatusInfo offlineNone = ThingStatusInfoBuilder.create(ThingStatus.OFFLINE, ThingStatusDetail.NONE).build();
bridgeState.callback.statusUpdated(bridge, offlineNone);
waitForAssert(() -> verify(thingHandler, times(1)).bridgeStatusChanged(any(ThingStatusInfo.class)));
// report a change to ONLINE
bridgeState.callback.statusUpdated(bridge, onlineNone);
waitForAssert(() -> verify(thingHandler, times(2)).bridgeStatusChanged(any(ThingStatusInfo.class)));
ThingRegistry thingRegistry = getService(ThingRegistry.class);
thingRegistry.remove(bridge.getUID());
waitForAssert(() -> {
assertThat(bridge.getStatus(), is(equalTo(ThingStatus.UNINITIALIZED)));
waitForAssert(() -> verify(thingHandler, times(2)).bridgeStatusChanged(any(ThingStatusInfo.class)));
});
}
use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.
the class ThingEventOSGiTest method assertThingEventsAreSent.
@Test
public void assertThingEventsAreSent() {
ThingRegistry thingRegistry = getService(ThingRegistry.class);
ThingEventSubscriber eventSubscriber = new ThingEventSubscriber();
registerService(eventSubscriber);
ThingType thingType = ThingTypeBuilder.instance("bindingId", "thingTypeId", "label").build();
ThingUID thingUID = new ThingUID(thingType.getUID(), "thingId");
Configuration configuration = new Configuration();
Thing thing = ThingFactory.createThing(thingType, thingUID, configuration);
thingRegistry.add(thing);
waitFor(() -> eventSubscriber.getLastReceivedEvent() != null);
waitForAssert(() -> assertThat(eventSubscriber.getLastReceivedEvent().getType(), is(ThingAddedEvent.TYPE)));
assertThat(eventSubscriber.getLastReceivedEvent().getTopic(), is(ThingEventFactory.THING_ADDED_EVENT_TOPIC.replace("{thingUID}", thingUID.getAsString())));
thingRegistry.update(thing);
waitForAssert(() -> assertThat(eventSubscriber.getLastReceivedEvent().getType(), is(ThingUpdatedEvent.TYPE)));
assertThat(eventSubscriber.getLastReceivedEvent().getTopic(), is(ThingEventFactory.THING_UPDATED_EVENT_TOPIC.replace("{thingUID}", thingUID.getAsString())));
thingRegistry.forceRemove(thing.getUID());
waitForAssert(() -> assertThat(eventSubscriber.getLastReceivedEvent().getType(), is(ThingRemovedEvent.TYPE)));
assertThat(eventSubscriber.getLastReceivedEvent().getTopic(), is(ThingEventFactory.THING_REMOVED_EVENT_TOPIC.replace("{thingUID}", thingUID.getAsString())));
}
Aggregations