2 * Copyright (c) 2010-2021 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.wemo.internal.handler.test;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URISyntaxException;
24 import java.util.List;
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.jupnp.model.ValidationException;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mockito;
32 import org.openhab.binding.wemo.internal.WemoBindingConstants;
33 import org.openhab.binding.wemo.internal.handler.WemoLightHandler;
34 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
35 import org.openhab.binding.wemo.internal.test.GenericWemoLightOSGiTestParent;
36 import org.openhab.core.library.types.IncreaseDecreaseType;
37 import org.openhab.core.library.types.OnOffType;
38 import org.openhab.core.library.types.PercentType;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.ThingHandler;
44 import org.openhab.core.types.Command;
45 import org.openhab.core.types.RefreshType;
48 * Tests for {@link WemoLightHandler}.
50 * @author Svilen Valkanov - Initial contribution
51 * @author Stefan Triller - Ported Tests from Groovy to Java
53 public class WemoLightHandlerOSGiTest extends GenericWemoLightOSGiTestParent {
55 private static final String GET_ACTION = "GetDeviceStatus";
56 private static final String SET_ACTION = "SetDeviceStatus";
59 public void setUp() throws IOException {
64 public void tearDown() {
69 public void handleONcommandForBRIGHTNESSchannel()
70 throws MalformedURLException, URISyntaxException, ValidationException {
71 Command command = OnOffType.ON;
72 String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
74 // Command ON for this channel sends the following data to the device
75 String action = SET_ACTION;
76 // ON is equal to brightness value of 255
77 String value = "255:0";
78 String capitability = "10008";
80 assertRequestForCommand(channelID, command, action, value, capitability);
84 public void handlePercentCommandForBRIGHTNESSChannel()
85 throws MalformedURLException, URISyntaxException, ValidationException {
86 // Set brightness value to 20 Percent
87 Command command = new PercentType(20);
88 String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
90 String action = SET_ACTION;
91 // 20 Percent brightness is equal to a brightness value of 51
92 String value = "51:0";
93 String capitability = "10008";
95 assertRequestForCommand(channelID, command, action, value, capitability);
99 public void handleIncreaseCommandForBRIGHTNESSchannel()
100 throws MalformedURLException, URISyntaxException, ValidationException {
101 // The value is increased by 5 Percents by default
102 Command command = IncreaseDecreaseType.INCREASE;
103 String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
105 String action = SET_ACTION;
106 // 5 Percents brightness is equal to a brightness value of 12
107 String value = "12:0";
108 String capitability = "10008";
110 assertRequestForCommand(channelID, command, action, value, capitability);
114 public void handleDecreaseCommandForBRIGHTNESSchannel()
115 throws MalformedURLException, URISyntaxException, ValidationException {
116 // The value can not be decreased below 0
117 Command command = IncreaseDecreaseType.DECREASE;
118 String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
120 String action = SET_ACTION;
121 String value = "0:0";
122 String capitability = "10008";
124 assertRequestForCommand(channelID, command, action, value, capitability);
128 public void handleOnCommandForSTATEChannel() throws MalformedURLException, URISyntaxException, ValidationException {
129 Command command = OnOffType.ON;
130 String channelID = WemoBindingConstants.CHANNEL_STATE;
132 // Command ON for this channel sends the following data to the device
133 String action = SET_ACTION;
135 String capitability = "10006";
137 assertRequestForCommand(channelID, command, action, value, capitability);
141 public void handleREFRESHCommandForChannelSTATE()
142 throws MalformedURLException, URISyntaxException, ValidationException {
143 Command command = RefreshType.REFRESH;
144 String channelID = WemoBindingConstants.CHANNEL_STATE;
146 String action = GET_ACTION;
148 String capitability = null;
150 assertRequestForCommand(channelID, command, action, value, capitability);
153 private void assertRequestForCommand(String channelID, Command command, String action, String value,
154 String capitability) throws MalformedURLException, URISyntaxException, ValidationException {
155 Thing bridge = createBridge(BRIDGE_TYPE_UID);
157 WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
158 Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
160 waitForAssert(() -> {
161 assertThat(bridge.getStatus(), is(ThingStatus.ONLINE));
164 waitForAssert(() -> {
165 assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
168 // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
170 addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, DEVICE_MODEL_NAME);
172 ThingUID thingUID = new ThingUID(THING_TYPE_UID, TEST_THING_ID);
173 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
174 ThingHandler handler = thing.getHandler();
175 assertNotNull(handler);
176 handler.handleCommand(channelUID, command);
178 ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
180 verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
182 List<String> results = captur.getAllValues();
183 // we might catch multiple calls. iterate through them to find the one matching our settings
184 boolean found = false;
185 for (String result : results) {
186 boolean matchesCapability = result.contains("CapabilityID>" + capitability + "<");
187 boolean matchesValue = result.contains("CapabilityValue>" + value + "<");
188 boolean matchesAction = result.contains("<s:Body><u:" + action);
190 if (action != null) {
191 if (!matchesAction) {
195 if (capitability != null) {
196 if (!matchesCapability) {