]> git.basschouten.com Git - openhab-addons.git/blob
76898ffaeeb2f459b480ec10fa571e566d286bd3
[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.*;
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.smokedetector.SmokeDetectorHandler;
27 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
28 import org.openhab.binding.boschshc.internal.services.smokedetectorcheck.SmokeDetectorCheckState;
29 import org.openhab.binding.boschshc.internal.services.smokedetectorcheck.dto.SmokeDetectorCheckServiceState;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PlayPauseType;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingStatusInfo;
37 import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
38
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
41
42 /**
43  * Unit Tests for {@link SmokeDetectorHandler}.
44  *
45  * @author Gerd Zanker - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 public abstract class AbstractSmokeDetectorHandlerTest<T extends AbstractSmokeDetectorHandler>
50         extends AbstractBatteryPoweredDeviceHandlerTest<T> {
51
52     @Captor
53     private @NonNullByDefault({}) ArgumentCaptor<SmokeDetectorCheckServiceState> smokeDetectorCheckStateCaptor;
54
55     @Test
56     public void testHandleCommand()
57             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
58         // valid commands with valid thing & channel
59         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
60                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED.toString()));
61         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
62                 smokeDetectorCheckStateCaptor.capture());
63         SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
64         assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
65
66         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
67                 new StringType(SmokeDetectorCheckState.NONE.toString()));
68         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
69                 smokeDetectorCheckStateCaptor.capture());
70         state = smokeDetectorCheckStateCaptor.getValue();
71         assertSame(SmokeDetectorCheckState.NONE, state.value);
72
73         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
74                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_OK.toString()));
75         verify(getBridgeHandler(), times(3)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
76                 smokeDetectorCheckStateCaptor.capture());
77         state = smokeDetectorCheckStateCaptor.getValue();
78         assertSame(SmokeDetectorCheckState.SMOKE_TEST_OK, state.value);
79
80         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
81                 new StringType(SmokeDetectorCheckState.SMOKE_TEST_FAILED.toString()));
82         verify(getBridgeHandler(), times(4)).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
83                 smokeDetectorCheckStateCaptor.capture());
84         state = smokeDetectorCheckStateCaptor.getValue();
85         assertSame(SmokeDetectorCheckState.SMOKE_TEST_FAILED, state.value);
86     }
87
88     @Test
89     public void testHandleCommandPlayPauseType()
90             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
91         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
92                 PlayPauseType.PLAY);
93         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
94                 smokeDetectorCheckStateCaptor.capture());
95         SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
96         assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
97     }
98
99     @Test
100     public void testHandleCommandUnknownCommand()
101             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
102         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
103                 OnOffType.ON);
104         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
105                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
106                 .withDescription(
107                         "Error when service SmokeDetectorCheck should handle command org.openhab.core.library.types.OnOffType: SmokeDetectorCheck: Can not handle command org.openhab.core.library.types.OnOffType")
108                 .build();
109         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
110     }
111
112     @Test
113     public void testUpdateChannelSmokeDetectorCheckServiceStateNone() {
114         JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"smokeDetectorCheckState\",\"value\":NONE}");
115         getFixture().processUpdate("SmokeDetectorCheck", jsonObject);
116         verify(getCallback()).stateUpdated(
117                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
118                 new StringType("NONE"));
119     }
120
121     @Test
122     public void testUpdateChannelSmokeDetectorCheckServiceStateRequests() {
123         JsonElement jsonObject = JsonParser
124                 .parseString("{\"@type\":\"smokeDetectorCheckState\",\"value\":SMOKE_TEST_REQUESTED}");
125         getFixture().processUpdate("SmokeDetectorCheck", jsonObject);
126         verify(getCallback()).stateUpdated(
127                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
128                 new StringType("SMOKE_TEST_REQUESTED"));
129     }
130 }