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.Mockito.*;
17 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
18 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.WINE_STORAGE_DEVICE_THING_UID;
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.DeviceType;
30 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.library.unit.SIUnits;
38 * @author Björn Lange - Initial contribution
39 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
42 public class WineStorageDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
44 protected AbstractMieleThingHandler setUpThingHandler() {
45 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_WINE_STORAGE, WINE_STORAGE_DEVICE_THING_UID,
46 WineStorageDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
50 public void testChannelUpdatesForNullValues() throws Exception {
52 setUpBridgeAndThing();
54 DeviceState deviceState = mock(DeviceState.class);
55 when(deviceState.getDeviceIdentifier()).thenReturn(WINE_STORAGE_DEVICE_THING_UID.getId());
56 when(deviceState.getRawType()).thenReturn(DeviceType.WINE_CONDITIONING_UNIT);
57 when(deviceState.getStateType()).thenReturn(Optional.empty());
58 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
59 when(deviceState.getStatus()).thenReturn(Optional.empty());
60 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
61 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.empty());
62 when(deviceState.getTemperature(0)).thenReturn(Optional.empty());
63 when(deviceState.getTargetTemperature(1)).thenReturn(Optional.empty());
64 when(deviceState.getTemperature(1)).thenReturn(Optional.empty());
65 when(deviceState.getTargetTemperature(2)).thenReturn(Optional.empty());
66 when(deviceState.getTemperature(2)).thenReturn(Optional.empty());
69 getBridgeHandler().onDeviceStateUpdated(deviceState);
73 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
74 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
75 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_TARGET));
76 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_CURRENT));
77 assertEquals(NULL_VALUE_STATE, getChannelState(TOP_TEMPERATURE_TARGET));
78 assertEquals(NULL_VALUE_STATE, getChannelState(TOP_TEMPERATURE_CURRENT));
79 assertEquals(NULL_VALUE_STATE, getChannelState(MIDDLE_TEMPERATURE_TARGET));
80 assertEquals(NULL_VALUE_STATE, getChannelState(MIDDLE_TEMPERATURE_CURRENT));
81 assertEquals(NULL_VALUE_STATE, getChannelState(BOTTOM_TEMPERATURE_TARGET));
82 assertEquals(NULL_VALUE_STATE, getChannelState(BOTTOM_TEMPERATURE_CURRENT));
83 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
88 public void testChannelUpdatesForValidValues() throws Exception {
90 setUpBridgeAndThing();
92 DeviceState deviceState = mock(DeviceState.class);
93 when(deviceState.getDeviceIdentifier()).thenReturn(WINE_STORAGE_DEVICE_THING_UID.getId());
94 when(deviceState.getRawType()).thenReturn(DeviceType.WINE_CONDITIONING_UNIT);
95 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
96 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
97 when(deviceState.getStatus()).thenReturn(Optional.of("Im Betrieb"));
98 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
99 when(deviceState.getTargetTemperature(0)).thenReturn(Optional.of(8));
100 when(deviceState.getTemperature(0)).thenReturn(Optional.of(9));
101 when(deviceState.getTargetTemperature(1)).thenReturn(Optional.of(10));
102 when(deviceState.getTemperature(1)).thenReturn(Optional.of(11));
103 when(deviceState.getTargetTemperature(2)).thenReturn(Optional.of(12));
104 when(deviceState.getTemperature(2)).thenReturn(Optional.of(14));
105 when(deviceState.hasError()).thenReturn(true);
106 when(deviceState.hasInfo()).thenReturn(true);
109 getBridgeHandler().onDeviceStateUpdated(deviceState);
112 waitForAssert(() -> {
113 assertEquals(new StringType("Im Betrieb"), getChannelState(OPERATION_STATE));
114 assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
115 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_TARGET));
116 assertEquals(NULL_VALUE_STATE, getChannelState(TEMPERATURE_CURRENT));
117 assertEquals(new QuantityType<>(8, SIUnits.CELSIUS), getChannelState(TOP_TEMPERATURE_TARGET));
118 assertEquals(new QuantityType<>(9, SIUnits.CELSIUS), getChannelState(TOP_TEMPERATURE_CURRENT));
119 assertEquals(new QuantityType<>(10, SIUnits.CELSIUS), getChannelState(MIDDLE_TEMPERATURE_TARGET));
120 assertEquals(new QuantityType<>(11, SIUnits.CELSIUS), getChannelState(MIDDLE_TEMPERATURE_CURRENT));
121 assertEquals(new QuantityType<>(12, SIUnits.CELSIUS), getChannelState(BOTTOM_TEMPERATURE_TARGET));
122 assertEquals(new QuantityType<>(14, SIUnits.CELSIUS), getChannelState(BOTTOM_TEMPERATURE_CURRENT));
123 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
124 assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
125 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
130 public void testActionsChannelUpdatesForValidValues() throws Exception {
132 setUpBridgeAndThing();
134 ActionsState actionsState = mock(ActionsState.class);
135 when(actionsState.getDeviceIdentifier()).thenReturn(WINE_STORAGE_DEVICE_THING_UID.getId());
136 when(actionsState.canBeSwitchedOn()).thenReturn(true);
137 when(actionsState.canBeSwitchedOff()).thenReturn(false);
140 getBridgeHandler().onProcessActionUpdated(actionsState);
143 waitForAssert(() -> {
144 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
145 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));