]> git.basschouten.com Git - openhab-addons.git/blob
9610181be857dc6cf1d838bc8a9197ac839db74f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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() {
48         // given:
49         DeviceState deviceState = mock(DeviceState.class);
50         when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
51         when(deviceState.getStateType()).thenReturn(Optional.empty());
52         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
53         when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
54         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
55         when(deviceState.getStatus()).thenReturn(Optional.empty());
56         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
57         when(deviceState.getVentilationStep()).thenReturn(Optional.empty());
58         when(deviceState.getVentilationStepRaw()).thenReturn(Optional.empty());
59         when(deviceState.getLightState()).thenReturn(Optional.empty());
60
61         // when:
62         getBridgeHandler().onDeviceStateUpdated(deviceState);
63
64         // then:
65         waitForAssert(() -> {
66             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
67             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
68             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
69             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
70             assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER));
71             assertEquals(NULL_VALUE_STATE, getChannelState(VENTILATION_POWER_RAW));
72             assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
73             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
74         });
75     }
76
77     @Test
78     public void testChannelUpdatesForValidValues() {
79         // given:
80         DeviceState deviceState = mock(DeviceState.class);
81         when(deviceState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
82         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
83         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(false));
84         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Kochen"));
85         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(5));
86         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
87         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
88         when(deviceState.getVentilationStep()).thenReturn(Optional.of("2"));
89         when(deviceState.getVentilationStepRaw()).thenReturn(Optional.of(2));
90         when(deviceState.hasError()).thenReturn(false);
91         when(deviceState.hasInfo()).thenReturn(true);
92         when(deviceState.getLightState()).thenReturn(Optional.of(false));
93
94         // when:
95         getBridgeHandler().onDeviceStateUpdated(deviceState);
96
97         // then:
98         waitForAssert(() -> {
99             assertEquals(new StringType("Kochen"), getChannelState(PROGRAM_PHASE));
100             assertEquals(new DecimalType(5), getChannelState(PROGRAM_PHASE_RAW));
101             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
102             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
103             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
104             assertEquals(new StringType("2"), getChannelState(VENTILATION_POWER));
105             assertEquals(new DecimalType(2), getChannelState(VENTILATION_POWER_RAW));
106             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
107             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
108             assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
109         });
110     }
111
112     @Test
113     public void testActionsChannelUpdatesForValidValues() {
114         // given:
115         ActionsState actionsState = mock(ActionsState.class);
116         when(actionsState.getDeviceIdentifier()).thenReturn(HOOD_DEVICE_THING_UID.getId());
117         when(actionsState.canBeSwitchedOn()).thenReturn(true);
118         when(actionsState.canBeSwitchedOff()).thenReturn(false);
119         when(actionsState.canControlLight()).thenReturn(true);
120
121         // when:
122         getBridgeHandler().onProcessActionUpdated(actionsState);
123
124         // then:
125         waitForAssert(() -> {
126             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
127             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
128             assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));
129         });
130     }
131 }