]> git.basschouten.com Git - openhab-addons.git/blob
e493c57cc7890c0177ac677731ff9c53b4489856
[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.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;
43
44 import com.google.gson.JsonElement;
45 import com.google.gson.JsonParser;
46
47 /**
48  * Unit Tests for {@link ThermostatHandler}.
49  *
50  * @author David Pace - Initial contribution
51  *
52  */
53 @NonNullByDefault
54 public class ThermostatHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest<ThermostatHandler> {
55
56     private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildLockServiceState> childLockServiceStateCaptor;
57
58     @Override
59     protected ThermostatHandler createFixture() {
60         return new ThermostatHandler(getThing());
61     }
62
63     @Override
64     protected String getDeviceID() {
65         return "hdm:ZigBee:000d6f0017f1ace2";
66     }
67
68     @Override
69     protected ThingTypeUID getThingTypeUID() {
70         return BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
71     }
72
73     @Test
74     public void testHandleCommand()
75             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
76         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
77                 OnOffType.ON);
78         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("Thermostat"), childLockServiceStateCaptor.capture());
79         ChildLockServiceState state = childLockServiceStateCaptor.getValue();
80         assertSame(ChildLockState.ON, state.childLock);
81     }
82
83     @Test
84     public void testHandleCommandUnknownCommand() {
85         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
86                 new DecimalType(42));
87         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
88                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
89                 .withDescription(
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")
91                 .build();
92         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
93     }
94
95     @Test
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));
103     }
104
105     @Test
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));
113     }
114
115     @Test
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);
122     }
123 }