2 * Copyright (c) 2010-2020 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.*;
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URISyntaxException;
23 import java.util.List;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.jupnp.model.ValidationException;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mockito;
38 import org.openhab.binding.wemo.internal.WemoBindingConstants;
39 import org.openhab.binding.wemo.internal.handler.WemoHandler;
40 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
41 import org.openhab.binding.wemo.internal.test.GenericWemoOSGiTest;
44 * Tests for {@link WemoHandler}.
46 * @author Svilen Valkanov - Initial contribution
47 * @author Stefan Triller - Ported Tests from Groovy to Java
49 public class WemoHandlerOSGiTest extends GenericWemoOSGiTest {
52 private final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_STATE;
53 private final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
54 private final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_SOCKET;
57 private final String MODEL_NAME = WemoBindingConstants.THING_TYPE_SOCKET.getId();
58 private final String SERVICE_ID = "basicevent";
59 private final String SERVICE_NUMBER = "1";
62 public void setUp() throws IOException {
67 public void tearDown() {
72 public void assertThatThingHandlesOnOffCommandCorrectly()
73 throws MalformedURLException, URISyntaxException, ValidationException {
74 Command command = OnOffType.OFF;
76 WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
77 Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
80 assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
83 // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
85 addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, MODEL_NAME);
87 WemoHandler handler = (WemoHandler) thing.getHandler();
88 assertNotNull(handler);
90 ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
91 handler.handleCommand(channelUID, command);
93 ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
94 verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
96 List<String> results = captur.getAllValues();
97 boolean found = false;
98 for (String result : results) {
99 // Binary state 0 is equivalent to OFF
100 if (result.contains("<BinaryState>0</BinaryState>")) {
109 public void assertThatThingHandlesREFRESHCommandCorrectly()
110 throws MalformedURLException, URISyntaxException, ValidationException {
111 Command command = RefreshType.REFRESH;
113 WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
114 Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
116 waitForAssert(() -> {
117 assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
120 // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
122 addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, MODEL_NAME);
124 WemoHandler handler = (WemoHandler) thing.getHandler();
125 assertNotNull(handler);
127 ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
128 handler.handleCommand(channelUID, command);
130 ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
132 verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
134 List<String> results = captur.getAllValues();
135 boolean found = false;
136 for (String result : results) {
137 if (result.contains("<u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"></u:GetBinaryState>")) {
145 private void removeThing() {
147 Thing removedThing = thingRegistry.remove(thing.getUID());
148 assertThat(removedThing, is(notNullValue()));
151 waitForAssert(() -> {
152 assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));