11
11
package org .zowe .apiml .gateway .ws ;
12
12
13
13
import org .junit .jupiter .api .BeforeEach ;
14
+ import org .junit .jupiter .api .Nested ;
14
15
import org .junit .jupiter .api .Test ;
16
+ import org .junit .jupiter .params .ParameterizedTest ;
17
+ import org .junit .jupiter .params .provider .ValueSource ;
15
18
import org .springframework .cloud .client .ServiceInstance ;
16
19
import org .springframework .cloud .client .discovery .DiscoveryClient ;
17
20
import org .springframework .web .socket .CloseStatus ;
@@ -52,36 +55,7 @@ public void setup() {
52
55
);
53
56
}
54
57
55
- /**
56
- * Happy Path
57
- * <p>
58
- * The Handler is properly created
59
- * Specified Route is added to the list
60
- * The connection is established
61
- * The URI contains the valid service Id
62
- * The service associated with given URI is retrieved
63
- * Proper WebSocketSession is stored.
64
- */
65
- @ Test
66
- void givenValidRoute_whenTheConnectionIsEstablished_thenTheValidSessionIsStoredInternally () throws Exception {
67
- RoutedServices routesForSpecificValidService = mock (RoutedServices .class );
68
- when (routesForSpecificValidService .findServiceByGatewayUrl ("ws/1" ))
69
- .thenReturn (new RoutedService ("api-v1" , "api/v1" , "/api-v1/api/v1" ));
70
- ServiceInstance foundService = validServiceInstance ();
71
- when (discoveryClient .getInstances ("api-v1" )).thenReturn (Collections .singletonList (foundService ));
72
- underTest .addRoutedServices ("api-v1" , routesForSpecificValidService );
73
- when (webSocketRoutedSessionFactory .session (any (), any (), any ())).thenReturn (mock (WebSocketRoutedSession .class ));
74
-
75
- WebSocketSession establishedSession = mock (WebSocketSession .class );
76
- String establishedSessionId = "validAndUniqueId" ;
77
- when (establishedSession .getId ()).thenReturn (establishedSessionId );
78
- when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/gateway/1/api-v1/api/v1" ));
79
- underTest .afterConnectionEstablished (establishedSession );
80
-
81
- verify (webSocketRoutedSessionFactory ).session (any (), any (), any ());
82
- WebSocketRoutedSession preparedSession = routedSessions .get (establishedSessionId );
83
- assertThat (preparedSession , is (notNullValue ()));
84
- }
58
+
85
59
86
60
private ServiceInstance validServiceInstance () {
87
61
ServiceInstance validService = mock (ServiceInstance .class );
@@ -92,95 +66,159 @@ private ServiceInstance validServiceInstance() {
92
66
return validService ;
93
67
}
94
68
95
- /**
96
- * Error Path
97
- * <p>
98
- * The Handler is properly created
99
- * The connection is established
100
- * The URI doesn't contain all needed parts
101
- * The WebSocketSession is closed
102
- */
103
- @ Test
104
- void givenInvalidURI_whenTheConnectionIsEstablished_thenTheSocketIsClosedAsNotAcceptable () throws Exception {
105
- WebSocketSession establishedSession = mock (WebSocketSession .class );
106
- when (establishedSession .isOpen ()).thenReturn (true );
107
- when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/invalidUrl" ));
108
-
109
- underTest .afterConnectionEstablished (establishedSession );
110
-
111
- verify (establishedSession ).close (new CloseStatus (CloseStatus .NOT_ACCEPTABLE .getCode (), "Invalid URL format" ));
69
+ @ Nested
70
+ class WhenTheConnectionIsEstablished {
71
+ WebSocketSession establishedSession ;
72
+
73
+ @ BeforeEach
74
+ void prepareSessionMock () {
75
+ establishedSession = mock (WebSocketSession .class );
76
+ }
77
+
78
+ @ Nested
79
+ class ThenTheValidSessionIsStoredInternally {
80
+ @ BeforeEach
81
+ void prepareRoutedService () {
82
+ String serviceId = "valid-service" ;
83
+
84
+ RoutedServices routesForSpecificValidService = mock (RoutedServices .class );
85
+ when (routesForSpecificValidService .findServiceByGatewayUrl ("ws/v1" ))
86
+ .thenReturn (new RoutedService ("ws-v1" , "ws/v1" , "/valid-service/ws/v1" ));
87
+ ServiceInstance foundService = validServiceInstance ();
88
+ when (discoveryClient .getInstances (serviceId )).thenReturn (Collections .singletonList (foundService ));
89
+
90
+ underTest .addRoutedServices (serviceId , routesForSpecificValidService );
91
+ }
92
+
93
+ /**
94
+ * Happy Path
95
+ * <p>
96
+ * The Handler is properly created
97
+ * Specified Route is added to the list
98
+ * The connection is established
99
+ * The URI contains the valid service Id
100
+ * The service associated with given URI is retrieved
101
+ * Proper WebSocketSession is stored.
102
+ */
103
+ @ ParameterizedTest (name = "WhenTheConnectionIsEstablished.ThenTheValidSessionIsStoredInternally#givenValidRoute {0}" )
104
+ @ ValueSource (strings = {"wss://gatewayHost:1443/valid-service/ws/v1/valid-path" , "wss://gatewayHost:1443/ws/v1/valid-service/valid-path" })
105
+ void givenValidRoute (String path ) throws Exception {
106
+ when (webSocketRoutedSessionFactory .session (any (), any (), any ())).thenReturn (mock (WebSocketRoutedSession .class ));
107
+
108
+ String establishedSessionId = "validAndUniqueId" ;
109
+ when (establishedSession .getId ()).thenReturn (establishedSessionId );
110
+ when (establishedSession .getUri ()).thenReturn (new URI (path ));
111
+
112
+ underTest .afterConnectionEstablished (establishedSession );
113
+
114
+ verify (webSocketRoutedSessionFactory ).session (any (), any (), any ());
115
+ WebSocketRoutedSession preparedSession = routedSessions .get (establishedSessionId );
116
+ assertThat (preparedSession , is (notNullValue ()));
117
+ }
118
+ }
119
+
120
+
121
+ @ Nested
122
+ class ThenTheSocketIsClosed {
123
+ @ BeforeEach
124
+ void sessionIsOpen () {
125
+ when (establishedSession .isOpen ()).thenReturn (true );
126
+ }
127
+
128
+ /**
129
+ * Error Path
130
+ * <p>
131
+ * The Handler is properly created
132
+ * The connection is established
133
+ * The URI doesn't contain all needed parts
134
+ * The WebSocketSession is closed
135
+ */
136
+ @ Test
137
+ void givenInvalidURI () throws Exception {
138
+ when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/invalidUrl" ));
139
+
140
+ underTest .afterConnectionEstablished (establishedSession );
141
+
142
+ verify (establishedSession ).close (new CloseStatus (CloseStatus .NOT_ACCEPTABLE .getCode (), "Invalid URL format" ));
143
+ }
144
+
145
+ /**
146
+ * Error Path
147
+ * <p>
148
+ * The Handler is properly created
149
+ * The connection is established
150
+ * The URI contains the service Id for which there is no service
151
+ * The WebSocketSession is closed
152
+ */
153
+ @ Test
154
+ void givenInvalidRoute () throws Exception {
155
+ when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/ws/v1/non_existent_service/valid-path" ));
156
+
157
+ underTest .afterConnectionEstablished (establishedSession );
158
+
159
+ verify (establishedSession ).close (new CloseStatus (CloseStatus .NOT_ACCEPTABLE .getCode (), "Requested service non_existent_service is not known by the gateway" ));
160
+ }
161
+
162
+ /**
163
+ * Error Path
164
+ * <p>
165
+ * The Handler is properly created
166
+ * Specified Route is added to the list
167
+ * The connection is established
168
+ * The URI contains the valid service Id
169
+ * The service associated with given URI is retrieved
170
+ * The service isn't available in the Discovery Service
171
+ * Proper WebSocketSession is stored.
172
+ */
173
+ @ Test
174
+ void givenNoInstanceOfTheServiceIsInTheRepository () throws Exception {
175
+ when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/service-without-instance/ws/v1/valid-path" ));
176
+
177
+ RoutedServices routesForSpecificValidService = mock (RoutedServices .class );
178
+ when (routesForSpecificValidService .findServiceByGatewayUrl ("ws/v1" ))
179
+ .thenReturn (new RoutedService ("api-v1" , "api/v1" , "/api-v1/api/v1" ));
180
+ underTest .addRoutedServices ("service-without-instance" , routesForSpecificValidService );
181
+
182
+ underTest .afterConnectionEstablished (establishedSession );
183
+
184
+ verify (establishedSession ).close (new CloseStatus (CloseStatus .SERVICE_RESTARTED .getCode (), "Requested service service-without-instance does not have available instance" ));
185
+ }
186
+ }
112
187
}
113
188
114
- /**
115
- * Error Path
116
- * <p>
117
- * The Handler is properly created
118
- * The connection is established
119
- * The URI contains the service Id for which there is no service
120
- * The WebSocketSession is closed
121
- */
122
- @ Test
123
- void givenInvalidRoute_whenTheConnectionIsEstablished_thenTheSocketIsClosedAsNotAcceptable () throws Exception {
124
- WebSocketSession establishedSession = mock (WebSocketSession .class );
125
- when (establishedSession .isOpen ()).thenReturn (true );
126
- when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/api/v1/non_existent_service/api/v1" ));
127
-
128
- underTest .afterConnectionEstablished (establishedSession );
129
-
130
- verify (establishedSession ).close (new CloseStatus (CloseStatus .NOT_ACCEPTABLE .getCode (), "Requested service non_existent_service is not known by the gateway" ));
131
- }
189
+ @ Nested
190
+ class GivenValidExistingSession {
191
+ WebSocketSession establishedSession ;
192
+ WebSocketRoutedSession internallyStoredSession ;
132
193
133
- /**
134
- * Error Path
135
- * <p>
136
- * The Handler is properly created
137
- * Specified Route is added to the list
138
- * The connection is established
139
- * The URI contains the valid service Id
140
- * The service associated with given URI is retrieved
141
- * The service isn't available in the Discovery Service
142
- * Proper WebSocketSession is stored.
143
- */
144
- @ Test
145
- void givenNoInstanceOfTheServiceIsInTheRepository_whenTheConnectionIsEstablished_thenTheSocketIsClosedAsServiceRestarted () throws Exception {
146
- WebSocketSession establishedSession = mock (WebSocketSession .class );
147
- when (establishedSession .isOpen ()).thenReturn (true );
148
- when (establishedSession .getUri ()).thenReturn (new URI ("wss://gatewayHost:1443/api/v1/api-v1/api/v1" ));
149
- RoutedServices routesForSpecificValidService = mock (RoutedServices .class );
150
- when (routesForSpecificValidService .findServiceByGatewayUrl ("ws/v1" ))
151
- .thenReturn (new RoutedService ("api-v1" , "api/v1" , "/api-v1/api/v1" ));
152
- underTest .addRoutedServices ("api-v1" , routesForSpecificValidService );
153
-
154
- underTest .afterConnectionEstablished (establishedSession );
155
-
156
- verify (establishedSession ).close (new CloseStatus (CloseStatus .SERVICE_RESTARTED .getCode (), "Requested service api-v1 does not have available instance" ));
157
- }
194
+ @ BeforeEach
195
+ void prepareSessionMock () {
196
+ establishedSession = mock (WebSocketSession .class );
197
+ String validSessionId = "123" ;
198
+ when (establishedSession .getId ()).thenReturn (validSessionId );
158
199
159
- @ Test
160
- void givenValidSession_whenTheConnectionIsClosed_thenTheSessionIsClosedAndRemovedFromRepository () throws Exception {
161
- CloseStatus normalClose = CloseStatus .NORMAL ;
162
- WebSocketSession establishedSession = mock (WebSocketSession .class );
163
- String validSessionId = "123" ;
164
- when (establishedSession .getId ()).thenReturn (validSessionId );
165
- routedSessions .put (validSessionId , mock (WebSocketRoutedSession .class ));
200
+ internallyStoredSession = mock (WebSocketRoutedSession .class );
201
+ routedSessions .put (validSessionId , internallyStoredSession );
202
+ }
166
203
167
- underTest .afterConnectionClosed (establishedSession , normalClose );
204
+ @ Test
205
+ void whenTheConnectionIsClosed_thenTheSessionIsClosedAndRemovedFromRepository () throws Exception {
206
+ CloseStatus normalClose = CloseStatus .NORMAL ;
168
207
169
- verify (establishedSession ).close (normalClose );
170
- assertThat (routedSessions .entrySet (), hasSize (0 ));
171
- }
208
+ underTest .afterConnectionClosed (establishedSession , normalClose );
209
+
210
+ verify (establishedSession ).close (normalClose );
211
+ assertThat (routedSessions .entrySet (), hasSize (0 ));
212
+ }
213
+
214
+ @ Test
215
+ void whenTheMessageIsReceived_thenTheMessageIsPassedToTheSession () throws Exception {
216
+ WebSocketMessage <String > passedMessage = mock (WebSocketMessage .class );
172
217
173
- @ Test
174
- void givenValidSession_whenTheMessageIsReceived_thenTheMessageIsPassedToTheSession () throws Exception {
175
- WebSocketSession establishedSession = mock (WebSocketSession .class );
176
- String validSessionId = "123" ;
177
- when (establishedSession .getId ()).thenReturn (validSessionId );
178
- WebSocketRoutedSession internallyStoredSession = mock (WebSocketRoutedSession .class );
179
- routedSessions .put (validSessionId , internallyStoredSession );
180
- WebSocketMessage <String > passedMessage = mock (WebSocketMessage .class );
218
+ underTest .handleMessage (establishedSession , passedMessage );
181
219
182
- underTest .handleMessage (establishedSession , passedMessage );
220
+ verify (internallyStoredSession ).sendMessageToServer (passedMessage );
221
+ }
183
222
184
- verify (internallyStoredSession ).sendMessageToServer (passedMessage );
185
223
}
186
224
}
0 commit comments