]> git.basschouten.com Git - openhab-addons.git/blob
ef87d664860cce0ff8f913425b8c040d45f3061c
[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.Mockito.*;
17 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
18 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.HOOD_DEVICE_THING_UID;
19
20 import java.util.Optional;
21
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.StringType;
33
34 /**
35  * @author Björn Lange - Initial contribution
36  * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
37  */
38 @NonNullByDefault
39 public class HoodDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
40     @Override
41     protected AbstractMieleThingHandler setUpThingHandler() {
42         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_HOOD, HOOD_DEVICE_THING_UID,
43                 HoodDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
44     }
45
46     @Test
47     public void testChannelUpdatesForNullValues() throws Exception {
48         // given:
49         setUpBridgeAndThing();
50
51         DeviceState deviceState = mock(DeviceState.class);
52         when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
53         when(deviceState.getStateType()).thenReturn(Optional.empty());
54         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
55         when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
56         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
57         when(deviceState.getStatus()).thenReturn(Optional.empty());
58         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
59         when(deviceState.getVentilationStep()).thenReturn(Optional.empty());
60         when(deviceState.getVentilationStepRaw()).thenReturn(Optional.empty());
61         when(deviceState.getLightState()).thenReturn(Optional.empty());
62
63         // when:
64         getBridgeHandler().onDeviceStateUpdated(deviceState);
65
66         // then:
67         waitForAssert(() -> {
68             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
69             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
70             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
71             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
72             assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER));
73             assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER_RAW));
74             assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
75             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
76         });
77     }
78
79     @Test
80     public void testChannelUpdatesForValidValues() throws Exception {
81         // given:
82         setUpBridgeAndThing();
83
84         DeviceState deviceState = mock(DeviceState.class);
85         when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
86         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
87         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(false));
88         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Kochen"));
89         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(5));
90         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
91         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
92         when(deviceState.getVentilationStep()).thenReturn(Optional.of("2"));
93         when(deviceState.getVentilationStepRaw()).thenReturn(Optional.of(2));
94         when(deviceState.hasError()).thenReturn(false);
95         when(deviceState.hasInfo()).thenReturn(true);
96         when(deviceState.getLightState()).thenReturn(Optional.of(false));
97
98         // when:
99         getBridgeHandler().onDeviceStateUpdated(deviceState);
100
101         // then:
102         waitForAssert(() -> {
103             assertEquals(new StringType("Kochen"), getChannelState(PROGRAM_PHASE));
104             assertEquals(new DecimalType(5), getChannelState(PROGRAM_PHASE_RAW));
105             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
106             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
107             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
108             assertEquals(new StringType("2"), getChannelState(VENTILATION_POWER));
109             assertEquals(new DecimalType(2), getChannelState(VENTILATION_POWER_RAW));
110             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
111             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
112             assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
113         });
114     }
115
116     @Test
117     public void testActionsChannelUpdatesForValidValues() throws Exception {
118         // given:
119         setUpBridgeAndThing();
120
121         ActionsState actionsState = mock(ActionsState.class);
122         when(actionsState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
123         when(actionsState.canBeSwitchedOn()).thenReturn(true);
124         when(actionsState.canBeSwitchedOff()).thenReturn(false);
125         when(actionsState.canControlLight()).thenReturn(true);
126
127         // when:
128         getBridgeHandler().onProcessActionUpdated(actionsState);
129
130         // then:
131         waitForAssert(() -> {
132             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
133             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
134             assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));
135         });
136     }
137 }