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.irobot.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.TestInstance;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.openhab.binding.irobot.internal.config.IRobotConfiguration;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.ThingStatusInfo;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandlerCallback;
39 import org.openhab.core.thing.internal.ThingImpl;
40 import org.slf4j.LoggerFactory;
42 import ch.qos.logback.classic.Level;
43 import ch.qos.logback.classic.Logger;
46 * Test the MQTT protocol with local iRobot (without openhab running).
47 * This class is used to test the binding against a local iRobot instance.
49 * @author Florian Binder - Initial contribution
53 @ExtendWith(MockitoExtension.class)
54 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
55 public class RoombaHandlerTest {
57 private static final String IP_ADDRESS = "<iRobotIP>";
58 private static final String PASSWORD = "<PasswordForIRobot>";
61 private RoombaHandler handler;
62 // We have to initialize it to avoid compile errors
63 private @Mock Thing thing = new ThingImpl(new ThingTypeUID("AA:BB"), "test");
65 private ThingHandlerCallback callback;
68 void setUp() throws Exception {
69 final Logger logger = (Logger) LoggerFactory.getLogger(RoombaHandler.class);
70 logger.setLevel(Level.TRACE);
71 Configuration config = new Configuration();
72 config.put("ipaddress", RoombaHandlerTest.IP_ADDRESS);
73 config.put("password", RoombaHandlerTest.PASSWORD);
74 Mockito.when(thing.getConfiguration()).thenReturn(config);
75 Mockito.lenient().when(thing.getStatusInfo())
76 .thenReturn(new ThingStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE, "mocked"));
77 Mockito.lenient().when(thing.getUID()).thenReturn(new ThingUID("mocked", "irobot", "uid"));
79 callback = Mockito.mock(ThingHandlerCallback.class);
81 handler = new RoombaHandler(thing);
82 handler.setCallback(callback);
86 void testConfiguration() throws InterruptedException, IOException {
89 IRobotConfiguration config = thing.getConfiguration().as(IRobotConfiguration.class);
90 assertEquals(config.getIpAddress(), IP_ADDRESS);
91 assertEquals(config.getPassword(), PASSWORD);
97 void testCleanRegion() throws InterruptedException, IOException {
100 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
101 handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
107 void testDock() throws InterruptedException, IOException {
108 handler.initialize();
110 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
111 handler.handleCommand(cmd, new StringType("dock"));
117 void testStop() throws InterruptedException, IOException {
118 handler.initialize();
120 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
121 handler.handleCommand(cmd, new StringType("stop"));