|
| 1 | +package gdsc.konkuk.platformcore.application.attendance; |
| 2 | + |
| 3 | +import gdsc.konkuk.platformcore.util.fixture.attendance.AttendanceFixture; |
| 4 | +import java.time.LocalDateTime; |
| 5 | +import java.util.List; |
| 6 | +import gdsc.konkuk.platformcore.application.attendance.dtos.AttendanceStatus; |
| 7 | +import gdsc.konkuk.platformcore.application.attendance.exceptions.QrInvalidException; |
| 8 | +import gdsc.konkuk.platformcore.application.member.MemberService; |
| 9 | +import gdsc.konkuk.platformcore.application.member.exceptions.UserNotFoundException; |
| 10 | +import gdsc.konkuk.platformcore.domain.attendance.entity.Attendance; |
| 11 | +import gdsc.konkuk.platformcore.domain.attendance.entity.AttendanceType; |
| 12 | +import gdsc.konkuk.platformcore.domain.attendance.entity.Participant; |
| 13 | +import gdsc.konkuk.platformcore.domain.attendance.repository.AttendanceRepository; |
| 14 | +import gdsc.konkuk.platformcore.domain.attendance.repository.ParticipantRepository; |
| 15 | +import gdsc.konkuk.platformcore.domain.member.entity.Member; |
| 16 | +import gdsc.konkuk.platformcore.domain.member.repository.MemberRepository; |
| 17 | +import gdsc.konkuk.platformcore.util.fixture.member.MemberRegisterRequestFixture; |
| 18 | +import java.time.LocalDate; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import org.junit.jupiter.api.DisplayName; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.test.context.SpringBootTest; |
| 26 | + |
| 27 | +@SpringBootTest |
| 28 | +public class AttendanceIntegrationTest { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private AttendanceService attendanceService; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private MemberService memberService; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private MemberRepository memberRepository; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private AttendanceRepository attendanceRepository; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private ParticipantRepository participantRepository; |
| 44 | + |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + void tearDown() { |
| 48 | + memberRepository.deleteAll(); |
| 49 | + participantRepository.deleteAll(); |
| 50 | + attendanceRepository.deleteAll(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("출석 등록 : 정상적인 출석 등록") |
| 55 | + void should_success_when_register_attendance() { |
| 56 | + // given |
| 57 | + String title = "example"; |
| 58 | + String batch = "24-25"; |
| 59 | + |
| 60 | + Member batchMember1 = memberService.register(MemberRegisterRequestFixture.builder() |
| 61 | + .batch(batch) |
| 62 | + .studentId("student1") |
| 63 | + |
| 64 | + .build() |
| 65 | + .getFixture() |
| 66 | + .toCommand()); |
| 67 | + Member batchMember2 = memberService.register(MemberRegisterRequestFixture.builder() |
| 68 | + .batch(batch) |
| 69 | + .studentId("student2") |
| 70 | + |
| 71 | + .build() |
| 72 | + .getFixture() |
| 73 | + .toCommand()); |
| 74 | + Member anotherBatchMember = memberService.register(MemberRegisterRequestFixture.builder() |
| 75 | + .batch("26-27") |
| 76 | + .studentId("student3") |
| 77 | + |
| 78 | + .build() |
| 79 | + .getFixture() |
| 80 | + .toCommand()); |
| 81 | + |
| 82 | + // when |
| 83 | + Attendance savedAttendance = attendanceService.registerAttendance(title, batch); |
| 84 | + AttendanceStatus attendanceStatus = attendanceService.getAttendanceStatus( |
| 85 | + savedAttendance.getId()); |
| 86 | + |
| 87 | + // then |
| 88 | + assertThat(savedAttendance.getTitle()).isEqualTo(title); |
| 89 | + assertThat(attendanceStatus.getTotal()).isEqualTo(2); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @DisplayName("출석 조회 : 이달의 모든 출석 조회 성공") |
| 94 | + void should_success_when_get_all_month_attendance() { |
| 95 | + //given |
| 96 | + Attendance attendance1 = attendanceService.registerAttendance("example", "24-25"); |
| 97 | + Attendance attendance2 = attendanceService.registerAttendance("example", "24-25"); |
| 98 | + Attendance attendance3 = attendanceService.registerAttendance("example", "24-25"); |
| 99 | + Attendance outOfPeriodAttendance = attendanceRepository.save( |
| 100 | + AttendanceFixture.builder() |
| 101 | + .attendanceTime(LocalDateTime.now().minusMonths(1)) |
| 102 | + .build().getFixture()); |
| 103 | + |
| 104 | + // when |
| 105 | + List<Attendance> attendanceList = attendanceService.getAllByPeriod(LocalDate.now()); |
| 106 | + |
| 107 | + // then |
| 108 | + assertThat(attendanceList.size()).isEqualTo(3); |
| 109 | + assertThat(attendanceList.stream().map(Attendance::getId)) |
| 110 | + .contains(attendance1.getId(), attendance2.getId(), attendance3.getId()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + @DisplayName("출석 : 정상적인 출석") |
| 115 | + void should_success_when_attend() { |
| 116 | + //given |
| 117 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 118 | + .batch("24-25") |
| 119 | + .build() |
| 120 | + .getFixture() |
| 121 | + .toCommand()); |
| 122 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 123 | + |
| 124 | + // when |
| 125 | + Participant participant = attendanceService.attend( |
| 126 | + member.getEmail(), attendance.getId(), attendance.getActiveQrUuid()); |
| 127 | + |
| 128 | + // then |
| 129 | + assertThat(participant.getMemberId()).isEqualTo(member.getId()); |
| 130 | + assertThat(participant.getAttendanceType()).isEqualTo(AttendanceType.ATTEND); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("출석 : 출석이 종료된 경우 실패") |
| 135 | + void should_fail_when_attendance_end() { |
| 136 | + //given |
| 137 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 138 | + .batch("24-25") |
| 139 | + .build() |
| 140 | + .getFixture() |
| 141 | + .toCommand()); |
| 142 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 143 | + String qrUuid = attendance.getActiveQrUuid(); |
| 144 | + |
| 145 | + // when |
| 146 | + attendanceService.expireQr(attendance.getId()); |
| 147 | + |
| 148 | + // then |
| 149 | + assertThrows(QrInvalidException.class, |
| 150 | + () -> attendanceService.attend(member.getEmail(), attendance.getId(), qrUuid)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + @DisplayName("출석 : QR코드가 null이면 실패") |
| 155 | + void should_fail_when_no_Qr() { |
| 156 | + //given |
| 157 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 158 | + .batch("24-25") |
| 159 | + .build() |
| 160 | + .getFixture() |
| 161 | + .toCommand()); |
| 162 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 163 | + |
| 164 | + // when, then |
| 165 | + assertThrows(QrInvalidException.class, |
| 166 | + () -> attendanceService.attend(member.getEmail(), attendance.getId(), null)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + @DisplayName("출석 : QR코드가 불일치하면 실패") |
| 171 | + void should_fail_when_wrong_Qr() { |
| 172 | + //given |
| 173 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 174 | + .batch("24-25") |
| 175 | + .build() |
| 176 | + .getFixture() |
| 177 | + .toCommand()); |
| 178 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 179 | + |
| 180 | + // when, then |
| 181 | + assertThrows(QrInvalidException.class, |
| 182 | + () -> attendanceService.attend(member.getEmail(), attendance.getId(), |
| 183 | + attendance.getActiveQrUuid() + "make it wrong")); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + @DisplayName("출석 : 존재하지 않는 멤버가 출석 시도 시 실패") |
| 188 | + void should_fail_when_member_not_exist() { |
| 189 | + //given |
| 190 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 191 | + |
| 192 | + // when, then |
| 193 | + assertThrows(UserNotFoundException.class, |
| 194 | + () -> attendanceService.attend("not exist", attendance.getId(), |
| 195 | + attendance.getActiveQrUuid())); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + @DisplayName("출석 현황 조회 : 출석 현황 조회 성공") |
| 200 | + void should_success_when_get_attendance_status() { |
| 201 | + //given |
| 202 | + Member member1 = memberService.register(MemberRegisterRequestFixture.builder() |
| 203 | + .batch("24-25") |
| 204 | + .studentId("student1") |
| 205 | + |
| 206 | + .build() |
| 207 | + .getFixture() |
| 208 | + .toCommand()); |
| 209 | + Member member2 = memberService.register(MemberRegisterRequestFixture.builder() |
| 210 | + .batch("24-25") |
| 211 | + .studentId("student2") |
| 212 | + |
| 213 | + .build() |
| 214 | + .getFixture() |
| 215 | + .toCommand()); |
| 216 | + Member member3 = memberService.register(MemberRegisterRequestFixture.builder() |
| 217 | + .batch("24-25") |
| 218 | + .studentId("student3") |
| 219 | + |
| 220 | + .build() |
| 221 | + .getFixture() |
| 222 | + .toCommand()); |
| 223 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 224 | + attendanceService.attend(member1.getEmail(), attendance.getId(), |
| 225 | + attendance.getActiveQrUuid()); |
| 226 | + |
| 227 | + // when |
| 228 | + AttendanceStatus attendanceStatus = attendanceService.getAttendanceStatus( |
| 229 | + attendance.getId()); |
| 230 | + |
| 231 | + // then |
| 232 | + assertThat(attendanceStatus.getAttendanceId()).isEqualTo(attendance.getId()); |
| 233 | + assertThat(attendanceStatus.getTotal()).isEqualTo(3); |
| 234 | + assertThat(attendanceStatus.getAttended()).isEqualTo(1); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + @DisplayName("출석 삭제 : 삭제한 출석에는 참여자가 없어야 함") |
| 239 | + void should_success_when_delete_attendance() { |
| 240 | + //given |
| 241 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 242 | + .batch("24-25") |
| 243 | + .build() |
| 244 | + .getFixture() |
| 245 | + .toCommand()); |
| 246 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 247 | + |
| 248 | + assert attendanceRepository.findById(attendance.getId()).isPresent(); |
| 249 | + assert participantRepository.findAllByAttendanceId(attendance.getId()).size() == 1; |
| 250 | + |
| 251 | + // when |
| 252 | + attendanceService.deleteAttendance(attendance.getId()); |
| 253 | + |
| 254 | + // then |
| 255 | + assertThat(attendanceRepository.findById(attendance.getId())).isEmpty(); |
| 256 | + assertThat(participantRepository.findAllByAttendanceId(attendance.getId()).size()) |
| 257 | + .isEqualTo(0); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + @DisplayName("출석 만료 : 만료된 출석에는 참여자가 유지되어야 함") |
| 262 | + void should_success_when_expire_attendance() { |
| 263 | + //given |
| 264 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 265 | + .batch("24-25") |
| 266 | + .build() |
| 267 | + .getFixture() |
| 268 | + .toCommand()); |
| 269 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 270 | + |
| 271 | + assert attendanceRepository.findById(attendance.getId()).isPresent(); |
| 272 | + assert participantRepository.findAllByAttendanceId(attendance.getId()).size() == 1; |
| 273 | + |
| 274 | + // when |
| 275 | + attendanceService.expireQr(attendance.getId()); |
| 276 | + |
| 277 | + // then |
| 278 | + assertThat(attendanceRepository.findById(attendance.getId())).isPresent(); |
| 279 | + assertThat(participantRepository.findAllByAttendanceId(attendance.getId()).size()) |
| 280 | + .isEqualTo(1); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + @DisplayName("출석 재시작 : 만료된 출석 재시작 성공") |
| 285 | + void should_success_when_restart_attendance() { |
| 286 | + //given |
| 287 | + Member member = memberService.register(MemberRegisterRequestFixture.builder() |
| 288 | + .batch("24-25") |
| 289 | + .build() |
| 290 | + .getFixture() |
| 291 | + .toCommand()); |
| 292 | + Attendance attendance = attendanceService.registerAttendance("example", "24-25"); |
| 293 | + attendanceService.expireQr(attendance.getId()); |
| 294 | + |
| 295 | + // when |
| 296 | + Attendance restartedAttendance = attendanceService.generateQr(attendance.getId()); |
| 297 | + Participant participant = attendanceService.attend( |
| 298 | + member.getEmail(), restartedAttendance.getId(), |
| 299 | + restartedAttendance.getActiveQrUuid()); |
| 300 | + |
| 301 | + // then |
| 302 | + assertThat(restartedAttendance.getActiveQrUuid()).isNotNull(); |
| 303 | + assertThat(participant.getAttendanceType()).isEqualTo(AttendanceType.ATTEND); |
| 304 | + } |
| 305 | +} |
0 commit comments