Skip to content

Commit

Permalink
fix: 빌더 패턴으로 인해 기본 값이 설정되지 않는 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Jan 3, 2024
1 parent 1f28e90 commit e036f55
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
44 changes: 30 additions & 14 deletions src/main/java/com/backend/blooming/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public class User extends BaseTimeEntity {
private Email email;

@Column(unique = true, length = 50, nullable = false)
private String name = DEFAULT_NAME;
private String name;

@Enumerated(EnumType.STRING)
@Column(name = "theme_color", nullable = false)
private ThemeColor color = DEFAULT_THEME_COLOR;
private ThemeColor color;

@Column(columnDefinition = "text", nullable = false)
private String statusMessage = DEFAULT_STATUS_MESSAGE;
private String statusMessage;

@Column(name = "is_deleted")
private boolean deleted = false;
Expand All @@ -70,9 +70,33 @@ private User(
this.oAuthId = oAuthId;
this.oAuthType = oAuthType;
this.email = email;
this.name = name;
this.color = color;
this.statusMessage = statusMessage;
this.name = processName(name);
this.color = processColor(color);
this.statusMessage = processStatusMessage(statusMessage);
}

private String processName(final String name) {
if (name == null) {
return DEFAULT_NAME;
}

return name;
}

private ThemeColor processColor(final ThemeColor color) {
if (color == null) {
return DEFAULT_THEME_COLOR;
}

return color;
}

private String processStatusMessage(final String statusMessage) {
if (statusMessage == null) {
return DEFAULT_STATUS_MESSAGE;
}

return statusMessage;
}

public void delete() {
Expand All @@ -96,18 +120,10 @@ public String getEmail() {
}

public String getColorName() {
if (color == null) {
return null;
}

return color.name();
}

public String getColorCode() {
if (color == null) {
return null;
}

return color.getCode();
}
}
51 changes: 28 additions & 23 deletions src/test/java/com/backend/blooming/user/domain/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.backend.blooming.authentication.infrastructure.oauth.OAuthType;
import com.backend.blooming.themecolor.domain.ThemeColor;
import org.assertj.core.api.*;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -13,6 +14,26 @@
@SuppressWarnings("NonAsciiCharacters")
class UserTest extends UserTestFixture {

@Test
void 사용자_생성시_이름_색상_상태메시지를_설정하지_않을시_기본값으로_설정한다() {
// when
final User actual = User.builder()
.oAuthId("12345")
.oAuthType(OAuthType.KAKAO)
.email(new Email("[email protected]"))
.build();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.getOAuthId()).isEqualTo("12345");
softAssertions.assertThat(actual.getOAuthType()).isEqualTo(OAuthType.KAKAO);
softAssertions.assertThat(actual.getEmail()).isEqualTo("[email protected]");
softAssertions.assertThat(actual.getName()).isEqualTo("");
softAssertions.assertThat(actual.getColor()).isEqualTo(ThemeColor.INDIGO);
softAssertions.assertThat(actual.getStatusMessage()).isEqualTo("");
});
}

@Test
void 사용자를_삭제한다() {
// when
Expand Down Expand Up @@ -80,7 +101,7 @@ class UserTest extends UserTestFixture {
}

@Test
void 테마_색상의_이름_조회시_테마_색상을_선택했다면_해당_색상의_이름을_반환한다() {
void 이메일_조회시_이메일_값을_반환한다() {
// given
final User user = User.builder()
.oAuthId("12345")
Expand All @@ -91,31 +112,32 @@ class UserTest extends UserTestFixture {
.build();

// when
final String actual = user.getColorName();
final String actual = user.getEmail();

// then
assertThat(actual).isEqualTo(ThemeColor.BEIGE.name());
assertThat(actual).isEqualTo("[email protected]");
}

@Test
void 테마_색상의_이름_조회시_테마_색상을_선택하기_전이라면_null_반환한다() {
void 테마_색상의_이름_조회시_해당_색상의_이름을_반환한다() {
// given
final User user = User.builder()
.oAuthId("12345")
.oAuthType(OAuthType.KAKAO)
.name("사용자")
.email(new Email("[email protected]"))
.color(ThemeColor.BEIGE)
.build();

// when
final String actual = user.getColorName();

// then
assertThat(actual).isNull();
assertThat(actual).isEqualTo(ThemeColor.BEIGE.name());
}

@Test
void 테마_색상의_코드_조회시_테마_색상을_선택했다면_해당_색상의_코드를_반환한다() {
void 테마_색상의_코드_조회시_해당_색상의_코드를_반환한다() {
// given
final User user = User.builder()
.oAuthId("12345")
Expand All @@ -131,21 +153,4 @@ class UserTest extends UserTestFixture {
// then
assertThat(actual).isEqualTo(ThemeColor.BEIGE.getCode());
}

@Test
void 테마_색상의_코드_조회시_테마_색상을_선택하기_전이라면_null_반환한다() {
// given
final User user = User.builder()
.oAuthId("12345")
.oAuthType(OAuthType.KAKAO)
.name("사용자")
.email(new Email("[email protected]"))
.build();

// when
final String actual = user.getColorCode();

// then
assertThat(actual).isNull();
}
}

0 comments on commit e036f55

Please sign in to comment.