From 4f7a9d9a3d36d8e06134d0d3f775e0f67a8cd53a Mon Sep 17 00:00:00 2001 From: swonny Date: Wed, 1 May 2024 17:36:54 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EB=A9=94=EC=8B=9C=EC=A7=80=20?= =?UTF-8?q?=EC=A0=84=EC=86=A1=20=EB=B0=98=ED=99=98=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 22 ++++++++++ .../websocket/handler/dto/ChattingType.java | 19 +++----- .../handler/dto/TextMessageType.java | 15 ++++++- .../UnsupportedChattingTypeException.java | 7 +++ .../handler/dto/ChattingTypeTest.java | 3 +- .../handler/dto/TextMessageTypeTest.java | 44 +++++++++++++++++++ 6 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/exception/UnsupportedChattingTypeException.java create mode 100644 backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/TextMessageTypeTest.java diff --git a/backend/ddang/src/main/java/com/ddang/ddang/exception/GlobalExceptionHandler.java b/backend/ddang/src/main/java/com/ddang/ddang/exception/GlobalExceptionHandler.java index 5831faf10..835daeb4b 100644 --- a/backend/ddang/src/main/java/com/ddang/ddang/exception/GlobalExceptionHandler.java +++ b/backend/ddang/src/main/java/com/ddang/ddang/exception/GlobalExceptionHandler.java @@ -42,6 +42,8 @@ import com.ddang.ddang.review.application.exception.ReviewNotFoundException; import com.ddang.ddang.user.application.exception.AlreadyExistsNameException; import com.ddang.ddang.user.application.exception.UserNotFoundException; +import com.ddang.ddang.websocket.handler.exception.UnsupportedChattingTypeException; +import com.ddang.ddang.websocket.handler.exception.UnsupportedTextMessageTypeException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; @@ -436,6 +438,26 @@ public ResponseEntity handleWithdrawalNotAllowedException(fin .body(new ExceptionResponse(ex.getMessage())); } + @ExceptionHandler(UnsupportedTextMessageTypeException.class) + public ResponseEntity handleUnsupportedTextMessageTypeException( + final UnsupportedTextMessageTypeException ex + ) { + logger.warn(String.format(LOG_MESSAGE_FORMAT, ex.getClass().getSimpleName(), ex.getMessage())); + + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(new ExceptionResponse(ex.getMessage())); + } + + @ExceptionHandler(UnsupportedChattingTypeException.class) + public ResponseEntity handleUnsupportedChattingTypeException( + final UnsupportedChattingTypeException ex + ) { + logger.warn(String.format(LOG_MESSAGE_FORMAT, ex.getClass().getSimpleName(), ex.getMessage())); + + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(new ExceptionResponse(ex.getMessage())); + } + @Override protected ResponseEntity handleMethodArgumentNotValid( final MethodArgumentNotValidException ex, diff --git a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/ChattingType.java b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/ChattingType.java index 8a8e7a361..da6b8fa6d 100644 --- a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/ChattingType.java +++ b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/ChattingType.java @@ -1,23 +1,18 @@ package com.ddang.ddang.websocket.handler.dto; +import com.ddang.ddang.websocket.handler.exception.UnsupportedChattingTypeException; + import java.util.Arrays; public enum ChattingType { - MESSAGE("message"), - PING("ping"), - ; - - private final String value; - - ChattingType(final String value) { - this.value = value; - } + MESSAGE, + PING; public static ChattingType findValue(final String value) { return Arrays.stream(ChattingType.values()) - .filter(chattingType -> chattingType.value.equals(value)) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException("잘못된 채팅 타입입니다.")); + .filter(chattingType -> chattingType.name().equalsIgnoreCase(value)) + .findFirst() + .orElseThrow(() -> new UnsupportedChattingTypeException("잘못된 채팅 타입입니다.")); } } diff --git a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/TextMessageType.java b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/TextMessageType.java index 70fc40615..8dd7dc5b4 100644 --- a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/TextMessageType.java +++ b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/TextMessageType.java @@ -1,7 +1,20 @@ package com.ddang.ddang.websocket.handler.dto; +import com.ddang.ddang.websocket.handler.exception.UnsupportedTextMessageTypeException; +import com.fasterxml.jackson.annotation.JsonCreator; + +import java.util.Arrays; + public enum TextMessageType { CHATTINGS, - BIDS + BIDS; + + @JsonCreator + public static TextMessageType fromString(final String value) { + return Arrays.stream(TextMessageType.values()) + .filter(messageType -> messageType.name().equalsIgnoreCase(value)) + .findFirst() + .orElseThrow(() -> new UnsupportedTextMessageTypeException("잘못된 메시지 타입입니다.")); + } } diff --git a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/exception/UnsupportedChattingTypeException.java b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/exception/UnsupportedChattingTypeException.java new file mode 100644 index 000000000..d7b842b8e --- /dev/null +++ b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/exception/UnsupportedChattingTypeException.java @@ -0,0 +1,7 @@ +package com.ddang.ddang.websocket.handler.exception; + +public class UnsupportedChattingTypeException extends IllegalStateException { + public UnsupportedChattingTypeException(final String message) { + super(message); + } +} diff --git a/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/ChattingTypeTest.java b/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/ChattingTypeTest.java index db0da73ac..789d8653c 100644 --- a/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/ChattingTypeTest.java +++ b/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/ChattingTypeTest.java @@ -1,5 +1,6 @@ package com.ddang.ddang.websocket.handler.dto; +import com.ddang.ddang.websocket.handler.exception.UnsupportedChattingTypeException; import org.junit.jupiter.api.Test; import java.util.Map; @@ -29,6 +30,6 @@ class ChattingTypeTest { final Map data = Map.of("type", "wrong type"); // when & then - assertThatThrownBy(() -> ChattingType.findValue(data.get("type"))).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> ChattingType.findValue(data.get("type"))).isInstanceOf(UnsupportedChattingTypeException.class); } } diff --git a/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/TextMessageTypeTest.java b/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/TextMessageTypeTest.java new file mode 100644 index 000000000..3a4e82e5f --- /dev/null +++ b/backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/TextMessageTypeTest.java @@ -0,0 +1,44 @@ +package com.ddang.ddang.websocket.handler.dto; + +import com.ddang.ddang.websocket.handler.exception.UnsupportedTextMessageTypeException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +class TextMessageTypeTest { + + @Test + void 메시지_타입에_해당하는_문자열을_enum으로_반환한다() { + // given + final String type = "chattings"; + + // when + final TextMessageType actual = TextMessageType.fromString(type); + + // then + assertThat(actual).isEqualTo(TextMessageType.CHATTINGS); + } + + @Test + void 메시지_타입_입력값은_대소문자를_구분하지_않는다() { + // given + final String type = "CHATTINGS"; + + // when + final TextMessageType actual = TextMessageType.fromString(type); + + // then + assertThat(actual).isEqualTo(TextMessageType.CHATTINGS); + } + + @Test + void 잘못된_타입이_전송되면_예외를_반환한다() { + // given + final String wrongType = "wrong type"; + + // when & then + assertThatThrownBy(() -> TextMessageType.fromString(wrongType)) + .isInstanceOf(UnsupportedTextMessageTypeException.class); + } +}