2 * Copyright (c) 2010-2021 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() {
51 DeviceState deviceState = mock(DeviceState.class);
52 when(deviceState.getDeviceIdentifier())
53 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
54 when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
55 when(deviceState.getStatus()).thenReturn(Optional.empty());
56 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
57 when(deviceState.getStateType()).thenReturn(Optional.empty());
58 when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
59 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.empty());
60 when(deviceState.getTemperature(0)).thenReturn(Optional.empty());
61 when(deviceState.hasError()).thenReturn(false);
62 when(deviceState.hasInfo()).thenReturn(false);
63 when(deviceState.getDoorState()).thenReturn(Optional.empty());
66 getBridgeHandler().onDeviceStateUpdated(deviceState);
70 assertEquals(NULL_VALUE_STATE, getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
71 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
72 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
73 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
74 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
75 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_TARGET));
76 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_CURRENT));
77 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
78 assertEquals(OnOffType.OFF, getChannelState(INFO_STATE));
79 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
84 public void testChannelUpdatesForValidValues() {
86 DeviceState deviceState = mock(DeviceState.class);
87 when(deviceState.getDeviceIdentifier())
88 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
89 when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(2L));
90 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
91 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
92 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
93 when(deviceState.getElapsedTime()).thenReturn(Optional.of(98));
94 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.of(30));
95 when(deviceState.getTemperature(0)).thenReturn(Optional.of(29));
96 when(deviceState.hasError()).thenReturn(true);
97 when(deviceState.hasInfo()).thenReturn(true);
98 when(deviceState.getDoorState()).thenReturn(Optional.of(false));
101 getBridgeHandler().onDeviceStateUpdated(deviceState);
104 waitForAssert(() -> {
105 assertEquals(new StringType("2"), getChannelState(DISH_WARMER_PROGRAM_ACTIVE));
106 assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
107 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
108 assertEquals(new DecimalType(98), getChannelState(PROGRAM_ELAPSED_TIME));
109 assertEquals(new QuantityType<>(30, SIUnits.CELSIUS), getChannelState(TEMPERATURE_TARGET));
110 assertEquals(new QuantityType<>(29, SIUnits.CELSIUS), getChannelState(TEMPERATURE_CURRENT));
111 assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
112 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
113 assertEquals(OnOffType.OFF, getChannelState(DOOR_STATE));
118 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() {
120 DeviceState deviceStateBefore = mock(DeviceState.class);
121 when(deviceStateBefore.getDeviceIdentifier())
122 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
123 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
124 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
126 getBridgeHandler().onDeviceStateUpdated(deviceStateBefore);
128 DeviceState deviceStateAfter = mock(DeviceState.class);
129 when(deviceStateAfter.getDeviceIdentifier())
130 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
131 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
132 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
135 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
138 waitForAssert(() -> {
139 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
144 public void testTransitionChannelUpdatesForNullValues() {
146 DeviceState deviceStateBefore = mock(DeviceState.class);
147 when(deviceStateBefore.getDeviceIdentifier())
148 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
149 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
150 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
151 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
152 when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
154 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
156 DeviceState deviceStateAfter = mock(DeviceState.class);
157 when(deviceStateAfter.getDeviceIdentifier())
158 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
159 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
160 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
161 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
162 when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
165 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
167 waitForAssert(() -> {
168 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
169 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
174 public void testTransitionChannelUpdatesForValidValues() {
176 DeviceState deviceStateBefore = mock(DeviceState.class);
177 when(deviceStateBefore.getDeviceIdentifier())
178 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
179 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
180 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
181 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
182 when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
184 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
186 DeviceState deviceStateAfter = mock(DeviceState.class);
187 when(deviceStateAfter.getDeviceIdentifier())
188 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
189 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
190 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
191 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
192 when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
195 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
197 waitForAssert(() -> {
198 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
199 assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
204 public void testActionsChannelUpdatesForValidValues() {
206 ActionsState actionsState = mock(ActionsState.class);
207 when(actionsState.getDeviceIdentifier())
208 .thenReturn(MieleCloudBindingIntegrationTestConstants.DISH_WARMER_DEVICE_THING_UID.getId());
209 when(actionsState.canBeSwitchedOn()).thenReturn(true);
210 when(actionsState.canBeSwitchedOff()).thenReturn(false);
213 getBridgeHandler().onProcessActionUpdated(actionsState);
216 waitForAssert(() -> {
217 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
218 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
223 public void testHandleCommandDishWarmerProgramActive() {
225 getThingHandler().handleCommand(channel(DISH_WARMER_PROGRAM_ACTIVE), new StringType("3"));
228 waitForAssert(() -> {
229 verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 3);