1+ package grep .neogul_coder .domain .attendance .service ;
2+
3+ import grep .neogul_coder .domain .IntegrationTestSupport ;
4+ import grep .neogul_coder .domain .attendance .Attendance ;
5+ import grep .neogul_coder .domain .attendance .controller .dto .response .AttendanceInfoResponse ;
6+ import grep .neogul_coder .domain .attendance .repository .AttendanceRepository ;
7+ import grep .neogul_coder .domain .study .Study ;
8+ import grep .neogul_coder .domain .study .StudyMember ;
9+ import grep .neogul_coder .domain .study .repository .StudyMemberRepository ;
10+ import grep .neogul_coder .domain .study .repository .StudyRepository ;
11+ import grep .neogul_coder .domain .users .entity .User ;
12+ import grep .neogul_coder .domain .users .repository .UserRepository ;
13+ import grep .neogul_coder .global .exception .business .BusinessException ;
14+ import org .junit .jupiter .api .BeforeEach ;
15+ import org .junit .jupiter .api .DisplayName ;
16+ import org .junit .jupiter .api .Test ;
17+ import org .springframework .beans .factory .annotation .Autowired ;
18+
19+ import java .time .LocalDate ;
20+ import java .time .LocalDateTime ;
21+
22+ import static grep .neogul_coder .domain .attendance .exception .code .AttendanceErrorCode .ATTENDANCE_ALREADY_CHECKED ;
23+ import static org .assertj .core .api .Assertions .assertThat ;
24+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
25+
26+ class AttendanceServiceTest extends IntegrationTestSupport {
27+
28+ @ Autowired
29+ private UserRepository userRepository ;
30+
31+ @ Autowired
32+ private StudyRepository studyRepository ;
33+
34+ @ Autowired
35+ private StudyMemberRepository studyMemberRepository ;
36+
37+ @ Autowired
38+ private AttendanceService attendanceService ;
39+
40+ @ Autowired
41+ private AttendanceRepository attendanceRepository ;
42+
43+ private Long userId ;
44+ private Long studyId ;
45+
46+ @ BeforeEach
47+ void init () {
48+ User user = createUser ("test1" );
49+ userRepository .save (user );
50+ userId = user .getId ();
51+
52+ Study study = createStudy ("스터디" , LocalDateTime .parse ("2025-07-25T20:20:20" ), LocalDateTime .parse ("2025-07-28T20:20:20" ));
53+ studyRepository .save (study );
54+ studyId = study .getId ();
55+
56+ StudyMember studyMember = createStudyMember (study , userId );
57+ studyMemberRepository .save (studyMember );
58+ }
59+
60+ @ DisplayName ("출석을 조회합니다." )
61+ @ Test
62+ void getAttendances () {
63+ // given
64+ Attendance attendance = Attendance .create (studyId , userId );
65+ attendanceRepository .save (attendance );
66+
67+ // when
68+ AttendanceInfoResponse response = attendanceService .getAttendances (studyId , userId );
69+
70+ // then
71+ assertThat (response .getAttendances ().getFirst ().getStudyId ()).isEqualTo (studyId );
72+ }
73+
74+ @ DisplayName ("스터디에 출석 체크를 합니다." )
75+ @ Test
76+ void createAttendance () {
77+ // given
78+ Attendance attendance = Attendance .create (studyId , userId );
79+
80+ // when
81+ attendanceRepository .save (attendance );
82+
83+ // then
84+ assertThat (attendance .getAttendanceDate ().toLocalDate ()).isEqualTo (LocalDate .now ());
85+ }
86+
87+ @ DisplayName ("스터디에 이미 출석한 경우 예외가 발생합니다." )
88+ @ Test
89+ void createAttendanceFail () {
90+ // given
91+ Attendance attendance = Attendance .create (studyId , userId );
92+ attendanceRepository .save (attendance );
93+
94+ // when then
95+ assertThatThrownBy (() ->
96+ attendanceService .createAttendance (studyId , userId ))
97+ .isInstanceOf (BusinessException .class ).hasMessage (ATTENDANCE_ALREADY_CHECKED .getMessage ());
98+ }
99+
100+ private static User createUser (String nickname ) {
101+ return User .builder ()
102+ .nickname (nickname )
103+ .build ();
104+ }
105+
106+ private static Study createStudy (String name , LocalDateTime startDate , LocalDateTime endDate ) {
107+ return Study .builder ()
108+ .name (name )
109+ .startDate (startDate )
110+ .endDate (endDate )
111+ .build ();
112+ }
113+
114+ private StudyMember createStudyMember (Study study , long userId ) {
115+ return StudyMember .builder ()
116+ .study (study )
117+ .userId (userId )
118+ .build ();
119+ }
120+ }
0 commit comments