Skip to content

Commit

Permalink
fix-be: 모집 공고 시작 날짜 예외 처리 (#564)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kwoun Ki Ho <fingercut3822@gmail.com>
  • Loading branch information
github-actions[bot] and Chocochip101 authored Aug 21, 2024
1 parent 796f3e5 commit d190970
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
12 changes: 12 additions & 0 deletions backend/src/main/java/com/cruru/applyform/domain/ApplyForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cruru.BaseEntity;
import com.cruru.applyform.exception.badrequest.StartDateAfterEndDateException;
import com.cruru.applyform.exception.badrequest.StartDatePastException;
import com.cruru.dashboard.domain.Dashboard;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -65,11 +66,22 @@ public ApplyForm(
}

private void validateDate(LocalDateTime startDate, LocalDateTime endDate) {
validateStartDateBeforeEndDate(startDate, endDate);
validateStartDateNotInPast(startDate);
}

private void validateStartDateBeforeEndDate(LocalDateTime startDate, LocalDateTime endDate) {
if (startDate.isAfter(endDate)) {
throw new StartDateAfterEndDateException(startDate, endDate);
}
}

private void validateStartDateNotInPast(LocalDateTime startDate) {
if (startDate.isBefore(LocalDateTime.now())) {
throw new StartDatePastException(startDate, LocalDateTime.now());
}
}

public ApplyForm(
String title,
String description,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cruru.applyform.exception.badrequest;

import com.cruru.advice.badrequest.BadRequestException;
import java.time.LocalDateTime;

public class StartDatePastException extends BadRequestException {

private static final String TEXT = "접수 시작일 (%s)이 현재 시간 (%s)보다 이전일 수 없습니다.";

public StartDatePastException(LocalDateTime startDate, LocalDateTime now) {
super(String.format(TEXT, startDate, now));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.cruru.applyform.exception.badrequest.StartDateAfterEndDateException;
import com.cruru.applyform.exception.badrequest.StartDatePastException;
import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("지원서 폼 도메인 테스트")
class ApplyFormTest {

@DisplayName("시작 날짜가 마감 날짜보다 늦을 경우 예외가 발생한다")
@DisplayName("시작 날짜가 마감 날짜보다 늦을 경우 예외가 발생한다.")
@Test
void invalidDate() {
// given
Expand All @@ -23,4 +24,18 @@ void invalidDate() {
assertThatThrownBy(() -> new ApplyForm(title, description, startDate, endDate, null))
.isInstanceOf(StartDateAfterEndDateException.class);
}

@DisplayName("시작 날짜가 현재 날짜보다 이전일 경우 예외가 발생한다.")
@Test
void startDateInPast() {
// given
String title = "title";
String description = "description";
LocalDateTime startDate = LocalDateTime.now().minusDays(1);
LocalDateTime endDate = LocalDateTime.now().plusDays(1);

// when&then
assertThatThrownBy(() -> new ApplyForm(title, description, startDate, endDate, null))
.isInstanceOf(StartDatePastException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void create() {
"크루루대시보드",
"# 공고 내용",
questionCreateRequests,
LocalDateTime.of(2000, 1, 1, 0, 0),
LocalDateTime.now().plusDays(1),
LocalDateTime.of(2999, 12, 31, 23, 59)
);
String url = String.format("/v1/dashboards?clubId=%d", club.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void create() {
new QuestionCreateRequest("DROPDOWN", "객관식질문1", choiceCreateRequests, 1, false));
String title = "크루루대시보드";
String postingContent = "# 공고 내용";
LocalDateTime startDate = LocalDateTime.of(2000, 1, 1, 0, 0);
LocalDateTime startDate = LocalDateTime.now().plusDays(1);
LocalDateTime endDate = LocalDateTime.of(2999, 12, 31, 23, 59);
DashboardCreateRequest request = new DashboardCreateRequest(
title,
Expand Down

0 comments on commit d190970

Please sign in to comment.