]> git.basschouten.com Git - openhab-addons.git/blob
a46472fef00e8b569457506174bf14d14068dfdf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.webthing.internal.client;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 import java.net.URI;
20 import java.nio.file.Files;
21 import java.nio.file.Paths;
22 import java.time.Duration;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26
27 /**
28  *
29  *
30  * @author Gregor Roth - Initial contribution
31  */
32 @NonNullByDefault
33 public class DescriptionTest {
34
35     @Test
36     public void testDescriptionEventStreamUri() throws Exception {
37         var httpClient = mock(org.eclipse.jetty.client.HttpClient.class);
38         var request = Mocks.mockRequest(null, load("/awning_response.json"));
39         when(httpClient.newRequest(URI.create("http://example.org:8090"))).thenReturn(request);
40
41         var loader = new DescriptionLoader(httpClient);
42         var description = loader.loadWebthingDescription(URI.create("http://example.org:8090"), Duration.ofSeconds(2));
43         assertEquals("ws://192.168.4.12:9040/0", description.getEventStreamUri().get().toString());
44     }
45
46     @Test
47     public void testDescriptionEventStreamUriServerlaAlternateParts() throws Exception {
48         var httpClient = mock(org.eclipse.jetty.client.HttpClient.class);
49         var request = Mocks.mockRequest(null, load("/virtual-things_response.json"));
50         when(httpClient.newRequest(URI.create("http://example.org:8090"))).thenReturn(request);
51
52         var loader = new DescriptionLoader(httpClient);
53         var description = loader.loadWebthingDescription(URI.create("http://example.org:8090"), Duration.ofSeconds(2));
54         assertEquals("ws://webthings/things/virtual-things-7", description.getEventStreamUri().get().toString());
55     }
56
57     public static String load(String name) throws Exception {
58         return new String(Files.readAllBytes(Paths.get(WebthingTest.class.getResource(name).toURI())));
59     }
60 }