]> git.basschouten.com Git - openhab-addons.git/blob
a634f0c4c275ef591b33d441c8d2bb0cb682ef4f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.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;
24
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
27
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;
42
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonParser;
45
46 /**
47  * Abstract unit test implementation for devices with power switches and energy monitoring.
48  *
49  * @author David Pace - Initial contribution
50  *
51  * @param <T> type of the handler to be tested
52  */
53 @NonNullByDefault
54 public abstract class AbstractPowerSwitchHandlerTest<T extends AbstractPowerSwitchHandler>
55         extends AbstractBoschSHCDeviceHandlerTest<T> {
56
57     private @Captor @NonNullByDefault({}) ArgumentCaptor<PowerSwitchServiceState> serviceStateCaptor;
58
59     @BeforeEach
60     @Override
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);
66
67         super.beforeEach();
68     }
69
70     @Test
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);
77
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);
83     }
84
85     @ParameterizedTest
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);
90
91         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
92
93         verify(getCallback()).statusUpdated(same(getThing()),
94                 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
95                         && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));
96     }
97
98     @Test
99     public void testUpdateChannelPowerSwitchState() {
100         JsonElement jsonObject = JsonParser
101                 .parseString("{\n" + "  \"@type\": \"powerSwitchState\",\n" + "  \"switchState\": \"ON\"\n" + "}");
102
103         getFixture().processUpdate("PowerSwitch", jsonObject);
104
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),
107                 OnOffType.ON);
108
109         jsonObject = JsonParser
110                 .parseString("{\n" + "  \"@type\": \"powerSwitchState\",\n" + "  \"switchState\": \"OFF\"\n" + "}");
111
112         getFixture().processUpdate("PowerSwitch", jsonObject);
113
114         verify(getCallback()).stateUpdated(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
115     }
116
117     @Test
118     public void testHandleCommandRefreshPowerSwitchChannel() {
119         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
120
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),
123                 OnOffType.ON);
124     }
125
126     @ParameterizedTest
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)))
131                 .thenThrow(e);
132
133         getFixture().handleCommand(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), RefreshType.REFRESH);
134
135         verify(getCallback()).statusUpdated(same(getThing()),
136                 argThat(status -> status.getStatus().equals(ThingStatus.OFFLINE)
137                         && status.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));
138     }
139 }