]> git.basschouten.com Git - openhab-addons.git/blob
ec887fe7c624158761c1e3c9309671b5be9d1f9b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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() {
50         // given:
51         DeviceState deviceState = mock(DeviceState.class);
52         when(deviceState.getDeviceIdentifier())
53                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
54         when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
55         when(deviceState.getStatus()).thenReturn(Optional.empty());
56         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
57         when(deviceState.getStateType()).thenReturn(Optional.empty());
58         when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
59         when(deviceState.getTargetTemperature(0)).thenReturn(Optional.empty());
60         when(deviceState.getTemperature(0)).thenReturn(Optional.empty());
61         when(deviceState.hasError()).thenReturn(false);
62         when(deviceState.hasInfo()).thenReturn(false);
63         when(deviceState.getDoorState()).thenReturn(Optional.empty());
64
65         // when:
66         getBridgeHandler().onDeviceStateUpdated(deviceState);
67
68         // then:
69         waitForAssert(() -> {
70             assertEquals(NULL_VALUE_STATE, getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
71             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
72             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
73             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
74             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
75             assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_TARGET));
76             assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_CURRENT));
77             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
78             assertEquals(OnOffType.OFF, getChannelState(INFO_STATE));
79             assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
80         });
81     }
82
83     @Test
84     public void testChannelUpdatesForValidValues() {
85         // given:
86         DeviceState deviceState = mock(DeviceState.class);
87         when(deviceState.getDeviceIdentifier())
88                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
89         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(2L));
90         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
91         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
92         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
93         when(deviceState.getElapsedTime()).thenReturn(Optional.of(98));
94         when(deviceState.getTargetTemperature(0)).thenReturn(Optional.of(30));
95         when(deviceState.getTemperature(0)).thenReturn(Optional.of(29));
96         when(deviceState.hasError()).thenReturn(true);
97         when(deviceState.hasInfo()).thenReturn(true);
98         when(deviceState.getDoorState()).thenReturn(Optional.of(false));
99
100         // when:
101         getBridgeHandler().onDeviceStateUpdated(deviceState);
102
103         // then:
104         waitForAssert(() -> {
105             assertEquals(new StringType("2"), getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
106             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
107             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
108             assertEquals(new DecimalType(98), getChannelState(PROGRAM_ELAPSED_TIME));
109             assertEquals(new QuantityType<>(30, SIUnits.CELSIUS), getChannelState(TEMPERATURE_TARGET));
110             assertEquals(new QuantityType<>(29, SIUnits.CELSIUS), getChannelState(TEMPERATURE_CURRENT));
111             assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
112             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
113             assertEquals(OnOffType.OFF, getChannelState(DOOR_STATE));
114         });
115     }
116
117     @Test
118     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() {
119         // given:
120         DeviceState deviceStateBefore = mock(DeviceState.class);
121         when(deviceStateBefore.getDeviceIdentifier())
122                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
123         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
124         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
125
126         getBridgeHandler().onDeviceStateUpdated(deviceStateBefore);
127
128         DeviceState deviceStateAfter = mock(DeviceState.class);
129         when(deviceStateAfter.getDeviceIdentifier())
130                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
131         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
132         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
133
134         // when:
135         getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
136
137         // then:
138         waitForAssert(() -> {
139             assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
140         });
141     }
142
143     @Test
144     public void testTransitionChannelUpdatesForNullValues() {
145         // given:
146         DeviceState deviceStateBefore = mock(DeviceState.class);
147         when(deviceStateBefore.getDeviceIdentifier())
148                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
149         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
150         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
151         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
152         when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
153
154         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
155
156         DeviceState deviceStateAfter = mock(DeviceState.class);
157         when(deviceStateAfter.getDeviceIdentifier())
158                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
159         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
160         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
161         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
162         when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
163
164         // when:
165         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
166
167         waitForAssert(() -> {
168             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
169             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
170         });
171     }
172
173     @Test
174     public void testTransitionChannelUpdatesForValidValues() {
175         // given:
176         DeviceState deviceStateBefore = mock(DeviceState.class);
177         when(deviceStateBefore.getDeviceIdentifier())
178                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
179         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
180         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
181         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
182         when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
183
184         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
185
186         DeviceState deviceStateAfter = mock(DeviceState.class);
187         when(deviceStateAfter.getDeviceIdentifier())
188                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
189         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
190         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
191         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
192         when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
193
194         // when:
195         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
196
197         waitForAssert(() -> {
198             assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
199             assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
200         });
201     }
202
203     @Test
204     public void testActionsChannelUpdatesForValidValues() {
205         // given:
206         ActionsState actionsState = mock(ActionsState.class);
207         when(actionsState.getDeviceIdentifier())
208                 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
209         when(actionsState.canBeSwitchedOn()).thenReturn(true);
210         when(actionsState.canBeSwitchedOff()).thenReturn(false);
211
212         // when:
213         getBridgeHandler().onProcessActionUpdated(actionsState);
214
215         // then:
216         waitForAssert(() -> {
217             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
218             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
219         });
220     }
221
222     @Test
223     public void testHandleCommandDishWarmerProgramActive() {
224         // when:
225         getThingHandler().handleCommand(channel(DISH_WARMER_PROGRAM_ACTIVE), new StringType("3"));
226
227         // then:
228         waitForAssert(() -> {
229             verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 3);
230         });
231     }
232 }