Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/name constraints #153

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/com/snackgame/server/member/domain/Name.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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자"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading