]> git.basschouten.com Git - openhab-addons.git/blob
e3a9a5a07503055fa314d3e44627a7e1350ccbbc
[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.shuttercontrol;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
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.AbstractBoschSHCDeviceHandlerTest;
27 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
28 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
29 import org.openhab.binding.boschshc.internal.services.shuttercontrol.OperationState;
30 import org.openhab.binding.boschshc.internal.services.shuttercontrol.dto.ShutterControlServiceState;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.library.types.StopMoveType;
33 import org.openhab.core.library.types.UpDownType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.ThingTypeUID;
36
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
39
40 /**
41  * Unit tests for {@link ShutterControlHandler}.
42  *
43  * @author David Pace - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 public class ShutterControlHandlerTest extends AbstractBoschSHCDeviceHandlerTest<ShutterControlHandler> {
48
49     private @Captor @NonNullByDefault({}) ArgumentCaptor<ShutterControlServiceState> shutterControlServiceStateCaptor;
50
51     @Override
52     protected String getDeviceID() {
53         return "hdm:ZigBee:abcd6fc012ad25b1";
54     }
55
56     @Override
57     protected ShutterControlHandler createFixture() {
58         return new ShutterControlHandler(getThing());
59     }
60
61     @Override
62     protected ThingTypeUID getThingTypeUID() {
63         return BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL;
64     }
65
66     @Test
67     public void testHandleCommandUpDownType()
68             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
69         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
70                 UpDownType.UP);
71         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
72                 shutterControlServiceStateCaptor.capture());
73         ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
74         assertEquals(1d, state.level);
75
76         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
77                 UpDownType.DOWN);
78         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("ShutterControl"),
79                 shutterControlServiceStateCaptor.capture());
80         state = shutterControlServiceStateCaptor.getValue();
81         assertEquals(0d, state.level);
82     }
83
84     @Test
85     public void testHandleCommandStopMoveType()
86             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
87         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
88                 StopMoveType.STOP);
89         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
90                 shutterControlServiceStateCaptor.capture());
91         ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
92         assertEquals(OperationState.STOPPED, state.operationState);
93     }
94
95     @Test
96     public void testHandleCommandPercentType()
97             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
98         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
99                 new PercentType(42));
100         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
101                 shutterControlServiceStateCaptor.capture());
102         ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
103         assertEquals(0.58d, state.level);
104     }
105
106     @Test
107     public void testUpdateChannelsShutterControlService() {
108         JsonElement jsonObject = JsonParser
109                 .parseString("{\n" + "   \"@type\": \"shutterControlState\",\n" + "   \"level\": 0.58\n" + " }");
110         getFixture().processUpdate("ShutterControl", jsonObject);
111         verify(getCallback()).stateUpdated(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
112                 new PercentType(42));
113     }
114 }