]> git.basschouten.com Git - openhab-addons.git/blob
a07030a04110d1f4b4cfaf7f42ab944004d9eef6
[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.innogysmarthome.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.net.ConnectException;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.ScheduledFuture;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.innogysmarthome.internal.InnogyBindingConstants;
29 import org.openhab.binding.innogysmarthome.internal.InnogyWebSocket;
30 import org.openhab.binding.innogysmarthome.internal.client.InnogyClient;
31 import org.openhab.binding.innogysmarthome.internal.client.entity.device.Device;
32 import org.openhab.binding.innogysmarthome.internal.client.entity.device.DeviceConfig;
33 import org.openhab.binding.innogysmarthome.internal.manager.FullDeviceManager;
34 import org.openhab.core.auth.client.oauth2.OAuthClientService;
35 import org.openhab.core.auth.client.oauth2.OAuthFactory;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.ThingUID;
39
40 /**
41  * @author Sven Strohschein - Initial contribution
42  */
43 public class InnogyBridgeHandlerTest {
44
45     private static final int MAXIMUM_RETRY_EXECUTIONS = 10;
46
47     private InnogyBridgeHandlerAccessible bridgeHandler;
48     private Bridge bridgeMock;
49     private InnogyWebSocket webSocketMock;
50
51     @BeforeEach
52     public void before() throws Exception {
53         bridgeMock = mock(Bridge.class);
54         when(bridgeMock.getUID()).thenReturn(new ThingUID("innogysmarthome", "bridge"));
55
56         webSocketMock = mock(InnogyWebSocket.class);
57
58         OAuthClientService oAuthService = mock(OAuthClientService.class);
59
60         OAuthFactory oAuthFactoryMock = mock(OAuthFactory.class);
61         when(oAuthFactoryMock.createOAuthClientService(any(), any(), any(), any(), any(), any(), any()))
62                 .thenReturn(oAuthService);
63
64         HttpClient httpClientMock = mock(HttpClient.class);
65
66         bridgeHandler = new InnogyBridgeHandlerAccessible(bridgeMock, oAuthFactoryMock, httpClientMock);
67     }
68
69     @Test
70     public void testInitializeBridgeNotAvailable() throws Exception {
71         Configuration bridgeConfig = new Configuration();
72         HashMap<String, Object> map = new HashMap<>();
73         map.put("brand", "XY");
74         bridgeConfig.setProperties(map);
75
76         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
77
78         bridgeHandler.initialize();
79
80         verify(webSocketMock, never()).start();
81         assertEquals(0, bridgeHandler.getDirectExecutionCount());
82     }
83
84     @Test
85     public void testInitialize() throws Exception {
86         Configuration bridgeConfig = new Configuration();
87
88         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
89
90         bridgeHandler.initialize();
91
92         verify(webSocketMock).start();
93         assertEquals(1, bridgeHandler.getDirectExecutionCount());
94     }
95
96     @Test
97     public void testInitializeErrorOnStartingWebSocket() throws Exception {
98         Configuration bridgeConfig = new Configuration();
99
100         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
101
102         doThrow(new RuntimeException("Test-Exception")).when(webSocketMock).start();
103
104         bridgeHandler.initialize();
105
106         verify(webSocketMock, times(MAXIMUM_RETRY_EXECUTIONS)).start();
107         assertEquals(1, bridgeHandler.getDirectExecutionCount()); // only the first execution should be without a delay
108     }
109
110     @Test
111     public void testConnectionClosed() throws Exception {
112         Configuration bridgeConfig = new Configuration();
113
114         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
115
116         bridgeHandler.initialize();
117
118         verify(webSocketMock).start();
119         assertEquals(1, bridgeHandler.getDirectExecutionCount());
120
121         bridgeHandler.connectionClosed();
122
123         verify(webSocketMock, times(2)).start(); // automatically restarted (with a delay)
124         assertEquals(1, bridgeHandler.getDirectExecutionCount());
125
126         bridgeHandler.connectionClosed();
127
128         verify(webSocketMock, times(3)).start(); // automatically restarted (with a delay)
129         assertEquals(1, bridgeHandler.getDirectExecutionCount());
130     }
131
132     @Test
133     public void testConnectionClosedReconnectNotPossible() throws Exception {
134         Configuration bridgeConfig = new Configuration();
135
136         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
137
138         bridgeHandler.initialize();
139
140         verify(webSocketMock).start();
141         assertEquals(1, bridgeHandler.getDirectExecutionCount());
142
143         doThrow(new ConnectException("Connection refused")).when(webSocketMock).start();
144
145         bridgeHandler.connectionClosed();
146
147         verify(webSocketMock, times(10)).start(); // automatic reconnect attempts (with a delay)
148         assertEquals(1, bridgeHandler.getDirectExecutionCount());
149     }
150
151     @Test
152     public void testOnEventDisconnect() throws Exception {
153         final String disconnectEventJSON = "{ type: \"Disconnect\" }";
154
155         Configuration bridgeConfig = new Configuration();
156
157         when(bridgeMock.getConfiguration()).thenReturn(bridgeConfig);
158
159         bridgeHandler.initialize();
160
161         verify(webSocketMock).start();
162         assertEquals(1, bridgeHandler.getDirectExecutionCount());
163
164         bridgeHandler.onEvent(disconnectEventJSON);
165
166         verify(webSocketMock, times(2)).start(); // automatically restarted (with a delay)
167         assertEquals(1, bridgeHandler.getDirectExecutionCount());
168
169         bridgeHandler.onEvent(disconnectEventJSON);
170
171         verify(webSocketMock, times(3)).start(); // automatically restarted (with a delay)
172         assertEquals(1, bridgeHandler.getDirectExecutionCount());
173     }
174
175     private class InnogyBridgeHandlerAccessible extends InnogyBridgeHandler {
176
177         private final InnogyClient innogyClientMock;
178         private final FullDeviceManager fullDeviceManagerMock;
179         private final ScheduledExecutorService schedulerMock;
180         private int executionCount;
181         private int directExecutionCount;
182
183         private InnogyBridgeHandlerAccessible(Bridge bridge, OAuthFactory oAuthFactory, HttpClient httpClient)
184                 throws Exception {
185             super(bridge, oAuthFactory, httpClient);
186
187             Device bridgeDevice = new Device();
188             bridgeDevice.setId("bridgeId");
189             bridgeDevice.setType(InnogyBindingConstants.DEVICE_SHC);
190             bridgeDevice.setConfig(new DeviceConfig());
191
192             innogyClientMock = mock(InnogyClient.class);
193             fullDeviceManagerMock = mock(FullDeviceManager.class);
194             when(fullDeviceManagerMock.getFullDevices()).thenReturn(Collections.singletonList(bridgeDevice));
195
196             schedulerMock = mock(ScheduledExecutorService.class);
197
198             doAnswer(invocationOnMock -> {
199                 if (executionCount <= MAXIMUM_RETRY_EXECUTIONS) {
200                     executionCount++;
201                     invocationOnMock.getArgument(0, Runnable.class).run();
202                 }
203                 return null;
204             }).when(schedulerMock).execute(any());
205
206             doAnswer(invocationOnMock -> {
207                 if (executionCount <= MAXIMUM_RETRY_EXECUTIONS) {
208                     executionCount++;
209                     long seconds = invocationOnMock.getArgument(1);
210                     if (seconds <= 0) {
211                         directExecutionCount++;
212                     }
213
214                     invocationOnMock.getArgument(0, Runnable.class).run();
215                 }
216                 return mock(ScheduledFuture.class);
217             }).when(schedulerMock).schedule(any(Runnable.class), anyLong(), any());
218         }
219
220         public int getDirectExecutionCount() {
221             return directExecutionCount;
222         }
223
224         @Override
225         @NonNull
226         FullDeviceManager createFullDeviceManager(@NonNull InnogyClient client) {
227             return fullDeviceManagerMock;
228         }
229
230         @Override
231         @NonNull
232         InnogyClient createInnogyClient(@NonNull OAuthClientService oAuthService, @NonNull HttpClient httpClient) {
233             return innogyClientMock;
234         }
235
236         @Override
237         @NonNull
238         InnogyWebSocket createWebSocket() {
239             return webSocketMock;
240         }
241
242         @Override
243         @NonNull
244         ScheduledExecutorService getScheduler() {
245             return schedulerMock;
246         }
247     }
248 }