2 * Copyright (c) 2010-2023 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.math.BigDecimal;
22 import java.util.HashMap;
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;
39 * Tests cases for {@link TradfriGatewayHandler}.
41 * @author Kai Kreuzer - Initial contribution
43 public class TradfriHandlerOSGiTest extends JavaOSGiTest {
45 private ManagedThingProvider managedThingProvider;
46 private VolatileStorageService volatileStorageService = new VolatileStorageService();
47 private Bridge bridge;
52 registerService(volatileStorageService);
53 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
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();
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();
69 public void tearDown() {
70 managedThingProvider.remove(thing.getUID());
71 managedThingProvider.remove(bridge.getUID());
72 unregisterService(volatileStorageService);
76 public void creationOfTradfriGatewayHandler() {
77 assertThat(bridge.getHandler(), is(nullValue()));
78 managedThingProvider.add(bridge);
79 waitForAssert(() -> assertThat(bridge.getHandler(), notNullValue()));
81 configurationOfTradfriGatewayHandler();
84 private void configurationOfTradfriGatewayHandler() {
85 Configuration configuration = bridge.getConfiguration();
86 assertThat(configuration, is(notNullValue()));
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"));
94 public void creationOfTradfriLightHandler() {
95 assertThat(thing.getHandler(), is(nullValue()));
96 managedThingProvider.add(bridge);
97 managedThingProvider.add(thing);
98 waitForAssert(() -> assertThat(thing.getHandler(), notNullValue()));
100 configurationOfTradfriLightHandler();
103 private void configurationOfTradfriLightHandler() {
104 Configuration configuration = thing.getConfiguration();
105 assertThat(configuration, is(notNullValue()));
107 assertThat(configuration.get(CONFIG_ID), is(BigDecimal.valueOf(65537)));