]> git.basschouten.com Git - openhab-addons.git/blob
74c87460434d9e1539d2d64cdf30fb632fe2bfcc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.*;
20
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URISyntaxException;
24 import java.util.List;
25
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;
46
47 /**
48  * Tests for {@link WemoLightHandler}.
49  *
50  * @author Svilen Valkanov - Initial contribution
51  * @author Stefan Triller - Ported Tests from Groovy to Java
52  */
53 public class WemoLightHandlerOSGiTest extends GenericWemoLightOSGiTestParent {
54
55     private static final String GET_ACTION = "GetDeviceStatus";
56     private static final String SET_ACTION = "SetDeviceStatus";
57
58     @BeforeEach
59     public void setUp() throws IOException {
60         setUpServices();
61     }
62
63     @AfterEach
64     public void tearDown() {
65         removeThing();
66     }
67
68     @Test
69     public void handleONcommandForBRIGHTNESSchannel()
70             throws MalformedURLException, URISyntaxException, ValidationException {
71         Command command = OnOffType.ON;
72         String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;
73
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";
79
80         assertRequestForCommand(channelID, command, action, value, capitability);
81     }
82
83     @Test
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;
89
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";
94
95         assertRequestForCommand(channelID, command, action, value, capitability);
96     }
97
98     @Test
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;
104
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";
109
110         assertRequestForCommand(channelID, command, action, value, capitability);
111     }
112
113     @Test
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;
119
120         String action = SET_ACTION;
121         String value = "0:0";
122         String capitability = "10008";
123
124         assertRequestForCommand(channelID, command, action, value, capitability);
125     }
126
127     @Test
128     public void handleOnCommandForSTATEChannel() throws MalformedURLException, URISyntaxException, ValidationException {
129         Command command = OnOffType.ON;
130         String channelID = WemoBindingConstants.CHANNEL_STATE;
131
132         // Command ON for this channel sends the following data to the device
133         String action = SET_ACTION;
134         String value = "1";
135         String capitability = "10006";
136
137         assertRequestForCommand(channelID, command, action, value, capitability);
138     }
139
140     @Test
141     public void handleREFRESHCommandForChannelSTATE()
142             throws MalformedURLException, URISyntaxException, ValidationException {
143         Command command = RefreshType.REFRESH;
144         String channelID = WemoBindingConstants.CHANNEL_STATE;
145
146         String action = GET_ACTION;
147         String value = null;
148         String capitability = null;
149
150         assertRequestForCommand(channelID, command, action, value, capitability);
151     }
152
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);
156
157         WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
158         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
159
160         waitForAssert(() -> {
161             assertThat(bridge.getStatus(), is(ThingStatus.ONLINE));
162         });
163
164         waitForAssert(() -> {
165             assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
166         });
167
168         // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
169         // not start
170         addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, DEVICE_MODEL_NAME);
171
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);
177
178         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
179
180         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
181
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&gt;" + capitability + "&lt;");
187             boolean matchesValue = result.contains("CapabilityValue&gt;" + value + "&lt;");
188             boolean matchesAction = result.contains("<s:Body><u:" + action);
189
190             if (action != null) {
191                 if (!matchesAction) {
192                     continue;
193                 }
194             }
195             if (capitability != null) {
196                 if (!matchesCapability) {
197                     continue;
198                 }
199             }
200             if (value != null) {
201                 if (!matchesValue) {
202                     continue;
203                 }
204             }
205             found = true;
206             break;
207         }
208         assertTrue(found);
209     }
210 }