2 * Copyright (c) 2010-2022 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.*;
18 import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.*;
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;
49 * The {@link RFXComRawHandler} is responsible for extra validation for Raw things.
51 * @author James Hewitt-Thomas - Initial contribution
53 @ExtendWith(MockitoExtension.class)
54 public class RFXComHandlerTest {
60 RFXComBridgeHandler bridgeHandler;
66 ThingHandlerCallback callback;
69 RFXComMessageFactory messageFactory;
72 RFXComMessage message;
75 ArgumentCaptor<ThingStatusInfo> thingStatusInfoCaptor;
78 ArgumentCaptor<RFXComGenericDeviceConfiguration> deviceConfigurationCaptor;
81 ArgumentCaptor<RFXComDeviceMessage> deviceMessageCaptor;
83 RFXComHandler handler;
85 private void initBridge() {
86 when(bridge.getHandler()).thenReturn(bridgeHandler);
87 when(thing.getBridgeUID()).thenReturn(bridgeUID);
88 when(callback.getBridge(bridgeUID)).thenReturn(bridge);
91 private void initOfflineBridge() {
94 when(bridge.getStatus()).thenReturn(ThingStatus.OFFLINE);
97 private void initOnlineBridge() {
100 when(bridge.getStatus()).thenReturn(ThingStatus.ONLINE);
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());
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),
116 return deviceConfigurationCaptor.getValue();
120 public void before() {
121 when(thing.getUID()).thenReturn(thingUID);
122 when(thing.getThingTypeUID()).thenReturn(thingTypeUID);
124 handler = new RFXComHandler(thing, messageFactory);
125 handler.setCallback(callback);
129 public void testValidConfig() {
131 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
133 handler.initialize();
134 verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
138 public void testInvalidConfig() {
140 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of()));
142 handler.initialize();
143 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
147 public void testOfflineBridge() {
149 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
151 handler.initialize();
152 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
156 public void testUnititialisedBridge() {
158 when(thing.getConfiguration())
159 .thenReturn(new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1")));
161 handler.initialize();
162 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
166 public void testWithoutBridge() {
167 when(thing.getConfiguration()).thenReturn(new Configuration(Map.of("deviceId", "1088338.11", "subType", "AC")));
169 handler.initialize();
170 verifyStatusUpdated(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
174 public void testConfigType() throws RFXComException {
176 when(thing.getConfiguration()).thenReturn(
177 new Configuration(Map.of("deviceId", "RAW", "subType", "RAW_PACKET1", "onPulses", "1 2 3 4")));
179 handler.initialize();
180 verifyStatusUpdated(ThingStatus.ONLINE, ThingStatusDetail.NONE);
181 RFXComDeviceConfiguration config = sendMessageToGetConfig("command", OnOffType.ON);
182 assertEquals(RFXComRawDeviceConfiguration.class, config.getClass());