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());
}
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());
}
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());
}
Aggregations