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.shuttercontrol;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
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.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;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonParser;
41 * Unit tests for {@link ShutterControlHandler}.
43 * @author David Pace - Initial contribution
47 public class ShutterControlHandlerTest extends AbstractBoschSHCDeviceHandlerTest<ShutterControlHandler> {
49 private @Captor @NonNullByDefault({}) ArgumentCaptor<ShutterControlServiceState> shutterControlServiceStateCaptor;
52 protected String getDeviceID() {
53 return "hdm:ZigBee:abcd6fc012ad25b1";
57 protected ShutterControlHandler createFixture() {
58 return new ShutterControlHandler(getThing());
62 protected ThingTypeUID getThingTypeUID() {
63 return BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL;
67 public void testHandleCommandUpDownType()
68 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
69 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
71 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
72 shutterControlServiceStateCaptor.capture());
73 ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
74 assertEquals(1d, state.level);
76 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
78 verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("ShutterControl"),
79 shutterControlServiceStateCaptor.capture());
80 state = shutterControlServiceStateCaptor.getValue();
81 assertEquals(0d, state.level);
85 public void testHandleCommandStopMoveType()
86 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
87 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
89 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
90 shutterControlServiceStateCaptor.capture());
91 ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
92 assertEquals(OperationState.STOPPED, state.operationState);
96 public void testHandleCommandPercentType()
97 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
98 getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_LEVEL),
100 verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("ShutterControl"),
101 shutterControlServiceStateCaptor.capture());
102 ShutterControlServiceState state = shutterControlServiceStateCaptor.getValue();
103 assertEquals(0.58d, state.level);
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));