]> git.basschouten.com Git - openhab-addons.git/blob
0836e0d4533976262eb7c35c15d7afdaf03f01b8
[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.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URISyntaxException;
23 import java.util.List;
24
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.thing.binding.ThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.jupnp.model.ValidationException;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mockito;
39 import org.openhab.binding.wemo.internal.WemoBindingConstants;
40 import org.openhab.binding.wemo.internal.handler.WemoMakerHandler;
41 import org.openhab.binding.wemo.internal.http.WemoHttpCall;
42 import org.openhab.binding.wemo.internal.test.GenericWemoOSGiTest;
43
44 /**
45  * Tests for {@link WemoMakerHandler}.
46  *
47  * @author Svilen Valkanov - Initial contribution
48  * @author Stefan Triller - Ported Tests from Groovy to Java
49  */
50 public class WemoMakerHandlerOSGiTest extends GenericWemoOSGiTest {
51
52     // Specific Thing information
53     private final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_RELAY;
54     private final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
55     private final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_MAKER;
56
57     // Specific UpnP service information
58     private final String MODEL = THING_TYPE_UID.getId();
59     private final String BASIC_EVENT_SERVICE_ID = "basicevent";
60     private final String SERVICE_NUMBER = "1";
61
62     @Before
63     public void setUp() throws IOException {
64         setUpServices();
65     }
66
67     @After
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(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
87
88         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
89         ThingHandler handler = thing.getHandler();
90         assertNotNull(handler);
91         handler.handleCommand(channelUID, command);
92
93         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
94         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
95
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>")) {
101                 found = true;
102                 break;
103             }
104         }
105         assertTrue(found);
106     }
107
108     @Test
109     public void assertThatThingHandlesREFRESHCommand()
110             throws MalformedURLException, URISyntaxException, ValidationException {
111         Command command = RefreshType.REFRESH;
112
113         WemoHttpCall mockCaller = Mockito.spy(new WemoHttpCall());
114         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE, mockCaller);
115
116         waitForAssert(() -> {
117             assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
118         });
119
120         // The Device is registered as UPnP Device after the initialization, this will ensure that the polling job will
121         // not start
122         addUpnpDevice(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
123
124         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
125         ThingHandler handler = thing.getHandler();
126         assertNotNull(handler);
127         handler.handleCommand(channelUID, command);
128
129         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
130         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
131
132         List<String> results = captur.getAllValues();
133         boolean found = false;
134         for (String result : results) {
135             if (result.contains("<u:GetAttributes xmlns:u=\"urn:Belkin:service:deviceevent:1\"></u:GetAttributes>")) {
136                 found = true;
137                 break;
138             }
139         }
140         assertTrue(found);
141     }
142
143     private void removeThing() {
144         if (thing != null) {
145             Thing removedThing = thingRegistry.remove(thing.getUID());
146             assertThat(removedThing, is(notNullValue()));
147         }
148
149         waitForAssert(() -> {
150             assertThat(thing.getStatus(), is(ThingStatus.UNINITIALIZED));
151         });
152     }
153 }