]> git.basschouten.com Git - openhab-addons.git/blob
a04452bdf4b159e709d8a89ac157c031469000b4
[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;
14
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.openhab.binding.boschshc.internal.devices.smokedetector.SmokeDetectorHandler;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.binding.boschshc.internal.services.smokedetectorcheck.SmokeDetectorCheckState;
30 import org.openhab.binding.boschshc.internal.services.smokedetectorcheck.dto.SmokeDetectorCheckServiceState;
31 import org.openhab.core.library.types.PlayPauseType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.thing.ChannelUID;
34
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonParser;
37
38 /**
39  * Unit Tests for {@link SmokeDetectorHandler}.
40  *
41  * @author Gerd Zanker - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 public abstract class AbstractSmokeDetectorHandlerTest<T extends AbstractSmokeDetectorHandler>
46         extends AbstractBatteryPoweredDeviceHandlerTest<T> {
47
48     @Captor
49     private @NonNullByDefault({}) ArgumentCaptor<SmokeDetectorCheckServiceState> smokeDetectorCheckStateCaptor;
50
51     @Test
52     public void testHandleCommand()
53             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
54
55         // valid commands with valid thing & channel
56         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
57                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED.toString()));
58         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
59                 smokeDetectorCheckStateCaptor.capture());
60         SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
61         assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
62
63         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
64                 new StringType(SmokeDetectorCheckState.NONE.toString()));
65         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
66                 smokeDetectorCheckStateCaptor.capture());
67         state = smokeDetectorCheckStateCaptor.getValue();
68         assertSame(SmokeDetectorCheckState.NONE, state.value);
69
70         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
71                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_OK.toString()));
72         verify(getBridgeHandler(), times(3)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
73                 smokeDetectorCheckStateCaptor.capture());
74         state = smokeDetectorCheckStateCaptor.getValue();
75         assertSame(SmokeDetectorCheckState.SMOKE_TEST_OK, state.value);
76
77         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
78                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_FAILED.toString()));
79         verify(getBridgeHandler(), times(4)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
80                 smokeDetectorCheckStateCaptor.capture());
81         state = smokeDetectorCheckStateCaptor.getValue();
82         assertSame(SmokeDetectorCheckState.SMOKE_TEST_FAILED, state.value);
83     }
84
85     @Test
86     public void testHandleCommand_PlayPauseType()
87             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
88
89         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
90                 PlayPauseType.PLAY);
91         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
92                 smokeDetectorCheckStateCaptor.capture());
93         SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
94         assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
95     }
96
97     @Test
98     public void testUpdateChannel_SmokeDetectorCheckServiceState_none() {
99         JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"smokeDetectorCheckState\",\"value\":NONE}");
100         getFixture().processUpdate("SmokeDetectorCheck", jsonObject);
101         verify(getCallback()).stateUpdated(
102                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
103                 new StringType("NONE"));
104     }
105
106     @Test
107     public void testUpdateChannel_SmokeDetectorCheckServiceState_Requests() {
108         JsonElement jsonObject = JsonParser
109                 .parseString("{\"@type\":\"smokeDetectorCheckState\",\"value\":SMOKE_TEST_REQUESTED}");
110         getFixture().processUpdate("SmokeDetectorCheck", jsonObject);
111         verify(getCallback()).stateUpdated(
112                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
113                 new StringType("SMOKE_TEST_REQUESTED"));
114     }
115 }