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