diff --git a/src/main/java/com/snackgame/server/member/domain/Name.java b/src/main/java/com/snackgame/server/member/domain/Name.java index b794919b..436be195 100644 --- a/src/main/java/com/snackgame/server/member/domain/Name.java +++ b/src/main/java/com/snackgame/server/member/domain/Name.java @@ -1,7 +1,8 @@ package com.snackgame.server.member.domain; +import static java.util.regex.Pattern.matches; + import java.util.Objects; -import java.util.regex.Pattern; import javax.persistence.Column; import javax.persistence.Embeddable; @@ -16,7 +17,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Name { - private static final Pattern NUMBERED_PATTERN = Pattern.compile(".+_\\d+"); + private static final String NUMBERED_PATTERN = ".+_\\d+"; @Column(name = "name") private String string; @@ -33,13 +34,13 @@ private void validateNotNull(String string) { } private void validateLengthOf(String string) { - if (string.length() < 2) { + if (string.length() < 2 || string.length() > 16) { throw new NameLengthException(); } } public Name nextAvailable() { - if (NUMBERED_PATTERN.matcher(string).matches()) { + if (matches(NUMBERED_PATTERN, string)) { int underscoreIndex = string.lastIndexOf('_'); long currentNumber = Long.parseLong(string.substring(underscoreIndex + 1)); return with(currentNumber + 1); diff --git a/src/main/java/com/snackgame/server/member/service/DistinctNaming.java b/src/main/java/com/snackgame/server/member/service/DistinctNaming.java index 51ddbf7f..c02a6c04 100644 --- a/src/main/java/com/snackgame/server/member/service/DistinctNaming.java +++ b/src/main/java/com/snackgame/server/member/service/DistinctNaming.java @@ -24,9 +24,9 @@ public void validate(Name name) { } public Name ofGuest() { - Name name = nameRandomizer.getBy("guest"); + Name name = nameRandomizer.getWith("guest"); while (members.existsByName(name)) { - name = nameRandomizer.getBy("guest"); + name = nameRandomizer.getWith("guest"); } return name; } diff --git a/src/main/java/com/snackgame/server/member/service/NameRandomizer.kt b/src/main/java/com/snackgame/server/member/service/NameRandomizer.kt index 6fddc6a9..8b0808c1 100644 --- a/src/main/java/com/snackgame/server/member/service/NameRandomizer.kt +++ b/src/main/java/com/snackgame/server/member/service/NameRandomizer.kt @@ -5,12 +5,12 @@ import org.springframework.stereotype.Component @Component class NameRandomizer { - fun getBy(prefix: String): Name { + fun getWith(prefix: String): Name { return Name(prefix + "_" + getRandomizedAlphabets(RANDOMIZED_LENGTH)) } companion object { - private const val RANDOMIZED_LENGTH = 12 + private const val RANDOMIZED_LENGTH = 10 private const val ALPHABET_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" private fun getRandomizedAlphabets(length: Int): String { diff --git a/src/main/java/com/snackgame/server/member/service/OidcMemberService.kt b/src/main/java/com/snackgame/server/member/service/OidcMemberService.kt index a0691047..e514ad3d 100644 --- a/src/main/java/com/snackgame/server/member/service/OidcMemberService.kt +++ b/src/main/java/com/snackgame/server/member/service/OidcMemberService.kt @@ -39,7 +39,7 @@ open class OidcMemberService( private val IdTokenPayload.distinctName: Name get() { if (this.name.isNullOrBlank()) { - return distinctNaming.from(nameRandomizer.getBy(this.provider)) + return distinctNaming.from(nameRandomizer.getWith(this.provider)) } return distinctNaming.from(Name(this.name)) } diff --git a/src/test/java/com/snackgame/server/member/domain/NameTest.java b/src/test/java/com/snackgame/server/member/domain/NameTest.java index f2f8872b..428ed3a2 100644 --- a/src/test/java/com/snackgame/server/member/domain/NameTest.java +++ b/src/test/java/com/snackgame/server/member/domain/NameTest.java @@ -29,7 +29,13 @@ class NameTest { } @Test - void 이름이_2글자_이상이면_잘_생성된다() { + void 이름이_16글자보다_길면_예외를_던진다() { + assertThatThrownBy(() -> new Name("123456789abcdfegh")) + .isInstanceOf(NameLengthException.class); + } + + @Test + void 이름이_2글자_이상_16글자_이하_이면_잘_생성된다() { assertThatNoException() .isThrownBy(() -> new Name("2자")); } diff --git a/src/test/java/com/snackgame/server/member/service/NameRandomizerTest.kt b/src/test/java/com/snackgame/server/member/service/NameRandomizerTest.kt index 5891f4fa..5e668f91 100644 --- a/src/test/java/com/snackgame/server/member/service/NameRandomizerTest.kt +++ b/src/test/java/com/snackgame/server/member/service/NameRandomizerTest.kt @@ -8,18 +8,19 @@ import org.junit.jupiter.api.Test class NameRandomizerTest { private val nameRandomizer = NameRandomizer() + private val randomizedLength = 10 @Test fun `이름 앞에 접두사 및 '_' 가 붙는다`() { - val randomized = nameRandomizer.getBy("guest").string + val randomized = nameRandomizer.getWith("guest").string assertThat(randomized).startsWith("guest_") } @Test - fun `'_' 뒤 알파벳 12자리를 무작위로 생성한다`() { - val randomized = nameRandomizer.getBy("guest").string + fun `'_' 뒤 알파벳 10자리를 무작위로 생성한다`() { + val randomized = nameRandomizer.getWith("guest").string - assertThat(randomized.substringAfter('_')).hasSize(12) + assertThat(randomized.substringAfter('_')).hasSize(randomizedLength) } }