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