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