]> git.basschouten.com Git - openhab-addons.git/blob
faf441ac51800a07e14c317a080f1b5dad1ad093
[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.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
18 import static org.mockito.Mockito.*;
19
20 import javax.jmdns.ServiceInfo;
21
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;
39
40 /**
41  * BridgeDiscoveryParticipant Tester.
42  *
43  * @author Gerd Zanker - Initial contribution
44  */
45 @ExtendWith(MockitoExtension.class)
46 @MockitoSettings(strictness = Strictness.LENIENT)
47 @NonNullByDefault
48 public class BridgeDiscoveryParticipantTest {
49
50     @Nullable
51     private BridgeDiscoveryParticipant fixture;
52
53     private final String url = "https://192.168.0.123:8446/smarthome/public/information";
54
55     private @Mock @NonNullByDefault({}) ServiceInfo shcBridge;
56     private @Mock @NonNullByDefault({}) ServiceInfo otherDevice;
57
58     @BeforeEach
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" });
62
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);
67
68         Request mockRequest = mock(Request.class);
69         when(mockRequest.send()).thenReturn(contentResponse);
70         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
71
72         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
73         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
74
75         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
76     }
77
78     /**
79      * 
80      * Method: getSupportedThingTypeUIDs()
81      * 
82      */
83
84     @Test
85     public void testGetSupportedThingTypeUIDs() {
86         assert fixture != null;
87         assertTrue(fixture.getSupportedThingTypeUIDs().contains(BoschSHCBindingConstants.THING_TYPE_SHC));
88     }
89
90     /**
91      * 
92      * Method: getServiceType()
93      * 
94      */
95     @Test
96     public void testGetServiceType() throws Exception {
97         assert fixture != null;
98         assertThat(fixture.getServiceType(), is("_http._tcp.local."));
99     }
100
101     @Test
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)"));
110     }
111
112     @Test
113     public void testCreateResultOtherDevice() throws Exception {
114         assert fixture != null;
115         DiscoveryResult result = fixture.createResult(otherDevice);
116         assertNull(result);
117     }
118
119     @Test
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"));
126     }
127
128     @Test
129     public void testGetThingUIDOtherDevice() throws Exception {
130         assert fixture != null;
131         assertNull(fixture.getThingUID(otherDevice));
132     }
133
134     @Test
135     public void testGetBridgeAddress() throws Exception {
136         assert fixture != null;
137         assertThat(fixture.discoverBridge(shcBridge).shcIpAddress, is("192.168.0.123"));
138     }
139
140     @Test
141     public void testGetBridgeAddressOtherDevice() throws Exception {
142         assert fixture != null;
143         assertThat(fixture.discoverBridge(otherDevice).shcIpAddress, is(""));
144     }
145
146     @Test
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"));
151     }
152
153     @Test
154     public void testGetPublicInformationFromPossibleBridgeAddressInvalidContent() throws Exception {
155         assert fixture != null;
156
157         ContentResponse contentResponse = mock(ContentResponse.class);
158         when(contentResponse.getContentAsString()).thenReturn("{\"nothing\":\"useful\"}");
159         when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200);
160
161         Request mockRequest = mock(Request.class);
162         when(mockRequest.send()).thenReturn(contentResponse);
163         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
164
165         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
166         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
167
168         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
169         assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
170     }
171
172     @Test
173     public void testGetPublicInformationFromPossibleBridgeAddressInvalidStatus() throws Exception {
174         assert fixture != null;
175
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);
179
180         Request mockRequest = mock(Request.class);
181         when(mockRequest.send()).thenReturn(contentResponse);
182         when(mockRequest.method((HttpMethod) any())).thenReturn(mockRequest);
183
184         HttpClient mockHttpClient = spy(HttpClient.class); // spy needed, because some final methods can't be mocked
185         when(mockHttpClient.newRequest(url)).thenReturn(mockRequest);
186
187         fixture = new BridgeDiscoveryParticipant(mockHttpClient);
188         assertThat(fixture.getPublicInformationFromPossibleBridgeAddress("shcAddress").shcIpAddress, is(""));
189     }
190 }