2 * Copyright (c) 2010-2023 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.boschshc.internal.devices.intrusion;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCHandlerTest;
27 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
28 import org.openhab.binding.boschshc.internal.services.intrusion.actions.arm.dto.ArmActionRequest;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.ThingTypeUID;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonParser;
38 * Unit test for {@link IntrusionDetectionHandler}.
40 * @author David Pace - Initial contribution
44 public class IntrusionDetectionHandlerTest extends AbstractBoschSHCHandlerTest<IntrusionDetectionHandler> {
46 private @Captor @NonNullByDefault({}) ArgumentCaptor<ArmActionRequest> armActionRequestCaptor;
49 protected IntrusionDetectionHandler createFixture() {
50 return new IntrusionDetectionHandler(getThing());
54 protected ThingTypeUID getThingTypeUID() {
55 return BoschSHCBindingConstants.THING_TYPE_INTRUSION_DETECTION_SYSTEM;
59 public void testHandleCommandArmAction() throws InterruptedException, TimeoutException, ExecutionException {
60 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARM_ACTION),
62 verify(getBridgeHandler()).postAction(eq("intrusion/actions/arm"), armActionRequestCaptor.capture());
63 ArmActionRequest armRequest = armActionRequestCaptor.getValue();
64 assertEquals("0", armRequest.profileId);
68 public void testHandleCommandDisarmAction() throws InterruptedException, TimeoutException, ExecutionException {
69 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_DISARM_ACTION),
71 verify(getBridgeHandler()).postAction("intrusion/actions/disarm");
75 public void testHandleCommandMuteAction() throws InterruptedException, TimeoutException, ExecutionException {
76 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_MUTE_ACTION),
78 verify(getBridgeHandler()).postAction("intrusion/actions/mute");
82 public void testUpdateChannelsIntrusionDetectionSystemState() {
83 JsonElement jsonObject = JsonParser.parseString("{\n" + " \"@type\": \"systemState\",\n"
84 + " \"systemAvailability\": {\n" + " \"@type\": \"systemAvailabilityState\",\n"
85 + " \"available\": true,\n" + " \"deleted\": false\n" + " },\n"
86 + " \"armingState\": {\n" + " \"@type\": \"armingState\",\n"
87 + " \"state\": \"SYSTEM_DISARMED\",\n" + " \"deleted\": false\n" + " },\n"
88 + " \"alarmState\": {\n" + " \"@type\": \"alarmState\",\n"
89 + " \"value\": \"ALARM_OFF\",\n" + " \"incidents\": [],\n"
90 + " \"deleted\": false\n" + " },\n" + " \"activeConfigurationProfile\": {\n"
91 + " \"@type\": \"activeConfigurationProfile\",\n" + " \"deleted\": false\n"
92 + " },\n" + " \"securityGapState\": {\n" + " \"@type\": \"securityGapState\",\n"
93 + " \"securityGaps\": [],\n" + " \"deleted\": false\n" + " },\n"
94 + " \"deleted\": false\n" + " }\n");
95 getFixture().processUpdate(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION, jsonObject);
96 verify(getCallback()).stateUpdated(
97 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SYSTEM_AVAILABILITY),
99 verify(getCallback()).stateUpdated(
100 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARMING_STATE),
101 new StringType("SYSTEM_DISARMED"));
102 verify(getCallback()).stateUpdated(
103 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ALARM_STATE),
104 new StringType("ALARM_OFF"));
105 verify(getCallback()).stateUpdated(
106 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ACTIVE_CONFIGURATION_PROFILE),
107 new StringType(null));
111 public void testUpdateChannelsIntrusionDetectionControlState() {
112 JsonElement jsonObject = JsonParser.parseString("{\n" + " \"@type\": \"intrusionDetectionControlState\",\n"
113 + " \"activeProfile\": \"0\",\n" + " \"alarmActivationDelayTime\": 30,\n" + " \"actuators\": [\n"
114 + " {\n" + " \"readonly\": false,\n" + " \"active\": true,\n"
115 + " \"id\": \"intrusion:video\"\n" + " },\n" + " {\n" + " \"readonly\": false,\n"
116 + " \"active\": false,\n" + " \"id\": \"intrusion:siren\"\n" + " }\n" + " ],\n"
117 + " \"remainingTimeUntilArmed\": 29559,\n" + " \"armActivationDelayTime\": 30,\n"
118 + " \"triggers\": [\n" + " {\n" + " \"readonly\": false,\n" + " \"active\": true,\n"
119 + " \"id\": \"hdm:ZigBee:000d6f0012f02378\"\n" + " }\n" + " ],\n"
120 + " \"value\": \"SYSTEM_ARMING\"\n" + " }");
121 getFixture().processUpdate("IntrusionDetectionControl", jsonObject);
122 verify(getCallback()).stateUpdated(
123 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARMING_STATE),
124 new StringType("SYSTEM_ARMING"));
128 public void testUpdateChannelsSurveillanceAlarmState() {
129 JsonElement jsonObject = JsonParser.parseString("{\n" + " \"@type\": \"surveillanceAlarmState\",\n"
130 + " \"incidents\": [\n" + " {\n" + " \"triggerName\": \"Motion Detector\",\n"
131 + " \"locationId\": \"hz_5\",\n" + " \"location\": \"Living Room\",\n"
132 + " \"id\": \"hdm:ZigBee:000d6f0012f02342\",\n" + " \"time\": 1652615755336,\n"
133 + " \"type\": \"INTRUSION\"\n" + " }\n" + " ],\n" + " \"value\": \"ALARM_ON\"\n" + " }");
134 getFixture().processUpdate("SurveillanceAlarm", jsonObject);
135 verify(getCallback()).stateUpdated(
136 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ALARM_STATE),
137 new StringType("ALARM_ON"));