]> git.basschouten.com Git - openhab-addons.git/blob
49b9f5b610418ab7803574e2a4356ffcd583a304
[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.ArgumentMatchers.any;
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.COFFEE_SYSTEM_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.PowerStatus;
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.StringType;
34
35 /**
36  * @author Björn Lange - Initial contribution
37  * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
38  * @author Björn Lange - Add elapsed time channel
39  */
40 @NonNullByDefault
41 public class CoffeeDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
42     @Override
43     protected AbstractMieleThingHandler setUpThingHandler() {
44         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_COFFEE_SYSTEM, COFFEE_SYSTEM_THING_UID,
45                 CoffeeSystemThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
46     }
47
48     @Test
49     public void testChannelUpdatesForNullValues() {
50         // given:
51         DeviceState deviceState = mock(DeviceState.class);
52         when(deviceState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
53         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
54         when(deviceState.getSelectedProgram()).thenReturn(Optional.empty());
55         when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
56         when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
57         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
58         when(deviceState.getStateType()).thenReturn(Optional.empty());
59         when(deviceState.getStatus()).thenReturn(Optional.empty());
60         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
61         when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
62         when(deviceState.getLightState()).thenReturn(Optional.empty());
63
64         // when:
65         getBridgeHandler().onDeviceStateUpdated(deviceState);
66
67         // then:
68         waitForAssert(() -> {
69             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
70             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
71             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
72             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
73             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
74             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
75             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
76             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
77             assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
78         });
79     }
80
81     @Test
82     public void testChannelUpdatesForValidValues() {
83         // given:
84         DeviceState deviceState = mock(DeviceState.class);
85         when(deviceState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
86         when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
87         when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Latte Macchiato"));
88         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(5L));
89         when(deviceState.getProgramPhase()).thenReturn(Optional.of("Spühlen"));
90         when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(1));
91         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
92         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
93         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
94         when(deviceState.hasError()).thenReturn(false);
95         when(deviceState.hasInfo()).thenReturn(true);
96         when(deviceState.getLightState()).thenReturn(Optional.of(false));
97         when(deviceState.getElapsedTime()).thenReturn(Optional.of(3));
98
99         // when:
100         getBridgeHandler().onDeviceStateUpdated(deviceState);
101
102         // then:
103         waitForAssert(() -> {
104             assertEquals(new StringType("Latte Macchiato"), getChannelState(PROGRAM_ACTIVE));
105             assertEquals(new DecimalType(5), getChannelState(PROGRAM_ACTIVE_RAW));
106             assertEquals(new StringType("Spühlen"), getChannelState(PROGRAM_PHASE));
107             assertEquals(new DecimalType(1), getChannelState(PROGRAM_PHASE_RAW));
108             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
109             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
110             assertEquals(new DecimalType(3), getChannelState(PROGRAM_ELAPSED_TIME));
111             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
112             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
113             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
114             assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
115         });
116     }
117
118     @Test
119     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() {
120         // given:
121         DeviceState deviceStateBefore = mock(DeviceState.class);
122         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
123         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
124         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
125
126         getBridgeHandler().onDeviceStateUpdated(deviceStateBefore);
127
128         DeviceState deviceStateAfter = mock(DeviceState.class);
129         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
130         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
131         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
132
133         // when:
134         getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
135
136         // then:
137         waitForAssert(() -> {
138             assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
139         });
140     }
141
142     @Test
143     public void testTransitionChannelUpdatesForNullValues() {
144         // given:
145         DeviceState deviceStateBefore = mock(DeviceState.class);
146         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
147         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
148         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
149         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
150
151         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
152
153         DeviceState deviceStateAfter = mock(DeviceState.class);
154         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
155         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
156         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
157         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
158
159         // when:
160         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
161
162         waitForAssert(() -> {
163             assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
164         });
165     }
166
167     @Test
168     public void testTransitionChannelUpdatesForValidValues() {
169         // given:
170         DeviceState deviceStateBefore = mock(DeviceState.class);
171         when(deviceStateBefore.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
172         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
173         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
174         when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
175
176         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
177
178         DeviceState deviceStateAfter = mock(DeviceState.class);
179         when(deviceStateAfter.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
180         when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
181         when(deviceStateAfter.isInState(any())).thenCallRealMethod();
182         when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
183
184         // when:
185         getThingHandler().onDeviceStateUpdated(deviceStateAfter);
186
187         waitForAssert(() -> {
188             assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
189         });
190     }
191
192     @Test
193     public void testActionsChannelUpdatesForValidValues() {
194         // given:
195         ActionsState actionsState = mock(ActionsState.class);
196         when(actionsState.getDeviceIdentifier()).thenReturn(COFFEE_SYSTEM_THING_UID.getId());
197         when(actionsState.canBeSwitchedOn()).thenReturn(true);
198         when(actionsState.canBeSwitchedOff()).thenReturn(false);
199         when(actionsState.canControlLight()).thenReturn(true);
200
201         // when:
202         getBridgeHandler().onProcessActionUpdated(actionsState);
203
204         // then:
205         waitForAssert(() -> {
206             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
207             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
208             assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));
209         });
210     }
211 }