]> git.basschouten.com Git - openhab-addons.git/blob
3b97c5ac1364ab16263aec612f8d4485c135dec9
[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.boschshc.internal.devices.intrusion;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
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;
33
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonParser;
36
37 /**
38  * Unit test for {@link IntrusionDetectionHandler}.
39  *
40  * @author David Pace - Initial contribution
41  *
42  */
43 @NonNullByDefault
44 class IntrusionDetectionHandlerTest extends AbstractBoschSHCHandlerTest<IntrusionDetectionHandler> {
45
46     private @Captor @NonNullByDefault({}) ArgumentCaptor<ArmActionRequest> armActionRequestCaptor;
47
48     @Override
49     protected IntrusionDetectionHandler createFixture() {
50         return new IntrusionDetectionHandler(getThing());
51     }
52
53     @Override
54     protected ThingTypeUID getThingTypeUID() {
55         return BoschSHCBindingConstants.THING_TYPE_INTRUSION_DETECTION_SYSTEM;
56     }
57
58     @Test
59     void testHandleCommandArmAction() throws InterruptedException, TimeoutException, ExecutionException {
60         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARM_ACTION),
61                 new StringType("0"));
62         verify(getBridgeHandler()).postAction(eq("intrusion/actions/arm"), armActionRequestCaptor.capture());
63         ArmActionRequest armRequest = armActionRequestCaptor.getValue();
64         assertEquals("0", armRequest.profileId);
65     }
66
67     @Test
68     void testHandleCommandDisarmAction() throws InterruptedException, TimeoutException, ExecutionException {
69         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_DISARM_ACTION),
70                 OnOffType.ON);
71         verify(getBridgeHandler()).postAction("intrusion/actions/disarm");
72     }
73
74     @Test
75     void testHandleCommandMuteAction() throws InterruptedException, TimeoutException, ExecutionException {
76         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_MUTE_ACTION),
77                 OnOffType.ON);
78         verify(getBridgeHandler()).postAction("intrusion/actions/mute");
79     }
80
81     @Test
82     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),
98                 OnOffType.ON);
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));
108     }
109
110     @Test
111     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"));
125     }
126
127     @Test
128     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"));
138     }
139 }