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