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.devices;
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;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
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;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonParser;
39 * Unit Tests for {@link SmokeDetectorHandler}.
41 * @author Gerd Zanker - Initial contribution
45 public abstract class AbstractSmokeDetectorHandlerTest<T extends AbstractSmokeDetectorHandler>
46 extends AbstractBatteryPoweredDeviceHandlerTest<T> {
49 private @NonNullByDefault({}) ArgumentCaptor<SmokeDetectorCheckServiceState> smokeDetectorCheckStateCaptor;
52 public void testHandleCommand()
53 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
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);
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);
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);
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);
86 public void testHandleCommand_PlayPauseType()
87 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
89 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
91 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
92 smokeDetectorCheckStateCaptor.capture());
93 SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
94 assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
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"));
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"));