]> git.basschouten.com Git - openhab-addons.git/blob
238f1a33fe6eb763bcc2df6ef9a07974fb25ffe8
[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.rfxcom.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Map;
20
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
29 import org.openhab.binding.rfxcom.internal.config.RFXComGenericDeviceConfiguration;
30 import org.openhab.binding.rfxcom.internal.config.RFXComRawDeviceConfiguration;
31 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
32 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
33 import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
34 import org.openhab.binding.rfxcom.internal.messages.RFXComMessage;
35 import org.openhab.binding.rfxcom.internal.messages.RFXComMessageFactory;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.library.types.OnOffType;
38 import org.openhab.core.thing.Bridge;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingStatusDetail;
43 import org.openhab.core.thing.ThingStatusInfo;
44 import org.openhab.core.thing.ThingTypeUID;
45 import org.openhab.core.thing.ThingUID;
46 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 import org.openhab.core.types.Command;
48
49 /**
50  * The {@link RFXComRawHandler} is responsible for extra validation for Raw things.
51  *
52  * @author James Hewitt-Thomas - Initial contribution
53  */
54 @ExtendWith(MockitoExtension.class)
55 public class RFXComHandlerTest {
56
57     static ThingUID bridgeUID = new ThingUID("rfxcom", "tcpbridge", "rfxtrx0");
58     static ThingUID thingUID = new ThingUID("rfxcom", bridgeUID, "mocked");
59     static ThingTypeUID thingTypeUID = new ThingTypeUID("rfxcom", "raw");
60
61     @Mock
62     Bridge bridge;
63
64     @Mock
65     RFXComBridgeHandler bridgeHandler;
66
67     @Mock
68     Thing thing;
69
70     @Mock
71     ThingHandlerCallback callback;
72
73     @Mock
74     RFXComMessageFactory messageFactory;
75
76     @Mock
77     RFXComMessage message;
78
79     @Captor
80     ArgumentCaptor<ThingStatusInfo> thingStatusInfoCaptor;
81
82     @Captor
83     ArgumentCaptor<RFXComGenericDeviceConfiguration> deviceConfigurationCaptor;
84
85     @Captor
86     ArgumentCaptor<RFXComDeviceMessage> deviceMessageCaptor;
87
88     RFXComHandler handler;
89
90     private void initBridge() {
91         when(bridge.getHandler()).thenReturn(bridgeHandler);
92         when(thing.getBridgeUID()).thenReturn(bridgeUID);
93         when(callback.getBridge(bridgeUID)).thenReturn(bridge);
94     }
95
96     private void initOfflineBridge() {
97         initBridge();
98
99         when(bridge.getStatus()).thenReturn(ThingStatus.OFFLINE);
100     }
101
102     private void initOnlineBridge() {
103         initBridge();
104
105         when(bridge.getStatus()).thenReturn(ThingStatus.ONLINE);
106     }
107
108     private void verifyStatusUpdated(ThingStatus status, ThingStatusDetail thingStatusDetail) {
109         verify(callback).statusUpdated(eq(thing), thingStatusInfoCaptor.capture());
110         ThingStatusInfo tsi = thingStatusInfoCaptor.getValue();
111         assertEquals(status, tsi.getStatus());
112         assertEquals(thingStatusDetail, tsi.getStatusDetail());
113     }
114
115     private RFXComGenericDeviceConfiguration sendMessageToGetConfig(String channel, Command command)
116             throws RFXComException {
117         when(messageFactory.createMessage(any(PacketType.class))).thenReturn(message);
118         ChannelUID cuid = new ChannelUID(thing.getUID(), channel);
119         handler.handleCommand(cuid, command);
120         verify(message).setConfig(deviceConfigurationCaptor.capture());
121         return deviceConfigurationCaptor.getValue();
122     }
123
124     @BeforeEach
125     public void before() {
126         when(thing.getUID()).thenReturn(thingUID);
127         when(thing.getThingTypeUID()).thenReturn(thingTypeUID);
128
129         handler = new RFXComHandler(thing, messageFactory);
130         handler.setCallback(callback);
131     }
132
133     @Test
134     public void testValidConfig() {
135         initOnlineBridge();
136         when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
137
138         handler.initialize();
139         verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
140     }
141
142     @Test
143     public void testInvalidConfig() {
144         initOnlineBridge();
145         when(thing.getConfiguration()).thenReturn(new Configuration(Map.of()));
146
147         handler.initialize();
148         verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
149     }
150
151     @Test
152     public void testOfflineBridge() {
153         initOfflineBridge();
154         when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
155
156         handler.initialize();
157         verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
158     }
159
160     @Test
161     public void testUnititialisedBridge() {
162         initBridge();
163         when(thing.getConfiguration())
164                 .thenReturn(new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1")));
165
166         handler.initialize();
167         verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
168     }
169
170     @Test
171     public void testWithoutBridge() {
172         when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
173
174         handler.initialize();
175         verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
176     }
177
178     @Test
179     public void testConfigType() throws RFXComException {
180         initOnlineBridge();
181         when(thing.getConfiguration()).thenReturn(
182                 new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1", "onPulses", "1 2 3 4")));
183
184         handler.initialize();
185         verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
186         RFXComDeviceConfiguration config = sendMessageToGetConfig("command", OnOffType.ON);
187         assertEquals(RFXComRawDeviceConfiguration.class, config.getClass());
188     }
189 }