]> git.basschouten.com Git - openhab-addons.git/blob
3dda05a41c768b585ca2265e73f536cad4fbe703
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.wemo.internal.handler.test;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URISyntaxException;
23 import java.util.List;
24
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.jupnp.model.ValidationException;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Mockito;
41 import org.openhab.binding.wemo.internal.WemoBindingConstants;
42 import org.openhab.binding.wemo.internal.handler.WemoLightHandler;
43 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
44 import org.openhab.binding.wemo.internal.test.GenericWemoLightOSGiTestParent;
45
46 /**
47  * Tests for {@link WemoLightHandler}.
48  *
49  * @author Svilen Valkanov - Initial contribution
50  * @author Stefan Triller - Ported Tests from Groovy to Java
51  */
52 public class WemoLightHandlerOSGiTest extends GenericWemoLightOSGiTestParent {
53
54     private static final String GET_ACTION = "GetDeviceStatus";
55     private static final String SET_ACTION = "SetDeviceStatus";
56
57     @Before
58     public void setUp() throws IOException {
59         setUpServices();
60     }
61
62     @After
63     public void tearDown() {
64         removeThing();
65     }
66
67     @Test
68     public void handleONcommandForBRIGHTNESSchannel()
69             throws MalformedURLException, URISyntaxException, ValidationException {
70         Command command = OnOffType.ON;
71         String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
72
73         // Command ON for this channel sends the following data to the device
74         String action = SET_ACTION;
75         // ON is equal to brightness value of 255
76         String value = "255:0";
77         String capitability = "10008";
78
79         assertRequestForCommand(channelID, command, action, value, capitability);
80     }
81
82     @Test
83     public void handlePercentCommandForBRIGHTNESSChannel()
84             throws MalformedURLException, URISyntaxException, ValidationException {
85         // Set brightness value to 20 Percent
86         Command command = new PercentType(20);
87         String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
88
89         String action = SET_ACTION;
90         // 20 Percent brightness is equal to a brightness value of 51
91         String value = "51:0";
92         String capitability = "10008";
93
94         assertRequestForCommand(channelID, command, action, value, capitability);
95     }
96
97     @Test
98     public void handleIncreaseCommandForBRIGHTNESSchannel()
99             throws MalformedURLException, URISyntaxException, ValidationException {
100         // The value is increased by 5 Percents by default
101         Command command = IncreaseDecreaseType.INCREASE;
102         String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
103
104         String action = SET_ACTION;
105         // 5 Percents brightness is equal to a brightness value of 12
106         String value = "12:0";
107         String capitability = "10008";
108
109         assertRequestForCommand(channelID, command, action, value, capitability);
110     }
111
112     @Test
113     public void handleDecreaseCommandForBRIGHTNESSchannel()
114             throws MalformedURLException, URISyntaxException, ValidationException {
115         // The value can not be decreased below 0
116         Command command = IncreaseDecreaseType.DECREASE;
117         String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
118
119         String action = SET_ACTION;
120         String value = "0:0";
121         String capitability = "10008";
122
123         assertRequestForCommand(channelID, command, action, value, capitability);
124     }
125
126     @Test
127     public void handleOnCommandForSTATEChannel() throws MalformedURLException, URISyntaxException, ValidationException {
128         Command command = OnOffType.ON;
129         String channelID = WemoBindingConstants.CHANNEL_STATE;
130
131         // Command ON for this channel sends the following data to the device
132         String action = SET_ACTION;
133         String value = "1";
134         String capitability = "10006";
135
136         assertRequestForCommand(channelID, command, action, value, capitability);
137     }
138
139     @Test
140     public void handleREFRESHCommandForChannelSTATE()
141             throws MalformedURLException, URISyntaxException, ValidationException {
142         Command command = RefreshType.REFRESH;
143         String channelID = WemoBindingConstants.CHANNEL_STATE;
144
145         String action = GET_ACTION;
146         String value = null;
147         String capitability = null;
148
149         assertRequestForCommand(channelID, command, action, value, capitability);
150     }
151
152     private void assertRequestForCommand(String channelID, Command command, String action, String value,
153             String capitability) throws MalformedURLException, URISyntaxException, ValidationException {
154         Thing bridge = createBridge(BRIDGE_TYPE_UID);
155
156         WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
157         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
158
159         waitForAssert(() -> {
160             assertThat(bridge.getStatus(), is(ThingStatus.ONLINE));
161         });
162
163         waitForAssert(() -> {
164             assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
165         });
166
167         // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
168         // not start
169         addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, DEVICE_MODEL_NAME);
170
171         ThingUID thingUID = new ThingUID(THING_TYPE_UID, TEST_THING_ID);
172         ChannelUID channelUID = new ChannelUID(thingUID, channelID);
173         ThingHandler handler = thing.getHandler();
174         assertNotNull(handler);
175         handler.handleCommand(channelUID, command);
176
177         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
178
179         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
180
181         List<String> results = captur.getAllValues();
182         // we might catch multiple calls. iterate through them to find the one matching our settings
183         boolean found = false;
184         for (String result : results) {
185             boolean matchesCapability = result.contains("CapabilityID&gt;" + capitability + "&lt;");
186             boolean matchesValue = result.contains("CapabilityValue&gt;" + value + "&lt;");
187             boolean matchesAction = result.contains("<s:Body><u:" + action);
188
189             if (action != null) {
190                 if (!matchesAction) {
191                     continue;
192                 }
193             }
194             if (capitability != null) {
195                 if (!matchesCapability) {
196                     continue;
197                 }
198             }
199             if (value != null) {
200                 if (!matchesValue) {
201                     continue;
202                 }
203             }
204             found = true;
205             break;
206         }
207         assertTrue(found);
208     }
209 }