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