]> git.basschouten.com Git - openhab-addons.git/blob
098bd46a8a8cb01985b042ff793fb57756b9ccb8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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;
14
15 import static org.mockito.ArgumentMatchers.anyString;
16 import static org.mockito.Mockito.*;
17
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData;
25 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.types.RefreshType;
30 import org.openhab.core.types.UnDefType;
31
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonParser;
34
35 /**
36  * Abstract test implementation for battery-powered devices.
37  *
38  * @author David Pace - Initial contribution
39  *
40  * @param <T> type of the battery-powered device to be tested
41  */
42 @NonNullByDefault
43 public abstract class AbstractBatteryPoweredDeviceHandlerTest<T extends AbstractBatteryPoweredDeviceHandler>
44         extends AbstractBoschSHCDeviceHandlerTest<T> {
45
46     @BeforeEach
47     @Override
48     public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
49         DeviceServiceData deviceServiceData = new DeviceServiceData();
50         deviceServiceData.path = "/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel";
51         deviceServiceData.id = "BatteryLevel";
52         deviceServiceData.deviceId = "hdm:ZigBee:000d6f0004b93361";
53         when(getBridgeHandler().getServiceData(anyString(), anyString())).thenReturn(deviceServiceData);
54
55         super.beforeEach();
56     }
57
58     @Test
59     public void testProcessUpdateBatteryLevelLowBattery() {
60         JsonElement deviceServiceData = JsonParser.parseString("""
61                 {
62                     "@type":"DeviceServiceData",
63                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
64                     "id":"BatteryLevel",
65                     "deviceId":"hdm:ZigBee:000d6f0004b93361",
66                     "faults":{
67                         "entries":[
68                           {
69                             "type":"LOW_BATTERY",
70                             "category":"WARNING"
71                           }
72                         ]
73                     }
74                 }\
75                 """);
76         getFixture().processUpdate("BatteryLevel", deviceServiceData);
77         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
78                 new DecimalType(10));
79         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
80     }
81
82     @Test
83     public void testProcessUpdateBatteryLevelCriticalLow() {
84         JsonElement deviceServiceData = JsonParser.parseString("""
85                 {
86                     "@type":"DeviceServiceData",
87                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
88                     "id":"BatteryLevel",
89                     "deviceId":"hdm:ZigBee:000d6f0004b93361",
90                     "faults":{
91                         "entries":[
92                           {
93                             "type":"CRITICAL_LOW",
94                             "category":"WARNING"
95                           }
96                         ]
97                     }
98                 }\
99                 """);
100         getFixture().processUpdate("BatteryLevel", deviceServiceData);
101         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
102                 new DecimalType(1));
103         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
104     }
105
106     @Test
107     public void testProcessUpdateBatteryLevelCriticallyLowBattery() {
108         JsonElement deviceServiceData = JsonParser.parseString("""
109                 {
110                     "@type":"DeviceServiceData",
111                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
112                     "id":"BatteryLevel",
113                     "deviceId":"hdm:ZigBee:000d6f0004b93361",
114                     "faults":{
115                         "entries":[
116                           {
117                             "type":"CRITICALLY_LOW_BATTERY",
118                             "category":"WARNING"
119                           }
120                         ]
121                     }
122                 }\
123                 """);
124         getFixture().processUpdate("BatteryLevel", deviceServiceData);
125         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
126                 new DecimalType(1));
127         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
128     }
129
130     @Test
131     public void testProcessUpdateBatteryLevelOK() {
132         JsonElement deviceServiceData = JsonParser.parseString("""
133                 {
134                     "@type":"DeviceServiceData",
135                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
136                     "id":"BatteryLevel",
137                     "deviceId":"hdm:ZigBee:000d6f0004b93361" }\
138                 """);
139         getFixture().processUpdate("BatteryLevel", deviceServiceData);
140         // state is updated twice: via short poll in initialize() and via long poll result in this test
141         verify(getCallback(), times(2)).stateUpdated(
142                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
143                 new DecimalType(100));
144         verify(getCallback(), times(2)).stateUpdated(
145                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
146     }
147
148     @Test
149     public void testProcessUpdateBatteryLevelNotAvailable() {
150         JsonElement deviceServiceData = JsonParser.parseString("""
151                 {
152                     "@type":"DeviceServiceData",
153                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
154                     "id":"BatteryLevel",
155                     "deviceId":"hdm:ZigBee:000d6f0004b93361",
156                     "faults":{
157                         "entries":[
158                           {
159                             "type":"NOT_AVAILABLE",
160                             "category":"WARNING"
161                           }
162                         ]
163                     }
164                 }\
165                 """);
166         getFixture().processUpdate("BatteryLevel", deviceServiceData);
167         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
168                 UnDefType.UNDEF);
169         // state is updated twice: via short poll in initialize() and via long poll result in this test
170         verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY),
171                 OnOffType.OFF);
172     }
173
174     @Test
175     public void testHandleCommandRefreshBatteryLevelChannel() {
176         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL), RefreshType.REFRESH);
177         // state is updated twice: via short poll in initialize() and via long poll result in this test
178         verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
179                 new DecimalType(100));
180     }
181
182     @Test
183     public void testHandleCommandRefreshLowBatteryChannel() {
184         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), RefreshType.REFRESH);
185         // state is updated twice: via short poll in initialize() and via long poll result in this test
186         verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY),
187                 OnOffType.OFF);
188     }
189 }