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.any;
17 import static org.mockito.ArgumentMatchers.anyString;
18 import static org.mockito.ArgumentMatchers.argThat;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.ArgumentMatchers.same;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.params.ParameterizedTest;
32 import org.junit.jupiter.params.provider.MethodSource;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Captor;
35 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
36 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
37 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.types.RefreshType;
44 import com.google.gson.JsonElement;
45 import com.google.gson.JsonParser;
48 * Abstract unit test implementation for devices with power switches and energy monitoring.
50 * @author David Pace - Initial contribution
52 * @param <T> type of the handler to be tested
55 public abstract class AbstractPowerSwitchHandlerTest<T extends AbstractPowerSwitchHandler>
56 extends AbstractBoschSHCDeviceHandlerTest<T> {
58 private @Captor @NonNullByDefault({}) ArgumentCaptor<PowerSwitchServiceState> serviceStateCaptor;
62 public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
63 PowerSwitchServiceState powerSwitchServiceState = new PowerSwitchServiceState();
64 powerSwitchServiceState.switchState = PowerSwitchState.ON;
65 when(getBridgeHandler().getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
66 .thenReturn(powerSwitchServiceState);
72 public void testHandleCommandPowerSwitchChannel()
73 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
74 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
75 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("PowerSwitch"), serviceStateCaptor.capture());
76 PowerSwitchServiceState state = serviceStateCaptor.getValue();
77 assertSame(PowerSwitchState.ON, state.switchState);
79 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
80 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("PowerSwitch"),
81 serviceStateCaptor.capture());
82 state = serviceStateCaptor.getValue();
83 assertSame(PowerSwitchState.OFF, state.switchState);
87 @MethodSource("org.openhab.binding.boschshc.internal.tests.common.CommonTestUtils#getExecutionAndTimeoutAndInterruptedExceptionArguments()")
88 public void testHandleCommandPowerSwitchChannelHandleExceptions(Exception e)
89 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
90 when(getBridgeHandler().putState(any(), any(), any())).thenThrow(e);
92 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
94 verify(getCallback()).statusUpdated(any(Thing.class),
95 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
96 && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));
100 public void testUpdateChannelPowerSwitchState() {
101 JsonElement jsonObject = JsonParser
102 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"ON\"\n" + "}");
104 getFixture().processUpdate("PowerSwitch", jsonObject);
106 // state is updated twice: via short poll in initialize() and via long poll result in this test
107 verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
110 jsonObject = JsonParser
111 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"OFF\"\n" + "}");
113 getFixture().processUpdate("PowerSwitch", jsonObject);
115 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
119 public void testHandleCommandRefreshPowerSwitchChannel() {
120 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
122 // state is updated twice: via short poll in initialize() and via long poll result in this test
123 verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
128 @MethodSource("org.openhab.binding.boschshc.internal.tests.common.CommonTestUtils#getBoschShcAndExecutionAndTimeoutAndInterruptedExceptionArguments()")
129 public void testHandleCommandRefreshPowerSwitchChannelHandleExceptions(Exception e)
130 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
131 when(getBridgeHandler().getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
134 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
136 verify(getCallback()).statusUpdated(any(Thing.class),
137 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
138 && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));