Skip to content

Commit

Permalink
refactor: 테스트 추
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Apr 18, 2024
1 parent 48fcc60 commit 33f4148
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class WebSocketHandler extends TextWebSocketHandler {

private static final String TYPE_KEY = "type";
private static final String PING_STATUS_KEY = "pingStatus";

private final WebSocketHandleTextMessageProviderComposite providerComposite;
private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -47,8 +48,8 @@ public void afterConnectionClosed(final WebSocketSession session, final CloseSta
}

@Override
public void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
public void handlePongMessage(WebSocketSession session, PongMessage message) {
final Map<String, Object> attributes = session.getAttributes();
attributes.put("pingStatus", true);
attributes.put(PING_STATUS_KEY, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ddang.ddang.chat.application.event.MessageNotificationEvent;
import com.ddang.ddang.chat.application.event.UpdateReadMessageLogEvent;
import com.ddang.ddang.chat.domain.WebSocketChatSessions;
import com.ddang.ddang.chat.domain.WebSocketSessions;
import com.ddang.ddang.chat.domain.repository.ReadMessageLogRepository;
import com.ddang.ddang.chat.handler.fixture.ChatWebSocketHandleTextMessageProviderTestFixture;
import com.ddang.ddang.configuration.IsolateDatabase;
Expand All @@ -22,16 +23,22 @@
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.event.ApplicationEvents;
import org.springframework.test.context.event.RecordApplicationEvents;
import org.springframework.web.socket.PingMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@IsolateDatabase
@RecordApplicationEvents
Expand All @@ -48,6 +55,9 @@ class ChatWebSocketHandleTextMessageProviderTest extends ChatWebSocketHandleText
@SpyBean
WebSocketChatSessions sessions;

@SpyBean
WebSocketSessions webSocketSessions;

@Mock
WebSocketSession writerSession;

Expand Down Expand Up @@ -186,4 +196,35 @@ class ChatWebSocketHandleTextMessageProviderTest extends ChatWebSocketHandleText
final boolean actual = sessions.containsByUserId(채팅방.getId(), 발신자.getId());
assertThat(actual).isFalse();
}

@Test
void 저장되어_있는_세션에_ping_메시지를_보낸다() throws IOException {
// given
given(writerSession.getAttributes()).willReturn(발신자_세션_attribute_정보);
given(webSocketSessions.getSessions()).willReturn(Set.of(writerSession));
given(sessions.getChatRoomSessions()).willReturn(Map.of(채팅방.getId(), webSocketSessions));

// when
provider.sendPingSessions();

// then
verify(sessions, never()).remove(writerSession);
verify(writerSession, times(1)).sendMessage(new PingMessage());
}

@Test
void 연결이_끊긴_세션은_삭제한다() throws IOException {
// given
given(writerSession.getAttributes()).willReturn(연결이_끊긴_세션_attribute_정보);
given(webSocketSessions.getSessions()).willReturn(Set.of(writerSession));
given(sessions.getChatRoomSessions()).willReturn(Map.of(채팅방.getId(), webSocketSessions));
willDoNothing().given(sessions).remove(writerSession);

// when
provider.sendPingSessions();

// then
verify(sessions, times(1)).remove(writerSession);
verify(writerSession, never()).sendMessage(new PingMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.ddang.ddang.chat.application.event.CreateReadMessageLogEvent;
import com.ddang.ddang.chat.domain.ChatRoom;
import com.ddang.ddang.chat.domain.repository.ChatRoomRepository;
import com.ddang.ddang.chat.domain.repository.ReadMessageLogRepository;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.Reliability;
import com.ddang.ddang.user.domain.User;
Expand Down Expand Up @@ -46,6 +45,7 @@ public class ChatWebSocketHandleTextMessageProviderTestFixture {

protected Map<String, Object> 발신자_세션_attribute_정보;
protected Map<String, Object> 수신자_세션_attribute_정보;
protected Map<String, Object> 연결이_끊긴_세션_attribute_정보;
protected Map<String, String> 메시지_전송_데이터;

protected CreateReadMessageLogEvent 메시지_로그_생성_이벤트;
Expand Down Expand Up @@ -86,8 +86,21 @@ void setUpFixture() {

chatRoomRepository.save(채팅방);

발신자_세션_attribute_정보 = new HashMap<>(Map.of("userId", 발신자.getId(), "baseUrl", "/images"));
수신자_세션_attribute_정보 = new HashMap<>(Map.of("userId", 수신자.getId(), "baseUrl", "/images"));
발신자_세션_attribute_정보 = new HashMap<>(Map.of(
"userId", 발신자.getId(),
"baseUrl", "/images",
"pingStatus", true
));
수신자_세션_attribute_정보 = new HashMap<>(Map.of(
"userId", 수신자.getId(),
"baseUrl", "/images",
"pingStatus", true
));
연결이_끊긴_세션_attribute_정보 = new HashMap<>(Map.of(
"userId", 수신자.getId(),
"baseUrl", "/images",
"pingStatus", false
));
메시지_전송_데이터 = Map.of(
"chatRoomId", String.valueOf(채팅방.getId()),
"receiverId", String.valueOf(수신자.getId()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.web.socket.PongMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.BDDMockito.given;
Expand Down Expand Up @@ -69,4 +71,16 @@ class WebSocketHandlerTest extends WebSocketHandlerTestFixture {
// then
verify(provider, times(1)).remove(any(WebSocketSession.class));
}

@Test
void pong_메시지_수신시_ping_상태를_참으로_변환한다() {
// given
given(session.getAttributes()).willReturn(세션_attribute_정보);

// when
webSocketHandler.handlePongMessage(session, new PongMessage());

// then
assertThat((boolean) 세션_attribute_정보.get(ping_상태_키)).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
public class WebSocketHandlerTestFixture {

protected Long 사용자_아이디 = 1L;
protected String ping_상태_키 = "pingStatus";
protected Map<String, Object> 세션_attribute_정보 = new HashMap<>(
Map.of(
"type", TextMessageType.CHATTINGS.name(),
"data", Map.of("userId", 사용자_아이디, "baseUrl", "/images")
"data", Map.of("userId", 사용자_아이디, "baseUrl", "/images", ping_상태_키, false)
)
);
protected TextMessage 전송할_메시지 = new TextMessage("메시지");
Expand Down

0 comments on commit 33f4148

Please sign in to comment.