2 * Copyright (c) 2010-2022 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;
18 import java.lang.reflect.Field;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.TestInstance;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.irobot.internal.config.IRobotConfiguration;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingStatusInfo;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.ThingHandlerCallback;
40 import org.openhab.core.thing.internal.ThingImpl;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.slf4j.spi.LocationAwareLogger;
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 Logger logger = LoggerFactory.getLogger(RoombaHandler.class);
70 Field logLevelField = logger.getClass().getDeclaredField("currentLogLevel");
71 logLevelField.setAccessible(true);
72 logLevelField.set(logger, LocationAwareLogger.TRACE_INT);
74 Configuration config = new Configuration();
75 config.put("ipaddress", RoombaHandlerTest.IP_ADDRESS);
76 config.put("password", RoombaHandlerTest.PASSWORD);
77 Mockito.when(thing.getConfiguration()).thenReturn(config);
78 Mockito.lenient().when(thing.getStatusInfo())
79 .thenReturn(new ThingStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.NONE, "mocked"));
80 Mockito.lenient().when(thing.getUID()).thenReturn(new ThingUID("mocked", "irobot", "uid"));
82 callback = Mockito.mock(ThingHandlerCallback.class);
84 handler = new RoombaHandler(thing);
85 handler.setCallback(callback);
89 void testConfiguration() throws InterruptedException, IOException {
92 IRobotConfiguration config = thing.getConfiguration().as(IRobotConfiguration.class);
93 assertEquals(config.getIpAddress(), IP_ADDRESS);
94 assertEquals(config.getPassword(), PASSWORD);
100 void testCleanRegion() throws InterruptedException, IOException {
101 handler.initialize();
103 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
104 handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
110 void testDock() throws InterruptedException, IOException {
111 handler.initialize();
113 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
114 handler.handleCommand(cmd, new StringType("dock"));
120 void testStop() throws InterruptedException, IOException {
121 handler.initialize();
123 ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
124 handler.handleCommand(cmd, new StringType("stop"));