2 * Copyright (c) 2010-2021 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 java.io.IOException;
16 import java.lang.reflect.Field;
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.TestInstance;
20 import org.junit.jupiter.api.TestInstance.Lifecycle;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandlerCallback;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.slf4j.spi.LocationAwareLogger;
36 * Test the MQTT protocol with local iRobot (without openhab running).
37 * This class is used to test the binding against a local iRobot instance.
39 * @author Florian Binder - Initial contribution
42 @ExtendWith(MockitoExtension.class)
43 @TestInstance(Lifecycle.PER_CLASS)
44 class RoombaHandlerTest {
46 private static final String IP_ADDRESS = "<iRobotIP>";
47 private static final String PASSWORD = "<PasswordForIRobot>";
49 private RoombaHandler handler;
50 private @Mock Thing myThing;
51 private ThingHandlerCallback callback;
54 void setUp() throws Exception {
55 Logger l = LoggerFactory.getLogger(RoombaHandler.class);
56 Field f = l.getClass().getDeclaredField("currentLogLevel");
57 f.setAccessible(true);
58 f.set(l, LocationAwareLogger.TRACE_INT);
60 Configuration config = new Configuration();
61 config.put("ipaddress", RoombaHandlerTest.IP_ADDRESS);
62 config.put("password", RoombaHandlerTest.PASSWORD);
64 Mockito.when(myThing.getConfiguration()).thenReturn(config);
65 Mockito.when(myThing.getUID()).thenReturn(new ThingUID("mocked", "irobot", "uid"));
67 callback = Mockito.mock(ThingHandlerCallback.class);
69 handler = new RoombaHandler(myThing);
70 handler.setCallback(callback);
74 void testInit() throws InterruptedException, IOException {
76 Mockito.verify(myThing, Mockito.times(1)).getConfiguration();
83 void testCleanRegion() throws IOException, InterruptedException {
88 ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
89 handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
96 void testDock() throws IOException, InterruptedException {
101 ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
102 handler.handleCommand(cmd, new StringType("dock"));
109 void testStop() throws IOException, InterruptedException {
110 handler.initialize();
114 ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
115 handler.handleCommand(cmd, new StringType("stop"));