]> git.basschouten.com Git - openhab-addons.git/blob
3f677fb34ac91f7fb9c3e73323ebd88f2eaf1fa9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tradfri.internal.handler;
14
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;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
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;
36
37 /**
38  * Tests cases for {@link TradfriGatewayHandler}.
39  *
40  * @author Kai Kreuzer - Initial contribution
41  */
42 public class TradfriHandlerOSGiTest extends JavaOSGiTest {
43
44     private ManagedThingProvider managedThingProvider;
45     private VolatileStorageService volatileStorageService = new VolatileStorageService();
46     private Bridge bridge;
47     private Thing thing;
48
49     @BeforeEach
50     public void setUp() {
51         registerService(volatileStorageService);
52         managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
53
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();
60
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();
65     }
66
67     @AfterEach
68     public void tearDown() {
69         managedThingProvider.remove(thing.getUID());
70         managedThingProvider.remove(bridge.getUID());
71         unregisterService(volatileStorageService);
72     }
73
74     @Test
75     public void creationOfTradfriGatewayHandler() {
76         assertThat(bridge.getHandler(), is(nullValue()));
77         managedThingProvider.add(bridge);
78         waitForAssert(() -> assertThat(bridge.getHandler(), notNullValue()));
79
80         configurationOfTradfriGatewayHandler();
81     }
82
83     private void configurationOfTradfriGatewayHandler() {
84         Configuration configuration = bridge.getConfiguration();
85         assertThat(configuration, is(notNullValue()));
86
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"));
90     }
91
92     @Test
93     public void creationOfTradfriLightHandler() {
94         assertThat(thing.getHandler(), is(nullValue()));
95         managedThingProvider.add(bridge);
96         managedThingProvider.add(thing);
97         waitForAssert(() -> assertThat(thing.getHandler(), notNullValue()));
98
99         configurationOfTradfriLightHandler();
100     }
101
102     private void configurationOfTradfriLightHandler() {
103         Configuration configuration = thing.getConfiguration();
104         assertThat(configuration, is(notNullValue()));
105
106         assertThat(configuration.get(CONFIG_ID), is("65537"));
107     }
108 }