2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.webthing.internal.client;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
20 import java.nio.file.Files;
21 import java.nio.file.Paths;
22 import java.time.Duration;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
30 * @author Gregor Roth - Initial contribution
33 public class DescriptionTest {
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);
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());
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);
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());
57 public static String load(String name) throws Exception {
58 return new String(Files.readAllBytes(Paths.get(WebthingTest.class.getResource(name).toURI())));