|
| 1 | +package oncoding.concoder.controller; |
| 2 | + |
| 3 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 4 | + |
| 5 | +import io.restassured.RestAssured; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.BlockingQueue; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | +import java.util.concurrent.LinkedBlockingDeque; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.TimeoutException; |
| 13 | +import oncoding.concoder.dto.ChatDTO.MessageRequest; |
| 14 | +import oncoding.concoder.dto.ChatDTO.MessageResponse; |
| 15 | +import oncoding.concoder.dto.ChatDTO.SessionRequest; |
| 16 | +import oncoding.concoder.dto.ChatDTO.SessionResponse; |
| 17 | +import oncoding.concoder.dto.ChatDTO.UserResponse; |
| 18 | +import oncoding.concoder.model.Room; |
| 19 | +import oncoding.concoder.model.User; |
| 20 | +import oncoding.concoder.repository.RoomRepository; |
| 21 | +import oncoding.concoder.repository.UserRepository; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.test.context.SpringBootTest; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 29 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 30 | +import org.springframework.messaging.converter.MappingJackson2MessageConverter; |
| 31 | +import org.springframework.messaging.simp.stomp.StompSession; |
| 32 | +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; |
| 33 | +import org.springframework.test.context.ActiveProfiles; |
| 34 | +import org.springframework.util.concurrent.ListenableFuture; |
| 35 | +import org.springframework.web.socket.client.standard.StandardWebSocketClient; |
| 36 | +import org.springframework.web.socket.messaging.WebSocketStompClient; |
| 37 | +import org.springframework.web.socket.sockjs.client.SockJsClient; |
| 38 | +import org.springframework.web.socket.sockjs.client.Transport; |
| 39 | +import org.springframework.web.socket.sockjs.client.WebSocketTransport; |
| 40 | + |
| 41 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 42 | +@ActiveProfiles("local") |
| 43 | +class ChattingControllerTest { |
| 44 | + |
| 45 | + @LocalServerPort |
| 46 | + private int port; |
| 47 | + |
| 48 | + private BlockingQueue<SessionResponse> users; |
| 49 | + private BlockingQueue<MessageResponse> messages; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private UserRepository userRepository; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private RoomRepository roomRepository; |
| 56 | + |
| 57 | + private void 유저_초기화(){ |
| 58 | + userRepository.deleteAll(); |
| 59 | + } |
| 60 | + |
| 61 | + private void 방_초기화(){ |
| 62 | + roomRepository.deleteAll(); |
| 63 | + } |
| 64 | + |
| 65 | + private void 유저_삽입() { |
| 66 | + userRepository.save(new User("와일더")); |
| 67 | + userRepository.save(new User("마이클")); |
| 68 | + userRepository.save(new User("제이슨")); |
| 69 | + userRepository.save(new User("오스카")); |
| 70 | + } |
| 71 | + |
| 72 | + private void 방_생성() { |
| 73 | + roomRepository.save(new Room(2)); |
| 74 | + roomRepository.save(new Room(4)); |
| 75 | + roomRepository.save(new Room(5)); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @BeforeEach |
| 80 | + public void setUp() { |
| 81 | + RestAssured.port = port; |
| 82 | + 유저_초기화(); |
| 83 | + 방_초기화(); |
| 84 | + users = new LinkedBlockingDeque<>(); |
| 85 | + messages = new LinkedBlockingDeque<>(); |
| 86 | + 유저_삽입(); |
| 87 | + 방_생성(); |
| 88 | + } |
| 89 | + |
| 90 | + private WebSocketStompClient 웹_소켓_STOMP_CLIENT() { |
| 91 | + StandardWebSocketClient standardWebSocketClient = new StandardWebSocketClient(); |
| 92 | + WebSocketTransport webSocketTransport = new WebSocketTransport(standardWebSocketClient); |
| 93 | + List<Transport> transports = Collections.singletonList(webSocketTransport); |
| 94 | + SockJsClient sockJsClient = new SockJsClient(transports); |
| 95 | + |
| 96 | + return new WebSocketStompClient(sockJsClient); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @DisplayName("유저가 입장하고 메시지를 보내면 해당 방에 메시지가 브로드 캐스팅된다.") |
| 101 | + @Test |
| 102 | + void enterUserAndBroadCastMessage() |
| 103 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 104 | + Room room = roomRepository.findAll().get(0); |
| 105 | + User user = userRepository.findAll().get(0); |
| 106 | + |
| 107 | + //expected result |
| 108 | + UserResponse expectedUser = UserResponse.from(user); |
| 109 | + MessageResponse expectedMessage = new MessageResponse(user.getId(), "채팅을 보내 봅니다."); |
| 110 | + |
| 111 | + |
| 112 | + // Settings |
| 113 | + WebSocketStompClient webSocketStompClient = 웹_소켓_STOMP_CLIENT(); |
| 114 | + webSocketStompClient.setMessageConverter(new MappingJackson2MessageConverter()); |
| 115 | + |
| 116 | + |
| 117 | + // Connection |
| 118 | + ListenableFuture<StompSession> connect = webSocketStompClient |
| 119 | + .connect("ws://localhost:" + port + "/api/ws-connection", new StompSessionHandlerAdapter() { |
| 120 | + }); |
| 121 | + StompSession stompSession = connect.get(60, TimeUnit.SECONDS); |
| 122 | + |
| 123 | + stompSession.subscribe(String.format("/sub/rooms/%s", room.getId()), new StompFrameHandlerImpl(new SessionResponse(), users)); |
| 124 | + stompSession.send(String.format("/pub/rooms/%s", room.getId()), new SessionRequest(user.getId(), "1A2B3C4D")); |
| 125 | + |
| 126 | + stompSession.subscribe(String.format("/sub/rooms/%s/chat", room.getId()), new StompFrameHandlerImpl(new MessageResponse(), messages)); |
| 127 | + stompSession.send(String.format("/sub/rooms/%s/chat", room.getId()), new MessageRequest(user.getId(), "채팅을 보내 봅니다.")); |
| 128 | + |
| 129 | + SessionResponse sessionResponse = users.poll(5, TimeUnit.SECONDS); |
| 130 | + MessageResponse messageResponse = messages.poll(5, TimeUnit.SECONDS); |
| 131 | + |
| 132 | + // Then |
| 133 | + assertThat(sessionResponse.getUserResponses().get(0)).usingRecursiveComparison().isEqualTo(expectedUser); |
| 134 | + assertThat(messageResponse).usingRecursiveComparison().isEqualTo(expectedMessage); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments