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