-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 7 additions & 12 deletions
19
backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/ChattingType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("잘못된 채팅 타입입니다.")); | ||
} | ||
} |
15 changes: 14 additions & 1 deletion
15
backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/TextMessageType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("잘못된 메시지 타입입니다.")); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...in/java/com/ddang/ddang/websocket/handler/exception/UnsupportedChattingTypeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.ddang.ddang.websocket.handler.exception; | ||
|
||
public class UnsupportedChattingTypeException extends IllegalStateException { | ||
public UnsupportedChattingTypeException(final String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
backend/ddang/src/test/java/com/ddang/ddang/websocket/handler/dto/TextMessageTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |