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