]> git.basschouten.com Git - openhab-addons.git/blob
bb300acc2ab2b1eb4d548653940dd56d6e61babb
[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.*;
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.WemoHandler;
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.types.Command;
42 import org.openhab.core.types.RefreshType;
43
44 /**
45  * Tests for {@link WemoHandler}.
46  *
47  * @author Svilen Valkanov - Initial contribution
48  * @author Stefan Triller - Ported Tests from Groovy to Java
49  */
50 public class WemoHandlerOSGiTest extends GenericWemoOSGiTest {
51
52     // Thing information
53     private final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_STATE;
54     private final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
55     private final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_SOCKET;
56
57     // UPnP information
58     private final String MODEL_NAME = WemoBindingConstants.THING_TYPE_SOCKET.getId();
59     private final String SERVICE_ID = "basicevent";
60     private final String SERVICE_NUMBER = "1";
61
62     @BeforeEach
63     public void setUp() throws IOException {
64         setUpServices();
65     }
66
67     @AfterEach
68     public void tearDown() {
69         removeThing();
70     }
71
72     @Test
73     public void assertThatThingHandlesOnOffCommandCorrectly()
74             throws MalformedURLException, URISyntaxException, ValidationException {
75         Command command = OnOffType.OFF;
76
77         WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
78         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
79
80         waitForAssert(() -> {
81             assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
82         });
83
84         // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
85         // not start
86         addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, MODEL_NAME);
87
88         WemoHandler handler = (WemoHandler) thing.getHandler();
89         assertNotNull(handler);
90
91         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
92         handler.handleCommand(channelUID, command);
93
94         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
95         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
96
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>")) {
102                 found = true;
103                 break;
104             }
105         }
106         assertTrue(found);
107     }
108
109     @Test
110     public void assertThatThingHandlesREFRESHCommandCorrectly()
111             throws MalformedURLException, URISyntaxException, ValidationException {
112         Command command = RefreshType.REFRESH;
113
114         WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
115         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
116
117         waitForAssert(() -> {
118             assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
119         });
120
121         // The device is registered as UPnP Device after the initialization, this will ensure that the polling job will
122         // not start
123         addUpnpDevice(SERVICE_ID, SERVICE_NUMBER, MODEL_NAME);
124
125         WemoHandler handler = (WemoHandler) thing.getHandler();
126         assertNotNull(handler);
127
128         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
129         handler.handleCommand(channelUID, command);
130
131         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
132
133         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
134
135         List<String> results = captur.getAllValues();
136         boolean found = false;
137         for (String result : results) {
138             if (result.contains("<u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"></u:GetBinaryState>")) {
139                 found = true;
140                 break;
141             }
142         }
143         assertTrue(found);
144     }
145
146     private void removeThing() {
147         if (thing != null) {
148             Thing removedThing = thingRegistry.remove(thing.getUID());
149             assertThat(removedThing, is(notNullValue()));
150         }
151
152         waitForAssert(() -> {
153             assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
154         });
155     }
156 }