]> git.basschouten.com Git - openhab-addons.git/blob
6e85cefdf6bcd8e0ddacfff14aefe1d52c16b2ca
[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("{ \n" + "    \"@type\":\"DeviceServiceData\",\n"
61                 + "    \"path\":\"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel\",\n"
62                 + "    \"id\":\"BatteryLevel\",\n" + "    \"deviceId\":\"hdm:ZigBee:000d6f0004b93361\",\n"
63                 + "    \"faults\":{ \n" + "        \"entries\":[\n" + "          {\n"
64                 + "            \"type\":\"LOW_BATTERY\",\n" + "            \"category\":\"WARNING\"\n" + "          }\n"
65                 + "        ]\n" + "    }\n" + "}");
66         getFixture().processUpdate("BatteryLevel", deviceServiceData);
67         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
68                 new DecimalType(10));
69         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
70     }
71
72     @Test
73     public void testProcessUpdateBatteryLevelCriticalLow() {
74         JsonElement deviceServiceData = JsonParser.parseString("{ \n" + "    \"@type\":\"DeviceServiceData\",\n"
75                 + "    \"path\":\"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel\",\n"
76                 + "    \"id\":\"BatteryLevel\",\n" + "    \"deviceId\":\"hdm:ZigBee:000d6f0004b93361\",\n"
77                 + "    \"faults\":{ \n" + "        \"entries\":[\n" + "          {\n"
78                 + "            \"type\":\"CRITICAL_LOW\",\n" + "            \"category\":\"WARNING\"\n"
79                 + "          }\n" + "        ]\n" + "    }\n" + "}");
80         getFixture().processUpdate("BatteryLevel", deviceServiceData);
81         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
82                 new DecimalType(1));
83         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
84     }
85
86     @Test
87     public void testProcessUpdateBatteryLevelCriticallyLowBattery() {
88         JsonElement deviceServiceData = JsonParser.parseString("{ \n" + "    \"@type\":\"DeviceServiceData\",\n"
89                 + "    \"path\":\"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel\",\n"
90                 + "    \"id\":\"BatteryLevel\",\n" + "    \"deviceId\":\"hdm:ZigBee:000d6f0004b93361\",\n"
91                 + "    \"faults\":{ \n" + "        \"entries\":[\n" + "          {\n"
92                 + "            \"type\":\"CRITICALLY_LOW_BATTERY\",\n" + "            \"category\":\"WARNING\"\n"
93                 + "          }\n" + "        ]\n" + "    }\n" + "}");
94         getFixture().processUpdate("BatteryLevel", deviceServiceData);
95         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
96                 new DecimalType(1));
97         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
98     }
99
100     @Test
101     public void testProcessUpdateBatteryLevelOK() {
102         JsonElement deviceServiceData = JsonParser.parseString("{ \n" + "    \"@type\":\"DeviceServiceData\",\n"
103                 + "    \"path\":\"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel\",\n"
104                 + "    \"id\":\"BatteryLevel\",\n" + "    \"deviceId\":\"hdm:ZigBee:000d6f0004b93361\" }");
105         getFixture().processUpdate("BatteryLevel", deviceServiceData);
106         verify(getCallback()).stateUpdated(
107                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
108                 new DecimalType(100));
109         verify(getCallback()).stateUpdated(
110                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
111     }
112
113     @Test
114     public void testProcessUpdateBatteryLevelNotAvailable() {
115         JsonElement deviceServiceData = JsonParser.parseString("{ \n" + "    \"@type\":\"DeviceServiceData\",\n"
116                 + "    \"path\":\"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel\",\n"
117                 + "    \"id\":\"BatteryLevel\",\n" + "    \"deviceId\":\"hdm:ZigBee:000d6f0004b93361\",\n"
118                 + "    \"faults\":{ \n" + "        \"entries\":[\n" + "          {\n"
119                 + "            \"type\":\"NOT_AVAILABLE\",\n" + "            \"category\":\"WARNING\"\n"
120                 + "          }\n" + "        ]\n" + "    }\n" + "}");
121         getFixture().processUpdate("BatteryLevel", deviceServiceData);
122         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
123                 UnDefType.UNDEF);
124         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
125     }
126
127     @Test
128     public void testHandleCommandRefreshBatteryLevelChannel() {
129         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL), RefreshType.REFRESH);
130         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
131                 new DecimalType(100));
132     }
133
134     @Test
135     public void testHandleCommandRefreshLowBatteryChannel() {
136         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), RefreshType.REFRESH);
137         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
138     }
139 }