-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#16] feat: username validation guava 전환
- Loading branch information
1 parent
3b1a5b2
commit ca19bfd
Showing
2 changed files
with
43 additions
and
78 deletions.
There are no files selected for viewing
19 changes: 16 additions & 3 deletions
19
src/main/java/com/study/realworld/user/domain/Username.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
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,27 +1,15 @@ | ||
package com.study.realworld.user.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
import java.util.Collection; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class UsernameTest { | ||
|
||
private Validator validator; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
validator = Validation.buildDefaultValidatorFactory().getValidator(); | ||
} | ||
|
||
@Test | ||
void usernameTest() { | ||
Username username = new Username(); | ||
|
@@ -32,106 +20,70 @@ void usernameTest() { | |
void usernameNullTest() { | ||
|
||
// given | ||
Username username = new Username(null); | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
String input = null; | ||
|
||
// then | ||
assertEquals(1, constraintViolations.size()); | ||
assertEquals("username must be provided.", | ||
constraintViolations.iterator().next().getMessage()); | ||
// when & then | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> new Username(input)) | ||
.withMessageMatching("username must be provided."); | ||
} | ||
|
||
@Test | ||
@ParameterizedTest | ||
@ValueSource(strings = {"", " ", " "}) | ||
@DisplayName("유저 네임은 빈칸이 들어오면 invalid되어야 한다.") | ||
void usernameNothingTest() { | ||
void usernameNothingTest(String input) { | ||
|
||
// given | ||
Username username = new Username(""); | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
|
||
// then | ||
assertEquals(1, constraintViolations.size()); | ||
assertEquals("username must be provided.", | ||
constraintViolations.iterator().next().getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저 네임은 공백이 들어오면 invalid되어야 한다.") | ||
void usernameBlankTest() { | ||
|
||
// given | ||
Username username = new Username(" "); | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
|
||
// then | ||
assertEquals(2, constraintViolations.size()); | ||
// when & then | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> new Username(input)) | ||
.withMessageMatching("username must be provided."); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저네임이 20글자가 넘어가면 invalid되어야 한다.") | ||
void usernameMaxSizeTest() { | ||
|
||
// given | ||
Username username = new Username("123456789012345678901"); | ||
String input = "123456789012345678901"; | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
|
||
// then | ||
assertEquals(1, constraintViolations.size()); | ||
assertEquals("username length must be less than 20 characters.", | ||
constraintViolations.iterator().next().getMessage()); | ||
// when & then | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> new Username(input)) | ||
.withMessageMatching("username length must be less then 20 characters."); | ||
} | ||
|
||
@Test | ||
@DisplayName("유저네임은 한글, 숫자, 영어만 들어갈 수 있다.") | ||
void usernameValidTest() { | ||
|
||
// given | ||
Username username = new Username("123가믜힣abcABC"); | ||
String input = "123가믜힣abcABC"; | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
Username username = new Username(input); | ||
|
||
// then | ||
assertEquals(0, constraintViolations.size()); | ||
assertThat(username.toString()).isEqualTo(input); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"테스트1#", "@test", "test."}) | ||
@DisplayName("유저네임에 한글, 숫자, 영어 말고 다른 값이 들어오면 invalid되어야한다.") | ||
void usernameInvalidTest(String input) { | ||
|
||
// given | ||
Username username = new Username(input); | ||
|
||
// when | ||
Collection<ConstraintViolation<Username>> constraintViolations | ||
= validator.validate(username); | ||
|
||
// then | ||
assertEquals(1, constraintViolations.size()); | ||
assertEquals("Invalid username name", constraintViolations.iterator().next().getMessage()); | ||
// when & then | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> new Username(input)) | ||
.withMessageMatching("usernmae must be provided by limited pattern."); | ||
} | ||
|
||
@Test | ||
@DisplayName("equals hashCode 테스트") | ||
void usernameEqualsHashCodeTest() { | ||
|
||
// given | ||
Username username = new Username("[email protected]"); | ||
Username copyUsername = new Username("[email protected]"); | ||
Username username = new Username("username"); | ||
Username copyUsername = new Username("username"); | ||
|
||
// when & then | ||
assertThat(username) | ||
|