PageRenderTime 76ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/core/azure-core-amqp/src/test/java/com/azure/core/amqp/implementation/handler/WebSocketsConnectionHandlerTest.java

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 219 lines | 165 code | 38 blank | 16 comment | 4 complexity | 151800e069101c5a9a0ff2d3b391e3b6 MD5 | raw file
Possible License(s): MIT
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.core.amqp.implementation.handler;
  4. import com.azure.core.amqp.AmqpRetryOptions;
  5. import com.azure.core.amqp.AmqpTransportType;
  6. import com.azure.core.amqp.ProxyOptions;
  7. import com.azure.core.amqp.implementation.ClientConstants;
  8. import com.azure.core.amqp.implementation.ConnectionOptions;
  9. import com.azure.core.amqp.models.CbsAuthorizationType;
  10. import com.azure.core.credential.TokenCredential;
  11. import com.azure.core.util.ClientOptions;
  12. import org.apache.qpid.proton.Proton;
  13. import org.apache.qpid.proton.amqp.Symbol;
  14. import org.apache.qpid.proton.engine.Connection;
  15. import org.apache.qpid.proton.engine.EndpointState;
  16. import org.apache.qpid.proton.engine.Event;
  17. import org.apache.qpid.proton.engine.SslDomain;
  18. import org.apache.qpid.proton.engine.SslPeerDetails;
  19. import org.apache.qpid.proton.engine.impl.TransportInternal;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.Assertions;
  22. import org.junit.jupiter.api.BeforeEach;
  23. import org.junit.jupiter.api.Test;
  24. import org.mockito.ArgumentCaptor;
  25. import org.mockito.Captor;
  26. import org.mockito.Mock;
  27. import org.mockito.Mockito;
  28. import org.mockito.MockitoAnnotations;
  29. import reactor.core.scheduler.Scheduler;
  30. import reactor.test.StepVerifier;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import static com.azure.core.amqp.implementation.handler.ConnectionHandler.FRAMEWORK;
  34. import static com.azure.core.amqp.implementation.handler.ConnectionHandler.PLATFORM;
  35. import static com.azure.core.amqp.implementation.handler.WebSocketsConnectionHandler.HTTPS_PORT;
  36. import static com.azure.core.amqp.implementation.handler.WebSocketsConnectionHandler.MAX_FRAME_SIZE;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. public class WebSocketsConnectionHandlerTest {
  42. private static final ClientOptions CLIENT_OPTIONS = new ClientOptions();
  43. private static final String CONNECTION_ID = "some-random-id";
  44. private static final String HOSTNAME = "hostname-random";
  45. private static final SslDomain.VerifyMode VERIFY_MODE = SslDomain.VerifyMode.VERIFY_PEER_NAME;
  46. private static final String PRODUCT = "my-product";
  47. private static final String CLIENT_VERSION = "1.5.1-alpha";
  48. private final SslPeerDetails peerDetails = Proton.sslPeerDetails(HOSTNAME, 2919);
  49. private WebSocketsConnectionHandler handler;
  50. private ConnectionOptions connectionOptions;
  51. @Captor
  52. ArgumentCaptor<Map<Symbol, Object>> argumentCaptor;
  53. @Mock
  54. private TokenCredential tokenCredential;
  55. @Mock
  56. private Scheduler scheduler;
  57. private AutoCloseable mocksCloseable;
  58. @BeforeEach
  59. public void setup() {
  60. mocksCloseable = MockitoAnnotations.openMocks(this);
  61. this.connectionOptions = new ConnectionOptions(HOSTNAME, tokenCredential,
  62. CbsAuthorizationType.SHARED_ACCESS_SIGNATURE, "authorization-scope",
  63. AmqpTransportType.AMQP_WEB_SOCKETS, new AmqpRetryOptions(), ProxyOptions.SYSTEM_DEFAULTS,
  64. scheduler, CLIENT_OPTIONS, VERIFY_MODE, PRODUCT, CLIENT_VERSION);
  65. this.handler = new WebSocketsConnectionHandler(CONNECTION_ID, connectionOptions, peerDetails);
  66. }
  67. @AfterEach
  68. public void teardown() throws Exception {
  69. if (handler != null) {
  70. handler.close();
  71. }
  72. Mockito.framework().clearInlineMock(this);
  73. if (mocksCloseable != null) {
  74. mocksCloseable.close();
  75. }
  76. }
  77. @Test
  78. public void createHandler() {
  79. // Arrange
  80. final Map<String, String> expected = new HashMap<>();
  81. expected.put(PLATFORM.toString(), ClientConstants.PLATFORM_INFO);
  82. expected.put(FRAMEWORK.toString(), ClientConstants.FRAMEWORK_INFO);
  83. // Assert
  84. Assertions.assertEquals(connectionOptions.getHostname(), handler.getHostname());
  85. Assertions.assertEquals(MAX_FRAME_SIZE, handler.getMaxFrameSize());
  86. Assertions.assertEquals(HTTPS_PORT, connectionOptions.getPort());
  87. Assertions.assertEquals(HTTPS_PORT, handler.getProtocolPort());
  88. final Map<String, Object> properties = handler.getConnectionProperties();
  89. expected.forEach((key, value) -> {
  90. Assertions.assertTrue(properties.containsKey(key));
  91. final Object actual = properties.get(key);
  92. Assertions.assertTrue(actual instanceof String);
  93. Assertions.assertEquals(value, actual);
  94. });
  95. }
  96. @Test
  97. public void addsSslLayer() {
  98. // Arrange
  99. final TransportInternal transport = mock(TransportInternal.class);
  100. final Connection connection = mock(Connection.class);
  101. when(connection.getRemoteState()).thenReturn(EndpointState.CLOSED);
  102. final Event event = mock(Event.class);
  103. when(event.getTransport()).thenReturn(transport);
  104. when(event.getConnection()).thenReturn(connection);
  105. // Act
  106. handler.onConnectionBound(event);
  107. StepVerifier.create(handler.getEndpointStates())
  108. .expectNext(EndpointState.CLOSED)
  109. .then(handler::close)
  110. .verifyComplete();
  111. // Assert
  112. verify(transport).ssl(any(SslDomain.class), any(SslPeerDetails.class));
  113. }
  114. @Test
  115. public void onConnectionInit() {
  116. // Arrange
  117. final Map<String, Object> expectedProperties = new HashMap<>(handler.getConnectionProperties());
  118. final Connection connection = mock(Connection.class);
  119. final Event event = mock(Event.class);
  120. when(event.getConnection()).thenReturn(connection);
  121. // Act
  122. handler.onConnectionInit(event);
  123. // Assert
  124. verify(connection).setHostname(HOSTNAME);
  125. verify(connection).setContainer(CONNECTION_ID);
  126. verify(connection).open();
  127. verify(connection).setProperties(argumentCaptor.capture());
  128. Map<Symbol, Object> actualProperties = argumentCaptor.getValue();
  129. Assertions.assertEquals(expectedProperties.size(), actualProperties.size());
  130. expectedProperties.forEach((key, value) -> {
  131. final Symbol symbol = Symbol.getSymbol(key);
  132. final Object removed = actualProperties.remove(symbol);
  133. Assertions.assertNotNull(removed);
  134. final String expected = String.valueOf(value);
  135. final String actual = String.valueOf(removed);
  136. Assertions.assertEquals(expected, actual);
  137. });
  138. Assertions.assertTrue(actualProperties.isEmpty());
  139. }
  140. /**
  141. * verifies that with a different connection endpoint, we still use the correct remote host.
  142. */
  143. @Test
  144. public void onConnectionInitDifferentEndpoint() {
  145. // Arrange
  146. final Map<String, Object> expectedProperties = new HashMap<>(handler.getConnectionProperties());
  147. final Connection connection = mock(Connection.class);
  148. final Event event = mock(Event.class);
  149. when(event.getConnection()).thenReturn(connection);
  150. final String fullyQualifiedNamespace = "foo.servicebus.windows.net";
  151. final String customEndpoint = "internal.service.net";
  152. final int port = 9888;
  153. final ConnectionOptions connectionOptions = new ConnectionOptions(fullyQualifiedNamespace, tokenCredential,
  154. CbsAuthorizationType.SHARED_ACCESS_SIGNATURE, "authorization-scope",
  155. AmqpTransportType.AMQP_WEB_SOCKETS, new AmqpRetryOptions(), ProxyOptions.SYSTEM_DEFAULTS, scheduler,
  156. CLIENT_OPTIONS, VERIFY_MODE, PRODUCT, CLIENT_VERSION, customEndpoint, port);
  157. try (WebSocketsConnectionHandler handler = new WebSocketsConnectionHandler(CONNECTION_ID, connectionOptions,
  158. peerDetails)) {
  159. // Act
  160. handler.onConnectionInit(event);
  161. // Assert
  162. verify(connection).setHostname(connectionOptions.getFullyQualifiedNamespace());
  163. verify(connection).setContainer(CONNECTION_ID);
  164. verify(connection).open();
  165. verify(connection).setProperties(argumentCaptor.capture());
  166. Map<Symbol, Object> actualProperties = argumentCaptor.getValue();
  167. Assertions.assertEquals(expectedProperties.size(), actualProperties.size());
  168. expectedProperties.forEach((key, value) -> {
  169. final Symbol symbol = Symbol.getSymbol(key);
  170. final Object removed = actualProperties.remove(symbol);
  171. Assertions.assertNotNull(removed);
  172. final String expected = String.valueOf(value);
  173. final String actual = String.valueOf(removed);
  174. Assertions.assertEquals(expected, actual);
  175. });
  176. Assertions.assertTrue(actualProperties.isEmpty());
  177. Assertions.assertEquals(port, connectionOptions.getPort());
  178. Assertions.assertEquals(port, handler.getProtocolPort());
  179. }
  180. }
  181. }