]> git.basschouten.com Git - openhab-addons.git/blob
1e318efc7c48b9ec5d77265cd650ac410d1895d1
[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("""
84                 {
85                      "@type": "systemState",
86                      "systemAvailability": {
87                          "@type": "systemAvailabilityState",
88                          "available": true,
89                          "deleted": false
90                      },
91                      "armingState": {
92                          "@type": "armingState",
93                          "state": "SYSTEM_DISARMED",
94                          "deleted": false
95                      },
96                      "alarmState": {
97                          "@type": "alarmState",
98                          "value": "ALARM_OFF",
99                          "incidents": [],
100                          "deleted": false
101                      },
102                      "activeConfigurationProfile": {
103                          "@type": "activeConfigurationProfile",
104                          "deleted": false
105                      },
106                      "securityGapState": {
107                          "@type": "securityGapState",
108                          "securityGaps": [],
109                          "deleted": false
110                      },
111                      "deleted": false
112                  }
113                 """);
114         getFixture().processUpdate(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION, jsonObject);
115         verify(getCallback()).stateUpdated(
116                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SYSTEM_AVAILABILITY),
117                 OnOffType.ON);
118         verify(getCallback()).stateUpdated(
119                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARMING_STATE),
120                 new StringType("SYSTEM_DISARMED"));
121         verify(getCallback()).stateUpdated(
122                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ALARM_STATE),
123                 new StringType("ALARM_OFF"));
124         verify(getCallback()).stateUpdated(
125                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ACTIVE_CONFIGURATION_PROFILE),
126                 new StringType(null));
127     }
128
129     @Test
130     void testUpdateChannelsIntrusionDetectionControlState() {
131         JsonElement jsonObject = JsonParser.parseString("""
132                 {
133                    "@type": "intrusionDetectionControlState",
134                    "activeProfile": "0",
135                    "alarmActivationDelayTime": 30,
136                    "actuators": [
137                      {
138                        "readonly": false,
139                        "active": true,
140                        "id": "intrusion:video"
141                      },
142                      {
143                        "readonly": false,
144                        "active": false,
145                        "id": "intrusion:siren"
146                      }
147                    ],
148                    "remainingTimeUntilArmed": 29559,
149                    "armActivationDelayTime": 30,
150                    "triggers": [
151                      {
152                        "readonly": false,
153                        "active": true,
154                        "id": "hdm:ZigBee:000d6f0012f02378"
155                      }
156                    ],
157                    "value": "SYSTEM_ARMING"
158                  }\
159                 """);
160         getFixture().processUpdate("IntrusionDetectionControl", jsonObject);
161         verify(getCallback()).stateUpdated(
162                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ARMING_STATE),
163                 new StringType("SYSTEM_ARMING"));
164         verify(getCallback()).stateUpdated(
165                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ACTIVE_CONFIGURATION_PROFILE),
166                 new StringType("0"));
167     }
168
169     @Test
170     void testUpdateChannelsSurveillanceAlarmState() {
171         JsonElement jsonObject = JsonParser.parseString("""
172                 {
173                    "@type": "surveillanceAlarmState",
174                    "incidents": [
175                      {
176                        "triggerName": "Motion Detector",
177                        "locationId": "hz_5",
178                        "location": "Living Room",
179                        "id": "hdm:ZigBee:000d6f0012f02342",
180                        "time": 1652615755336,
181                        "type": "INTRUSION"
182                      }
183                    ],
184                    "value": "ALARM_ON"
185                  }\
186                 """);
187         getFixture().processUpdate("SurveillanceAlarm", jsonObject);
188         verify(getCallback()).stateUpdated(
189                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_ALARM_STATE),
190                 new StringType("ALARM_ON"));
191     }
192 }