2 * Copyright (c) 2010-2024 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.shuttercontrol;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.verify;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
23 import javax.measure.quantity.Energy;
24 import javax.measure.quantity.Power;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Captor;
30 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
31 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
32 import org.openhab.binding.boschshc.internal.services.childprotection.dto.ChildProtectionServiceState;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.QuantityType;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.ThingTypeUID;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
43 * Unit tests for {@link ShutterControl2Handler}
45 * @author David Pace - Initial contribution
49 class ShutterControl2HandlerTest extends ShutterControlHandlerTest {
51 private @Captor @NonNullByDefault({}) ArgumentCaptor<QuantityType<Power>> powerCaptor;
53 private @Captor @NonNullByDefault({}) ArgumentCaptor<QuantityType<Energy>> energyCaptor;
55 private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildProtectionServiceState> childProtectionServiceStateCaptor;
58 protected ShutterControlHandler createFixture() {
59 return new ShutterControl2Handler(getThing());
63 protected ThingTypeUID getThingTypeUID() {
64 return BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL_2;
68 void testUpdateChannelsCommunicationQualityService() {
71 "@type": "communicationQualityState",
75 JsonElement jsonObject = JsonParser.parseString(json);
77 getFixture().processUpdate("CommunicationQuality", jsonObject);
78 verify(getCallback()).stateUpdated(
79 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SIGNAL_STRENGTH),
84 "@type": "communicationQualityState",
88 jsonObject = JsonParser.parseString(json);
90 getFixture().processUpdate("CommunicationQuality", jsonObject);
91 verify(getCallback()).stateUpdated(
92 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SIGNAL_STRENGTH),
97 void testUpdateChannelsChildProtectionService() {
100 "@type": "ChildProtectionState",
101 "childLockActive": true
104 JsonElement jsonObject = JsonParser.parseString(json);
106 getFixture().processUpdate("ChildProtection", jsonObject);
107 verify(getCallback()).stateUpdated(
108 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_PROTECTION), OnOffType.ON);
112 void testUpdateChannelPowerMeterServiceState() {
113 JsonElement jsonObject = JsonParser.parseString("""
115 "@type": "powerMeterState",
116 "powerConsumption": "23",
117 "energyConsumption": 42
120 getFixture().processUpdate("PowerMeter", jsonObject);
122 verify(getCallback()).stateUpdated(eq(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_CONSUMPTION)),
123 powerCaptor.capture());
124 QuantityType<Power> powerValue = powerCaptor.getValue();
125 assertEquals(23, powerValue.intValue());
127 verify(getCallback()).stateUpdated(eq(getChannelUID(BoschSHCBindingConstants.CHANNEL_ENERGY_CONSUMPTION)),
128 energyCaptor.capture());
129 QuantityType<Energy> energyValue = energyCaptor.getValue();
130 assertEquals(42, energyValue.intValue());
134 void testHandleCommandChildProtection()
135 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
136 getFixture().handleCommand(
137 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_PROTECTION), OnOffType.ON);
138 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ChildProtection"),
139 childProtectionServiceStateCaptor.capture());
140 ChildProtectionServiceState state = childProtectionServiceStateCaptor.getValue();
141 assertTrue(state.childLockActive);