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);
48 public void testChannelUpdatesForNullValues() throws Exception {
50 setUpBridgeAndThing();
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());
64 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
81 public void testChannelUpdatesForValidValues() throws Exception {
83 setUpBridgeAndThing();
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));
98 getBridgeHandler().onDeviceStateUpdated(deviceState);
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));
115 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
117 setUpBridgeAndThing();
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();
125 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
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();
134 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
137 waitForAssert(() -> {
138 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
143 public void testActionsChannelUpdatesForValidValues() throws Exception {
145 setUpBridgeAndThing();
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);
156 getBridgeHandler().onProcessActionUpdated(actionsState);
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));
168 public void testHandleCommandVacuumCleanerProgramActive() throws Exception {
170 setUpBridgeAndThing();
173 getThingHandler().handleCommand(channel(VACUUM_CLEANER_PROGRAM_ACTIVE), new StringType("1"));
176 waitForAssert(() -> {
177 verify(getWebserviceMock()).putProgram(getThingHandler().getDeviceId(), 1);