2 * Copyright (c) 2010-2023 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.boschshc.internal.discovery;
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.*;
21 import javax.jmdns.ServiceInfo;
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;
42 * BridgeDiscoveryParticipant Tester.
44 * @author Gerd Zanker - Initial contribution
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.LENIENT)
49 class BridgeDiscoveryParticipantTest {
52 private BridgeDiscoveryParticipant fixture;
54 private final String url = "https://192.168.0.123:8446/smarthome/public/information";
56 private @Mock @NonNullByDefault({}) ServiceInfo shcBridge;
57 private @Mock @NonNullByDefault({}) ServiceInfo otherDevice;
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" });
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);
69 Request mockRequest = mock(Request.class);
70 when(mockRequest.send()).thenReturn(contentResponse);
71 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
73 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
74 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
76 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
81 * Method: getSupportedThingTypeUIDs()
86 void testGetSupportedThingTypeUIDs() {
87 assert fixture != null;
88 assertTrue(fixture.getSupportedThingTypeUIDs().contains(BoschSHCBindingConstants.THING_TYPE_SHC));
93 * Method: getServiceType()
97 void testGetServiceType() throws Exception {
98 assert fixture != null;
99 assertThat(fixture.getServiceType(), is("_http._tcp.local."));
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)"));
114 void testCreateResultOtherDevice() throws Exception {
115 assert fixture != null;
116 DiscoveryResult result = fixture.createResult(otherDevice);
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"));
130 void testGetThingUIDOtherDevice() throws Exception {
131 assert fixture != null;
132 assertNull(fixture.getThingUID(otherDevice));
136 void testGetBridgeAddress() throws Exception {
137 assert fixture != null;
138 assertThat(fixture.discoverBridge(shcBridge).shcIpAddress, is("192.168.0.123"));
142 void testGetBridgeAddressOtherDevice() throws Exception {
143 assert fixture != null;
144 assertThat(fixture.discoverBridge(otherDevice).shcIpAddress, is(""));
148 void testGetPublicInformationFromPossibleBridgeAddress() throws Exception {
149 assert fixture != null;
150 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("192.168.0.123").shcIpAddress,
151 is("192.168.0.123"));
155 void testGetPublicInformationFromPossibleBridgeAddressInvalidContent() throws Exception {
156 assert fixture != null;
158 ContentResponse contentResponse = mock(ContentResponse.class);
159 when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}");
160 when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
162 Request mockRequest = mock(Request.class);
163 when(mockRequest.send()).thenReturn(contentResponse);
164 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
166 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
167 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
169 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
170 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
174 void testGetPublicInformationFromPossibleBridgeAddressInvalidStatus() throws Exception {
175 assert fixture != null;
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);
181 Request mockRequest = mock(Request.class);
182 when(mockRequest.send()).thenReturn(contentResponse);
183 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
185 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
186 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
188 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
189 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));