]> git.basschouten.com Git - openhab-addons.git/blob
3f4fe0c8334e79d956b08aecbfac11aece8a9a3e
[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 = "{\n" + "\"@type\": \"intrusionDetectionControlState\",\n" + "\"activeProfile\": \"0\",\n"
90                 + "\"alarmActivationDelayTime\": 30,\n" + "\"actuators\": [\n" + "{\n" + "\"readonly\": false,\n"
91                 + "\"active\": true,\n" + "\"id\": \"intrusion:video\"\n" + "},\n" + "{\n" + "\"readonly\": false,\n"
92                 + "\"active\": false,\n" + "\"id\": \"intrusion:siren\"\n" + "}\n" + "],\n"
93                 + "\"remainingTimeUntilArmed\": 28959,\n" + "\"armActivationDelayTime\": 30,\n" + "\"triggers\": [\n"
94                 + "{\n" + "\"readonly\": false,\n" + "\"active\": true,\n" + "\"id\": \"hdm:ZigBee:000d6f0422f42378\"\n"
95                 + "}\n" + "],\n" + "\"value\": \"SYSTEM_ARMING\"\n" + "}";
96         JsonElement jsonElement = JsonParser.parseString(json);
97         fixture.onStateUpdate(jsonElement);
98         verify(consumer).accept(any());
99     }
100
101     @Test
102     void refreshState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
103         when(bridgeHandler.getState(anyString(), anyString(), any())).thenReturn(testState);
104         fixture.refreshState();
105         verify(consumer).accept(testState);
106     }
107 }