Search in sources :

Example 1 with AsyncResultWrapper

use of org.eclipse.smarthome.test.AsyncResultWrapper 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 2 with AsyncResultWrapper

use of org.eclipse.smarthome.test.AsyncResultWrapper in project smarthome by eclipse.

the class HueLightDiscoveryServiceOSGiTest method hueLightRegistration.

@Test
public void hueLightRegistration() {
    FullLight light = new FullLight();
    light.setId("1");
    light.setModelID("LCT001");
    light.setType("Extended color light");
    AsyncResultWrapper<DiscoveryResult> resultWrapper = new AsyncResultWrapper<DiscoveryResult>();
    registerDiscoveryListener(new DiscoveryListener() {

        @Override
        public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
            resultWrapper.set(result);
        }

        @Override
        public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
        }

        @Override
        public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
            return null;
        }
    });
    discoveryService.onLightAdded(null, light);
    waitForAssert(() -> {
        assertTrue(resultWrapper.isSet());
    });
    final DiscoveryResult result = resultWrapper.getWrappedObject();
    assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
    assertThat(result.getThingUID().toString(), is("hue:0210:testBridge:" + light.getId()));
    assertThat(result.getThingTypeUID(), is(THING_TYPE_EXTENDED_COLOR_LIGHT));
    assertThat(result.getBridgeUID(), is(hueBridge.getUID()));
    assertThat(result.getProperties().get(LIGHT_ID), is(light.getId()));
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) DiscoveryService(org.eclipse.smarthome.config.discovery.DiscoveryService) HueLightDiscoveryService(org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService) DiscoveryListener(org.eclipse.smarthome.config.discovery.DiscoveryListener) AbstractHueOSGiTest(org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest) Test(org.junit.Test)

Example 3 with AsyncResultWrapper

use of org.eclipse.smarthome.test.AsyncResultWrapper in project smarthome by eclipse.

the class HueLightDiscoveryServiceOSGiTest method startSearchIsCalled.

@Test
public void startSearchIsCalled() {
    final AtomicBoolean searchHasBeenTriggered = new AtomicBoolean(false);
    AsyncResultWrapper<String> addressWrapper = new AsyncResultWrapper<String>();
    AsyncResultWrapper<String> bodyWrapper = new AsyncResultWrapper<String>();
    MockedHttpClient mockedHttpClient = new MockedHttpClient() {

        @Override
        public Result put(String address, String body) throws IOException {
            addressWrapper.set(address);
            bodyWrapper.set(body);
            return new Result("", 200);
        }

        @Override
        public Result get(String address) throws IOException {
            if (address.endsWith("testUserName")) {
                String body = "{\"lights\":{}}";
                return new Result(body, 200);
            } else {
                return new Result("", 404);
            }
        }

        @Override
        public Result post(String address, String body) throws IOException {
            if (address.endsWith("lights")) {
                String bodyReturn = "{\"success\": {\"/lights\": \"Searching for new devices\"}}";
                searchHasBeenTriggered.set(true);
                return new Result(bodyReturn, 200);
            } else {
                return new Result("", 404);
            }
        }
    };
    installHttpClientMock(hueBridgeHandler, mockedHttpClient);
    ThingStatusInfo online = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
    waitForAssert(() -> {
        assertThat(hueBridge.getStatusInfo(), is(online));
    });
    discoveryService.startScan();
    waitForAssert(() -> {
        assertTrue(searchHasBeenTriggered.get());
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) AbstractHueOSGiTest(org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest) Test(org.junit.Test)

Aggregations

AsyncResultWrapper (org.eclipse.smarthome.test.AsyncResultWrapper)3 Test (org.junit.Test)3 AbstractHueOSGiTest (org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest)2 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)2 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 Collection (java.util.Collection)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 HueLightDiscoveryService (org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)1 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)1 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)1 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)1 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1