]> git.basschouten.com Git - openhab-addons.git/blob
69247421d260d9d394502ed621622e4cc3e3ee38
[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;
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         super.beforeEach();
50
51         DeviceServiceData deviceServiceData = new DeviceServiceData();
52         deviceServiceData.path = "/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel";
53         deviceServiceData.id = "BatteryLevel";
54         deviceServiceData.deviceId = "hdm:ZigBee:000d6f0004b93361";
55         lenient().when(bridgeHandler.getServiceData(anyString(), anyString())).thenReturn(deviceServiceData);
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         verify(getCallback()).stateUpdated(
141                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
142                 new DecimalType(100));
143         verify(getCallback()).stateUpdated(
144                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
145     }
146
147     @Test
148     public void testProcessUpdateBatteryLevelNotAvailable() {
149         JsonElement deviceServiceData = JsonParser.parseString("""
150                 {
151                     "@type":"DeviceServiceData",
152                     "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
153                     "id":"BatteryLevel",
154                     "deviceId":"hdm:ZigBee:000d6f0004b93361",
155                     "faults":{
156                         "entries":[
157                           {
158                             "type":"NOT_AVAILABLE",
159                             "category":"WARNING"
160                           }
161                         ]
162                     }
163                 }\
164                 """);
165         getFixture().processUpdate("BatteryLevel", deviceServiceData);
166         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
167                 UnDefType.UNDEF);
168         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
169     }
170
171     @Test
172     public void testHandleCommandRefreshBatteryLevelChannel() {
173         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL), RefreshType.REFRESH);
174         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
175                 new DecimalType(100));
176     }
177
178     @Test
179     public void testHandleCommandRefreshLowBatteryChannel() {
180         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), RefreshType.REFRESH);
181         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
182     }
183 }