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 = """
91 "@type":"intrusionDetectionControlState",
93 "alarmActivationDelayTime":30,
98 "id":"intrusion:video"
103 "id":"intrusion:siren"
106 "remainingTimeUntilArmed":28959,
107 "armActivationDelayTime":30,
112 "id":"hdm:ZigBee:000d6f0422f42378"
115 "value":"SYSTEM_ARMING"
118 JsonElement jsonElement = JsonParser.parseString(json);
119 fixture.onStateUpdate(jsonElement);
120 verify(consumer).accept(any());
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);