]> git.basschouten.com Git - openhab-addons.git/blob
f58d921ac5f5225f886b2b489682a6e2df207b05
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.tplinksmarthome.internal.handler;
14
15 import static org.junit.Assert.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18 import static org.mockito.MockitoAnnotations.initMocks;
19 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_SWITCH;
20 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
21
22 import java.io.IOException;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants;
32 import org.openhab.binding.tplinksmarthome.internal.Commands;
33 import org.openhab.binding.tplinksmarthome.internal.Connection;
34 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeConfiguration;
35 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeDiscoveryService;
36 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType;
37 import org.openhab.binding.tplinksmarthome.internal.device.SmartHomeDevice;
38 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
39 import org.openhab.core.config.core.Configuration;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.library.types.QuantityType;
42 import org.openhab.core.thing.ChannelUID;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingStatus;
45 import org.openhab.core.thing.ThingStatusInfo;
46 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 import org.openhab.core.types.RefreshType;
48 import org.openhab.core.types.State;
49
50 /**
51  * Tests cases for {@link SmartHomeHandler} class.
52  *
53  * @author Hilbrand Bouwkamp - Initial contribution
54  */
55 @NonNullByDefault
56 public class SmartHomeHandlerTest {
57
58     private @NonNullByDefault({}) SmartHomeHandler handler;
59
60     @Mock
61     private @NonNullByDefault({}) Connection connection;
62     @Mock
63     private @NonNullByDefault({}) ThingHandlerCallback callback;
64     @Mock
65     private @NonNullByDefault({}) Thing thing;
66     @Mock
67     private @NonNullByDefault({}) SmartHomeDevice smartHomeDevice;
68     @Mock
69     private @NonNullByDefault({}) TPLinkSmartHomeDiscoveryService discoveryService;
70
71     private final Configuration configuration = new Configuration();
72
73     @Before
74     public void setUp() throws IOException {
75         initMocks(this);
76         configuration.put(CONFIG_IP, "localhost");
77         configuration.put(CONFIG_REFRESH, 1);
78         when(thing.getConfiguration()).thenReturn(configuration);
79         when(smartHomeDevice.getUpdateCommand()).thenReturn(Commands.getSysinfo());
80         when(connection.sendCommand(Commands.getSysinfo()))
81                 .thenReturn(ModelTestUtil.readJson("plug_get_sysinfo_response"));
82         handler = new SmartHomeHandler(thing, smartHomeDevice, TPLinkSmartHomeThingType.HS100, discoveryService) {
83             @Override
84             Connection createConnection(TPLinkSmartHomeConfiguration config) {
85                 return connection;
86             }
87         };
88         when(smartHomeDevice.handleCommand(eq(CHANNEL_UID_SWITCH), any())).thenReturn(true);
89         when(callback.isChannelLinked(any())).thenReturn(true);
90         handler.setCallback(callback);
91     }
92
93     @After
94     public void after() {
95         handler.dispose();
96     }
97
98     @Test
99     public void testInitializeShouldCallTheCallback() throws InterruptedException {
100         handler.initialize();
101         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
102
103         verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
104         ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
105         assertEquals("Device should be unknown", ThingStatus.UNKNOWN, thingStatusInfo.getStatus());
106     }
107
108     @Test
109     public void testHandleCommandRefreshType() {
110         handler.initialize();
111         assertHandleCommandRefreshType(-53);
112     }
113
114     @Test
115     public void testHandleCommandRefreshTypeRangeExtender() throws IOException {
116         when(connection.sendCommand(Commands.getSysinfo()))
117                 .thenReturn(ModelTestUtil.readJson("rangeextender_get_sysinfo_response"));
118         handler.initialize();
119         assertHandleCommandRefreshType(-70);
120     }
121
122     private void assertHandleCommandRefreshType(int expectedRssi) {
123         handler.initialize();
124         ChannelUID channelUID = ChannelUIDConstants.CHANNEL_UID_RSSI;
125         handler.handleCommand(channelUID, RefreshType.REFRESH);
126         ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
127         verify(callback).stateUpdated(eq(channelUID), stateCaptor.capture());
128         assertEquals("State of RSSI channel should be set", new QuantityType<>(expectedRssi + " dBm"),
129                 stateCaptor.getValue());
130     }
131
132     @Test
133     public void testHandleCommandOther() throws InterruptedException {
134         handler.initialize();
135         ChannelUID channelUID = ChannelUIDConstants.CHANNEL_UID_SWITCH;
136         Mockito.doReturn(OnOffType.ON).when(smartHomeDevice).updateChannel(eq(channelUID), any());
137         handler.handleCommand(channelUID, RefreshType.REFRESH);
138         ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
139         verify(callback).stateUpdated(eq(channelUID), stateCaptor.capture());
140         assertSame("State of channel switch should be set", OnOffType.ON, stateCaptor.getValue());
141     }
142
143     @Test
144     public void testRefreshChannels() {
145         handler.initialize();
146         handler.refreshChannels();
147     }
148 }