]> git.basschouten.com Git - openhab-addons.git/blob
e24af83368d8360cfd623ec72237c6eac993df5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 static org.junit.jupiter.api.Assertions.assertEquals;
16
17 import java.io.IOException;
18
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;
41
42 import ch.qos.logback.classic.Level;
43 import ch.qos.logback.classic.Logger;
44
45 /**
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.
48  *
49  * @author Florian Binder - Initial contribution
50  */
51
52 @NonNullByDefault
53 @ExtendWith(MockitoExtension.class)
54 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
55 public class RoombaHandlerTest {
56
57     private static final String IP_ADDRESS = "<iRobotIP>";
58     private static final String PASSWORD = "<PasswordForIRobot>";
59
60     @Nullable
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");
64     @Nullable
65     private ThingHandlerCallback callback;
66
67     @BeforeEach
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"));
78
79         callback = Mockito.mock(ThingHandlerCallback.class);
80
81         handler = new RoombaHandler(thing);
82         handler.setCallback(callback);
83     }
84
85     @Test
86     void testConfiguration() throws InterruptedException, IOException {
87         handler.initialize();
88
89         IRobotConfiguration config = thing.getConfiguration().as(IRobotConfiguration.class);
90         assertEquals(config.getIpAddress(), IP_ADDRESS);
91         assertEquals(config.getPassword(), PASSWORD);
92
93         handler.dispose();
94     }
95
96     @Test
97     void testCleanRegion() throws InterruptedException, IOException {
98         handler.initialize();
99
100         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
101         handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
102
103         handler.dispose();
104     }
105
106     @Test
107     void testDock() throws InterruptedException, IOException {
108         handler.initialize();
109
110         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
111         handler.handleCommand(cmd, new StringType("dock"));
112
113         handler.dispose();
114     }
115
116     @Test
117     void testStop() throws InterruptedException, IOException {
118         handler.initialize();
119
120         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
121         handler.handleCommand(cmd, new StringType("stop"));
122
123         handler.dispose();
124     }
125 }