2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.handler;
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.*;
20 import java.util.Optional;
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;
36 * @author Björn Lange - Initial contribution
39 public class RoboticVacuumCleanerDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
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,
49 public void testChannelUpdatesForNullValues() throws Exception {
51 setUpBridgeAndThing();
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());
65 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
82 public void testChannelUpdatesForValidValues() throws Exception {
84 setUpBridgeAndThing();
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));
99 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
116 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
118 setUpBridgeAndThing();
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();
126 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
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();
135 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
138 waitForAssert(() -> {
139 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
144 public void testActionsChannelUpdatesForValidValues() throws Exception {
146 setUpBridgeAndThing();
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);
157 getBridgeHandler().onProcessActionUpdated(actionsState);
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));
169 public void testHandleCommandVacuumCleanerProgramActive() throws Exception {
171 setUpBridgeAndThing();
174 getThingHandler().handleCommand(channel(VACUUM_CLEANER_PROGRAM_ACTIVE), new StringType("1"));
177 waitForAssert(() -> {
178 verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 1);