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.*;
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.FRIDGE_FREEZER_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.json.DeviceType;
30 import org.openhab.binding.mielecloud.internal.webservice.api.json.ProcessAction;
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.QuantityType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.library.unit.SIUnits;
39 * @author Björn Lange - Initial contribution
40 * @author Benjamin Bolte - Add door state and door alarm
41 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
44 public class CoolingDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
46 protected AbstractMieleThingHandler setUpThingHandler() {
47 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_FRIDGE_FREEZER, FRIDGE_FREEZER_DEVICE_THING_UID,
48 CoolingDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
52 public void testChannelUpdatesForNullValues() throws Exception {
54 setUpBridgeAndThing();
56 DeviceState deviceState = mock(DeviceState.class);
57 when(deviceState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
58 when(deviceState.getRawType()).thenReturn(DeviceType.FRIDGE_FREEZER_COMBINATION);
59 when(deviceState.getStateType()).thenReturn(Optional.empty());
60 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
61 when(deviceState.getStatus()).thenReturn(Optional.empty());
62 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
63 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.empty());
64 when(deviceState.getTargetTemperature(1)).thenReturn(Optional.empty());
65 when(deviceState.getTemperature(0)).thenReturn(Optional.empty());
66 when(deviceState.getTemperature(1)).thenReturn(Optional.empty());
67 when(deviceState.getDoorState()).thenReturn(Optional.empty());
68 when(deviceState.getDoorAlarm()).thenReturn(Optional.empty());
71 getBridgeHandler().onDeviceStateUpdated(deviceState);
75 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
76 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
77 assertEquals(NULL_VALUE_STATE, getChannelState(FRIDGE_SUPER_COOL));
78 assertEquals(NULL_VALUE_STATE, getChannelState(FREEZER_SUPER_FREEZE));
79 assertEquals(NULL_VALUE_STATE, getChannelState(FRIDGE_TEMPERATURE_TARGET));
80 assertEquals(NULL_VALUE_STATE, getChannelState(FREEZER_TEMPERATURE_TARGET));
81 assertEquals(NULL_VALUE_STATE, getChannelState(FRIDGE_TEMPERATURE_CURRENT));
82 assertEquals(NULL_VALUE_STATE, getChannelState(FREEZER_TEMPERATURE_CURRENT));
83 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
84 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_ALARM));
89 public void testChannelUpdatesForValidValues() throws Exception {
91 setUpBridgeAndThing();
93 DeviceState deviceState = mock(DeviceState.class);
94 when(deviceState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
95 when(deviceState.getRawType()).thenReturn(DeviceType.FRIDGE_FREEZER_COMBINATION);
96 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.SUPERCOOLING));
97 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
98 when(deviceState.getStatus()).thenReturn(Optional.of("Super Cooling"));
99 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.SUPERCOOLING.getCode()));
100 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.of(6));
101 when(deviceState.getTargetTemperature(1)).thenReturn(Optional.of(-18));
102 when(deviceState.getTemperature(0)).thenReturn(Optional.of(8));
103 when(deviceState.getTemperature(1)).thenReturn(Optional.of(-10));
104 when(deviceState.hasError()).thenReturn(false);
105 when(deviceState.getDoorState()).thenReturn(Optional.of(true));
106 when(deviceState.getDoorAlarm()).thenReturn(Optional.of(false));
109 getBridgeHandler().onDeviceStateUpdated(deviceState);
112 waitForAssert(() -> {
113 assertEquals(new StringType("Super Cooling"), getChannelState(OPERATION_STATE));
114 assertEquals(new DecimalType(StateType.SUPERCOOLING.getCode()), getChannelState(OPERATION_STATE_RAW));
115 assertEquals(OnOffType.ON, getChannelState(FRIDGE_SUPER_COOL));
116 assertEquals(OnOffType.OFF, getChannelState(FREEZER_SUPER_FREEZE));
117 assertEquals(new QuantityType<>(6, SIUnits.CELSIUS), getChannelState(FRIDGE_TEMPERATURE_TARGET));
118 assertEquals(new QuantityType<>(-18, SIUnits.CELSIUS), getChannelState(FREEZER_TEMPERATURE_TARGET));
119 assertEquals(new QuantityType<>(8, SIUnits.CELSIUS), getChannelState(FRIDGE_TEMPERATURE_CURRENT));
120 assertEquals(new QuantityType<>(-10, SIUnits.CELSIUS), getChannelState(FREEZER_TEMPERATURE_CURRENT));
121 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
122 assertEquals(OnOffType.ON, getChannelState(DOOR_STATE));
123 assertEquals(OnOffType.OFF, getChannelState(DOOR_ALARM));
128 public void testChannelUpdatesForSuperCooling() throws Exception {
130 setUpBridgeAndThing();
132 DeviceState deviceState = mock(DeviceState.class);
133 when(deviceState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
134 when(deviceState.getRawType()).thenReturn(DeviceType.FRIDGE_FREEZER_COMBINATION);
135 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.SUPERCOOLING));
138 getBridgeHandler().onDeviceStateUpdated(deviceState);
141 waitForAssert(() -> {
142 assertEquals(OnOffType.ON, getChannelState(FRIDGE_SUPER_COOL));
143 assertEquals(OnOffType.OFF, getChannelState(FREEZER_SUPER_FREEZE));
148 public void testChannelUpdatesForSuperFreezing() throws Exception {
150 setUpBridgeAndThing();
152 DeviceState deviceState = mock(DeviceState.class);
153 when(deviceState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
154 when(deviceState.getRawType()).thenReturn(DeviceType.FRIDGE_FREEZER_COMBINATION);
155 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.SUPERFREEZING));
158 getBridgeHandler().onDeviceStateUpdated(deviceState);
161 waitForAssert(() -> {
162 assertEquals(OnOffType.OFF, getChannelState(FRIDGE_SUPER_COOL));
163 assertEquals(OnOffType.ON, getChannelState(FREEZER_SUPER_FREEZE));
168 public void testChannelUpdatesForSuperCollingSuperFreezing() throws Exception {
170 setUpBridgeAndThing();
172 DeviceState deviceState = mock(DeviceState.class);
173 when(deviceState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
174 when(deviceState.getRawType()).thenReturn(DeviceType.FRIDGE_FREEZER_COMBINATION);
175 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.SUPERCOOLING_SUPERFREEZING));
178 getBridgeHandler().onDeviceStateUpdated(deviceState);
181 waitForAssert(() -> {
182 assertEquals(OnOffType.ON, getChannelState(FRIDGE_SUPER_COOL));
183 assertEquals(OnOffType.ON, getChannelState(FREEZER_SUPER_FREEZE));
188 public void testActionsChannelUpdatesForValidValues() throws Exception {
190 setUpBridgeAndThing();
192 ActionsState actionsState = mock(ActionsState.class);
193 when(actionsState.getDeviceIdentifier()).thenReturn(FRIDGE_FREEZER_DEVICE_THING_UID.getId());
194 when(actionsState.canContolSupercooling()).thenReturn(true);
195 when(actionsState.canControlSuperfreezing()).thenReturn(false);
198 getBridgeHandler().onProcessActionUpdated(actionsState);
201 waitForAssert(() -> {
202 assertEquals(OnOffType.ON, getChannelState(SUPER_COOL_CAN_BE_CONTROLLED));
203 assertEquals(OnOffType.OFF, getChannelState(SUPER_FREEZE_CAN_BE_CONTROLLED));
209 public void testHandleCommandDoesNothingWhenCommandIsNotOfOnOffType() throws Exception {
211 setUpBridgeAndThing();
214 getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), new DecimalType(50));
217 verify(getWebserviceMock(), never()).putProcessAction(anyString(), any());
221 public void testHandleCommandStartsSupercoolingWhenRequested() throws Exception {
223 setUpBridgeAndThing();
226 getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), OnOffType.ON);
229 waitForAssert(() -> {
230 verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
231 ProcessAction.START_SUPERCOOLING);
236 public void testHandleCommandStopsSupercoolingWhenRequested() throws Exception {
238 setUpBridgeAndThing();
241 getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), OnOffType.OFF);
244 waitForAssert(() -> {
245 verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
246 ProcessAction.STOP_SUPERCOOLING);
251 public void testHandleCommandStartsSuperfreezingWhenRequested() throws Exception {
253 setUpBridgeAndThing();
256 getThingHandler().handleCommand(channel(FREEZER_SUPER_FREEZE), OnOffType.ON);
259 waitForAssert(() -> {
260 verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
261 ProcessAction.START_SUPERFREEZING);
266 public void testHandleCommandStopsSuperfreezingWhenRequested() throws Exception {
268 setUpBridgeAndThing();
271 getThingHandler().handleCommand(channel(FREEZER_SUPER_FREEZE), OnOffType.OFF);
274 waitForAssert(() -> {
275 verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
276 ProcessAction.STOP_SUPERFREEZING);