]> git.basschouten.com Git - openhab-addons.git/blob
cc087d02a73bcb9079624f0eab41d52d8a7b7645
[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.discovery;
14
15 import static org.hamcrest.CoreMatchers.is;
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 javax.jmdns.ServiceInfo;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.client.api.ContentResponse;
27 import org.eclipse.jetty.client.api.Request;
28 import org.eclipse.jetty.http.HttpMethod;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35 import org.mockito.junit.jupiter.MockitoSettings;
36 import org.mockito.quality.Strictness;
37 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
38 import org.openhab.core.config.discovery.DiscoveryResult;
39 import org.openhab.core.thing.ThingUID;
40
41 /**
42  * BridgeDiscoveryParticipant Tester.
43  *
44  * @author Gerd Zanker - Initial contribution
45  */
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.LENIENT)
48 @NonNullByDefault
49 class BridgeDiscoveryParticipantTest {
50
51     @Nullable
52     private BridgeDiscoveryParticipant fixture;
53
54     private final String url = "https://192.168.0.123:8446/smarthome/public/information";
55
56     private @Mock @NonNullByDefault({}) ServiceInfo shcBridge;
57     private @Mock @NonNullByDefault({}) ServiceInfo otherDevice;
58
59     @BeforeEach
60     public void beforeEach() throws Exception {
61         when(shcBridge.getHostAddresses()).thenReturn(new String[] { "192.168.0.123" });
62         when(otherDevice.getHostAddresses()).thenReturn(new String[] { "192.168.0.1" });
63
64         ContentResponse contentResponse = mock(ContentResponse.class);
65         when(contentResponse.getContentAsString()).thenReturn(
66                 "{\"apiVersions\":[\"2.9\",\"3.2\"], \"shcIpAddress\":\"192.168.0.123\", \"shcGeneration\":\"SHC_1\"}");
67         when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
68
69         Request mockRequest = mock(Request.class);
70         when(mockRequest.send()).thenReturn(contentResponse);
71         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
72
73         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
74         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
75
76         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
77     }
78
79     /**
80      *
81      * Method: getSupportedThingTypeUIDs()
82      *
83      */
84
85     @Test
86     void testGetSupportedThingTypeUIDs() {
87         assert fixture != null;
88         assertTrue(fixture.getSupportedThingTypeUIDs().contains(BoschSHCBindingConstants.THING_TYPE_SHC));
89     }
90
91     /**
92      *
93      * Method: getServiceType()
94      *
95      */
96     @Test
97     void testGetServiceType() throws Exception {
98         assert fixture != null;
99         assertThat(fixture.getServiceType(), is("_http._tcp.local."));
100     }
101
102     @Test
103     void testCreateResult() throws Exception {
104         assert fixture != null;
105         DiscoveryResult result = fixture.createResult(shcBridge);
106         assertNotNull(result);
107         assertThat(result.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
108         assertThat(result.getThingUID().getId(), is("192-168-0-123"));
109         assertThat(result.getThingTypeUID().getId(), is("shc"));
110         assertThat(result.getLabel(), is("Bosch Smart Home Controller (192.168.0.123)"));
111     }
112
113     @Test
114     void testCreateResultOtherDevice() throws Exception {
115         assert fixture != null;
116         DiscoveryResult result = fixture.createResult(otherDevice);
117         assertNull(result);
118     }
119
120     @Test
121     void testGetThingUID() throws Exception {
122         assert fixture != null;
123         ThingUID thingUID = fixture.getThingUID(shcBridge);
124         assertNotNull(thingUID);
125         assertThat(thingUID.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
126         assertThat(thingUID.getId(), is("192-168-0-123"));
127     }
128
129     @Test
130     void testGetThingUIDOtherDevice() throws Exception {
131         assert fixture != null;
132         assertNull(fixture.getThingUID(otherDevice));
133     }
134
135     @Test
136     void testGetBridgeAddress() throws Exception {
137         assert fixture != null;
138         assertThat(fixture.discoverBridge(shcBridge).shcIpAddress, is("192.168.0.123"));
139     }
140
141     @Test
142     void testGetBridgeAddressOtherDevice() throws Exception {
143         assert fixture != null;
144         assertThat(fixture.discoverBridge(otherDevice).shcIpAddress, is(""));
145     }
146
147     @Test
148     void testGetPublicInformationFromPossibleBridgeAddress() throws Exception {
149         assert fixture != null;
150         assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("192.168.0.123").shcIpAddress,
151                 is("192.168.0.123"));
152     }
153
154     @Test
155     void testGetPublicInformationFromPossibleBridgeAddressInvalidContent() throws Exception {
156         assert fixture != null;
157
158         ContentResponse contentResponse = mock(ContentResponse.class);
159         when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}");
160         when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
161
162         Request mockRequest = mock(Request.class);
163         when(mockRequest.send()).thenReturn(contentResponse);
164         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
165
166         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
167         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
168
169         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
170         assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
171     }
172
173     @Test
174     void testGetPublicInformationFromPossibleBridgeAddressInvalidStatus() throws Exception {
175         assert fixture != null;
176
177         ContentResponse contentResponse = mock(ContentResponse.class);
178         // when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}"); no content needed
179         when(contentResponse.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
180
181         Request mockRequest = mock(Request.class);
182         when(mockRequest.send()).thenReturn(contentResponse);
183         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
184
185         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
186         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
187
188         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
189         assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
190     }
191 }