5
5
import nextstep .courses .domain .course .session .*;
6
6
import nextstep .courses .service .SessionService ;
7
7
import nextstep .payments .domain .Payment ;
8
+ import nextstep .qna .NotFoundException ;
8
9
import nextstep .users .domain .NsUser ;
9
10
import org .junit .jupiter .api .BeforeEach ;
10
11
import org .junit .jupiter .api .DisplayName ;
25
26
public class SessionServiceTest {
26
27
private static final NsUser JAVAJIGI =
new NsUser (
1L ,
"javajigi" ,
"password" ,
"name" ,
"[email protected] " );
27
28
private static final NsUser APPLE =
new NsUser (
3L ,
"apple" ,
"password" ,
"name" ,
"[email protected] " );
29
+ private static final LocalDate DATE_2023_12_5 = LocalDate .of (2023 , 12 , 5 );
30
+ private static final LocalDate DATE_2023_12_6 = LocalDate .of (2023 , 12 , 6 );
31
+ private static final LocalDate DATE_2023_12_10 = LocalDate .of (2023 , 12 , 10 );
32
+ private static final LocalDate DATE_2023_12_12 = LocalDate .of (2023 , 12 , 12 );
28
33
29
34
private Image image ;
30
35
private Payment payment ;
@@ -33,7 +38,9 @@ public class SessionServiceTest {
33
38
private Applicants applicants ;
34
39
private Duration duration ;
35
40
private SessionState sessionState ;
41
+ private SessionStatus sessionStatus ;
36
42
private Session session ;
43
+ private Session savedSession ;
37
44
38
45
@ Mock
39
46
private SessionRepository sessionRepository ;
@@ -49,10 +56,27 @@ public void setUp() {
49
56
localDate = LocalDate .of (2023 , 12 , 5 );
50
57
duration = new Duration (localDate , localDate );
51
58
sessionState = new SessionState (SessionType .FREE , 0L , Integer .MAX_VALUE );
59
+ sessionStatus = SessionStatus .RECRUIT ;
52
60
applicants = new Applicants ();
53
61
applicants .addApplicant (JAVAJIGI , sessionState );
54
62
session = new Session (1L , image , duration , sessionState , applicants ,
55
- SessionStatus .RECRUIT , 1L , localDateTime , localDateTime );
63
+ sessionStatus , 1L , localDateTime , localDateTime );
64
+ }
65
+
66
+ @ Test
67
+ @ DisplayName ("주어진 강의 정보로 강의를 생성한다." )
68
+ void create_success () {
69
+ Session newSession = new Session (image , duration , sessionState , 1L , localDateTime );
70
+ Session savedSession = new Session (1L , image , duration , sessionState , new Applicants (),
71
+ SessionStatus .READY , 1L , localDateTime , localDateTime );
72
+ when (sessionRepository .findById (1L )).thenReturn (Optional .of (savedSession ));
73
+
74
+ sessionService .create (1L , newSession , localDateTime );
75
+ Session findSession = sessionRepository .findById (1L ).orElseThrow (NotFoundException ::new );
76
+
77
+ assertThat (findSession .getId ()).isEqualTo (1L );
78
+ assertThat (findSession .getSessionStatus ()).isEqualTo (SessionStatus .READY );
79
+ assertThat (findSession .getApplicants ()).hasSize (0 );
56
80
}
57
81
58
82
@ Test
@@ -64,5 +88,48 @@ void apply_success() {
64
88
sessionService .applySession (APPLE , session .getId (), payment , localDateTime );
65
89
66
90
assertThat (session .applyCount ()).isEqualTo (2 );
91
+ assertThat (session .getApplicants ()).contains (APPLE );
92
+ }
93
+
94
+ @ Test
95
+ @ DisplayName ("강의 시작 날짜 전이라면 주어진 식별자에 해당하는 강의를 준비 상태로 변경한다." )
96
+ void changeOnReady_success () {
97
+ duration = new Duration (DATE_2023_12_6 , DATE_2023_12_12 );
98
+ savedSession = new Session (1L , image , duration , sessionState , new Applicants (),
99
+ SessionStatus .RECRUIT , 1L , localDateTime , localDateTime );
100
+ when (sessionRepository .findById (1L )).thenReturn (Optional .of (savedSession ));
101
+
102
+ sessionService .changeOnReady (1L , DATE_2023_12_5 );
103
+
104
+ assertThat (savedSession .getId ()).isEqualTo (1L );
105
+ assertThat (savedSession .getSessionStatus ()).isEqualTo (SessionStatus .READY );
106
+ }
107
+
108
+ @ Test
109
+ @ DisplayName ("강의 시작 날짜 전이라면 주어진 식별자에 해당하는 강의를 모집중 상태로 변경한다." )
110
+ void changeOnRecruit_success () {
111
+ duration = new Duration (DATE_2023_12_6 , DATE_2023_12_12 );
112
+ savedSession = new Session (1L , image , duration , sessionState , new Applicants (),
113
+ SessionStatus .READY , 1L , localDateTime , localDateTime );
114
+ when (sessionRepository .findById (1L )).thenReturn (Optional .of (savedSession ));
115
+
116
+ sessionService .changeOnRecruit (1L , DATE_2023_12_5 );
117
+
118
+ assertThat (savedSession .getId ()).isEqualTo (1L );
119
+ assertThat (savedSession .getSessionStatus ()).isEqualTo (SessionStatus .RECRUIT );
120
+ }
121
+
122
+ @ Test
123
+ @ DisplayName ("강의 종료날짜 이후라면 주어진 식별자에 해당하는 강의를 종료 상태로 변경한다." )
124
+ void changeOnEnd_success () {
125
+ duration = new Duration (DATE_2023_12_5 , DATE_2023_12_10 );
126
+ savedSession = new Session (1L , image , duration , sessionState , new Applicants (),
127
+ SessionStatus .READY , 1L , localDateTime , localDateTime );
128
+ when (sessionRepository .findById (1L )).thenReturn (Optional .of (savedSession ));
129
+
130
+ sessionService .changeOnEnd (1L , DATE_2023_12_12 );
131
+
132
+ assertThat (savedSession .getId ()).isEqualTo (1L );
133
+ assertThat (savedSession .getSessionStatus ()).isEqualTo (SessionStatus .END );
67
134
}
68
135
}
0 commit comments