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.innogysmarthome.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
20 import java.util.concurrent.Future;
22 import org.eclipse.jetty.websocket.api.Session;
23 import org.eclipse.jetty.websocket.api.StatusCode;
24 import org.eclipse.jetty.websocket.client.WebSocketClient;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.innogysmarthome.internal.listener.EventListener;
30 * @author Sven Strohschein - Initial contribution
32 public class InnogyWebSocketTest {
34 private InnogyWebSocketAccessible webSocket;
35 private EventListenerDummy eventListener;
36 private WebSocketClient webSocketClientMock;
37 private Session sessionMock;
40 public void before() throws Exception {
41 sessionMock = mock(Session.class);
43 Future<Session> futureMock = mock(Future.class);
44 when(futureMock.get()).thenReturn(sessionMock);
46 webSocketClientMock = mock(WebSocketClient.class);
47 when(webSocketClientMock.connect(any(), any())).thenReturn(futureMock);
49 eventListener = new EventListenerDummy();
50 webSocket = new InnogyWebSocketAccessible(eventListener, new URI(""), 1000);
54 public void testStart() throws Exception {
57 assertTrue(webSocket.isRunning());
61 public void testStop() throws Exception {
66 assertFalse(webSocket.isRunning());
70 public void testOnCloseAfterStop() throws Exception {
73 assertFalse(eventListener.isOnEventCalled());
74 assertFalse(eventListener.isOnErrorCalled());
75 assertFalse(eventListener.isConnectionClosedCalled());
78 webSocket.onClose(StatusCode.ABNORMAL, "Test");
80 assertFalse(eventListener.isOnEventCalled());
81 assertFalse(eventListener.isOnErrorCalled());
82 // stop() itself causes a (abnormal) close event, that shouldn't get noticed
83 // (otherwise it would cause a reconnect event which would lead to an infinite loop ...)
84 assertFalse(eventListener.isConnectionClosedCalled());
88 public void testOnCloseAfterRestart() throws Exception {
91 assertFalse(eventListener.isOnEventCalled());
92 assertFalse(eventListener.isOnErrorCalled());
93 assertFalse(eventListener.isConnectionClosedCalled());
96 webSocket.onClose(StatusCode.ABNORMAL, "Test");
98 assertFalse(eventListener.isOnEventCalled());
99 assertFalse(eventListener.isOnErrorCalled());
100 // stop() itself causes a (abnormal) close event, that shouldn't get noticed
101 // (otherwise it would cause a reconnect event which would lead to an infinite loop ...)
102 assertFalse(eventListener.isConnectionClosedCalled());
106 webSocket.onClose(StatusCode.ABNORMAL, "Test");
108 assertFalse(eventListener.isOnEventCalled());
109 assertFalse(eventListener.isOnErrorCalled());
110 // A close event after a restart of the web socket should get recognized again
111 assertTrue(eventListener.isConnectionClosedCalled());
115 public void testOnCloseAbnormal() throws Exception {
118 assertFalse(eventListener.isOnEventCalled());
119 assertFalse(eventListener.isOnErrorCalled());
120 assertFalse(eventListener.isConnectionClosedCalled());
122 webSocket.onClose(StatusCode.ABNORMAL, "Test");
124 assertFalse(eventListener.isOnEventCalled());
125 assertFalse(eventListener.isOnErrorCalled());
126 assertTrue(eventListener.isConnectionClosedCalled());
130 public void testOnCloseNormal() throws Exception {
133 assertFalse(eventListener.isOnEventCalled());
134 assertFalse(eventListener.isOnErrorCalled());
135 assertFalse(eventListener.isConnectionClosedCalled());
137 webSocket.onClose(StatusCode.NORMAL, "Test");
139 assertFalse(eventListener.isOnEventCalled());
140 assertFalse(eventListener.isOnErrorCalled());
141 // Nothing should get noticed when a normal close is executed (for example by stopping OpenHAB)
142 assertFalse(eventListener.isConnectionClosedCalled());
146 public void testOnMessage() throws Exception {
149 assertFalse(eventListener.isOnEventCalled());
150 assertFalse(eventListener.isOnErrorCalled());
151 assertFalse(eventListener.isConnectionClosedCalled());
153 webSocket.onMessage("Test-Message");
155 assertTrue(eventListener.isOnEventCalled());
156 assertFalse(eventListener.isOnErrorCalled());
157 assertFalse(eventListener.isConnectionClosedCalled());
161 public void testOnMessageAfterStop() throws Exception {
164 assertFalse(eventListener.isOnEventCalled());
165 assertFalse(eventListener.isOnErrorCalled());
166 assertFalse(eventListener.isConnectionClosedCalled());
169 webSocket.onClose(StatusCode.ABNORMAL, "Test");
170 webSocket.onMessage("Test-Message");
172 assertFalse(eventListener.isOnEventCalled());
173 assertFalse(eventListener.isOnErrorCalled());
174 assertFalse(eventListener.isConnectionClosedCalled());
178 public void testOnError() throws Exception {
181 assertFalse(eventListener.isOnEventCalled());
182 assertFalse(eventListener.isOnErrorCalled());
183 assertFalse(eventListener.isConnectionClosedCalled());
185 webSocket.onError(new RuntimeException("Test-Exception"));
187 assertFalse(eventListener.isOnEventCalled());
188 assertTrue(eventListener.isOnErrorCalled());
189 assertFalse(eventListener.isConnectionClosedCalled());
192 private void startWebSocket() throws Exception {
194 when(sessionMock.isOpen()).thenReturn(true);
196 webSocket.onConnect(sessionMock);
199 private class InnogyWebSocketAccessible extends InnogyWebSocket {
201 private InnogyWebSocketAccessible(EventListener eventListener, URI webSocketURI, int maxIdleTimeout) {
202 super(eventListener, webSocketURI, maxIdleTimeout);
206 WebSocketClient startWebSocketClient() {
207 return webSocketClientMock;
211 private class EventListenerDummy implements EventListener {
213 private boolean isOnEventCalled;
214 private boolean isOnErrorCalled;
215 private boolean isConnectionClosedCalled;
218 public void onEvent(String msg) {
219 isOnEventCalled = true;
223 public void onError(Throwable cause) {
224 isOnErrorCalled = true;
228 public void connectionClosed() {
229 isConnectionClosedCalled = true;
232 public boolean isOnEventCalled() {
233 return isOnEventCalled;
236 public boolean isOnErrorCalled() {
237 return isOnErrorCalled;
240 public boolean isConnectionClosedCalled() {
241 return isConnectionClosedCalled;