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.*;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
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;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
43 * Unit Tests for {@link SmokeDetectorHandler}.
45 * @author Gerd Zanker - Initial contribution
49 public abstract class AbstractSmokeDetectorHandlerTest<T extends AbstractSmokeDetectorHandler>
50 extends AbstractBatteryPoweredDeviceHandlerTest<T> {
53 private @NonNullByDefault({}) ArgumentCaptor<SmokeDetectorCheckServiceState> smokeDetectorCheckStateCaptor;
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);
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);
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);
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);
89 public void testHandleCommandPlayPauseType()
90 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
91 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
93 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SmokeDetectorCheck"),
94 smokeDetectorCheckStateCaptor.capture());
95 SmokeDetectorCheckServiceState state = smokeDetectorCheckStateCaptor.getValue();
96 assertSame(SmokeDetectorCheckState.SMOKE_TEST_REQUESTED, state.value);
100 public void testHandleCommandUnknownCommand()
101 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
102 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SMOKE_CHECK),
104 ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
105 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
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")
109 verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
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"));
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"));