Search in sources :

Example 1 with ThingProvider

use of org.openhab.core.thing.ThingProvider in project openhab-core by openhab.

the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.

@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
    ThingUID thingUID = new ThingUID("binding:type:thing");
    Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
    ThingHandler thingHandler = new BaseThingHandler(thing) {

        @Override
        public void handleCommand(ChannelUID channelUID, Command command) {
        }

        @Override
        public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
            changedParameters = configurationParameters;
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }
    };
    thing.setHandler(thingHandler);
    ThingProvider thingProvider = new ThingProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Thing> listener) {
        }

        @Override
        public Collection<Thing> getAll() {
            return Set.of(thing);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Thing> listener) {
        }
    };
    registerService(thingProvider);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    Map<String, Object> parameters = Map.ofEntries(entry("param1", "value1"), entry("param2", 1));
    thingRegistry.updateConfiguration(thingUID, parameters);
    assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Also used : BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingRegistry(org.openhab.core.thing.ThingRegistry) ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) ThingUID(org.openhab.core.thing.ThingUID) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) Map(java.util.Map) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with ThingProvider

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

the class AbstractModbusOSGiTest method setUpAbstractModbusOSGiTest.

/**
 * Before each test, configure mocked services
 */
@BeforeEach
public void setUpAbstractModbusOSGiTest() {
    logger.debug("setUpAbstractModbusOSGiTest BEGIN");
    registerVolatileStorageService();
    registerService(mockedModbusManager);
    registerService(stateSubscriber);
    swapModbusManagerToMocked();
    thingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
    assertThat("Could not get ManagedThingProvider", thingProvider, is(notNullValue()));
    itemProvider = getService(ItemProvider.class, ManagedItemProvider.class);
    assertThat("Could not get ManagedItemProvider", itemProvider, is(notNullValue()));
    itemChannelLinkProvider = getService(ItemChannelLinkProvider.class, ManagedItemChannelLinkProvider.class);
    assertThat("Could not get ManagedItemChannelLinkProvider", itemChannelLinkProvider, is(notNullValue()));
    itemRegistry = getService(ItemRegistry.class);
    assertThat("Could not get ItemRegistry", itemRegistry, is(notNullValue()));
    coreItemFactory = new CoreItemFactory();
    // Clean slate for all tests
    reset(mockedModbusManager);
    stateSubscriber.stateUpdates.clear();
    logger.debug("setUpAbstractModbusOSGiTest END");
}
Also used : ManagedItemProvider(org.openhab.core.items.ManagedItemProvider) ItemProvider(org.openhab.core.items.ItemProvider) ManagedItemProvider(org.openhab.core.items.ManagedItemProvider) CoreItemFactory(org.openhab.core.library.CoreItemFactory) ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ItemRegistry(org.openhab.core.items.ItemRegistry) ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) ItemChannelLinkProvider(org.openhab.core.thing.link.ItemChannelLinkProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ThingProvider

use of org.openhab.core.thing.ThingProvider 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)

Aggregations

ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)3 ThingProvider (org.openhab.core.thing.ThingProvider)3 Test (org.junit.jupiter.api.Test)2 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)2 Thing (org.openhab.core.thing.Thing)2 ThingRegistry (org.openhab.core.thing.ThingRegistry)2 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ProviderChangeListener (org.openhab.core.common.registry.ProviderChangeListener)1 ItemProvider (org.openhab.core.items.ItemProvider)1 ItemRegistry (org.openhab.core.items.ItemRegistry)1 ManagedItemProvider (org.openhab.core.items.ManagedItemProvider)1 CoreItemFactory (org.openhab.core.library.CoreItemFactory)1 ChannelUID (org.openhab.core.thing.ChannelUID)1 ThingManager (org.openhab.core.thing.ThingManager)1 ThingTypeMigrationService (org.openhab.core.thing.ThingTypeMigrationService)1 ThingUID (org.openhab.core.thing.ThingUID)1 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)1 ThingHandler (org.openhab.core.thing.binding.ThingHandler)1