2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.services.intrusion;
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;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeoutException;
24 import java.util.function.Consumer;
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;
36 import com.google.gson.JsonElement;
37 import com.google.gson.JsonParser;
40 * Unit tests for {@link IntrusionDetectionControlStateService}.
42 * @author David Pace - Initial contribution
45 @ExtendWith(MockitoExtension.class)
46 class IntrusionDetectionControlStateServiceTest {
48 private IntrusionDetectionControlStateService fixture;
51 private BridgeHandler bridgeHandler;
54 private Consumer<IntrusionDetectionControlState> consumer;
57 private IntrusionDetectionControlState testState;
60 void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
61 fixture = new IntrusionDetectionControlStateService();
62 fixture.initialize(bridgeHandler, BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION, consumer);
66 void getServiceName() {
67 assertEquals("IntrusionDetectionControl", fixture.getServiceName());
71 void getStateClass() {
72 assertSame(IntrusionDetectionControlState.class, fixture.getStateClass());
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);
85 void setState() throws InterruptedException, TimeoutException, ExecutionException {
86 fixture.setState(testState);
87 verify(bridgeHandler).putState(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION,
88 "IntrusionDetectionControl", testState);
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());
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);