]> git.basschouten.com Git - openhab-addons.git/blob
ca453fe30f02e0d06ac466fb3da90691dc61829a
[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.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 DishwasherDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
43     @Override
44     protected AbstractMieleThingHandler setUpThingHandler() {
45         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DISHWASHER, DISHWASHER_DEVICE_THING_UID,
46                 DishwasherDeviceThingHandler.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(DISHWASHER_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.getDoorState()).thenReturn(Optional.empty());
67
68         // when:
69         getBridgeHandler().onDeviceStateUpdated(deviceState);
70
71         // then:
72         waitForAssert(() -> {
73             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
74             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
75             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
76             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
77             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
78             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
79             assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()), getChannelState(PROGRAM_START_STOP));
80             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
81             assertEquals(NULL_VALUE_STATE, getChannelState(DELAYED_START_TIME));
82             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
83             assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
84         });
85     }
86
87     @Test
88     public void testChannelUpdatesForValidValues() throws Exception {
89         // given:
90         setUpBridgeAndThing();
91
92         DeviceState deviceState = mock(DeviceState.class);
93         when(deviceState.isInState(any())).thenCallRealMethod();
94         when(deviceState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
95         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
96         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
97         when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Eco"));
98         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(4L));
99         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Spülen"));
100         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(2));
101         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
102         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
103         when(deviceState.getStartTime()).thenReturn(Optional.of(3600));
104         when(deviceState.getElapsedTime()).thenReturn(Optional.of(4));
105         when(deviceState.hasError()).thenReturn(false);
106         when(deviceState.hasInfo()).thenReturn(true);
107         when(deviceState.getDoorState()).thenReturn(Optional.of(true));
108
109         // when:
110         getBridgeHandler().onDeviceStateUpdated(deviceState);
111
112         // then:
113         waitForAssert(() -> {
114             assertEquals(new StringType("Eco"), getChannelState(PROGRAM_ACTIVE));
115             assertEquals(new DecimalType(4), getChannelState(PROGRAM_ACTIVE_RAW));
116             assertEquals(new StringType("Spülen"), getChannelState(PROGRAM_PHASE));
117             assertEquals(new DecimalType(2), getChannelState(PROGRAM_PHASE_RAW));
118             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
119             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
120             assertEquals(new StringType(ProgramStatus.PROGRAM_STARTED.getState()), getChannelState(PROGRAM_START_STOP));
121             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
122             assertEquals(new DecimalType(3600), getChannelState(DELAYED_START_TIME));
123             assertEquals(new DecimalType(4), getChannelState(PROGRAM_ELAPSED_TIME));
124             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
125             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
126             assertEquals(OnOffType.ON, getChannelState(DOOR_STATE));
127         });
128     }
129
130     @Test
131     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
132         // given:
133         setUpBridgeAndThing();
134
135         DeviceState deviceStateBefore = mock(DeviceState.class);
136         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
137         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
138         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
139
140         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
141
142         DeviceState deviceStateAfter = mock(DeviceState.class);
143         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
144         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
145         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
146
147         // when:
148         getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
149
150         // then:
151         waitForAssert(() -> {
152             assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
153         });
154     }
155
156     @Test
157     public void testTransitionChannelUpdatesForNullValues() throws Exception {
158         // given:
159         setUpBridgeAndThing();
160
161         DeviceState deviceStateBefore = mock(DeviceState.class);
162         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
163         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
164         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
165         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
166         when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
167
168         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
169
170         DeviceState deviceStateAfter = mock(DeviceState.class);
171         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
172         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
173         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
174         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
175         when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
176
177         // when:
178         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
179
180         waitForAssert(() -> {
181             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
182             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
183         });
184     }
185
186     @Test
187     public void testTransitionChannelUpdatesForValidValues() throws Exception {
188         // given:
189         setUpBridgeAndThing();
190
191         DeviceState deviceStateBefore = mock(DeviceState.class);
192         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
193         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
194         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
195         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
196         when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
197
198         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
199
200         DeviceState deviceStateAfter = mock(DeviceState.class);
201         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
202         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
203         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
204         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
205         when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
206
207         // when:
208         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
209
210         waitForAssert(() -> {
211             assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
212             assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
213         });
214     }
215
216     @Test
217     public void testActionsChannelUpdatesForValidValues() throws Exception {
218         // given:
219         setUpBridgeAndThing();
220
221         ActionsState actionsState = mock(ActionsState.class);
222         when(actionsState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
223         when(actionsState.canBeStarted()).thenReturn(true);
224         when(actionsState.canBeStopped()).thenReturn(false);
225         when(actionsState.canBeSwitchedOn()).thenReturn(true);
226         when(actionsState.canBeSwitchedOff()).thenReturn(false);
227
228         // when:
229         getBridgeHandler().onProcessActionUpdated(actionsState);
230
231         // then:
232         waitForAssert(() -> {
233             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
234             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
235             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
236             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
237         });
238     }
239 }