1+ package com .back .domain .mentoring .slot .service ;
2+
3+ import com .back .domain .mentoring .slot .error .MentorSlotErrorCode ;
4+ import com .back .global .exception .ServiceException ;
5+ import org .junit .jupiter .api .DisplayName ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ import java .time .LocalDateTime ;
9+
10+ import static org .junit .jupiter .api .Assertions .*;
11+
12+ class MentorSlotValidatorTest {
13+
14+ @ Test
15+ @ DisplayName ("시작 일시, 종료 일시 기입 시 정상 처리" )
16+ void validateNotNull_success () {
17+ // given
18+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
19+ LocalDateTime end = LocalDateTime .now ().plusHours (2 );
20+
21+ // when & then
22+ assertDoesNotThrow (() -> MentorSlotValidator .validateNotNull (start , end ));
23+ }
24+
25+ @ Test
26+ @ DisplayName ("시작 일시 누락 시 예외 발생" )
27+ void validateNotNull_fail_startNull () {
28+ // given
29+ LocalDateTime end = LocalDateTime .now ().plusHours (1 );
30+
31+ // when & then
32+ ServiceException exception = assertThrows (ServiceException .class ,
33+ () -> MentorSlotValidator .validateNotNull (null , end ));
34+
35+ assertEquals (MentorSlotErrorCode .START_TIME_REQUIRED .getCode (), exception .getResultCode ());
36+ }
37+
38+ @ Test
39+ @ DisplayName ("종료 일시 누락 시 예외 발생" )
40+ void validateNotNull_fail_endNull () {
41+ // given
42+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
43+
44+ // when & then
45+ ServiceException exception = assertThrows (ServiceException .class ,
46+ () -> MentorSlotValidator .validateNotNull (start , null ));
47+
48+ assertEquals (MentorSlotErrorCode .END_TIME_REQUIRED .getCode (), exception .getResultCode ());
49+ }
50+
51+ @ Test
52+ @ DisplayName ("현재 이후의 시작 일시, 종료 일시 기입 시 정상 처리" )
53+ void validateTimeRange_success () {
54+ // given
55+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
56+ LocalDateTime end = start .plusHours (1 );
57+
58+ // when & then
59+ assertDoesNotThrow (() -> MentorSlotValidator .validateTimeRange (start , end ));
60+ }
61+
62+ @ Test
63+ @ DisplayName ("시작 일시가 현재보다 이전이면 예외 발생" )
64+ void validateTimeRange_fail_startTimeInPast () {
65+ // given
66+ LocalDateTime start = LocalDateTime .now ().minusHours (1 );
67+ LocalDateTime end = LocalDateTime .now ().plusHours (1 );
68+
69+ // when & then
70+ ServiceException exception = assertThrows (ServiceException .class ,
71+ () -> MentorSlotValidator .validateTimeRange (start , end ));
72+
73+ assertEquals (MentorSlotErrorCode .START_TIME_IN_PAST .getCode (), exception .getResultCode ());
74+ }
75+
76+ @ Test
77+ @ DisplayName ("종료 일시가 시작 일시보다 이전이면 예외 발생" )
78+ void validateTimeRange_fail_endTimeBeforeStart () {
79+ // given
80+ LocalDateTime start = LocalDateTime .now ().plusHours (2 );
81+ LocalDateTime end = LocalDateTime .now ().plusHours (1 );
82+
83+ // when & then
84+ ServiceException exception = assertThrows (ServiceException .class ,
85+ () -> MentorSlotValidator .validateTimeRange (start , end ));
86+
87+ assertEquals (MentorSlotErrorCode .END_TIME_BEFORE_START .getCode (), exception .getResultCode ());
88+ }
89+
90+ @ Test
91+ @ DisplayName ("20분 이상의 슬롯 기간 입력 시 정상 처리" )
92+ void validateMinimumDuration_success () {
93+ // given
94+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
95+ LocalDateTime end = start .plusHours (1 );
96+
97+ // when & then
98+ assertDoesNotThrow (() -> MentorSlotValidator .validateMinimumDuration (start , end ));
99+ }
100+
101+ @ Test
102+ @ DisplayName ("정확히 20분인 경우 정상 처리" )
103+ void validateMinimumDuration_success_exactly20Minutes () {
104+ // given
105+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
106+ LocalDateTime end = start .plusMinutes (20 );
107+
108+ // when & then
109+ assertDoesNotThrow (() -> MentorSlotValidator .validateMinimumDuration (start , end ));
110+ }
111+
112+ @ Test
113+ @ DisplayName ("슬롯 기간이 최소 시간(20분)보다 짧으면 예외 발생" )
114+ void validateMinimumDuration_fail_insufficientSlotDuration () {
115+ // given
116+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
117+ LocalDateTime end = start .plusMinutes (19 );
118+
119+ // when & then
120+ ServiceException exception = assertThrows (ServiceException .class ,
121+ () -> MentorSlotValidator .validateMinimumDuration (start , end ));
122+
123+ assertEquals (MentorSlotErrorCode .INSUFFICIENT_SLOT_DURATION .getCode (), exception .getResultCode ());
124+ }
125+
126+ @ Test
127+ @ DisplayName ("모든 검증을 통과하는 정상 케이스" )
128+ void validateTimeSlot_success () {
129+ // given
130+ LocalDateTime start = LocalDateTime .now ().plusHours (1 );
131+ LocalDateTime end = start .plusMinutes (30 );
132+
133+ // when & then
134+ assertDoesNotThrow (() -> MentorSlotValidator .validateTimeSlot (start , end ));
135+ }
136+
137+ @ Test
138+ @ DisplayName ("validateTimeSlot 메소드에서 null 체크 예외 발생" )
139+ void validateTimeSlot_fail_nullCheck () {
140+ // given
141+ LocalDateTime end = LocalDateTime .now ().plusHours (1 );
142+
143+ // when & then
144+ ServiceException exception = assertThrows (ServiceException .class ,
145+ () -> MentorSlotValidator .validateTimeSlot (null , end ));
146+
147+ assertEquals (MentorSlotErrorCode .START_TIME_REQUIRED .getCode (), exception .getResultCode ());
148+ }
149+ }
0 commit comments