]> git.basschouten.com Git - openhab-addons.git/blob
b5a21927b9ed0fb72ca0c77d762afa32042070fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.shuttercontrol;
14
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;
19
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
22
23 import javax.measure.quantity.Energy;
24 import javax.measure.quantity.Power;
25
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;
38
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
41
42 /**
43  * Unit tests for {@link ShutterControl2Handler}
44  * 
45  * @author David Pace - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 class ShutterControl2HandlerTest extends ShutterControlHandlerTest {
50
51     private @Captor @NonNullByDefault({}) ArgumentCaptor<QuantityType<Power>> powerCaptor;
52
53     private @Captor @NonNullByDefault({}) ArgumentCaptor<QuantityType<Energy>> energyCaptor;
54
55     private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildProtectionServiceState> childProtectionServiceStateCaptor;
56
57     @Override
58     protected ShutterControlHandler createFixture() {
59         return new ShutterControl2Handler(getThing());
60     }
61
62     @Override
63     protected ThingTypeUID getThingTypeUID() {
64         return BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL_2;
65     }
66
67     @Test
68     void testUpdateChannelsCommunicationQualityService() {
69         String json = """
70                 {
71                     "@type": "communicationQualityState",
72                     "quality": "UNKNOWN"
73                 }
74                 """;
75         JsonElement jsonObject = JsonParser.parseString(json);
76
77         getFixture().processUpdate("CommunicationQuality", jsonObject);
78         verify(getCallback()).stateUpdated(
79                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SIGNAL_STRENGTH),
80                 new DecimalType(0));
81
82         json = """
83                 {
84                     "@type": "communicationQualityState",
85                     "quality": "GOOD"
86                 }
87                 """;
88         jsonObject = JsonParser.parseString(json);
89
90         getFixture().processUpdate("CommunicationQuality", jsonObject);
91         verify(getCallback()).stateUpdated(
92                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SIGNAL_STRENGTH),
93                 new DecimalType(4));
94     }
95
96     @Test
97     void testUpdateChannelsChildProtectionService() {
98         String json = """
99                 {
100                     "@type": "ChildProtectionState",
101                     "childLockActive": true
102                 }
103                 """;
104         JsonElement jsonObject = JsonParser.parseString(json);
105
106         getFixture().processUpdate("ChildProtection", jsonObject);
107         verify(getCallback()).stateUpdated(
108                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_PROTECTION), OnOffType.ON);
109     }
110
111     @Test
112     void testUpdateChannelPowerMeterServiceState() {
113         JsonElement jsonObject = JsonParser.parseString("""
114                 {
115                   "@type": "powerMeterState",
116                   "powerConsumption": "23",
117                   "energyConsumption": 42
118                 }\
119                 """);
120         getFixture().processUpdate("PowerMeter", jsonObject);
121
122         verify(getCallback()).stateUpdated(eq(getChannelUID(BoschSHCBindingConstants.CHANNEL_POWER_CONSUMPTION)),
123                 powerCaptor.capture());
124         QuantityType<Power> powerValue = powerCaptor.getValue();
125         assertEquals(23, powerValue.intValue());
126
127         verify(getCallback()).stateUpdated(eq(getChannelUID(BoschSHCBindingConstants.CHANNEL_ENERGY_CONSUMPTION)),
128                 energyCaptor.capture());
129         QuantityType<Energy> energyValue = energyCaptor.getValue();
130         assertEquals(42, energyValue.intValue());
131     }
132
133     @Test
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);
142     }
143 }