]> git.basschouten.com Git - openhab-addons.git/blob
6654440c7249be8deab6656beccacf664186ac46
[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.services.intrusion;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21 import java.util.function.Consumer;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
30 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
31 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
32 import org.openhab.binding.boschshc.internal.services.intrusion.dto.IntrusionDetectionControlState;
33
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonParser;
36
37 /**
38  * Unit tests for {@link IntrusionDetectionControlStateService}.
39  *
40  * @author David Pace - Initial contribution
41  *
42  */
43 @NonNullByDefault
44 @ExtendWith(MockitoExtension.class)
45 class IntrusionDetectionControlStateServiceTest {
46
47     private @NonNullByDefault({}) IntrusionDetectionControlStateService fixture;
48
49     private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
50
51     private @Mock @NonNullByDefault({}) Consumer<IntrusionDetectionControlState> consumer;
52
53     private @Mock @NonNullByDefault({}) IntrusionDetectionControlState testState;
54
55     @BeforeEach
56     void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
57         fixture = new IntrusionDetectionControlStateService();
58         fixture.initialize(bridgeHandler, BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION, consumer);
59     }
60
61     @Test
62     void getServiceName() {
63         assertEquals("IntrusionDetectionControl", fixture.getServiceName());
64     }
65
66     @Test
67     void getStateClass() {
68         assertSame(IntrusionDetectionControlState.class, fixture.getStateClass());
69     }
70
71     @Test
72     void getState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
73         when(bridgeHandler.getState(anyString(), anyString(), any())).thenReturn(testState);
74         IntrusionDetectionControlState state = fixture.getState();
75         verify(bridgeHandler).getState(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION,
76                 "IntrusionDetectionControl", IntrusionDetectionControlState.class);
77         assertSame(testState, state);
78     }
79
80     @Test
81     void setState() throws InterruptedException, TimeoutException, ExecutionException {
82         fixture.setState(testState);
83         verify(bridgeHandler).putState(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION,
84                 "IntrusionDetectionControl", testState);
85     }
86
87     @Test
88     void onStateUpdate() {
89         final String json = """
90                 {
91                     "@type":"intrusionDetectionControlState",
92                     "activeProfile":"0",
93                     "alarmActivationDelayTime":30,
94                     "actuators":[
95                         {
96                             "readonly":false,
97                             "active":true,
98                             "id":"intrusion:video"
99                         },
100                         {
101                             "readonly":false,
102                             "active":false,
103                             "id":"intrusion:siren"
104                         }
105                     ],
106                     "remainingTimeUntilArmed":28959,
107                     "armActivationDelayTime":30,
108                     "triggers":[
109                         {
110                             "readonly":false,
111                             "active":true,
112                             "id":"hdm:ZigBee:000d6f0422f42378"
113                         }
114                     ],
115                     "value":"SYSTEM_ARMING"
116                 }\
117                 """;
118         JsonElement jsonElement = JsonParser.parseString(json);
119         fixture.onStateUpdate(jsonElement);
120         verify(consumer).accept(any());
121     }
122
123     @Test
124     void refreshState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
125         when(bridgeHandler.getState(anyString(), anyString(), any())).thenReturn(testState);
126         fixture.refreshState();
127         verify(consumer).accept(testState);
128     }
129 }