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.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.types.RefreshType;
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonParser;
47 * Abstract unit test implementation for devices with power switches and energy monitoring.
49 * @author David Pace - Initial contribution
51 * @param <T> type of the handler to be tested
54 public abstract class AbstractPowerSwitchHandlerTest<T extends AbstractPowerSwitchHandler>
55 extends AbstractBoschSHCDeviceHandlerTest<T> {
57 private @Captor @NonNullByDefault({}) ArgumentCaptor<PowerSwitchServiceState> serviceStateCaptor;
61 public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
62 PowerSwitchServiceState powerSwitchServiceState = new PowerSwitchServiceState();
63 powerSwitchServiceState.switchState = PowerSwitchState.ON;
64 when(getBridgeHandler().getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
65 .thenReturn(powerSwitchServiceState);
71 public void testHandleCommandPowerSwitchChannel()
72 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
73 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
74 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("PowerSwitch"), serviceStateCaptor.capture());
75 PowerSwitchServiceState state = serviceStateCaptor.getValue();
76 assertSame(PowerSwitchState.ON, state.switchState);
78 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
79 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("PowerSwitch"),
80 serviceStateCaptor.capture());
81 state = serviceStateCaptor.getValue();
82 assertSame(PowerSwitchState.OFF, state.switchState);
86 @MethodSource("org.openhab.binding.boschshc.internal.tests.common.CommonTestUtils#getExecutionAndTimeoutAndInterruptedExceptionArguments()")
87 public void testHandleCommandPowerSwitchChannelHandleExceptions(Exception e)
88 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
89 when(getBridgeHandler().putState(any(), any(), any())).thenThrow(e);
91 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
93 verify(getCallback()).statusUpdated(same(getThing()),
94 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
95 && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));
99 public void testUpdateChannelPowerSwitchState() {
100 JsonElement jsonObject = JsonParser
101 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"ON\"\n" + "}");
103 getFixture().processUpdate("PowerSwitch", jsonObject);
105 // state is updated twice: via short poll in initialize() and via long poll result in this test
106 verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
109 jsonObject = JsonParser
110 .parseString("{\n" + " \"@type\": \"powerSwitchState\",\n" + " \"switchState\": \"OFF\"\n" + "}");
112 getFixture().processUpdate("PowerSwitch", jsonObject);
114 verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
118 public void testHandleCommandRefreshPowerSwitchChannel() {
119 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
121 // state is updated twice: via short poll in initialize() and via long poll result in this test
122 verify(getCallback(), times(2)).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
127 @MethodSource("org.openhab.binding.boschshc.internal.tests.common.CommonTestUtils#getBoschShcAndExecutionAndTimeoutAndInterruptedExceptionArguments()")
128 public void testHandleCommandRefreshPowerSwitchChannelHandleExceptions(Exception e)
129 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
130 when(getBridgeHandler().getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
133 getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
135 verify(getCallback()).statusUpdated(same(getThing()),
136 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
137 && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));