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