]> git.basschouten.com Git - openhab-addons.git/blob
2430b9ea1b56efb3a3748c2a279ba1c35c35353b
[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.boschshc.internal.devices.thermostat;
14
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import javax.measure.quantity.Temperature;
23
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.binding.boschshc.internal.services.silentmode.SilentModeState;
34 import org.openhab.binding.boschshc.internal.services.silentmode.dto.SilentModeServiceState;
35 import org.openhab.core.library.types.DecimalType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.unit.SIUnits;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
45
46 import com.google.gson.JsonElement;
47 import com.google.gson.JsonParser;
48
49 /**
50  * Unit Tests for {@link ThermostatHandler}.
51  *
52  * @author David Pace - Initial contribution
53  *
54  */
55 @NonNullByDefault
56 class ThermostatHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest<ThermostatHandler> {
57
58     private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildLockServiceState> childLockServiceStateCaptor;
59
60     private @Captor @NonNullByDefault({}) ArgumentCaptor<SilentModeServiceState> silentModeServiceStateCaptor;
61
62     @Override
63     protected ThermostatHandler createFixture() {
64         return new ThermostatHandler(getThing());
65     }
66
67     @Override
68     protected String getDeviceID() {
69         return "hdm:ZigBee:000d6f0017f1ace2";
70     }
71
72     @Override
73     protected ThingTypeUID getThingTypeUID() {
74         return BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
75     }
76
77     @Test
78     void testHandleCommandChildLockService()
79             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
80         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
81                 OnOffType.ON);
82         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("Thermostat"), childLockServiceStateCaptor.capture());
83         ChildLockServiceState state = childLockServiceStateCaptor.getValue();
84         assertSame(ChildLockState.ON, state.childLock);
85     }
86
87     @Test
88     void testHandleCommandSilentModeService()
89             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
90         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
91                 OnOffType.ON);
92         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SilentMode"),
93                 silentModeServiceStateCaptor.capture());
94         SilentModeServiceState state = silentModeServiceStateCaptor.getValue();
95         assertSame(SilentModeState.MODE_SILENT, state.mode);
96     }
97
98     @Test
99     void testHandleCommandUnknownCommandChildLockService() {
100         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
101                 new DecimalType(42));
102         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
103                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
104                 .withDescription(
105                         "Error when service Thermostat should handle command org.openhab.core.library.types.DecimalType: Thermostat: Can not handle command org.openhab.core.library.types.DecimalType")
106                 .build();
107         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
108     }
109
110     @Test
111     void testHandleCommandUnknownCommandSilentModeService() {
112         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
113                 new DecimalType(42));
114         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
115                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
116                 .withDescription(
117                         "Error when service SilentMode should handle command org.openhab.core.library.types.DecimalType: SilentMode: Can not handle command org.openhab.core.library.types.DecimalType")
118                 .build();
119         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
120     }
121
122     @Test
123     void testUpdateChannelsTemperatureLevelService() {
124         JsonElement jsonObject = JsonParser.parseString(
125                 "{\n" + "   \"@type\": \"temperatureLevelState\",\n" + "   \"temperature\": 21.5\n" + " }");
126         getFixture().processUpdate("TemperatureLevel", jsonObject);
127         verify(getCallback()).stateUpdated(
128                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_TEMPERATURE),
129                 new QuantityType<Temperature>(21.5, SIUnits.CELSIUS));
130     }
131
132     @Test
133     void testUpdateChannelsValveTappetService() {
134         JsonElement jsonObject = JsonParser
135                 .parseString("{\n" + "   \"@type\": \"valveTappetState\",\n" + "   \"position\": 42\n" + " }");
136         getFixture().processUpdate("ValveTappet", jsonObject);
137         verify(getCallback()).stateUpdated(
138                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_VALVE_TAPPET_POSITION),
139                 new DecimalType(42));
140     }
141
142     @Test
143     void testUpdateChannelsChildLockService() {
144         JsonElement jsonObject = JsonParser
145                 .parseString("{\n" + "   \"@type\": \"childLockState\",\n" + "   \"childLock\": \"ON\"\n" + " }");
146         getFixture().processUpdate("Thermostat", jsonObject);
147         verify(getCallback()).stateUpdated(
148                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK), OnOffType.ON);
149     }
150
151     @Test
152     void testUpdateChannelsSilentModeService() {
153         JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_SILENT\"}");
154         getFixture().processUpdate("SilentMode", jsonObject);
155         verify(getCallback()).stateUpdated(
156                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.ON);
157     }
158
159     @Test
160     void testUpdateChannelsSilentModeServiceNormal() {
161         JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_NORMAL\"}");
162         getFixture().processUpdate("SilentMode", jsonObject);
163         verify(getCallback()).stateUpdated(
164                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.OFF);
165     }
166 }