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