]> git.basschouten.com Git - openhab-addons.git/blob
afb9b8250b6894e4e157ff704c0d40ed8842dbf2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.irobot.internal.handler;
14
15 import java.io.IOException;
16 import java.lang.reflect.Field;
17
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;
34
35 /**
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.
38  *
39  * @author Florian Binder - Initial contribution
40  */
41
42 @ExtendWith(MockitoExtension.class)
43 @TestInstance(Lifecycle.PER_CLASS)
44 class RoombaHandlerTest {
45
46     private static final String IP_ADDRESS = "<iRobotIP>";
47     private static final String PASSWORD = "<PasswordForIRobot>";
48
49     private RoombaHandler handler;
50     private @Mock Thing myThing;
51     private ThingHandlerCallback callback;
52
53     @BeforeEach
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);
59
60         Configuration config = new Configuration();
61         config.put("ipaddress", RoombaHandlerTest.IP_ADDRESS);
62         config.put("password", RoombaHandlerTest.PASSWORD);
63
64         Mockito.when(myThing.getConfiguration()).thenReturn(config);
65         Mockito.when(myThing.getUID()).thenReturn(new ThingUID("mocked", "irobot", "uid"));
66
67         callback = Mockito.mock(ThingHandlerCallback.class);
68
69         handler = new RoombaHandler(myThing);
70         handler.setCallback(callback);
71     }
72
73     // @Test
74     void testInit() throws InterruptedException, IOException {
75         handler.initialize();
76         Mockito.verify(myThing, Mockito.times(1)).getConfiguration();
77
78         System.in.read();
79         handler.dispose();
80     }
81
82     // @Test
83     void testCleanRegion() throws IOException, InterruptedException {
84         handler.initialize();
85
86         System.in.read();
87
88         ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
89         handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
90
91         System.in.read();
92         handler.dispose();
93     }
94
95     // @Test
96     void testDock() throws IOException, InterruptedException {
97         handler.initialize();
98
99         System.in.read();
100
101         ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
102         handler.handleCommand(cmd, new StringType("dock"));
103
104         System.in.read();
105         handler.dispose();
106     }
107
108     // @Test
109     void testStop() throws IOException, InterruptedException {
110         handler.initialize();
111
112         System.in.read();
113
114         ChannelUID cmd = new ChannelUID("my:thi:blabla:command");
115         handler.handleCommand(cmd, new StringType("stop"));
116
117         System.in.read();
118         handler.dispose();
119     }
120 }