]> git.basschouten.com Git - openhab-addons.git/blob
1f032f614658be735832fc2de252b5247377d9ef
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.math.BigDecimal;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.test.java.JavaOSGiTest;
30 import org.openhab.core.test.storage.VolatileStorageService;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ManagedThingProvider;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingProvider;
35 import org.openhab.core.thing.binding.builder.BridgeBuilder;
36 import org.openhab.core.thing.binding.builder.ThingBuilder;
37
38 /**
39  * Tests cases for {@link TradfriGatewayHandler}.
40  *
41  * @author Kai Kreuzer - Initial contribution
42  */
43 public class TradfriHandlerOSGiTest extends JavaOSGiTest {
44
45     private ManagedThingProvider managedThingProvider;
46     private VolatileStorageService volatileStorageService = new VolatileStorageService();
47     private Bridge bridge;
48     private Thing thing;
49
50     @BeforeEach
51     public void setUp() {
52         registerService(volatileStorageService);
53         managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
54
55         Map<String, Object> properties = new HashMap<>();
56         properties.put(GATEWAY_CONFIG_HOST, "1.2.3.4");
57         properties.put(GATEWAY_CONFIG_IDENTITY, "identity");
58         properties.put(GATEWAY_CONFIG_PRE_SHARED_KEY, "pre-shared-secret-key");
59         bridge = BridgeBuilder.create(GATEWAY_TYPE_UID, "1").withLabel("My Gateway")
60                 .withConfiguration(new Configuration(properties)).build();
61
62         properties = new HashMap<>();
63         properties.put(CONFIG_ID, "65537");
64         thing = ThingBuilder.create(THING_TYPE_DIMMABLE_LIGHT, "1").withLabel("My Bulb").withBridge(bridge.getUID())
65                 .withConfiguration(new Configuration(properties)).build();
66     }
67
68     @AfterEach
69     public void tearDown() {
70         managedThingProvider.remove(thing.getUID());
71         managedThingProvider.remove(bridge.getUID());
72         unregisterService(volatileStorageService);
73     }
74
75     @Test
76     public void creationOfTradfriGatewayHandler() {
77         assertThat(bridge.getHandler(), is(nullValue()));
78         managedThingProvider.add(bridge);
79         waitForAssert(() -> assertThat(bridge.getHandler(), notNullValue()));
80
81         configurationOfTradfriGatewayHandler();
82     }
83
84     private void configurationOfTradfriGatewayHandler() {
85         Configuration configuration = bridge.getConfiguration();
86         assertThat(configuration, is(notNullValue()));
87
88         assertThat(configuration.get(GATEWAY_CONFIG_HOST), is("1.2.3.4"));
89         assertThat(configuration.get(GATEWAY_CONFIG_IDENTITY), is("identity"));
90         assertThat(configuration.get(GATEWAY_CONFIG_PRE_SHARED_KEY), is("pre-shared-secret-key"));
91     }
92
93     @Test
94     public void creationOfTradfriLightHandler() {
95         assertThat(thing.getHandler(), is(nullValue()));
96         managedThingProvider.add(bridge);
97         managedThingProvider.add(thing);
98         waitForAssert(() -> assertThat(thing.getHandler(), notNullValue()));
99
100         configurationOfTradfriLightHandler();
101     }
102
103     private void configurationOfTradfriLightHandler() {
104         Configuration configuration = thing.getConfiguration();
105         assertThat(configuration, is(notNullValue()));
106
107         assertThat(configuration.get(CONFIG_ID), is(BigDecimal.valueOf(65537)));
108     }
109 }