2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.devices;
15 import static org.mockito.ArgumentMatchers.anyString;
16 import static org.mockito.Mockito.*;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
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;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonParser;
36 * Abstract test implementation for battery-powered devices.
38 * @author David Pace - Initial contribution
40 * @param <T> type of the battery-powered device to be tested
43 public abstract class AbstractBatteryPoweredDeviceHandlerTest<T extends AbstractBatteryPoweredDeviceHandler>
44 extends AbstractBoschSHCDeviceHandlerTest<T> {
48 public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
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);
59 public void testProcessUpdateBatteryLevelLowBattery() {
60 JsonElement deviceServiceData = JsonParser.parseString("""
62 "@type":"DeviceServiceData",
63 "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
65 "deviceId":"hdm:ZigBee:000d6f0004b93361",
76 getFixture().processUpdate("BatteryLevel", deviceServiceData);
77 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
79 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
83 public void testProcessUpdateBatteryLevelCriticalLow() {
84 JsonElement deviceServiceData = JsonParser.parseString("""
86 "@type":"DeviceServiceData",
87 "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
89 "deviceId":"hdm:ZigBee:000d6f0004b93361",
93 "type":"CRITICAL_LOW",
100 getFixture().processUpdate("BatteryLevel", deviceServiceData);
101 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
103 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
107 public void testProcessUpdateBatteryLevelCriticallyLowBattery() {
108 JsonElement deviceServiceData = JsonParser.parseString("""
110 "@type":"DeviceServiceData",
111 "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
113 "deviceId":"hdm:ZigBee:000d6f0004b93361",
117 "type":"CRITICALLY_LOW_BATTERY",
124 getFixture().processUpdate("BatteryLevel", deviceServiceData);
125 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
127 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.ON);
131 public void testProcessUpdateBatteryLevelOK() {
132 JsonElement deviceServiceData = JsonParser.parseString("""
134 "@type":"DeviceServiceData",
135 "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
137 "deviceId":"hdm:ZigBee:000d6f0004b93361" }\
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);
148 public void testProcessUpdateBatteryLevelNotAvailable() {
149 JsonElement deviceServiceData = JsonParser.parseString("""
151 "@type":"DeviceServiceData",
152 "path":"/devices/hdm:ZigBee:000d6f0004b93361/services/BatteryLevel",
154 "deviceId":"hdm:ZigBee:000d6f0004b93361",
158 "type":"NOT_AVAILABLE",
165 getFixture().processUpdate("BatteryLevel", deviceServiceData);
166 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_BATTERY_LEVEL),
168 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_LOW_BATTERY), OnOffType.OFF);
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));
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);