diff --git a/api/src/main/java/com/taskbuddy/api/controller/TaskController.java b/api/src/main/java/com/taskbuddy/api/controller/TaskController.java index 114837a..ffa08df 100644 --- a/api/src/main/java/com/taskbuddy/api/controller/TaskController.java +++ b/api/src/main/java/com/taskbuddy/api/controller/TaskController.java @@ -20,7 +20,8 @@ public class TaskController { ResponseEntity> getTask(@PathVariable("id") Long id) { Assert.state(id >= 0, "The id value must be positive."); - // FIXME 서비스 로직 구현하면 제거하기 / Custom Exception 구현하기 + // TODO Custom Exception 구현하기 + // FIXME 서비스 로직 구현하면 제거하기 if (id == 0) { throw new IllegalArgumentException("The given task with id does not exist"); } diff --git a/api/src/main/java/com/taskbuddy/api/controller/request/TaskCreateRequest.java b/api/src/main/java/com/taskbuddy/api/controller/request/TaskCreateRequest.java index 0ef8c55..70e5bfa 100644 --- a/api/src/main/java/com/taskbuddy/api/controller/request/TaskCreateRequest.java +++ b/api/src/main/java/com/taskbuddy/api/controller/request/TaskCreateRequest.java @@ -1,9 +1,16 @@ package com.taskbuddy.api.controller.request; import com.taskbuddy.api.controller.response.task.TimeFrame; +import org.springframework.util.Assert; public record TaskCreateRequest( String title, String description, TimeFrame timeFrame -) {} +) { + public TaskCreateRequest { + Assert.state(title != null && !title.isBlank(), "The title of task must not be blank."); + Assert.state(description == null || description.length() <= 500, "The description length must be equal or less than 500"); + Assert.notNull(timeFrame, "The timeFrame must not be null."); + } +} diff --git a/api/src/main/java/com/taskbuddy/api/controller/response/task/TimeFrame.java b/api/src/main/java/com/taskbuddy/api/controller/response/task/TimeFrame.java index 6ddbd2e..4b04652 100644 --- a/api/src/main/java/com/taskbuddy/api/controller/response/task/TimeFrame.java +++ b/api/src/main/java/com/taskbuddy/api/controller/response/task/TimeFrame.java @@ -1,7 +1,15 @@ package com.taskbuddy.api.controller.response.task; +import org.springframework.util.Assert; + import java.time.LocalDateTime; public record TimeFrame( LocalDateTime startDateTime, - LocalDateTime endDateTime) {} + LocalDateTime endDateTime) { + public TimeFrame { + Assert.notNull(startDateTime, "The startDateTime must not be null."); + Assert.notNull(endDateTime, "The endDateTime must not be null."); + Assert.isTrue(endDateTime.isAfter(startDateTime), "The endDateTime must be after than the startDateTime."); + } +} diff --git a/api/src/test/java/com/taskbuddy/api/controller/request/TaskCreateRequestTest.java b/api/src/test/java/com/taskbuddy/api/controller/request/TaskCreateRequestTest.java new file mode 100644 index 0000000..6bf8950 --- /dev/null +++ b/api/src/test/java/com/taskbuddy/api/controller/request/TaskCreateRequestTest.java @@ -0,0 +1,65 @@ +package com.taskbuddy.api.controller.request; + +import com.taskbuddy.api.controller.response.task.TimeFrame; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class TaskCreateRequestTest { + + @ParameterizedTest + @NullAndEmptySource + void Title이_Null이거나_비어있는_값이면_예외를_던진다(String emptyTitle) { + //given + TimeFrame dummyTimeFrame = new TimeFrame(LocalDateTime.now(), LocalDateTime.now().plusDays(1)); + + //when & then + assertThatThrownBy(() -> new TaskCreateRequest(emptyTitle, null, dummyTimeFrame)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The title of task must not be blank."); + } + + @Test + void description길이가_500자_초과라면_예외를_던진다() { + //given + String longDescription = "A".repeat(501); + TimeFrame dummyTimeFrame = new TimeFrame(LocalDateTime.now(), LocalDateTime.now().plusDays(1)); + + //when & then + assertThatThrownBy(() -> new TaskCreateRequest("sample title", longDescription, dummyTimeFrame)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("The description length must be equal or less than 500"); + } + + @Test + void description길이가_500자_이하라면_정상적으로_객체가_생성된다() { + //given + String longDescription = "A".repeat(500); + TimeFrame dummyTimeFrame = new TimeFrame(LocalDateTime.now(), LocalDateTime.now().plusDays(1)); + + //when & then + assertDoesNotThrow(() -> new TaskCreateRequest("sample title", longDescription, dummyTimeFrame)); + } + + @Test + void timeFrame이_null이라면_예외를_던진다() { + //given & when & then + assertThatThrownBy(() -> new TaskCreateRequest("sample title", null, null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The timeFrame must not be null."); + } + + @Test + void 정상적인_생성자_파라미터로_객체를_생성할_수_있다() { + //given + TimeFrame mockTimeFrame = new TimeFrame(LocalDateTime.now(), LocalDateTime.now().plusDays(1)); + + //when & then + assertDoesNotThrow(() -> new TaskCreateRequest("sample title", null, mockTimeFrame)); + } +} diff --git a/api/src/test/java/com/taskbuddy/api/controller/response/task/TimeFrameTest.java b/api/src/test/java/com/taskbuddy/api/controller/response/task/TimeFrameTest.java new file mode 100644 index 0000000..5627999 --- /dev/null +++ b/api/src/test/java/com/taskbuddy/api/controller/response/task/TimeFrameTest.java @@ -0,0 +1,49 @@ +package com.taskbuddy.api.controller.response.task; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class TimeFrameTest { + + @Test + void startDateTime이_null이면_예외를_던진다() { + //given & when & then + assertThatThrownBy(() -> new TimeFrame(null, LocalDateTime.now())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The startDateTime must not be null."); + } + + @Test + void endDateTime이_null이면_예외를_던진다() { + //given & when & then + assertThatThrownBy(() -> new TimeFrame(LocalDateTime.now(), null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The endDateTime must not be null."); + } + + @Test + void endDateTime이_startDateTime보다_과거일시라면_예외를_던진다() { + //given + LocalDateTime mockStartDateTime = LocalDateTime.now(); + LocalDateTime mockEndDateTime = mockStartDateTime.minusDays(1); + + //when & then + assertThatThrownBy(() -> new TimeFrame(mockStartDateTime, mockEndDateTime)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The endDateTime must be after than the startDateTime."); + } + + @Test + void 정상적인_생성자_파라미터로_객체를_생성할_수_있다() { + //given + LocalDateTime mockStartDateTime = LocalDateTime.now(); + LocalDateTime mockEndDateTime = mockStartDateTime.plusHours(1); + + //when & then + assertDoesNotThrow(() -> new TimeFrame(mockStartDateTime, mockEndDateTime)); + } +}