]> git.basschouten.com Git - openhab-addons.git/blob
3a9510c60ecbf6f56f2e148a88e64120468e1860
[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.devices.camera;
14
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Captor;
26 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCDeviceHandlerTest;
27 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.binding.boschshc.internal.services.cameranotification.CameraNotificationState;
30 import org.openhab.binding.boschshc.internal.services.cameranotification.dto.CameraNotificationServiceState;
31 import org.openhab.binding.boschshc.internal.services.privacymode.PrivacyModeState;
32 import org.openhab.binding.boschshc.internal.services.privacymode.dto.PrivacyModeServiceState;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.ThingTypeUID;
36
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
39
40 /**
41  * Unit tests for {@link CameraHandler}.
42  *
43  * @author David Pace - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 class CameraHandlerTest extends AbstractBoschSHCDeviceHandlerTest<CameraHandler> {
48
49     private @Captor @NonNullByDefault({}) ArgumentCaptor<PrivacyModeServiceState> privacyModeServiceStateCaptor;
50
51     private @Captor @NonNullByDefault({}) ArgumentCaptor<CameraNotificationServiceState> cameraNotificationServiceStateCaptor;
52
53     @Override
54     protected CameraHandler createFixture() {
55         return new CameraHandler(getThing());
56     }
57
58     @Override
59     protected ThingTypeUID getThingTypeUID() {
60         return BoschSHCBindingConstants.THING_TYPE_CAMERA_360;
61     }
62
63     @Override
64     protected String getDeviceID() {
65         return "8e28ce2d-e7bf-3e3d-8e3a-a78de61b493e";
66     }
67
68     @Test
69     void testHandleCommandPrivacyMode()
70             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
71         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_PRIVACY_MODE),
72                 OnOffType.ON);
73         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("PrivacyMode"),
74                 privacyModeServiceStateCaptor.capture());
75         PrivacyModeServiceState state = privacyModeServiceStateCaptor.getValue();
76         assertSame(PrivacyModeState.ENABLED, state.value);
77
78         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_PRIVACY_MODE),
79                 OnOffType.OFF);
80         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("PrivacyMode"),
81                 privacyModeServiceStateCaptor.capture());
82         state = privacyModeServiceStateCaptor.getValue();
83         assertSame(PrivacyModeState.DISABLED, state.value);
84     }
85
86     @Test
87     void testHandleCommandCameraNotification()
88             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
89         getFixture().handleCommand(
90                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CAMERA_NOTIFICATION),
91                 OnOffType.ON);
92         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("CameraNotification"),
93                 cameraNotificationServiceStateCaptor.capture());
94         CameraNotificationServiceState state = cameraNotificationServiceStateCaptor.getValue();
95         assertSame(CameraNotificationState.ENABLED, state.value);
96
97         getFixture().handleCommand(
98                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CAMERA_NOTIFICATION),
99                 OnOffType.OFF);
100         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("CameraNotification"),
101                 cameraNotificationServiceStateCaptor.capture());
102         state = cameraNotificationServiceStateCaptor.getValue();
103         assertSame(CameraNotificationState.DISABLED, state.value);
104     }
105
106     @Test
107     void testUpdateChannelsPrivacyModeState() {
108         JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"privacyModeState\",\"value\":\"ENABLED\"}");
109         getFixture().processUpdate("PrivacyMode", jsonObject);
110         verify(getCallback()).stateUpdated(
111                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_PRIVACY_MODE), OnOffType.ON);
112
113         jsonObject = JsonParser.parseString("{\"@type\":\"privacyModeState\",\"value\":\"DISABLED\"}");
114         getFixture().processUpdate("PrivacyMode", jsonObject);
115         verify(getCallback()).stateUpdated(
116                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_PRIVACY_MODE), OnOffType.OFF);
117     }
118
119     @Test
120     void testUpdateChannelsCameraNotificationState() {
121         JsonElement jsonObject = JsonParser
122                 .parseString("{\"@type\":\"cameraNotificationState\",\"value\":\"ENABLED\"}");
123         getFixture().processUpdate("CameraNotification", jsonObject);
124         verify(getCallback()).stateUpdated(
125                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CAMERA_NOTIFICATION),
126                 OnOffType.ON);
127
128         jsonObject = JsonParser.parseString("{\"@type\":\"cameraNotificationState\",\"value\":\"DISABLED\"}");
129         getFixture().processUpdate("CameraNotification", jsonObject);
130         verify(getCallback()).stateUpdated(
131                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CAMERA_NOTIFICATION),
132                 OnOffType.OFF);
133     }
134 }