Search in sources :

Example 16 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerChangesTheThingTypeCorrectlyEvenIfInitializeTakesLongAndCalledFromThere.

@Test
public void thingManagerChangesTheThingTypeCorrectlyEvenIfInitializeTakesLongAndCalledFromThere() {
    registerThingTypeProvider();
    ThingTypeUID newThingTypeUID = new ThingTypeUID("binding:type2");
    class ThingHandlerState {

        boolean initializeRunning;

        boolean raceCondition;

        boolean migrateBlocked;

        @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));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            if (state.initializeRunning) {
                state.raceCondition = true;
            }
            state.initializeRunning = true;
            long start = System.nanoTime();
            state.callback.migrateThingType(thing, newThingTypeUID, thing.getConfiguration());
            if (System.nanoTime() - start > TimeUnit.SECONDS.toNanos(1)) {
                state.migrateBlocked = true;
            }
            Thread.sleep(3000);
            state.initializeRunning = false;
            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);
    managedThingProvider.add(thing);
    waitForAssert(() -> assertThat(thing.getThingTypeUID().getAsString(), is(equalTo(newThingTypeUID.getAsString()))));
    assertThat(state.migrateBlocked, is(false));
    assertThat(state.raceCondition, is(false));
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) 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 17 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerDoesNotDelegateUpdateEventsToItsSource.

@Test
public void thingManagerDoesNotDelegateUpdateEventsToItsSource() {
    registerThingTypeProvider();
    class ThingHandlerState {

        boolean handleCommandWasCalled;

        @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));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.handleCommandWasCalled = true;
            state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
            return null;
        }
    }).when(thingHandler).handleCommand(any(ChannelUID.class), any(Command.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);
    String itemName = "name";
    managedThingProvider.add(thing);
    managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
    registerService(thingHandlerFactory);
    Item item = new StringItem(itemName);
    itemRegistry.add(item);
    waitForAssert(() -> assertThat(itemRegistry.get(itemName), is(notNullValue())));
    state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
    // event should be delivered
    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON));
    waitForAssert(() -> assertThat(state.handleCommandWasCalled, is(true)));
    state.handleCommandWasCalled = false;
    // event should not be delivered, because the source is the same
    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, OnOffType.ON, CHANNEL_UID.toString()));
    waitFor(() -> state.handleCommandWasCalled);
    assertThat(state.handleCommandWasCalled, is(false));
}
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) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) Command(org.openhab.core.types.Command) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelUID(org.openhab.core.thing.ChannelUID) 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 18 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerHandlesThingStatusUpdateUninitializedWithAnExceptionCorrectly.

@Test
public void thingManagerHandlesThingStatusUpdateUninitializedWithAnExceptionCorrectly() {
    String exceptionMessage = "Some runtime exception occurred!";
    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))).thenThrow(new RuntimeException(exceptionMessage));
    registerService(thingHandlerFactory);
    managedThingProvider.add(thing);
    ThingStatusInfo statusInfo = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_REGISTERING_ERROR).withDescription(exceptionMessage).build();
    assertThat(thing.getStatusInfo(), is(statusInfo));
}
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)

Example 19 with ThingHandlerFactory

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

the class ThingManagerOSGiTest method thingManagerWaitsWithInitializeUntilBundleProcessingIsFinished.

@Test
@SuppressWarnings("null")
public void thingManagerWaitsWithInitializeUntilBundleProcessingIsFinished() throws Exception {
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    class ThingHandlerState {

        @SuppressWarnings("unused")
        @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);
    final ReadyMarker marker = new ReadyMarker(ThingManagerImpl.XML_THING_TYPE, ReadyMarkerUtils.getIdentifier(FrameworkUtil.getBundle(this.getClass())));
    waitForAssert(() -> {
        // wait for the XML processing to be finished, then remove the ready marker again
        assertThat(readyService.isReady(marker), is(true));
        readyService.unmarkReady(marker);
    });
    ThingStatusInfo uninitializedNone = ThingStatusInfoBuilder.create(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE).build();
    assertThat(thing.getStatusInfo(), is(uninitializedNone));
    managedThingProvider.add(thing);
    // just wait a little to make sure really nothing happens
    Thread.sleep(1000);
    verify(thingHandler, never()).initialize();
    assertThat(thing.getStatusInfo(), is(uninitializedNone));
    readyService.markReady(marker);
    // ThingHandler.initialize() called, thing status is INITIALIZING.NONE
    ThingStatusInfo initializingNone = ThingStatusInfoBuilder.create(ThingStatus.INITIALIZING, ThingStatusDetail.NONE).build();
    waitForAssert(() -> {
        verify(thingHandler, times(1)).initialize();
        assertThat(thing.getStatusInfo(), is(initializingNone));
    });
}
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) ReadyMarker(org.openhab.core.service.ReadyMarker) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 20 with ThingHandlerFactory

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

the class GenericThingProviderMultipleBundlesTest method setup.

@BeforeEach
public void setup() {
    thingProvider = new GenericThingProvider();
    Bundle bundle = mock(Bundle.class);
    when(bundle.getSymbolicName()).thenReturn(BUNDLE_NAME);
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any(Class.class))).thenReturn(bundle);
    thingProvider.setBundleResolver(bundleResolver);
    ModelRepository modelRepository = mock(ModelRepository.class);
    ThingModel thingModel = mock(ThingModel.class);
    EList<ModelThing> dslThings = createModelBridge();
    when(thingModel.getThings()).thenReturn(dslThings);
    when(modelRepository.getModel(TEST_MODEL_THINGS)).thenReturn(thingModel);
    thingProvider.setModelRepository(modelRepository);
    // configure bridgeHandlerFactory to accept the bridge type UID and create a bridge:
    bridgeHandlerFactory = mock(ThingHandlerFactory.class);
    when(bridgeHandlerFactory.supportsThingType(BRIDGE_TYPE_UID)).thenReturn(true);
    when(bridgeHandlerFactory.createThing(eq(BRIDGE_TYPE_UID), any(Configuration.class), eq(BRIDGE_UID), eq(null))).thenReturn(BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_ID).build());
    thingProvider.addThingHandlerFactory(bridgeHandlerFactory);
    // configure thingHandlerFactory to accept the thing type UID and create a thing:
    thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(THING_TYPE_UID)).thenReturn(true);
    when(thingHandlerFactory.createThing(eq(THING_TYPE_UID), any(Configuration.class), eq(THING_UID), eq(BRIDGE_UID))).thenReturn(ThingBuilder.create(THING_TYPE_UID, THING_ID).build());
    thingProvider.addThingHandlerFactory(thingHandlerFactory);
}
Also used : BundleResolver(org.openhab.core.util.BundleResolver) ModelRepository(org.openhab.core.model.core.ModelRepository) Configuration(org.openhab.core.config.core.Configuration) Bundle(org.osgi.framework.Bundle) ThingModel(org.openhab.core.model.thing.thing.ThingModel) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ModelThing(org.openhab.core.model.thing.thing.ModelThing) BeforeEach(org.junit.jupiter.api.BeforeEach)

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