Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/main/java/com/chooz/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum ErrorCode {
TITLE_IS_REQUIRED("게시글 제목은 필수입니다."),
TITLE_LENGTH_EXCEEDED("게시글 제목 길이가 초과했습니다."),
INVALID_POLL_CHOICE_COUNT("투표 선택지 개수가 범위를 벗어났습니다."),
POLL_CHOICE_TITLE_LENGTH_EXCEEDED("투표 선택지 제목 길이가 초과했습니다."),
NOT_POST_AUTHOR("게시글 작성자가 아닙니다."),
POST_ALREADY_CLOSED("이미 마감된 게시글입니다."),
FILE_NAME_TOO_LONG("파일 이름이 너무 깁니다."),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/chooz/post/domain/PollChoice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.chooz.post.domain;

import com.chooz.common.exception.BadRequestException;
import com.chooz.common.exception.ErrorCode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -19,6 +21,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PollChoice {

private static final int MAX_TITLE_LENGTH = 10;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -32,6 +36,7 @@ public class PollChoice {

public PollChoice(Long id, Post post, String title, String imageUrl) {
validateNull(title, imageUrl);
validateTitleLength(title);
this.id = id;
this.post = post;
this.title = title;
Expand All @@ -46,4 +51,10 @@ public void setPost(Post post) {
validateNull(post);
this.post = post;
}

private void validateTitleLength(String title) {
if (title.length() > MAX_TITLE_LENGTH) {
throw new BadRequestException(ErrorCode.POLL_CHOICE_TITLE_LENGTH_EXCEEDED);
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/chooz/post/domain/PollChoiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chooz.post.domain;

import com.chooz.common.exception.BadRequestException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -24,4 +25,31 @@ void create() throws Exception {
() -> assertThat(pollChoice.getImageUrl()).isEqualTo(imageUrl)
);
}

@Test
@DisplayName("투표 선택지 제목은 10자를 초과할 수 없다")
void createWithTitleExceedingMaxLength() throws Exception {
//given
String title = "12345678901"; // 11자
String imageUrl = "https://example.com/image.jpg";

//when & then
assertThrows(BadRequestException.class, () -> {
PollChoice.create(title, imageUrl);
});
}

@Test
@DisplayName("투표 선택지 제목은 10자일 때 정상 생성된다")
void createWithTitleMaxLength() throws Exception {
//given
String title = "1234567890"; // 10자
String imageUrl = "https://example.com/image.jpg";

//when
PollChoice pollChoice = PollChoice.create(title, imageUrl);

//then
assertThat(pollChoice.getTitle()).isEqualTo(title);
}
}