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.*;
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.WemoMakerHandler;
34 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
35 import org.openhab.binding.wemo.internal.test.GenericWemoOSGiTest;
36 import org.openhab.core.library.types.OnOffType;
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.ThingTypeUID;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.RefreshType;
46 * Tests for {@link WemoMakerHandler}.
48 * @author Svilen Valkanov - Initial contribution
49 * @author Stefan Triller - Ported Tests from Groovy to Java
51 public class WemoMakerHandlerOSGiTest extends GenericWemoOSGiTest {
53 // Specific Thing information
54 private final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_RELAY;
55 private final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
56 private final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_MAKER;
58 // Specific UpnP service information
59 private final String MODEL = THING_TYPE_UID.getId();
60 private final String BASIC_EVENT_SERVICE_ID = "basicevent";
61 private final String SERVICE_NUMBER = "1";
64 public void setUp() throws IOException {
69 public void tearDown() {
74 public void assertThatThingHandlesOnOffCommandCorrectly()
75 throws MalformedURLException, URISyntaxException, ValidationException {
76 Command command = OnOffType.OFF;
78 WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
79 Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
82 assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
85 // The Device is registered as UPnP Device after the initialization, this will ensure that the polling job will
87 addUpnpDevice(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
89 ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
90 ThingHandler handler = thing.getHandler();
91 assertNotNull(handler);
92 handler.handleCommand(channelUID, command);
94 ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
95 verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
97 List<String> results = captur.getAllValues();
98 boolean found = false;
99 for (String result : results) {
100 // Binary state 0 is equivalent to OFF
101 if (result.contains("<BinaryState>0</BinaryState>")) {
110 public void assertThatThingHandlesREFRESHCommand()
111 throws MalformedURLException, URISyntaxException, ValidationException {
112 Command command = RefreshType.REFRESH;
114 WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
115 Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
117 waitForAssert(() -> {
118 assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
121 // The Device is registered as UPnP Device after the initialization, this will ensure that the polling job will
123 addUpnpDevice(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
125 ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
126 ThingHandler handler = thing.getHandler();
127 assertNotNull(handler);
128 handler.handleCommand(channelUID, command);
130 ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
131 verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
133 List<String> results = captur.getAllValues();
134 boolean found = false;
135 for (String result : results) {
136 if (result.contains("<u:GetAttributes xmlns:u=\"urn:Belkin:service:deviceevent:1\"></u:GetAttributes>")) {
144 private void removeThing() {
146 Thing removedThing = thingRegistry.remove(thing.getUID());
147 assertThat(removedThing, is(notNullValue()));
150 waitForAssert(() -> {
151 assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));