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.*;
20 import java.util.Optional;
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;
37 * @author Björn Lange - Initial contribution
40 public class DishWarmerDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
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);
49 public void testChannelUpdatesForNullValues() throws Exception {
51 setUpBridgeAndThing();
53 DeviceState deviceState = mock(DeviceState.class);
54 when(deviceState.getDeviceIdentifier())
55 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
56 when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
57 when(deviceState.getStatus()).thenReturn(Optional.empty());
58 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
59 when(deviceState.getStateType()).thenReturn(Optional.empty());
60 when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
61 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.empty());
62 when(deviceState.getTemperature(0)).thenReturn(Optional.empty());
63 when(deviceState.hasError()).thenReturn(false);
64 when(deviceState.hasInfo()).thenReturn(false);
65 when(deviceState.getDoorState()).thenReturn(Optional.empty());
68 getBridgeHandler().onDeviceStateUpdated(deviceState);
72 assertEquals(NULL_VALUE_STATE, getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
73 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
74 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
75 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
76 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
77 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_TARGET));
78 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_CURRENT));
79 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
80 assertEquals(OnOffType.OFF, getChannelState(INFO_STATE));
81 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
86 public void testChannelUpdatesForValidValues() throws Exception {
88 setUpBridgeAndThing();
90 DeviceState deviceState = mock(DeviceState.class);
91 when(deviceState.getDeviceIdentifier())
92 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
93 when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(2L));
94 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
95 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
96 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
97 when(deviceState.getElapsedTime()).thenReturn(Optional.of(98));
98 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.of(30));
99 when(deviceState.getTemperature(0)).thenReturn(Optional.of(29));
100 when(deviceState.hasError()).thenReturn(true);
101 when(deviceState.hasInfo()).thenReturn(true);
102 when(deviceState.getDoorState()).thenReturn(Optional.of(false));
105 getBridgeHandler().onDeviceStateUpdated(deviceState);
108 waitForAssert(() -> {
109 assertEquals(new StringType("2"), getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
110 assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
111 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
112 assertEquals(new DecimalType(98), getChannelState(PROGRAM_ELAPSED_TIME));
113 assertEquals(new QuantityType<>(30, SIUnits.CELSIUS), getChannelState(TEMPERATURE_TARGET));
114 assertEquals(new QuantityType<>(29, SIUnits.CELSIUS), getChannelState(TEMPERATURE_CURRENT));
115 assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
116 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
117 assertEquals(OnOffType.OFF, getChannelState(DOOR_STATE));
122 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
124 setUpBridgeAndThing();
126 DeviceState deviceStateBefore = mock(DeviceState.class);
127 when(deviceStateBefore.getDeviceIdentifier())
128 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
129 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
130 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
132 getBridgeHandler().onDeviceStateUpdated(deviceStateBefore);
134 DeviceState deviceStateAfter = mock(DeviceState.class);
135 when(deviceStateAfter.getDeviceIdentifier())
136 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
137 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
138 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
141 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
144 waitForAssert(() -> {
145 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
150 public void testTransitionChannelUpdatesForNullValues() throws Exception {
152 setUpBridgeAndThing();
154 DeviceState deviceStateBefore = mock(DeviceState.class);
155 when(deviceStateBefore.getDeviceIdentifier())
156 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
157 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
158 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
159 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
160 when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
162 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
164 DeviceState deviceStateAfter = mock(DeviceState.class);
165 when(deviceStateAfter.getDeviceIdentifier())
166 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
167 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
168 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
169 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
170 when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
173 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
175 waitForAssert(() -> {
176 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
177 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
182 public void testTransitionChannelUpdatesForValidValues() throws Exception {
184 setUpBridgeAndThing();
186 DeviceState deviceStateBefore = mock(DeviceState.class);
187 when(deviceStateBefore.getDeviceIdentifier())
188 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
189 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
190 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
191 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
192 when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
194 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
196 DeviceState deviceStateAfter = mock(DeviceState.class);
197 when(deviceStateAfter.getDeviceIdentifier())
198 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
199 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
200 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
201 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
202 when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
205 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
207 waitForAssert(() -> {
208 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
209 assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
214 public void testActionsChannelUpdatesForValidValues() throws Exception {
216 setUpBridgeAndThing();
218 ActionsState actionsState = mock(ActionsState.class);
219 when(actionsState.getDeviceIdentifier())
220 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
221 when(actionsState.canBeSwitchedOn()).thenReturn(true);
222 when(actionsState.canBeSwitchedOff()).thenReturn(false);
225 getBridgeHandler().onProcessActionUpdated(actionsState);
228 waitForAssert(() -> {
229 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
230 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
235 public void testHandleCommandDishWarmerProgramActive() throws Exception {
237 setUpBridgeAndThing();
240 getThingHandler().handleCommand(channel(DISH_WARMER_PROGRAM_ACTIVE), new StringType("3"));
243 waitForAssert(() -> {
244 verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 3);