Search in sources :

Example 51 with Thing

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());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 52 with Thing

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()));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 53 with Thing

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));
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 54 with Thing

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));
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 55 with Thing

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));
}
Also used : ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6