Skip to content

Commit

Permalink
refactor: (#95) 게시글의 마감 기한이 현재 시간보다 3일 이상을 초과한 경우 예외 처리 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdtls690 committed Jul 30, 2023
1 parent a25923b commit 9309f49
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public boolean hasPostOption(final PostOption postOption) {
public void validateDeadlineNotExceedThreeDays() {
LocalDateTime threeDaysFromNow = LocalDateTime.now().plusDays(3);
if (this.deadline.isAfter(threeDaysFromNow)) {
throw new IllegalStateException("마감 기한은 현재 시간으로부터 3일을 초과할 수 없습니다.");
throw new BadRequestException(PostExceptionType.DEADLINE_EXCEED_THREE_DAYS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum PostExceptionType implements ExceptionType {
UNRELATED_POST_OPTION(1002, "게시글 투표 옵션이 게시글과 연관되어 있지 않습니다."),
NOT_WRITER(1003, "해당 게시글 작성자가 아닙니다."),
NOT_VOTER(1004, "해당 게시글 작성자는 투표할 수 없습니다."),
DEADLINE_EXCEED_THREE_DAYS(1005, "마감 기한은 현재 시간으로부터 3일을 초과할 수 없습니다."),
;

private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public Long save(
) {
final List<Category> categories = categoryRepository.findAllById(postRequest.categoryIds());
final Post post = toPostEntity(postRequest, loginMember, contentImages, optionImages, categories);
post.validateDeadlineNotExceedThreeDays();

return postRepository.save(post).getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import static com.votogether.fixtures.MemberFixtures.MALE_30;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.votogether.domain.category.entity.Category;
import com.votogether.domain.member.entity.Member;
import com.votogether.domain.post.exception.PostExceptionType;
import com.votogether.exception.BadRequestException;
import com.votogether.fixtures.MemberFixtures;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;

class PostTest {

Expand Down Expand Up @@ -62,6 +69,33 @@ void mapPostOptionsByElements() {
assertThat(postOptions).hasSize(2);
}

@Test
@DisplayName("게시글 작성 시, 게시글의 마감 기한이 현재 시간보다 3일 초과 여부에 따라 예외를 던질 지 결정한다.")
void throwExceptionIsWriter() {
// given
final Member writer = MemberFixtures.MALE_30.get();
ReflectionTestUtils.setField(writer, "id", 1L);

Post post1 = Post.builder()
.writer(writer)
.deadline(LocalDateTime.now().plusDays(4))
.build();

Post post2 = Post.builder()
.writer(writer)
.deadline(LocalDateTime.now().plusDays(2))
.build();

// when, then
assertAll(
() -> assertThatThrownBy(post1::validateDeadlineNotExceedThreeDays)
.isInstanceOf(BadRequestException.class)
.hasMessage(PostExceptionType.DEADLINE_EXCEED_THREE_DAYS.getMessage()),
() -> assertThatNoException()
.isThrownBy(post2::validateDeadlineNotExceedThreeDays)
);
}

@Test
@DisplayName("게시글의 작성자 여부를 확인한다.")
void isWriter() {
Expand Down

0 comments on commit 9309f49

Please sign in to comment.