]> git.basschouten.com Git - openhab-addons.git/blob
fcb8d41b1045813e0d9bf260c2b72cf5f498e484
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.handler;
14
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;
20
21 import java.util.Optional;
22
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;
37
38 /**
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
42  */
43 @NonNullByDefault
44 public class CoolingDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
45     @Override
46     protected AbstractMieleThingHandler setUpThingHandler() {
47         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_FRIDGE_FREEZER, FRIDGE_FREEZER_DEVICE_THING_UID,
48                 CoolingDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
49     }
50
51     @Test
52     public void testChannelUpdatesForNullValues() throws Exception {
53         // given:
54         setUpBridgeAndThing();
55
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());
69
70         // when:
71         getBridgeHandler().onDeviceStateUpdated(deviceState);
72
73         // then:
74         waitForAssert(() -> {
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));
85         });
86     }
87
88     @Test
89     public void testChannelUpdatesForValidValues() throws Exception {
90         // given:
91         setUpBridgeAndThing();
92
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));
107
108         // when:
109         getBridgeHandler().onDeviceStateUpdated(deviceState);
110
111         // then:
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));
124         });
125     }
126
127     @Test
128     public void testChannelUpdatesForSuperCooling() throws Exception {
129         // given:
130         setUpBridgeAndThing();
131
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));
136
137         // when:
138         getBridgeHandler().onDeviceStateUpdated(deviceState);
139
140         // then:
141         waitForAssert(() -> {
142             assertEquals(OnOffType.ON, getChannelState(FRIDGE_SUPER_COOL));
143             assertEquals(OnOffType.OFF, getChannelState(FREEZER_SUPER_FREEZE));
144         });
145     }
146
147     @Test
148     public void testChannelUpdatesForSuperFreezing() throws Exception {
149         // given:
150         setUpBridgeAndThing();
151
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));
156
157         // when:
158         getBridgeHandler().onDeviceStateUpdated(deviceState);
159
160         // then:
161         waitForAssert(() -> {
162             assertEquals(OnOffType.OFF, getChannelState(FRIDGE_SUPER_COOL));
163             assertEquals(OnOffType.ON, getChannelState(FREEZER_SUPER_FREEZE));
164         });
165     }
166
167     @Test
168     public void testChannelUpdatesForSuperCollingSuperFreezing() throws Exception {
169         // given:
170         setUpBridgeAndThing();
171
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));
176
177         // when:
178         getBridgeHandler().onDeviceStateUpdated(deviceState);
179
180         // then:
181         waitForAssert(() -> {
182             assertEquals(OnOffType.ON, getChannelState(FRIDGE_SUPER_COOL));
183             assertEquals(OnOffType.ON, getChannelState(FREEZER_SUPER_FREEZE));
184         });
185     }
186
187     @Test
188     public void testActionsChannelUpdatesForValidValues() throws Exception {
189         // given:
190         setUpBridgeAndThing();
191
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);
196
197         // when:
198         getBridgeHandler().onProcessActionUpdated(actionsState);
199
200         // then:
201         waitForAssert(() -> {
202             assertEquals(OnOffType.ON, getChannelState(SUPER_COOL_CAN_BE_CONTROLLED));
203             assertEquals(OnOffType.OFF, getChannelState(SUPER_FREEZE_CAN_BE_CONTROLLED));
204         });
205     }
206
207     @Override
208     @Test
209     public void testHandleCommandDoesNothingWhenCommandIsNotOfOnOffType() throws Exception {
210         // given:
211         setUpBridgeAndThing();
212
213         // when:
214         getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), new DecimalType(50));
215
216         // then:
217         verify(getWebserviceMock(), never()).putProcessAction(anyString(), any());
218     }
219
220     @Test
221     public void testHandleCommandStartsSupercoolingWhenRequested() throws Exception {
222         // given:
223         setUpBridgeAndThing();
224
225         // when:
226         getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), OnOffType.ON);
227
228         // then:
229         waitForAssert(() -> {
230             verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
231                     ProcessAction.START_SUPERCOOLING);
232         });
233     }
234
235     @Test
236     public void testHandleCommandStopsSupercoolingWhenRequested() throws Exception {
237         // given:
238         setUpBridgeAndThing();
239
240         // when:
241         getThingHandler().handleCommand(channel(FRIDGE_SUPER_COOL), OnOffType.OFF);
242
243         // then:
244         waitForAssert(() -> {
245             verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
246                     ProcessAction.STOP_SUPERCOOLING);
247         });
248     }
249
250     @Test
251     public void testHandleCommandStartsSuperfreezingWhenRequested() throws Exception {
252         // given:
253         setUpBridgeAndThing();
254
255         // when:
256         getThingHandler().handleCommand(channel(FREEZER_SUPER_FREEZE), OnOffType.ON);
257
258         // then:
259         waitForAssert(() -> {
260             verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
261                     ProcessAction.START_SUPERFREEZING);
262         });
263     }
264
265     @Test
266     public void testHandleCommandStopsSuperfreezingWhenRequested() throws Exception {
267         // given:
268         setUpBridgeAndThing();
269
270         // when:
271         getThingHandler().handleCommand(channel(FREEZER_SUPER_FREEZE), OnOffType.OFF);
272
273         // then:
274         waitForAssert(() -> {
275             verify(getWebserviceMock()).putProcessAction(getThingHandler().getDeviceId(),
276                     ProcessAction.STOP_SUPERFREEZING);
277         });
278     }
279 }