]> git.basschouten.com Git - openhab-addons.git/blob
a4d73edbcabb9dd26952cc72dbb787975b8ef7af
[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.DISHWASHER_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.Quantity;
32 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.QuantityType;
36 import org.openhab.core.library.types.StringType;
37 import org.openhab.core.library.unit.Units;
38
39 /**
40  * @author Björn Lange - Initial contribution
41  * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
42  * @author Björn Lange - Add elapsed time, current water and energy consumption channels
43  */
44 @NonNullByDefault
45 public class DishwasherDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
46     @Override
47     protected AbstractMieleThingHandler setUpThingHandler() {
48         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DISHWASHER, DISHWASHER_DEVICE_THING_UID,
49                 DishwasherDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER, "1");
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(DISHWASHER_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.getDoorState()).thenReturn(Optional.empty());
70         when(deviceState.getCurrentWaterConsumption()).thenReturn(Optional.empty());
71         when(deviceState.getCurrentEnergyConsumption()).thenReturn(Optional.empty());
72
73         // when:
74         getBridgeHandler().onDeviceStateUpdated(deviceState);
75
76         // then:
77         waitForAssert(() -> {
78             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
79             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
80             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
81             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
82             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
83             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
84             assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()), getChannelState(PROGRAM_START_STOP));
85             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
86             assertEquals(NULL_VALUE_STATE, getChannelState(DELAYED_START_TIME));
87             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
88             assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
89             assertEquals(NULL_VALUE_STATE, getChannelState(WATER_CONSUMPTION_CURRENT));
90             assertEquals(NULL_VALUE_STATE, getChannelState(ENERGY_CONSUMPTION_CURRENT));
91         });
92     }
93
94     @Test
95     public void testChannelUpdatesForValidValues() throws Exception {
96         // given:
97         setUpBridgeAndThing();
98
99         DeviceState deviceState = mock(DeviceState.class);
100         when(deviceState.isInState(any())).thenCallRealMethod();
101         when(deviceState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
102         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
103         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
104         when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Eco"));
105         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(4L));
106         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Spülen"));
107         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(2));
108         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
109         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
110         when(deviceState.getStartTime()).thenReturn(Optional.of(3600));
111         when(deviceState.getElapsedTime()).thenReturn(Optional.of(4));
112         when(deviceState.hasError()).thenReturn(false);
113         when(deviceState.hasInfo()).thenReturn(true);
114         when(deviceState.getDoorState()).thenReturn(Optional.of(true));
115         when(deviceState.getCurrentWaterConsumption()).thenReturn(Optional.of(new Quantity(1.0, "l")));
116         when(deviceState.getCurrentEnergyConsumption()).thenReturn(Optional.of(new Quantity(2.5, "kWh")));
117
118         // when:
119         getBridgeHandler().onDeviceStateUpdated(deviceState);
120
121         // then:
122         waitForAssert(() -> {
123             assertEquals(new StringType("Eco"), getChannelState(PROGRAM_ACTIVE));
124             assertEquals(new DecimalType(4), getChannelState(PROGRAM_ACTIVE_RAW));
125             assertEquals(new StringType("Spülen"), getChannelState(PROGRAM_PHASE));
126             assertEquals(new DecimalType(2), 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(4), getChannelState(PROGRAM_ELAPSED_TIME));
133             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
134             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
135             assertEquals(OnOffType.ON, getChannelState(DOOR_STATE));
136             assertEquals(new QuantityType<>(1.0, Units.LITRE), getChannelState(WATER_CONSUMPTION_CURRENT));
137             assertEquals(new QuantityType<>(2.5, Units.KILOWATT_HOUR), getChannelState(ENERGY_CONSUMPTION_CURRENT));
138         });
139     }
140
141     @Test
142     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
143         // given:
144         setUpBridgeAndThing();
145
146         DeviceState deviceStateBefore = mock(DeviceState.class);
147         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
148         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
149         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
150
151         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
152
153         DeviceState deviceStateAfter = mock(DeviceState.class);
154         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
155         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
156         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
157
158         // when:
159         getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
160
161         // then:
162         waitForAssert(() -> {
163             assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
164         });
165     }
166
167     @Test
168     public void testTransitionChannelUpdatesForNullValues() throws Exception {
169         // given:
170         setUpBridgeAndThing();
171
172         DeviceState deviceStateBefore = mock(DeviceState.class);
173         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
174         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
175         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
176         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
177         when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
178
179         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
180
181         DeviceState deviceStateAfter = mock(DeviceState.class);
182         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
183         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
184         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
185         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
186         when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
187
188         // when:
189         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
190
191         waitForAssert(() -> {
192             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
193             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
194         });
195     }
196
197     @Test
198     public void testTransitionChannelUpdatesForValidValues() throws Exception {
199         // given:
200         setUpBridgeAndThing();
201
202         DeviceState deviceStateBefore = mock(DeviceState.class);
203         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
204         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
205         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
206         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
207         when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
208
209         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
210
211         DeviceState deviceStateAfter = mock(DeviceState.class);
212         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
213         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
214         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
215         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
216         when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
217
218         // when:
219         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
220
221         waitForAssert(() -> {
222             assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
223             assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
224         });
225     }
226
227     @Test
228     public void testActionsChannelUpdatesForValidValues() throws Exception {
229         // given:
230         setUpBridgeAndThing();
231
232         ActionsState actionsState = mock(ActionsState.class);
233         when(actionsState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
234         when(actionsState.canBeStarted()).thenReturn(true);
235         when(actionsState.canBeStopped()).thenReturn(false);
236         when(actionsState.canBeSwitchedOn()).thenReturn(true);
237         when(actionsState.canBeSwitchedOff()).thenReturn(false);
238
239         // when:
240         getBridgeHandler().onProcessActionUpdated(actionsState);
241
242         // then:
243         waitForAssert(() -> {
244             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
245             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
246             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
247             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
248         });
249     }
250 }