Skip to content

Commit 4ff1895

Browse files
committed
[feat] 강의 서비스 테스트를 추가하라
강의 생성, 강의 상태 변경 3가지 테스트를 추가합니다. 예외 테스트는 도메인 테스트에서 했기 때문에 정상 상황만 테스트합니다
1 parent f8fdd94 commit 4ff1895

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

Diff for: src/main/java/nextstep/courses/service/SessionService.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ public void create(Long courseId, Session session, LocalDateTime date) {
2121
sessionRepository.save(courseId, session);
2222
}
2323

24-
public void applySession(NsUser loginUser, long sessionId, Payment payment, LocalDateTime date) {
24+
public void applySession(NsUser loginUser, Long sessionId, Payment payment, LocalDateTime date) {
2525
Session session = getSession(sessionId);
2626
Apply apply = session.apply(loginUser, payment, date);
2727
sessionRepository.saveApply(apply);
2828
}
2929

30-
public void changeOnReady(long sessionId, LocalDate date) {
30+
public void changeOnReady(Long sessionId, LocalDate date) {
3131
Session session = getSession(sessionId);
3232
session.changeOnReady(date);
3333
sessionRepository.update(sessionId, session);
3434
}
3535

36-
public void changeOnRecruit(long sessionId, LocalDate date) {
36+
public void changeOnRecruit(Long sessionId, LocalDate date) {
3737
Session session = getSession(sessionId);
3838
session.changeOnRecruit(date);
3939
sessionRepository.update(sessionId, session);
4040
}
4141

42-
public void changeOnEnd(long sessionId, LocalDate date) {
42+
public void changeOnEnd(Long sessionId, LocalDate date) {
4343
Session session = getSession(sessionId);
4444
session.changeOnEnd(date);
4545
sessionRepository.update(sessionId, session);
4646
}
4747

48-
private Session getSession(long sessionId) {
48+
private Session getSession(Long sessionId) {
4949
return sessionRepository.findById(sessionId).orElseThrow(NotFoundException::new);
5050
}
5151
}

Diff for: src/test/java/nextstep/courses/domain/course/service/SessionServiceTest.java

+68-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import nextstep.courses.domain.course.session.*;
66
import nextstep.courses.service.SessionService;
77
import nextstep.payments.domain.Payment;
8+
import nextstep.qna.NotFoundException;
89
import nextstep.users.domain.NsUser;
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.DisplayName;
@@ -25,6 +26,10 @@
2526
public class SessionServiceTest {
2627
private static final NsUser JAVAJIGI = new NsUser(1L, "javajigi", "password", "name", "[email protected]");
2728
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);
2833

2934
private Image image;
3035
private Payment payment;
@@ -33,7 +38,9 @@ public class SessionServiceTest {
3338
private Applicants applicants;
3439
private Duration duration;
3540
private SessionState sessionState;
41+
private SessionStatus sessionStatus;
3642
private Session session;
43+
private Session savedSession;
3744

3845
@Mock
3946
private SessionRepository sessionRepository;
@@ -49,10 +56,27 @@ public void setUp() {
4956
localDate = LocalDate.of(2023, 12, 5);
5057
duration = new Duration(localDate, localDate);
5158
sessionState = new SessionState(SessionType.FREE, 0L, Integer.MAX_VALUE);
59+
sessionStatus = SessionStatus.RECRUIT;
5260
applicants = new Applicants();
5361
applicants.addApplicant(JAVAJIGI, sessionState);
5462
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);
5680
}
5781

5882
@Test
@@ -64,5 +88,48 @@ void apply_success() {
6488
sessionService.applySession(APPLE, session.getId(), payment, localDateTime);
6589

6690
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);
67134
}
68135
}

0 commit comments

Comments
 (0)