]> git.basschouten.com Git - openhab-addons.git/blob
49be02c8286b025615e5b93084343d8df5485c09
[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.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;
22
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
25
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;
36
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
39
40 /**
41  * Abstract unit test implementation for devices with power switches and energy monitoring.
42  *
43  * @author David Pace - Initial contribution
44  *
45  * @param <T> type of the handler to be tested
46  */
47 @NonNullByDefault
48 public abstract class AbstractPowerSwitchHandlerTest<T extends AbstractPowerSwitchHandler>
49         extends AbstractBoschSHCDeviceHandlerTest<T> {
50
51     private @Captor @NonNullByDefault({}) ArgumentCaptor<PowerSwitchServiceState> serviceStateCaptor;
52
53     @BeforeEach
54     @Override
55     public void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
56         super.beforeEach();
57
58         PowerSwitchServiceState powerSwitchServiceState = new PowerSwitchServiceState();
59         powerSwitchServiceState.switchState = PowerSwitchState.ON;
60         lenient().when(bridgeHandler.getState(anyString(), eq("PowerSwitch"), same(PowerSwitchServiceState.class)))
61                 .thenReturn(powerSwitchServiceState);
62     }
63
64     @Test
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);
71
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);
77     }
78
79     @Test
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);
85
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);
90     }
91
92     @Test
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);
96     }
97 }