]> git.basschouten.com Git - openhab-addons.git/blob
23a74a9e72ff699bd47d8d127c69adb1b061486e
[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
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.ProgramStatus;
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  */
38 @NonNullByDefault
39 public class RoboticVacuumCleanerDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
40     @Override
41     protected AbstractMieleThingHandler setUpThingHandler() {
42         return createThingHandler(MieleCloudBindingConstants.THING_TYPE_ROBOTIC_VACUUM_CLEANER,
43                 MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_THING_UID,
44                 RoboticVacuumCleanerDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
45     }
46
47     @Test
48     public void testChannelUpdatesForNullValues() throws Exception {
49         // given:
50         setUpBridgeAndThing();
51
52         DeviceState deviceState = mock(DeviceState.class);
53         when(deviceState.getDeviceIdentifier())
54                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_THING_UID.getId());
55         when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
56         when(deviceState.getStatus()).thenReturn(Optional.empty());
57         when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
58         when(deviceState.getStateType()).thenReturn(Optional.empty());
59         when(deviceState.hasError()).thenReturn(false);
60         when(deviceState.hasInfo()).thenReturn(false);
61         when(deviceState.getBatteryLevel()).thenReturn(Optional.empty());
62
63         // when:
64         getBridgeHandler().onDeviceStateUpdated(deviceState);
65
66         // then:
67         waitForAssert(() -> {
68             assertEquals(NULL_VALUE_STATE, getChannelState(VACUUM_CLEANER_PROGRAM_ACTIVE));
69             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
70             assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
71             assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()),
72                     getChannelState(PROGRAM_START_STOP_PAUSE));
73             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
74             assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
75             assertEquals(OnOffType.OFF, getChannelState(INFO_STATE));
76             assertEquals(NULL_VALUE_STATE, getChannelState(BATTERY_LEVEL));
77         });
78     }
79
80     @Test
81     public void testChannelUpdatesForValidValues() throws Exception {
82         // given:
83         setUpBridgeAndThing();
84
85         DeviceState deviceState = mock(DeviceState.class);
86         when(deviceState.isInState(any())).thenCallRealMethod();
87         when(deviceState.getDeviceIdentifier())
88                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_THING_UID.getId());
89         when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(1L));
90         when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
91         when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
92         when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
93         when(deviceState.hasError()).thenReturn(true);
94         when(deviceState.hasInfo()).thenReturn(true);
95         when(deviceState.getBatteryLevel()).thenReturn(Optional.of(25));
96
97         // when:
98         getBridgeHandler().onDeviceStateUpdated(deviceState);
99
100         // then:
101         waitForAssert(() -> {
102             assertEquals(new StringType("1"), getChannelState(VACUUM_CLEANER_PROGRAM_ACTIVE));
103             assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
104             assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
105             assertEquals(new StringType(ProgramStatus.PROGRAM_STARTED.getState()),
106                     getChannelState(PROGRAM_START_STOP_PAUSE));
107             assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
108             assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
109             assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
110             assertEquals(new DecimalType(25), getChannelState(BATTERY_LEVEL));
111         });
112     }
113
114     @Test
115     public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
116         // given:
117         setUpBridgeAndThing();
118
119         DeviceState deviceStateBefore = mock(DeviceState.class);
120         when(deviceStateBefore.getDeviceIdentifier())
121                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_THING_UID.getId());
122         when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
123         when(deviceStateBefore.isInState(any())).thenCallRealMethod();
124
125         getThingHandler().onDeviceStateUpdated(deviceStateBefore);
126
127         DeviceState deviceStateAfter = mock(DeviceState.class);
128         when(deviceStateAfter.getDeviceIdentifier())
129                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_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 testActionsChannelUpdatesForValidValues() throws Exception {
144         // given:
145         setUpBridgeAndThing();
146
147         ActionsState actionsState = mock(ActionsState.class);
148         when(actionsState.getDeviceIdentifier())
149                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ROBOTIC_VACUUM_CLEANER_THING_UID.getId());
150         when(actionsState.canBeStarted()).thenReturn(true);
151         when(actionsState.canBeStopped()).thenReturn(false);
152         when(actionsState.canBePaused()).thenReturn(true);
153         when(actionsState.canSetActiveProgramId()).thenReturn(false);
154
155         // when:
156         getBridgeHandler().onProcessActionUpdated(actionsState);
157
158         // then:
159         waitForAssert(() -> {
160             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
161             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
162             assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_PAUSED));
163             assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_SET_PROGRAM_ACTIVE));
164         });
165     }
166
167     @Test
168     public void testHandleCommandVacuumCleanerProgramActive() throws Exception {
169         // given:
170         setUpBridgeAndThing();
171
172         // when:
173         getThingHandler().handleCommand(channel(VACUUM_CLEANER_PROGRAM_ACTIVE), new StringType("1"));
174
175         // then:
176         waitForAssert(() -> {
177             verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 1);
178         });
179     }
180 }