]> git.basschouten.com Git - openhab-addons.git/blob
3bbf82a3acca0ad424a933cef015e1807f9a2d04
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 getPairingUrl() {
53         assertEquals("https://127.0.0.1:8443/smarthome/clients", httpClient.getPairingUrl());
54     }
55
56     @Test
57     void getBoschShcUrl() {
58         assertEquals("https://127.0.0.1:8444/testEndpoint", httpClient.getBoschShcUrl("testEndpoint"));
59     }
60
61     @Test
62     void getBoschSmartHomeUrl() {
63         assertEquals("https://127.0.0.1:8444/smarthome/endpointForTest",
64                 httpClient.getBoschSmartHomeUrl("endpointForTest"));
65     }
66
67     @Test
68     void getServiceUrl() {
69         assertEquals("https://127.0.0.1:8444/smarthome/devices/testDevice/services/testService/state",
70                 httpClient.getServiceUrl("testService", "testDevice"));
71     }
72
73     @Test
74     void isAccessPossible() throws InterruptedException {
75         assertFalse(httpClient.isAccessPossible());
76     }
77
78     @Test
79     void doPairing() throws InterruptedException {
80         assertFalse(httpClient.doPairing());
81     }
82
83     @Test
84     void createRequest() {
85         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET);
86         assertNotNull(request);
87     }
88
89     @Test
90     void createRequestWithObject() {
91         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET, "someData");
92         assertNotNull(request);
93     }
94
95     @Test
96     void sendRequest() {
97         Request request = httpClient.createRequest("https://127.0.0.1", HttpMethod.GET);
98         // Null pointer exception is expected, because localhost will not answer request
99         assertThrows(NullPointerException.class, () -> {
100             httpClient.sendRequest(request, SubscribeResult.class);
101         });
102     }
103 }