]> git.basschouten.com Git - openhab-addons.git/blob
0c6b8518808ac8cb8e5c3448d477c4bc49fb61cd
[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.boschshc.internal.devices.bridge;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.api.Request;
20 import org.eclipse.jetty.http.HttpMethod;
21 import org.eclipse.jetty.util.ssl.SslContextFactory;
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.boschshc.internal.devices.bridge.dto.SubscribeResult;
26 import org.openhab.binding.boschshc.internal.exceptions.PairingFailedException;
27
28 /**
29  * Tests cases for {@link BoschHttpClient}.
30  *
31  * @author Gerd Zanker - Initial contribution
32  */
33 @NonNullByDefault
34 class BoschHttpClientTest {
35
36     @Nullable
37     private BoschHttpClient httpClient;
38
39     @BeforeAll
40     static void beforeAll() {
41         BoschSslUtilTest.prepareTempFolderForKeyStore();
42     }
43
44     @BeforeEach
45     void beforeEach() throws PairingFailedException {
46         SslContextFactory sslFactory = new BoschSslUtil("127.0.0.1").getSslContextFactory();
47         httpClient = new BoschHttpClient("127.0.0.1", "dummy", sslFactory);
48         assertNotNull(httpClient);
49     }
50
51     @Test
52     void getPublicInformationUrl() {
53         assertEquals("https://127.0.0.1:8446/smarthome/public/information", httpClient.getPublicInformationUrl());
54     }
55
56     @Test
57     void getPairingUrl() {
58         assertEquals("https://127.0.0.1:8443/smarthome/clients", httpClient.getPairingUrl());
59     }
60
61     @Test
62     void getBoschShcUrl() {
63         assertEquals("https://127.0.0.1:8444/testEndpoint", httpClient.getBoschShcUrl("testEndpoint"));
64     }
65
66     @Test
67     void getBoschSmartHomeUrl() {
68         assertEquals("https://127.0.0.1:8444/smarthome/endpointForTest",
69                 httpClient.getBoschSmartHomeUrl("endpointForTest"));
70     }
71
72     @Test
73     void getServiceUrl() {
74         assertEquals("https://127.0.0.1:8444/smarthome/devices/testDevice/services/testService",
75                 httpClient.getServiceUrl("testService", "testDevice"));
76     }
77
78     @Test
79     void getServiceStateUrl() {
80         assertEquals("https://127.0.0.1:8444/smarthome/devices/testDevice/services/testService/state",
81                 httpClient.getServiceStateUrl("testService", "testDevice"));
82     }
83
84     @Test
85     void isAccessPossible() throws InterruptedException {
86         assertFalse(httpClient.isAccessPossible());
87     }
88
89     @Test
90     void isOnline() throws InterruptedException {
91         assertFalse(httpClient.isOnline());
92     }
93
94     @Test
95     void doPairing() throws InterruptedException {
96         assertFalse(httpClient.doPairing());
97     }
98
99     @Test
100     void createRequest() {
101         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET);
102         assertNotNull(request);
103     }
104
105     @Test
106     void createRequestWithObject() {
107         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET, "someData");
108         assertNotNull(request);
109     }
110
111     @Test
112     void sendRequest() {
113         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET);
114         // Null pointer exception is expected, because localhost will not answer request
115         assertThrows(NullPointerException.class, () -> {
116             httpClient.sendRequest(request, SubscribeResult.class, SubscribeResult::isValid, null);
117         });
118     }
119 }