Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. package nl.mblokhuijzen.springwebsockets;
  2.  
  3. import nl.mblokhuijzen.springwebsockets.beans.GameState;
  4. import nl.mblokhuijzen.springwebsockets.beans.Move;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.messaging.converter.MappingJackson2MessageConverter;
  11. import org.springframework.messaging.simp.stomp.StompFrameHandler;
  12. import org.springframework.messaging.simp.stomp.StompHeaders;
  13. import org.springframework.messaging.simp.stomp.StompSession;
  14. import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
  15. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  16. import org.springframework.web.socket.client.standard.StandardWebSocketClient;
  17. import org.springframework.web.socket.messaging.WebSocketStompClient;
  18. import org.springframework.web.socket.sockjs.client.SockJsClient;
  19. import org.springframework.web.socket.sockjs.client.Transport;
  20. import org.springframework.web.socket.sockjs.client.WebSocketTransport;
  21.  
  22. import java.lang.reflect.Type;
  23. import java.net.URISyntaxException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.UUID;
  27. import java.util.concurrent.CompletableFuture;
  28. import java.util.concurrent.ExecutionException;
  29. import java.util.concurrent.TimeoutException;
  30.  
  31. import static java.util.concurrent.TimeUnit.SECONDS;
  32. import static org.junit.Assert.assertNotNull;
  33.  
  34. @RunWith(SpringJUnit4ClassRunner.class)
  35. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  36. public class WebsocketEndpointIT {
  37. @Value("${local.server.port}")
  38. private int port;
  39. private String URL;
  40.  
  41. private static final String SEND_CREATE_BOARD_ENDPOINT = "/app/create/";
  42. private static final String SEND_MOVE_ENDPOINT = "/app/move/";
  43. private static final String SUBSCRIBE_CREATE_BOARD_ENDPOINT = "/topic/board/";
  44. private static final String SUBSCRIBE_MOVE_ENDPOINT = "/topic/move/";
  45.  
  46. private CompletableFuture<GameState> completableFuture;
  47.  
  48. @Before
  49. public void setup() {
  50. completableFuture = new CompletableFuture<>();
  51. URL = "ws://localhost:" + port + "/game";
  52. }
  53.  
  54.  
  55. @Test
  56. public void testCreateGameEndpoint() throws URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
  57. String uuid = UUID.randomUUID().toString();
  58.  
  59. WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
  60. stompClient.setMessageConverter(new MappingJackson2MessageConverter());
  61.  
  62. StompSession stompSession = stompClient.connect(URL, new StompSessionHandlerAdapter() {
  63. }).get(1, SECONDS);
  64.  
  65. stompSession.subscribe(SUBSCRIBE_CREATE_BOARD_ENDPOINT + uuid, new CreateGameStompFrameHandler());
  66. stompSession.send(SEND_CREATE_BOARD_ENDPOINT + uuid, null);
  67.  
  68. GameState gameState = completableFuture.get(10, SECONDS);
  69.  
  70. assertNotNull(gameState);
  71. }
  72.  
  73. @Test
  74. public void testMakeMoveEndpoint() throws InterruptedException, ExecutionException, TimeoutException {
  75. String uuid = UUID.randomUUID().toString();
  76.  
  77. WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
  78. stompClient.setMessageConverter(new MappingJackson2MessageConverter());
  79.  
  80. StompSession stompSession = stompClient.connect(URL, new StompSessionHandlerAdapter() {
  81. }).get(1, SECONDS);
  82.  
  83. stompSession.subscribe(SUBSCRIBE_MOVE_ENDPOINT + uuid, new CreateGameStompFrameHandler());
  84. stompSession.send(SEND_MOVE_ENDPOINT + uuid, new Move(1, 0));
  85. GameState gameStateAfterMove = completableFuture.get(5, SECONDS);
  86.  
  87. assertNotNull(gameStateAfterMove);
  88. }
  89.  
  90. private List<Transport> createTransportClient() {
  91. List<Transport> transports = new ArrayList<>(1);
  92. transports.add(new WebSocketTransport(new StandardWebSocketClient()));
  93. return transports;
  94. }
  95.  
  96. private class CreateGameStompFrameHandler implements StompFrameHandler {
  97. @Override
  98. public Type getPayloadType(StompHeaders stompHeaders) {
  99. return GameState.class;
  100. }
  101.  
  102. @Override
  103. public void handleFrame(StompHeaders stompHeaders, Object o) {
  104. completableFuture.complete((GameState) o);
  105. }
  106. }
  107. }
  108. Contact GitHub API Training Shop Blog About
  109. © 2017 GitHub, Inc. Terms Privacy Security Status Help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement