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.thermostat;
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
22 import javax.measure.quantity.Temperature;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.openhab.binding.boschshc.internal.devices.AbstractBatteryPoweredDeviceHandlerTest;
29 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
30 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
31 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockServiceState;
32 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockState;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.QuantityType;
36 import org.openhab.core.library.unit.SIUnits;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.ThingStatusInfo;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
44 import com.google.gson.JsonElement;
45 import com.google.gson.JsonParser;
48 * Unit Tests for {@link ThermostatHandler}.
50 * @author David Pace - Initial contribution
54 public class ThermostatHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest<ThermostatHandler> {
56 private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildLockServiceState> childLockServiceStateCaptor;
59 protected ThermostatHandler createFixture() {
60 return new ThermostatHandler(getThing());
64 protected String getDeviceID() {
65 return "hdm:ZigBee:000d6f0017f1ace2";
69 protected ThingTypeUID getThingTypeUID() {
70 return BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
74 public void testHandleCommand()
75 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
76 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
78 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("Thermostat"), childLockServiceStateCaptor.capture());
79 ChildLockServiceState state = childLockServiceStateCaptor.getValue();
80 assertSame(ChildLockState.ON, state.childLock);
84 public void testHandleCommandUnknownCommand() {
85 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
87 ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
88 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
90 "Error when service Thermostat should handle command org.openhab.core.library.types.DecimalType: Thermostat: Can not handle command org.openhab.core.library.types.DecimalType")
92 verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
96 public void testUpdateChannelsTemperatureLevelService() {
97 JsonElement jsonObject = JsonParser.parseString(
98 "{\n" + " \"@type\": \"temperatureLevelState\",\n" + " \"temperature\": 21.5\n" + " }");
99 getFixture().processUpdate("TemperatureLevel", jsonObject);
100 verify(getCallback()).stateUpdated(
101 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_TEMPERATURE),
102 new QuantityType<Temperature>(21.5, SIUnits.CELSIUS));
106 public void testUpdateChannelsValveTappetService() {
107 JsonElement jsonObject = JsonParser
108 .parseString("{\n" + " \"@type\": \"valveTappetState\",\n" + " \"position\": 42\n" + " }");
109 getFixture().processUpdate("ValveTappet", jsonObject);
110 verify(getCallback()).stateUpdated(
111 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_VALVE_TAPPET_POSITION),
112 new DecimalType(42));
116 public void testUpdateChannelsChildLockService() {
117 JsonElement jsonObject = JsonParser
118 .parseString("{\n" + " \"@type\": \"childLockState\",\n" + " \"childLock\": \"ON\"\n" + " }");
119 getFixture().processUpdate("Thermostat", jsonObject);
120 verify(getCallback()).stateUpdated(
121 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK), OnOffType.ON);