From 99ff2b77b97596cca6ea49ab6028043353c8e889 Mon Sep 17 00:00:00 2001 From: SeungChul Chung Date: Wed, 3 Apr 2024 14:18:01 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[Unit=20Test]=20usertextcolorche?= =?UTF-8?q?ckservice=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?(#661)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/UserTextColorCheckService.java | 24 ++---- .../UserTextColorCheckServiceUnitTest.java | 81 +++++++++++++++++++ 2 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 gg-pingpong-api/src/test/java/gg/pingpong/api/user/user/service/UserTextColorCheckServiceUnitTest.java diff --git a/gg-pingpong-api/src/main/java/gg/pingpong/api/user/user/service/UserTextColorCheckService.java b/gg-pingpong-api/src/main/java/gg/pingpong/api/user/user/service/UserTextColorCheckService.java index 34614ca41..087d2f40e 100644 --- a/gg-pingpong-api/src/main/java/gg/pingpong/api/user/user/service/UserTextColorCheckService.java +++ b/gg-pingpong-api/src/main/java/gg/pingpong/api/user/user/service/UserTextColorCheckService.java @@ -7,23 +7,13 @@ @Service @RequiredArgsConstructor public class UserTextColorCheckService { + + /** + * textColor 의 유효성 검사 + * @param textColor + * @return boolean + */ public static boolean check(String textColor) { - if (textColor == null) { - return false; - } - if (textColor.length() != 7) { - return false; - } - if (textColor.charAt(0) != '#') { - return false; - } - for (int i = 1; i < 7; i++) { - char charTestColor = textColor.charAt(i); - if (!((charTestColor >= '0' && charTestColor <= '9') || (charTestColor >= 'a' && charTestColor <= 'f') || ( - charTestColor >= 'A' && charTestColor <= 'F'))) { - return false; - } - } - return true; + return textColor != null && textColor.matches("#[0-9a-fA-F]{6}"); } } diff --git a/gg-pingpong-api/src/test/java/gg/pingpong/api/user/user/service/UserTextColorCheckServiceUnitTest.java b/gg-pingpong-api/src/test/java/gg/pingpong/api/user/user/service/UserTextColorCheckServiceUnitTest.java new file mode 100644 index 000000000..2366262bb --- /dev/null +++ b/gg-pingpong-api/src/test/java/gg/pingpong/api/user/user/service/UserTextColorCheckServiceUnitTest.java @@ -0,0 +1,81 @@ +package gg.pingpong.api.user.user.service; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import gg.utils.annotation.UnitTest; + +@UnitTest +class UserTextColorCheckServiceUnitTest { + + UserTextColorCheckService userTextColorCheckService = new UserTextColorCheckService(); + + @Nested + @DisplayName("check") + class Check { + @Test + @DisplayName("textColor가 null일 경우 false 반환") + void nullCheck() { + //Arrange + String textColor = null; + + //Act + boolean check = userTextColorCheckService.check(textColor); + + //Assert + Assertions.assertThat(check).isFalse(); + } + + @ParameterizedTest + @DisplayName("7글자가 아닐경우 false 반환") + @ValueSource(strings = {"", "#", "#1", "#12", "#123", "#12345", "#1234567"}) + void lengthCheck(String textColor) { + //Arrange + //Act + boolean check = userTextColorCheckService.check(textColor); + + //Assert + Assertions.assertThat(check).isFalse(); + } + } + + @ParameterizedTest + @DisplayName("첫 글자 # 아닐경우 false 반환") + @ValueSource(strings = {"#,", "a#12345", "ㄱ#12345", "1#12345", "Z#12345", " #12345"}) + void startCheck(String textColor) { + //Arrange + //Act + boolean check = userTextColorCheckService.check(textColor); + + //Assert + Assertions.assertThat(check).isFalse(); + } + + @ParameterizedTest + @DisplayName("정규식 실패할경우 false 반환") + @ValueSource(strings = {"#12345G", "#gabcde", "#zABCDE", "#Ab3DEx"}) + void regexFail(String textColor) { + //Arrange + //Act + boolean check = userTextColorCheckService.check(textColor); + + //Assert + Assertions.assertThat(check).isFalse(); + } + + @ParameterizedTest + @DisplayName("모든 조건을 통과할경우 true 반환") + @ValueSource(strings = {"#023589", "#abcdef", "#ABCDEF", "#Ab3DeE"}) + void checkSuccess(String textColor) { + //Arrange + //Act + boolean check = userTextColorCheckService.check(textColor); + + //Assert + Assertions.assertThat(check).isTrue(); + } +}