Skip to content

Commit

Permalink
wip WebSocketProxyServerHandlerTest
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Hernán Carle <pablo.carle@broadcom.com>
  • Loading branch information
Pablo Hernán Carle committed Aug 23, 2024
1 parent c58fc48 commit 6a55037
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.scheduling.annotation.AsyncResult;
Expand All @@ -30,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -45,7 +49,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class WebSocketProxyServerHandlerTest {

private WebSocketProxyServerHandler underTest;
private WebSocketRoutedSessionFactory webSocketRoutedSessionFactory;
private Map<String, WebSocketRoutedSession> routedSessions;
Expand All @@ -68,6 +74,7 @@ public void setup() {

@Nested
class WhenTheConnectionIsEstablished {

WebSocketSession establishedSession;

@BeforeEach
Expand All @@ -77,6 +84,7 @@ void prepareSessionMock() {

@Nested
class ThenTheValidSessionIsStoredInternally {

@BeforeEach
void prepareRoutedService() {
String serviceId = "valid-service";
Expand All @@ -99,15 +107,19 @@ void prepareRoutedService() {
* Proper WebSocketSession is stored.
*/
@Test
void givenValidRoute() throws Exception {
void givenValidRoute() throws Exception {
String path = "wss://gatewayHost:1443/valid-service/ws/v1/valid-path";
when(webSocketRoutedSessionFactory.session(any(), any(), any())).thenReturn(mock(WebSocketRoutedSession.class));
WebSocketRoutedSession routedSession = mock(WebSocketRoutedSession.class);
when(webSocketRoutedSessionFactory.session(any(), any(), any())).thenReturn(routedSession);
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(lbClient.choose(any())).thenReturn(serviceInstance);
String establishedSessionId = "validAndUniqueId";
when(establishedSession.getId()).thenReturn(establishedSessionId);
when(establishedSession.getUri()).thenReturn(new URI(path));

CountDownLatch latch = new CountDownLatch(1);
when(routedSession.getWebSocketClientSession()).thenReturn(null);

underTest.afterConnectionEstablished(establishedSession);

verify(webSocketRoutedSessionFactory).session(any(), any(), any());
Expand All @@ -119,6 +131,7 @@ void givenValidRoute() throws Exception {

@Nested
class ThenTheSocketIsClosed {

@BeforeEach
void sessionIsOpen() {
when(establishedSession.isOpen()).thenReturn(true);
Expand Down Expand Up @@ -186,7 +199,6 @@ void givenNoInstanceOfTheServiceIsInTheRepository() throws Exception {
@Test
void givenNullService_thenCloseWebSocket() throws Exception {
when(establishedSession.getUri()).thenReturn(new URI("wss://gatewayHost:1443/service-without-instance/ws/v1/valid-path"));
when(lbClient.choose(any())).thenReturn(null);
RoutedServices routesForSpecificValidService = mock(RoutedServices.class);
when(routesForSpecificValidService.findServiceByGatewayUrl("ws/v1"))
.thenReturn(null);
Expand All @@ -201,17 +213,18 @@ void givenNullService_thenCloseWebSocket() throws Exception {

@Nested
class GivenValidExistingSession {

@Mock
WebSocketSession establishedSession;
@Mock
WebSocketRoutedSession internallyStoredSession;
@Mock
WebSocketMessage<String> passedMessage;

@BeforeEach
void prepareSessionMock() {
establishedSession = mock(WebSocketSession.class);
String validSessionId = "123";
when(establishedSession.getId()).thenReturn(validSessionId);
passedMessage = mock(WebSocketMessage.class);
internallyStoredSession = mock(WebSocketRoutedSession.class);
routedSessions.put(validSessionId, internallyStoredSession);
}

Expand Down Expand Up @@ -239,6 +252,10 @@ void whenTheConnectionIsClosed_thenClientSessionIsAlsoClosed() throws IOExceptio

@Test
void whenTheMessageIsReceived_thenTheMessageIsPassedToTheSession() throws Exception {
WebSocketSession clientSession = mock(WebSocketSession.class);
when(clientSession.isOpen()).thenReturn(true);
when(internallyStoredSession.getWebSocketClientSession()).thenReturn(AsyncResult.forValue(clientSession));

underTest.handleMessage(establishedSession, passedMessage);

verify(internallyStoredSession).sendMessageToServer(passedMessage);
Expand Down Expand Up @@ -280,15 +297,18 @@ void whenClosingRoutedSessionThrowException_thenCatchIt() throws IOException {

@Nested
class WhenGettingRoutedSessions {

@Test
void thenReturnThem() {
Map<String, WebSocketRoutedSession> expectedRoutedSessions = underTest.getRoutedSessions();
assertThat(expectedRoutedSessions, is(routedSessions));
}

}

@Nested
class WhenGettingSubProtocols {

@Test
void thenReturnThem() {
List<String> protocol = new ArrayList<>();
Expand All @@ -297,5 +317,7 @@ void thenReturnThem() {
List<String> subProtocols = underTest.getSubProtocols();
assertThat(subProtocols, is(protocol));
}

}

}

0 comments on commit 6a55037

Please sign in to comment.