Search in sources :

Example 1 with BaseBridgeHandler

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

the class ThingManagerOSGiJavaTest method testChildHandlerInitializedModifiedUninitializedThing.

@Test
public void testChildHandlerInitializedModifiedUninitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

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

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            assertDoesNotThrow(() -> childHandlerInitializedSemaphore.acquire());
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

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

        @Override
        public void initialize() {
            if (getBridge() == null) {
                throw new IllegalStateException("Fail because of missing bridge");
            }
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            assertDoesNotThrow(() -> thingUpdatedSemapthore.acquire());
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    thing.setBridgeUID(bridge.getUID());
    managedThingProvider.update(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Command(org.openhab.core.types.Command) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ChannelUID(org.openhab.core.thing.ChannelUID) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.openhab.core.thing.Bridge) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with BaseBridgeHandler

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

the class ThingManagerOSGiJavaTest method testChildHandlerInitializedReplacedInitializedThing.

@Test
public void testChildHandlerInitializedReplacedInitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

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

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            assertDoesNotThrow(() -> childHandlerInitializedSemaphore.acquire());
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

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

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            assertDoesNotThrow(() -> thingUpdatedSemapthore.acquire());
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
    managedThingProvider.update(thing2);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing2.getStatus());
    });
    // childHandlerInitialized(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    // ThingHandler.thingUpdated(...) must be called
    assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Command(org.openhab.core.types.Command) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ChannelUID(org.openhab.core.thing.ChannelUID) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.openhab.core.thing.Bridge) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with BaseBridgeHandler

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

the class ThingManagerOSGiJavaTest method testChildHandlerInitializedReplacedUnitializedThing.

@Test
public void testChildHandlerInitializedReplacedUnitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

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

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            assertDoesNotThrow(() -> childHandlerInitializedSemaphore.acquire());
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

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

        @Override
        public void initialize() {
            if (getBridge() == null) {
                throw new IllegalStateException("Fail because of missing bridge");
            }
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            assertDoesNotThrow(() -> thingUpdatedSemapthore.acquire());
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
    managedThingProvider.update(thing2);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing2.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Command(org.openhab.core.types.Command) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ChannelUID(org.openhab.core.thing.ChannelUID) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.openhab.core.thing.Bridge) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Semaphore (java.util.concurrent.Semaphore)3 Test (org.junit.jupiter.api.Test)3 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)3 Bridge (org.openhab.core.thing.Bridge)3 ChannelUID (org.openhab.core.thing.ChannelUID)3 Thing (org.openhab.core.thing.Thing)3 BaseBridgeHandler (org.openhab.core.thing.binding.BaseBridgeHandler)3 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)3 ThingHandler (org.openhab.core.thing.binding.ThingHandler)3 Command (org.openhab.core.types.Command)3