2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rfxcom.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
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;
50 * The {@link RFXComRawHandler} is responsible for extra validation for Raw things.
52 * @author James Hewitt-Thomas - Initial contribution
54 @ExtendWith(MockitoExtension.class)
55 public class RFXComHandlerTest {
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");
65 RFXComBridgeHandler bridgeHandler;
71 ThingHandlerCallback callback;
74 RFXComMessageFactory messageFactory;
77 RFXComMessage message;
80 ArgumentCaptor<ThingStatusInfo> thingStatusInfoCaptor;
83 ArgumentCaptor<RFXComGenericDeviceConfiguration> deviceConfigurationCaptor;
86 ArgumentCaptor<RFXComDeviceMessage> deviceMessageCaptor;
88 RFXComHandler handler;
90 private void initBridge() {
91 when(bridge.getHandler()).thenReturn(bridgeHandler);
92 when(thing.getBridgeUID()).thenReturn(bridgeUID);
93 when(callback.getBridge(bridgeUID)).thenReturn(bridge);
96 private void initOfflineBridge() {
99 when(bridge.getStatus()).thenReturn(ThingStatus.OFFLINE);
102 private void initOnlineBridge() {
105 when(bridge.getStatus()).thenReturn(ThingStatus.ONLINE);
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());
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();
125 public void before() {
126 when(thing.getUID()).thenReturn(thingUID);
127 when(thing.getThingTypeUID()).thenReturn(thingTypeUID);
129 handler = new RFXComHandler(thing, messageFactory);
130 handler.setCallback(callback);
134 public void testValidConfig() {
136 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
138 handler.initialize();
139 verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
143 public void testInvalidConfig() {
145 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of()));
147 handler.initialize();
148 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
152 public void testOfflineBridge() {
154 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
156 handler.initialize();
157 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
161 public void testUnititialisedBridge() {
163 when(thing.getConfiguration())
164 .thenReturn(new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1")));
166 handler.initialize();
167 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
171 public void testWithoutBridge() {
172 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
174 handler.initialize();
175 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
179 public void testConfigType() throws RFXComException {
181 when(thing.getConfiguration()).thenReturn(
182 new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1", "onPulses", "1 2 3 4")));
184 handler.initialize();
185 verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
186 RFXComDeviceConfiguration config = sendMessageToGetConfig("command", OnOffType.ON);
187 assertEquals(RFXComRawDeviceConfiguration.class, config.getClass());