]> git.basschouten.com Git - openhab-addons.git/blob
59eb414dc1644ca5ae5d6900b00795639800bc9d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.lang.reflect.Field;
19
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;
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         Logger logger = LoggerFactory.getLogger(RoombaHandler.class);
70         Field logLevelField = logger.getClass().getDeclaredField("currentLogLevel");
71         logLevelField.setAccessible(true);
72         logLevelField.set(logger, LocationAwareLogger.TRACE_INT);
73
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"));
81
82         callback = Mockito.mock(ThingHandlerCallback.class);
83
84         handler = new RoombaHandler(thing);
85         handler.setCallback(callback);
86     }
87
88     @Test
89     void testConfiguration() throws InterruptedException, IOException {
90         handler.initialize();
91
92         IRobotConfiguration config = thing.getConfiguration().as(IRobotConfiguration.class);
93         assertEquals(config.getIpAddress(), IP_ADDRESS);
94         assertEquals(config.getPassword(), PASSWORD);
95
96         handler.dispose();
97     }
98
99     @Test
100     void testCleanRegion() throws InterruptedException, IOException {
101         handler.initialize();
102
103         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
104         handler.handleCommand(cmd, new StringType("cleanRegions:AABBCCDDEEFFGGHH;2,3"));
105
106         handler.dispose();
107     }
108
109     @Test
110     void testDock() throws InterruptedException, IOException {
111         handler.initialize();
112
113         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
114         handler.handleCommand(cmd, new StringType("dock"));
115
116         handler.dispose();
117     }
118
119     @Test
120     void testStop() throws InterruptedException, IOException {
121         handler.initialize();
122
123         ChannelUID cmd = new ChannelUID(thing.getUID(), "command");
124         handler.handleCommand(cmd, new StringType("stop"));
125
126         handler.dispose();
127     }
128 }