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