Skip to content

Commit ac13cd3

Browse files
author
Ilkin Abdullayev
committed
Migrated ws tests to junit5
Signed-off-by: Ilkin Abdullayev <ilkin.abdullayev@broadcom.com>
1 parent 164a428 commit ac13cd3

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
11+
package org.zowe.apiml.gateway.ws;
12+
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.extension.ExtendWith;
16+
import org.mockito.junit.jupiter.MockitoExtension;
17+
import org.springframework.web.socket.WebSocketSession;
18+
import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession;
19+
20+
import java.net.InetSocketAddress;
21+
import java.net.URI;
22+
import java.util.ArrayList;
23+
import java.util.LinkedHashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.concurrent.ConcurrentHashMap;
27+
28+
import static org.junit.jupiter.api.Assertions.*;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.when;
31+
32+
@ExtendWith(MockitoExtension.class)
33+
class WebSocketActuatorEndpointTest {
34+
35+
private WebSocketActuatorEndpoint webSocketActuatorEndpoint;
36+
private WebSocketProxyServerHandler webSocketProxyServerHandler;
37+
private WebSocketRoutedSession webSocketRoutedSession;
38+
private WebSocketServerSockJsSession webSocketServerSockJsSession;
39+
private WebSocketSession session;
40+
41+
@BeforeEach
42+
public void setup() {
43+
webSocketProxyServerHandler = mock(WebSocketProxyServerHandler.class);
44+
session = mock(WebSocketSession.class);
45+
webSocketRoutedSession = mock(WebSocketRoutedSession.class);
46+
webSocketServerSockJsSession = mock(WebSocketServerSockJsSession.class);
47+
48+
webSocketActuatorEndpoint = new WebSocketActuatorEndpoint(webSocketProxyServerHandler);
49+
}
50+
51+
@Test
52+
public void should() throws Exception {
53+
54+
URI uri = new URI("ws://localhost:8080/abc");
55+
56+
Map<String, WebSocketRoutedSession> routedSessions = new ConcurrentHashMap<>();
57+
58+
when(webSocketRoutedSession.getWebSocketServerSession()).thenReturn(webSocketServerSockJsSession);
59+
when(webSocketRoutedSession.getWebSocketClientSession()).thenReturn(session);
60+
when(webSocketRoutedSession.getWebSocketServerSession().getRemoteAddress()).thenReturn(new InetSocketAddress(80));
61+
when(webSocketRoutedSession.getWebSocketServerSession().getUri()).thenReturn(uri);
62+
when(webSocketRoutedSession.getWebSocketClientSession().getUri()).thenReturn(uri);
63+
64+
routedSessions.put("websocket", webSocketRoutedSession);
65+
66+
when(webSocketProxyServerHandler.getRoutedSessions()).thenReturn(routedSessions);
67+
68+
List<Map<String, String>> expectedResult = getMockedResultMaps();
69+
70+
List<Map<String, String>> result = webSocketActuatorEndpoint.getAll();
71+
72+
assertEquals(expectedResult, result);
73+
}
74+
75+
private List<Map<String, String>> getMockedResultMaps() {
76+
77+
List<Map<String, String>> expectedResult = new ArrayList<>();
78+
Map<String, String> map = new LinkedHashMap<>();
79+
80+
map.put("sessionId", "websocket");
81+
map.put("clientAddress", "0.0.0.0/0.0.0.0:80");
82+
map.put("gatewayPath", "ws://localhost:8080/abc");
83+
map.put("serviceUrl", "ws://localhost:8080/abc");
84+
map.put("serviceSessionId", null);
85+
86+
expectedResult.add(map);
87+
return expectedResult;
88+
}
89+
90+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
11+
package org.zowe.apiml.gateway.ws;
12+
13+
import org.apache.http.conn.ssl.TrustStrategy;
14+
import org.apache.http.ssl.SSLContexts;
15+
import org.eclipse.jetty.util.ssl.SslContextFactory;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.springframework.cloud.client.DefaultServiceInstance;
18+
import org.springframework.cloud.client.discovery.DiscoveryClient;
19+
import org.springframework.web.socket.WebSocketHttpHeaders;
20+
import org.springframework.web.socket.WebSocketSession;
21+
import org.zowe.apiml.product.routing.RoutedService;
22+
import org.zowe.apiml.product.routing.RoutedServices;
23+
24+
import javax.net.ssl.SSLContext;
25+
import java.net.URI;
26+
import java.security.cert.X509Certificate;
27+
import java.util.Collections;
28+
29+
import static org.junit.jupiter.api.Assertions.*;
30+
import static org.mockito.Mockito.*;
31+
32+
class WebSocketProxyServerHandlerTest {
33+
private WebSocketSession session;
34+
private WebSocketProxyServerHandler webSocketProxyServerHandler;
35+
private SslContextFactory jettySslContextFactory;
36+
private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class);
37+
38+
39+
@BeforeEach
40+
public void setup() {
41+
session = mock(WebSocketSession.class);
42+
// session = new StandardWebSocketSession();
43+
// jettySslContextFactory = mock(SslContextFactory.class);
44+
jettySslContextFactory = new SslContextFactory();
45+
webSocketProxyServerHandler = new WebSocketProxyServerHandler(discoveryClient, jettySslContextFactory);
46+
}
47+
48+
// @Test
49+
public void should() throws Exception {
50+
jettySslContextFactory.setKeyStoreType("PKCS12");
51+
jettySslContextFactory.setCertAlias("localhost");
52+
jettySslContextFactory.setTrustStoreType("type");
53+
jettySslContextFactory.setProtocol("SSL");
54+
jettySslContextFactory.setKeyStorePath("src/test/resources/certs/keystore.jks");
55+
jettySslContextFactory.setTrustStorePath("src/test/resources/certs/truststore.jks");
56+
jettySslContextFactory.setKeyManagerFactoryAlgorithm("SunX509");
57+
jettySslContextFactory.setTrustManagerFactoryAlgorithm("PKIX");
58+
jettySslContextFactory.setKeyStorePassword("pass");
59+
jettySslContextFactory.setTrustStorePassword("pass");
60+
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
61+
public boolean isTrusted(X509Certificate[] certificate, String authType) {
62+
return true;
63+
}
64+
};
65+
SSLContext sslContext = SSLContexts.custom()
66+
.loadTrustMaterial(null, acceptingTrustStrategy).build();
67+
// SSLContext sslContext = SSLContext.getInstance("SSL");
68+
69+
jettySslContextFactory.setSslContext(sslContext);
70+
// when(session.isOpen()).thenReturn(true);
71+
RoutedServices routedServices = new RoutedServices();
72+
routedServices.addRoutedService(
73+
new RoutedService("api-v1", "api/v1", "/service/api/v1"));
74+
routedServices.addRoutedService(
75+
new RoutedService("ui-v1", "ui/v1", "/service"));
76+
routedServices.addRoutedService(
77+
new RoutedService("ws-v1", "ws/v1", "/service/ws"));
78+
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
79+
80+
headers.add("X-Test", "value");
81+
when(session.getUri()).thenReturn(new URI("/ws/v1/service/uppercase"));
82+
when(discoveryClient.getInstances("service")).thenReturn(
83+
Collections.singletonList(new DefaultServiceInstance("service", "localhost", 80, false, null)));
84+
when(session.getHandshakeHeaders()).thenReturn(headers);
85+
webSocketProxyServerHandler.addRoutedServices("service", routedServices);
86+
webSocketProxyServerHandler.afterConnectionEstablished(session);
87+
assertTrue(session.isOpen());
88+
89+
}
90+
91+
92+
}

0 commit comments

Comments
 (0)