2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tradfri.internal.handler;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
19 import static org.openhab.binding.tradfri.internal.config.TradfriDeviceConfig.CONFIG_ID;
21 import java.util.HashMap;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.test.java.JavaOSGiTest;
29 import org.openhab.core.test.storage.VolatileStorageService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ManagedThingProvider;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingProvider;
34 import org.openhab.core.thing.binding.builder.BridgeBuilder;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 * Tests cases for {@link TradfriGatewayHandler}.
40 * @author Kai Kreuzer - Initial contribution
42 public class TradfriHandlerOSGiTest extends JavaOSGiTest {
44 private ManagedThingProvider managedThingProvider;
45 private VolatileStorageService volatileStorageService = new VolatileStorageService();
46 private Bridge bridge;
51 registerService(volatileStorageService);
52 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
54 Map<String, Object> properties = new HashMap<>();
55 properties.put(GATEWAY_CONFIG_HOST, "1.2.3.4");
56 properties.put(GATEWAY_CONFIG_IDENTITY, "identity");
57 properties.put(GATEWAY_CONFIG_PRE_SHARED_KEY, "pre-shared-secret-key");
58 bridge = BridgeBuilder.create(GATEWAY_TYPE_UID, "1").withLabel("My Gateway")
59 .withConfiguration(new Configuration(properties)).build();
61 properties = new HashMap<>();
62 properties.put(CONFIG_ID, "65537");
63 thing = ThingBuilder.create(THING_TYPE_DIMMABLE_LIGHT, "1").withLabel("My Bulb").withBridge(bridge.getUID())
64 .withConfiguration(new Configuration(properties)).build();
68 public void tearDown() {
69 managedThingProvider.remove(thing.getUID());
70 managedThingProvider.remove(bridge.getUID());
71 unregisterService(volatileStorageService);
75 public void creationOfTradfriGatewayHandler() {
76 assertThat(bridge.getHandler(), is(nullValue()));
77 managedThingProvider.add(bridge);
78 waitForAssert(() -> assertThat(bridge.getHandler(), notNullValue()));
80 configurationOfTradfriGatewayHandler();
83 private void configurationOfTradfriGatewayHandler() {
84 Configuration configuration = bridge.getConfiguration();
85 assertThat(configuration, is(notNullValue()));
87 assertThat(configuration.get(GATEWAY_CONFIG_HOST), is("1.2.3.4"));
88 assertThat(configuration.get(GATEWAY_CONFIG_IDENTITY), is("identity"));
89 assertThat(configuration.get(GATEWAY_CONFIG_PRE_SHARED_KEY), is("pre-shared-secret-key"));
93 public void creationOfTradfriLightHandler() {
94 assertThat(thing.getHandler(), is(nullValue()));
95 managedThingProvider.add(bridge);
96 managedThingProvider.add(thing);
97 waitForAssert(() -> assertThat(thing.getHandler(), notNullValue()));
99 configurationOfTradfriLightHandler();
102 private void configurationOfTradfriLightHandler() {
103 Configuration configuration = thing.getConfiguration();
104 assertThat(configuration, is(notNullValue()));
106 assertThat(configuration.get(CONFIG_ID), is("65537"));