Search in sources :

Example 26 with ThingRegistry

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);
    }
}
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 27 with ThingRegistry

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);
    }
}
Also used : Field(java.lang.reflect.Field) ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeMigrationService(org.openhab.core.thing.ThingTypeMigrationService) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingManager(org.openhab.core.thing.ThingManager) Thing(org.openhab.core.thing.Thing) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 28 with ThingRegistry

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)));
    });
}
Also used : BridgeHandler(org.openhab.core.thing.binding.BridgeHandler) 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) ThingRegistry(org.openhab.core.thing.ThingRegistry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingUID(org.openhab.core.thing.ThingUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Bridge(org.openhab.core.thing.Bridge) 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 29 with ThingRegistry

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())));
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingUID(org.openhab.core.thing.ThingUID) ThingType(org.openhab.core.thing.type.ThingType) Thing(org.openhab.core.thing.Thing) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ThingRegistry (org.openhab.core.thing.ThingRegistry)29 Thing (org.openhab.core.thing.Thing)18 Test (org.junit.jupiter.api.Test)17 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)14 ThingUID (org.openhab.core.thing.ThingUID)13 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)11 Configuration (org.openhab.core.config.core.Configuration)9 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ThingHandler (org.openhab.core.thing.binding.ThingHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)6 Bridge (org.openhab.core.thing.Bridge)5 ThingProvider (org.openhab.core.thing.ThingProvider)5 ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)5 Item (org.openhab.core.items.Item)4 NumberItem (org.openhab.core.library.items.NumberItem)4 VolatileStorageService (org.openhab.core.test.storage.VolatileStorageService)4 Locale (java.util.Locale)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3