]> git.basschouten.com Git - openhab-addons.git/blob
c1bb65f9bdd9cadccb70f80518389b0c0502af56
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mielecloud.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
19 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.DRYER_DEVICE_THING_UID;
20
21 import java.util.Optional;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
26 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
27 import org.openhab.binding.mielecloud.internal.webservice.api.ActionsState;
28 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
29 import org.openhab.binding.mielecloud.internal.webservice.api.PowerStatus;
30 import org.openhab.binding.mielecloud.internal.webservice.api.ProgramStatus;
31 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.StringType;
35
36 /**
37  * @author Björn Lange - Initial contribution
38  * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
39  * @author Björn Lange - Add elapsed time channel
40  */
41 @NonNullByDefault
42 public class DryerDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
43     @Override
44     protected AbstractMieleThingHandler setUpThingHandler() {
45         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DRYER, DRYER_DEVICE_THING_UID,
46                 DryerDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
47     }
48
49     @Test
50     public void testChannelUpdatesForNullValues() throws Exception {
51         // given:
52         setUpBridgeAndThing();
53
54         DeviceState deviceState = mock(DeviceState.class);
55         when(deviceState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
56         when(deviceState.getStateType()).thenReturn(Optional.empty());
57         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
58         when(deviceState.getSelectedProgram()).thenReturn(Optional.empty());
59         when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
60         when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
61         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
62         when(deviceState.getStatus()).thenReturn(Optional.empty());
63         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
64         when(deviceState.getStartTime()).thenReturn(Optional.empty());
65         when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
66         when(deviceState.getDryingTarget()).thenReturn(Optional.empty());
67         when(deviceState.getDryingTargetRaw()).thenReturn(Optional.empty());
68         when(deviceState.getLightState()).thenReturn(Optional.empty());
69         when(deviceState.getDoorState()).thenReturn(Optional.empty());
70
71         // when:
72         getBridgeHandler().onDeviceStateUpdated(deviceState);
73
74         // then:
75         waitForAssert(() -> {
76             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
77             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
78             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
79             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
80             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
81             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
82             assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()), getChannelState(PROGRAM_START_STOP));
83             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
84             assertEquals(NULL_VALUE_STATE, getChannelState(DELAYED_START_TIME));
85             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
86             assertEquals(NULL_VALUE_STATE, getChannelState(DRYING_TARGET));
87             assertEquals(NULL_VALUE_STATE, getChannelState(DRYING_TARGET_RAW));
88             assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
89             assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
90         });
91     }
92
93     @Test
94     public void testChannelUpdatesForValidValues() throws Exception {
95         // given:
96         setUpBridgeAndThing();
97
98         DeviceState deviceState = mock(DeviceState.class);
99         when(deviceState.isInState(any())).thenCallRealMethod();
100         when(deviceState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
101         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
102         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
103         when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Baumwolle"));
104         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(34L));
105         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Schleudern"));
106         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(3));
107         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
108         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
109         when(deviceState.getStartTime()).thenReturn(Optional.of(3600));
110         when(deviceState.getElapsedTime()).thenReturn(Optional.of(61));
111         when(deviceState.getDryingTarget()).thenReturn(Optional.of("Schranktrocken"));
112         when(deviceState.getDryingTargetRaw()).thenReturn(Optional.of(3));
113         when(deviceState.hasError()).thenReturn(true);
114         when(deviceState.hasInfo()).thenReturn(true);
115         when(deviceState.getLightState()).thenReturn(Optional.of(false));
116         when(deviceState.getDoorState()).thenReturn(Optional.of(false));
117
118         // when:
119         getBridgeHandler().onDeviceStateUpdated(deviceState);
120
121         // then:
122         waitForAssert(() -> {
123             assertEquals(new StringType("Baumwolle"), getChannelState(PROGRAM_ACTIVE));
124             assertEquals(new DecimalType(34), getChannelState(PROGRAM_ACTIVE_RAW));
125             assertEquals(new StringType("Schleudern"), getChannelState(PROGRAM_PHASE));
126             assertEquals(new DecimalType(3), getChannelState(PROGRAM_PHASE_RAW));
127             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
128             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
129             assertEquals(new StringType(ProgramStatus.PROGRAM_STARTED.getState()), getChannelState(PROGRAM_START_STOP));
130             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
131             assertEquals(new DecimalType(3600), getChannelState(DELAYED_START_TIME));
132             assertEquals(new DecimalType(61), getChannelState(PROGRAM_ELAPSED_TIME));
133             assertEquals(new StringType("Schranktrocken"), getChannelState(DRYING_TARGET));
134             assertEquals(new DecimalType(3), getChannelState(DRYING_TARGET_RAW));
135             assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
136             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
137             assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
138             assertEquals(OnOffType.OFF, getChannelState(DOOR_STATE));
139         });
140     }
141
142     @Test
143     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
144         // given:
145         setUpBridgeAndThing();
146
147         DeviceState deviceStateBefore = mock(DeviceState.class);
148         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
149         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
150         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
151
152         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
153
154         DeviceState deviceStateAfter = mock(DeviceState.class);
155         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
156         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
157         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
158
159         // when:
160         getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
161
162         // then:
163         waitForAssert(() -> {
164             assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
165         });
166     }
167
168     @Test
169     public void testTransitionChannelUpdatesForNullValues() throws Exception {
170         // given:
171         setUpBridgeAndThing();
172
173         DeviceState deviceStateBefore = mock(DeviceState.class);
174         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
175         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
176         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
177         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
178         when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
179
180         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
181
182         DeviceState deviceStateAfter = mock(DeviceState.class);
183         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
184         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
185         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
186         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
187         when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
188
189         // when:
190         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
191
192         waitForAssert(() -> {
193             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
194             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
195         });
196     }
197
198     @Test
199     public void testTransitionChannelUpdatesForValidValues() throws Exception {
200         // given:
201         setUpBridgeAndThing();
202
203         DeviceState deviceStateBefore = mock(DeviceState.class);
204         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
205         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
206         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
207         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
208         when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
209
210         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
211
212         DeviceState deviceStateAfter = mock(DeviceState.class);
213         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
214         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
215         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
216         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
217         when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
218
219         // when:
220         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
221
222         waitForAssert(() -> {
223             assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
224             assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
225         });
226     }
227
228     @Test
229     public void testActionsChannelUpdatesForValidValues() throws Exception {
230         // given:
231         setUpBridgeAndThing();
232
233         ActionsState actionsState = mock(ActionsState.class);
234         when(actionsState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
235         when(actionsState.canBeStarted()).thenReturn(true);
236         when(actionsState.canBeStopped()).thenReturn(false);
237         when(actionsState.canBeSwitchedOn()).thenReturn(true);
238         when(actionsState.canBeSwitchedOff()).thenReturn(false);
239         when(actionsState.canControlLight()).thenReturn(true);
240
241         // when:
242         getBridgeHandler().onProcessActionUpdated(actionsState);
243
244         // then:
245         waitForAssert(() -> {
246             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
247             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
248             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
249             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
250             assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));
251         });
252     }
253 }