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.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.ArgumentMatchers.same;
19 import static org.mockito.Mockito.lenient;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Captor;
31 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
32 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
33 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.types.RefreshType;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
41 * Abstract unit test implementation for devices with power switches and energy monitoring.
43 * @author David Pace - Initial contribution
45 * @param <T> type of the handler to be tested
48 public abstract class AbstractPowerSwitchHandlerTest<T extends AbstractPowerSwitchHandler>
49 extends AbstractBoschSHCDeviceHandlerTest<T> {
51 private @Captor @NonNullByDefault({}) ArgumentCaptor<PowerSwitchServiceState> serviceStateCaptor;
55 public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
58 PowerSwitchServiceState powerSwitchServiceState = new PowerSwitchServiceState();
59 powerSwitchServiceState.switchState = PowerSwitchState.ON;
60 lenient().when(bridgeHandler.getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
61 .thenReturn(powerSwitchServiceState);
65 public void testHandleCommandPowerSwitchChannel()
66 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
67 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
68 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("PowerSwitch"), serviceStateCaptor.capture());
69 PowerSwitchServiceState state = serviceStateCaptor.getValue();
70 assertSame(PowerSwitchState.ON, state.switchState);
72 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
73 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("PowerSwitch"),
74 serviceStateCaptor.capture());
75 state = serviceStateCaptor.getValue();
76 assertSame(PowerSwitchState.OFF, state.switchState);
80 public void testUpdateChannelPowerSwitchState() {
81 JsonElement jsonObject = JsonParser
82 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"ON\"\n" + "}");
83 getFixture().processUpdate("PowerSwitch", jsonObject);
84 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
86 jsonObject = JsonParser
87 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"OFF\"\n" + "}");
88 getFixture().processUpdate("PowerSwitch", jsonObject);
89 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
93 public void testHandleCommandRefreshPowerSwitchChannel() {
94 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
95 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);