]> git.basschouten.com Git - openhab-addons.git/blob
004886c89080d0987c99785124b53d88e90bbee8
[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.WemoMakerHandler;
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.thing.binding.ThingHandler;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.RefreshType;
42
43 /**
44  * Tests for {@link WemoMakerHandler}.
45  *
46  * @author Svilen Valkanov - Initial contribution
47  * @author Stefan Triller - Ported Tests from Groovy to Java
48  */
49 public class WemoMakerHandlerOSGiTest extends GenericWemoOSGiTest {
50
51     // Specific Thing information
52     private static final String DEFAULT_TEST_CHANNEL = WemoBindingConstants.CHANNEL_RELAY;
53     private static final String DEFAULT_TEST_CHANNEL_TYPE = "Switch";
54     private static final ThingTypeUID THING_TYPE_UID = WemoBindingConstants.THING_TYPE_MAKER;
55
56     // Specific UpnP service information
57     private static final String MODEL = THING_TYPE_UID.getId();
58     private static final String BASIC_EVENT_SERVICE_ID = "basicevent";
59     private static final String SERVICE_NUMBER = "1";
60
61     @BeforeEach
62     public void setUp() throws IOException {
63         setUpServices();
64     }
65
66     @AfterEach
67     public void tearDown() {
68         removeThing();
69     }
70
71     @Test
72     public void assertThatThingHandlesOnOffCommandCorrectly()
73             throws MalformedURLException, URISyntaxException, ValidationException, IOException {
74         Command command = OnOffType.OFF;
75
76         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE);
77
78         waitForAssert(() -> {
79             assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
80         });
81
82         // The Device is registered as UPnP Device after the initialization, this will ensure that the polling job will
83         // not start
84         addUpnpDevice(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
85
86         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
87         ThingHandler handler = thing.getHandler();
88         assertNotNull(handler);
89
90         handler.handleCommand(channelUID, command);
91
92         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
93         verify(mockCaller, atLeastOnce()).executeCall(any(), any(), captur.capture());
94
95         List<String> results = captur.getAllValues();
96         boolean found = false;
97         for (String result : results) {
98             // Binary state 0 is equivalent to OFF
99             if (result.contains("<BinaryState>0</BinaryState>")) {
100                 found = true;
101                 break;
102             }
103         }
104         assertTrue(found);
105     }
106
107     @Test
108     public void assertThatThingHandlesREFRESHCommand()
109             throws MalformedURLException, URISyntaxException, ValidationException, IOException {
110         Command command = RefreshType.REFRESH;
111
112         Thing thing = createThing(THING_TYPE_UID, DEFAULT_TEST_CHANNEL, DEFAULT_TEST_CHANNEL_TYPE);
113
114         waitForAssert(() -> {
115             assertThat(thing.getStatus(), is(ThingStatus.OFFLINE));
116         });
117
118         // The Device is registered as UPnP Device after the initialization, this will ensure that the polling job will
119         // not start
120         addUpnpDevice(BASIC_EVENT_SERVICE_ID, SERVICE_NUMBER, MODEL);
121
122         ChannelUID channelUID = new ChannelUID(thing.getUID(), DEFAULT_TEST_CHANNEL);
123         ThingHandler handler = thing.getHandler();
124         assertNotNull(handler);
125
126         handler.handleCommand(channelUID, command);
127
128         ArgumentCaptor<String> captur = ArgumentCaptor.forClass(String.class);
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:GetAttributes xmlns:u=\"urn:Belkin:service:deviceevent:1\"></u:GetAttributes>")) {
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 }