Skip to content

Commit

Permalink
feat: School logoUrl, backgroundImageUrl null 혹은 공백 문자열 시 빈 문자열 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 committed May 8, 2024
1 parent ffe0e26 commit f836b87
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private School crateSchool(SchoolRegion schoolRegion, String schoolName, String
return new School(
schoolEmail,
schoolName,
"",
"",
schoolRegion
);
}
Expand Down
26 changes: 9 additions & 17 deletions backend/src/main/java/com/festago/school/domain/School.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.school.domain;

import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -13,13 +14,11 @@
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class School extends BaseTimeEntity {

private static final String DEFAULT_URL = "https://picsum.photos/536/354";
private static final int MAX_DOMAIN_LENGTH = 50;
private static final int MAX_NAME_LENGTH = 255;
private static final int MAX_IMAGE_URL_LENGTH = 255;
Expand All @@ -46,27 +45,20 @@ public class School extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private SchoolRegion region;

public School(String domain, String name, String logoUrl, String backgroundUrl, SchoolRegion region) {
this(null, domain, name, logoUrl, backgroundUrl, region);
}

public School(Long id, String domain, String name, String logoUrl, String backgroundImageUrl, SchoolRegion region) {
validate(domain, name, region, logoUrl, backgroundImageUrl);
this.id = id;
this.domain = domain;
this.name = name;
this.logoUrl = getDefaultUrlIfBlank(logoUrl);
this.backgroundUrl = getDefaultUrlIfBlank(backgroundImageUrl);
this.logoUrl = ImageUrlHelper.getBlankStringIfBlank(logoUrl);
this.backgroundUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
this.region = region;
}

private String getDefaultUrlIfBlank(String imageUrl) {
if (StringUtils.hasText(imageUrl)) {
return imageUrl;
}
return DEFAULT_URL;
}

public School(String domain, String name, SchoolRegion region) {
this(null, domain, name, DEFAULT_URL, DEFAULT_URL, region);
}

private void validate(String domain, String name, SchoolRegion region, String logoUrl, String backgroundImageUrl) {
validateDomain(domain);
validateName(name);
Expand Down Expand Up @@ -112,12 +104,12 @@ public void changeRegion(SchoolRegion region) {

public void changeLogoUrl(String logoUrl) {
validateImageUrl(logoUrl, "logoUrl");
this.logoUrl = getDefaultUrlIfBlank(logoUrl);
this.logoUrl = ImageUrlHelper.getBlankStringIfBlank(logoUrl);
}

public void changeBackgroundImageUrl(String backgroundImageUrl) {
validateImageUrl(backgroundImageUrl, "backgroundImageUrl");
this.backgroundUrl = getDefaultUrlIfBlank(backgroundImageUrl);
this.backgroundUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
}

public Long getId() {
Expand Down
30 changes: 15 additions & 15 deletions backend/src/test/java/com/festago/school/domain/SchoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class 생성 {
String domain = "1".repeat(51);

// when & then
assertThatThrownBy(() -> new School(domain, "테코대학교", SchoolRegion.서울))
assertThatThrownBy(() -> new School(domain, "테코대학교", "", "", SchoolRegion.서울))
.isInstanceOf(ValidException.class);
}

Expand All @@ -36,7 +36,7 @@ class 생성 {
@ValueSource(strings = {"", " ", "\t", "\n"})
void 도메인이_null_또는_공백이면_예외(String domain) {
// when & then
assertThatThrownBy(() -> new School(domain, "테코대학교", SchoolRegion.서울))
assertThatThrownBy(() -> new School(domain, "테코대학교", "", "", SchoolRegion.서울))
.isInstanceOf(ValidException.class);
}

Expand All @@ -47,7 +47,7 @@ class 생성 {
String domain = "1".repeat(length);

// when
School school = new School(domain, "테코대학교", SchoolRegion.서울);
School school = new School(domain, "테코대학교", "", "", SchoolRegion.서울);

// then
assertThat(school.getDomain()).isEqualTo(domain);
Expand All @@ -59,7 +59,7 @@ class 생성 {
String name = "1".repeat(256);

// when & then
assertThatThrownBy(() -> new School("teco.ac.kr", name, SchoolRegion.서울))
assertThatThrownBy(() -> new School("teco.ac.kr", name, "", "", SchoolRegion.서울))
.isInstanceOf(ValidException.class);
}

Expand All @@ -68,7 +68,7 @@ class 생성 {
@ValueSource(strings = {"", " ", "\t", "\n"})
void 이름이_null_또는_공백이면_예외(String name) {
// when & then
assertThatThrownBy(() -> new School("teco.ac.kr", name, SchoolRegion.서울))
assertThatThrownBy(() -> new School("teco.ac.kr", name, "", "", SchoolRegion.서울))
.isInstanceOf(ValidException.class);
}

Expand All @@ -79,7 +79,7 @@ class 생성 {
String name = "1".repeat(length);

// when
School school = new School("teco.ac.kr", name, SchoolRegion.서울);
School school = new School("teco.ac.kr", name, "", "", SchoolRegion.서울);

// then
assertThat(school.getName()).isEqualTo(name);
Expand All @@ -91,20 +91,20 @@ class 생성 {
SchoolRegion region = null;

// when & then
assertThatThrownBy(() -> new School("teco.ac.kr", "테코대학교", region))
assertThatThrownBy(() -> new School("teco.ac.kr", "테코대학교", "", "", region))
.isInstanceOf(ValidException.class);
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "\t", "\n"})
void logoUrl이_null_또는_공백이어도_성공(String logoUrl) {
void logoUrl이_null_또는_공백이면_기본값이_할당된다(String logoUrl) {
// when
School school = new School(1L, "teco.ac.kr", "테코대학교", logoUrl, "https://image.com/backgroundImage.png",
SchoolRegion.서울);

// then
assertThat(school.getLogoUrl()).isNotBlank();
assertThat(school.getLogoUrl()).isEmpty();
}

@ParameterizedTest
Expand Down Expand Up @@ -136,13 +136,13 @@ class 생성 {
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "\t", "\n"})
void backgroundImageUrl이_null_또는_공백이어도_성공(String backgroundImageUrl) {
void backgroundImageUrl이_null_또는_공백이면_기본값이_할당된다(String backgroundImageUrl) {
// when
School school = new School(1L, "teco.ac.kr", "테코대학교", "https://image.com/logo.png", backgroundImageUrl,
SchoolRegion.서울);

// then
assertThat(school.getBackgroundUrl()).isNotBlank();
assertThat(school.getBackgroundUrl()).isEmpty();
}

@ParameterizedTest
Expand Down Expand Up @@ -295,12 +295,12 @@ void setUp() {
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "\t", "\n"})
void logoUrl이_null_또는_공백이어도_성공(String logoUrl) {
void logoUrl이_null_또는_공백이면_기본값이_할당된다(String logoUrl) {
// when
school.changeLogoUrl(logoUrl);

// then
assertThat(school.getLogoUrl()).isNotBlank();
assertThat(school.getLogoUrl()).isEmpty();
}

@ParameterizedTest
Expand Down Expand Up @@ -328,12 +328,12 @@ void setUp() {
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " ", "\t", "\n"})
void backgroundImageUrl이_null_또는_공백이어도_성공(String backgroundImageUrl) {
void backgroundImageUrl이_null_또는_공백이면_기본값이_할당된다(String backgroundImageUrl) {
// when
school.changeBackgroundImageUrl(backgroundImageUrl);

// then
assertThat(school.getBackgroundUrl()).isNotBlank();
assertThat(school.getBackgroundUrl()).isEmpty();
}

@ParameterizedTest
Expand Down

0 comments on commit f836b87

Please sign in to comment.