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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.Mockito.*;
20 import javax.jmdns.ServiceInfo;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.client.api.ContentResponse;
26 import org.eclipse.jetty.client.api.Request;
27 import org.eclipse.jetty.http.HttpMethod;
28 import org.eclipse.jetty.http.HttpStatus;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.mockito.junit.jupiter.MockitoSettings;
35 import org.mockito.quality.Strictness;
36 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
37 import org.openhab.core.config.discovery.DiscoveryResult;
38 import org.openhab.core.thing.ThingUID;
41 * BridgeDiscoveryParticipant Tester.
43 * @author Gerd Zanker - Initial contribution
45 @ExtendWith(MockitoExtension.class)
46 @MockitoSettings(strictness = Strictness.LENIENT)
48 public class BridgeDiscoveryParticipantTest {
51 private BridgeDiscoveryParticipant fixture;
53 private final String url = "https://192.168.0.123:8446/smarthome/public/information";
55 private @Mock @NonNullByDefault({}) ServiceInfo shcBridge;
56 private @Mock @NonNullByDefault({}) ServiceInfo otherDevice;
59 public void beforeEach() throws Exception {
60 when(shcBridge.getHostAddresses()).thenReturn(new String[] { "192.168.0.123" });
61 when(otherDevice.getHostAddresses()).thenReturn(new String[] { "192.168.0.1" });
63 ContentResponse contentResponse = mock(ContentResponse.class);
64 when(contentResponse.getContentAsString()).thenReturn(
65 "{\"apiVersions\":[\"2.9\",\"3.2\"], \"shcIpAddress\":\"192.168.0.123\", \"shcGeneration\":\"SHC_1\"}");
66 when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
68 Request mockRequest = mock(Request.class);
69 when(mockRequest.send()).thenReturn(contentResponse);
70 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
72 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
73 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
75 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
80 * Method: getSupportedThingTypeUIDs()
85 public void testGetSupportedThingTypeUIDs() {
86 assert fixture != null;
87 assertTrue(fixture.getSupportedThingTypeUIDs().contains(BoschSHCBindingConstants.THING_TYPE_SHC));
92 * Method: getServiceType()
96 public void testGetServiceType() throws Exception {
97 assert fixture != null;
98 assertThat(fixture.getServiceType(), is("_http._tcp.local."));
102 public void testCreateResult() throws Exception {
103 assert fixture != null;
104 DiscoveryResult result = fixture.createResult(shcBridge);
105 assertNotNull(result);
106 assertThat(result.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
107 assertThat(result.getThingUID().getId(), is("192-168-0-123"));
108 assertThat(result.getThingTypeUID().getId(), is("shc"));
109 assertThat(result.getLabel(), is("Bosch Smart Home Controller (192.168.0.123)"));
113 public void testCreateResultOtherDevice() throws Exception {
114 assert fixture != null;
115 DiscoveryResult result = fixture.createResult(otherDevice);
120 public void testGetThingUID() throws Exception {
121 assert fixture != null;
122 ThingUID thingUID = fixture.getThingUID(shcBridge);
123 assertNotNull(thingUID);
124 assertThat(thingUID.getBindingId(), is(BoschSHCBindingConstants.BINDING_ID));
125 assertThat(thingUID.getId(), is("192-168-0-123"));
129 public void testGetThingUIDOtherDevice() throws Exception {
130 assert fixture != null;
131 assertNull(fixture.getThingUID(otherDevice));
135 public void testGetBridgeAddress() throws Exception {
136 assert fixture != null;
137 assertThat(fixture.discoverBridge(shcBridge).shcIpAddress, is("192.168.0.123"));
141 public void testGetBridgeAddressOtherDevice() throws Exception {
142 assert fixture != null;
143 assertThat(fixture.discoverBridge(otherDevice).shcIpAddress, is(""));
147 public void testGetPublicInformationFromPossibleBridgeAddress() throws Exception {
148 assert fixture != null;
149 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("192.168.0.123").shcIpAddress,
150 is("192.168.0.123"));
154 public void testGetPublicInformationFromPossibleBridgeAddressInvalidContent() throws Exception {
155 assert fixture != null;
157 ContentResponse contentResponse = mock(ContentResponse.class);
158 when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}");
159 when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
161 Request mockRequest = mock(Request.class);
162 when(mockRequest.send()).thenReturn(contentResponse);
163 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
165 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
166 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
168 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
169 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
173 public void testGetPublicInformationFromPossibleBridgeAddressInvalidStatus() throws Exception {
174 assert fixture != null;
176 ContentResponse contentResponse = mock(ContentResponse.class);
177 // when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}"); no content needed
178 when(contentResponse.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
180 Request mockRequest = mock(Request.class);
181 when(mockRequest.send()).thenReturn(contentResponse);
182 when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
184 HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
185 when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
187 fixture = new BridgeDiscoveryParticipant(mockHttpClient);
188 assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));