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