2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.handler;
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;
21 import java.util.Optional;
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;
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
42 public class DishwasherDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
44 protected AbstractMieleThingHandler setUpThingHandler() {
45 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DISHWASHER, DISHWASHER_DEVICE_THING_UID,
46 DishwasherDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
50 public void testChannelUpdatesForNullValues() throws Exception {
52 setUpBridgeAndThing();
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());
69 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
88 public void testChannelUpdatesForValidValues() throws Exception {
90 setUpBridgeAndThing();
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));
110 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
131 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
133 setUpBridgeAndThing();
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();
140 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
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();
148 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
151 waitForAssert(() -> {
152 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
157 public void testTransitionChannelUpdatesForNullValues() throws Exception {
159 setUpBridgeAndThing();
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());
168 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
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());
178 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
180 waitForAssert(() -> {
181 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
182 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
187 public void testTransitionChannelUpdatesForValidValues() throws Exception {
189 setUpBridgeAndThing();
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));
198 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
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));
208 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
210 waitForAssert(() -> {
211 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
212 assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
217 public void testActionsChannelUpdatesForValidValues() throws Exception {
219 setUpBridgeAndThing();
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);
229 getBridgeHandler().onProcessActionUpdated(actionsState);
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));