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.boschshc.internal.devices.smartbulb;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
22 import org.junit.jupiter.api.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCDeviceHandlerTest;
26 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
27 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
28 import org.openhab.binding.boschshc.internal.services.binaryswitch.dto.BinarySwitchServiceState;
29 import org.openhab.binding.boschshc.internal.services.hsbcoloractuator.dto.HSBColorActuatorServiceState;
30 import org.openhab.binding.boschshc.internal.services.multilevelswitch.dto.MultiLevelSwitchServiceState;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonParser;
42 * Unit tests for {@link SmartBulbHandler}.
44 * @author David Pace - Initial contribution
47 public class SmartBulbHandlerTest extends AbstractBoschSHCDeviceHandlerTest<SmartBulbHandler> {
50 private ArgumentCaptor<BinarySwitchServiceState> binarySwitchServiceStateCaptor;
53 private ArgumentCaptor<MultiLevelSwitchServiceState> multiLevelSwitchServiceStateCaptor;
56 private ArgumentCaptor<HSBColorActuatorServiceState> hsbColorActuatorServiceStateCaptor;
59 protected SmartBulbHandler createFixture() {
60 return new SmartBulbHandler(getThing());
64 protected String getDeviceID() {
65 return "hdm:ZigBee:f0d1b80000f2a3e9";
69 protected ThingTypeUID getThingTypeUID() {
70 return BoschSHCBindingConstants.THING_TYPE_SMART_BULB;
74 public void testHandleCommand_BinarySwitch()
75 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
77 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
79 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("BinarySwitch"),
80 binarySwitchServiceStateCaptor.capture());
81 BinarySwitchServiceState state = binarySwitchServiceStateCaptor.getValue();
84 getFixture().handleCommand(new ChannelUID(new ThingUID(getThingTypeUID(), "abcdef"),
85 BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
86 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("BinarySwitch"),
87 binarySwitchServiceStateCaptor.capture());
88 state = binarySwitchServiceStateCaptor.getValue();
89 assertFalse(state.on);
93 public void testHandleCommand_MultiLevelSwitch()
94 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
96 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS),
98 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("MultiLevelSwitch"),
99 multiLevelSwitchServiceStateCaptor.capture());
100 MultiLevelSwitchServiceState state = multiLevelSwitchServiceStateCaptor.getValue();
101 assertEquals(42, state.level);
105 public void testHandleCommand_HSBColorActuator()
106 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
108 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
110 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("HSBColorActuator"),
111 hsbColorActuatorServiceStateCaptor.capture());
112 HSBColorActuatorServiceState state = hsbColorActuatorServiceStateCaptor.getValue();
113 assertEquals(-16776961, state.rgb);
117 public void testUpdateChannel_BinarySwitchState() {
118 JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":true}");
119 getFixture().processUpdate("BinarySwitch", jsonObject);
120 verify(getCallback()).stateUpdated(
121 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
123 jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":false}");
124 getFixture().processUpdate("BinarySwitch", jsonObject);
125 verify(getCallback()).stateUpdated(
126 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
130 public void testUpdateChannel_MultiLevelSwitchState() {
131 JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"multiLevelSwitchState\",\"level\":16}");
132 getFixture().processUpdate("MultiLevelSwitch", jsonObject);
133 verify(getCallback()).stateUpdated(
134 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS), new PercentType(16));
138 public void testUpdateChannel_HSBColorActuatorState() {
139 JsonElement jsonObject = JsonParser.parseString("{\"colorTemperatureRange\": {\n" + " \"minCt\": 153,\n"
140 + " \"maxCt\": 526\n" + " },\n" + " \"@type\": \"colorState\",\n"
141 + " \"gamut\": \"LEDVANCE_GAMUT_A\",\n" + " \"rgb\": -12427}");
142 getFixture().processUpdate("HSBColorActuator", jsonObject);
143 verify(getCallback()).stateUpdated(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
144 HSBType.fromRGB(255, 207, 117));