use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.
the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedUnitializedThing.
@Test
public void testChildHandlerInitialized_replacedUnitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
try {
childHandlerInitializedSemaphore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
}
@Override
public void initialize() {
if (getBridge() == null) {
throw new RuntimeException("Fail because of missing bridge");
}
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
try {
thingUpdatedSemapthore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
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());
}
use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.
the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.
@Test
public void assertThatCreateThingDelegatesToRegisteredThingHandlerFactory() {
ThingTypeUID expectedThingTypeUID = THING_TYPE_UID;
ThingUID expectedThingUID = new ThingUID(THING_TYPE_UID, THING1_ID);
Configuration expectedConfiguration = new Configuration();
ThingUID expectedBridgeUID = new ThingUID(THING_TYPE_UID, THING2_ID);
String expectedLabel = "Test Thing";
AsyncResultWrapper<Thing> thingResultWrapper = new AsyncResultWrapper<Thing>();
ThingRegistry thingRegistry = getService(ThingRegistry.class);
ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {
@Override
public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
return true;
}
@Override
@Nullable
protected ThingHandler createHandler(@NonNull Thing thing) {
return null;
}
@Override
@Nullable
public Thing createThing(@NonNull ThingTypeUID thingTypeUID, @NonNull Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
assertThat(thingTypeUID, is(expectedThingTypeUID));
assertThat(configuration, is(expectedConfiguration));
assertThat(thingUID, is(expectedThingUID));
assertThat(bridgeUID, is(expectedBridgeUID));
Thing thing = ThingBuilder.create(thingTypeUID, thingUID.getId()).withBridge(bridgeUID).build();
thingResultWrapper.set(thing);
return thing;
}
};
registerThingHandlerFactory(thingHandlerFactory);
Thing thing = thingRegistry.createThingOfType(expectedThingTypeUID, expectedThingUID, expectedBridgeUID, expectedLabel, expectedConfiguration);
waitForAssert(() -> {
assertTrue(thingResultWrapper.isSet());
});
assertThat(thing, is(thingResultWrapper.getWrappedObject()));
}
use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.
the class FirmwareTest method testModelRestrictedFirmwareIsSuitableForThingWithSameThingTypeAndSameModel.
@Test
public void testModelRestrictedFirmwareIsSuitableForThingWithSameThingTypeAndSameModel() {
Firmware firmware = new Firmware.Builder(new FirmwareUID(new ThingTypeUID("binding:thingTypeA"), "version")).withModelRestricted(true).withModel("someModel").build();
Thing thing = ThingBuilder.create(new ThingTypeUID("binding:thingTypeA"), "thing").build();
thing.setProperty(Thing.PROPERTY_MODEL_ID, "someModel");
assertThat(firmware.isSuitableFor(thing), is(true));
}
use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.
the class FirmwareTest method testModelRestrictedFirmwareIsNotSuitableForThingWithSameThingTypeAndAnotherModel.
@Test
public void testModelRestrictedFirmwareIsNotSuitableForThingWithSameThingTypeAndAnotherModel() {
Firmware firmware = new Firmware.Builder(new FirmwareUID(new ThingTypeUID("binding:thingTypeA"), "version")).withModelRestricted(true).withModel("someModel").build();
Thing thing = ThingBuilder.create(new ThingTypeUID("binding:thingTypeA"), "thing").build();
thing.setProperty(Thing.PROPERTY_MODEL_ID, "someOtherModel");
assertThat(firmware.isSuitableFor(thing), is(false));
}
use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.
the class FirmwareTest method testFirmwareIsNotSuitableForThingWithDifferentThingType.
@Test
public void testFirmwareIsNotSuitableForThingWithDifferentThingType() {
Firmware firmware = new Firmware.Builder(new FirmwareUID(new ThingTypeUID("binding:thingTypeA"), "version")).build();
Thing thing = ThingBuilder.create(new ThingTypeUID("binding:thingTypeB"), "thing").build();
assertThat(firmware.isSuitableFor(thing), is(false));
}
Aggregations