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.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21 import java.util.function.Consumer;
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;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonParser;
38 * Unit tests for {@link IntrusionDetectionControlStateService}.
40 * @author David Pace - Initial contribution
44 @ExtendWith(MockitoExtension.class)
45 class IntrusionDetectionControlStateServiceTest {
47 private @NonNullByDefault({}) IntrusionDetectionControlStateService fixture;
49 private @Mock @NonNullByDefault({}) BridgeHandler bridgeHandler;
51 private @Mock @NonNullByDefault({}) Consumer<IntrusionDetectionControlState> consumer;
53 private @Mock @NonNullByDefault({}) IntrusionDetectionControlState testState;
56 void beforeEach() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
57 fixture = new IntrusionDetectionControlStateService();
58 fixture.initialize(bridgeHandler, BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION, consumer);
62 void getServiceName() {
63 assertEquals("IntrusionDetectionControl", fixture.getServiceName());
67 void getStateClass() {
68 assertSame(IntrusionDetectionControlState.class, fixture.getStateClass());
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);
81 void setState() throws InterruptedException, TimeoutException, ExecutionException {
82 fixture.setState(testState);
83 verify(bridgeHandler).putState(BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION,
84 "IntrusionDetectionControl", testState);
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());
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);