|
| 1 | +package com.example.solidconnection.mentor.service; |
| 2 | + |
| 3 | +import com.example.solidconnection.common.VerifyStatus; |
| 4 | +import com.example.solidconnection.common.exception.CustomException; |
| 5 | +import com.example.solidconnection.mentor.domain.Mentor; |
| 6 | +import com.example.solidconnection.mentor.domain.Mentoring; |
| 7 | +import com.example.solidconnection.mentor.dto.MentoringApplyRequest; |
| 8 | +import com.example.solidconnection.mentor.dto.MentoringApplyResponse; |
| 9 | +import com.example.solidconnection.mentor.dto.MentoringCheckResponse; |
| 10 | +import com.example.solidconnection.mentor.dto.MentoringConfirmRequest; |
| 11 | +import com.example.solidconnection.mentor.dto.MentoringConfirmResponse; |
| 12 | +import com.example.solidconnection.mentor.fixture.MentorFixture; |
| 13 | +import com.example.solidconnection.mentor.fixture.MentoringFixture; |
| 14 | +import com.example.solidconnection.mentor.repository.MentorRepository; |
| 15 | +import com.example.solidconnection.mentor.repository.MentoringRepository; |
| 16 | +import com.example.solidconnection.siteuser.domain.SiteUser; |
| 17 | +import com.example.solidconnection.siteuser.fixture.SiteUserFixture; |
| 18 | +import com.example.solidconnection.support.TestContainerSpringBootTest; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.DisplayName; |
| 21 | +import org.junit.jupiter.api.Nested; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | + |
| 25 | +import static com.example.solidconnection.common.exception.ErrorCode.ALREADY_MENTOR; |
| 26 | +import static com.example.solidconnection.common.exception.ErrorCode.MENTORING_ALREADY_CONFIRMED; |
| 27 | +import static com.example.solidconnection.common.exception.ErrorCode.MENTORING_NOT_FOUND; |
| 28 | +import static com.example.solidconnection.common.exception.ErrorCode.REJECTED_REASON_REQUIRED; |
| 29 | +import static com.example.solidconnection.common.exception.ErrorCode.UNAUTHORIZED_MENTORING; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 33 | +@TestContainerSpringBootTest |
| 34 | +@DisplayName("멘토링 CUD 서비스 테스트") |
| 35 | +class MentoringCommandServiceTest { |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private MentoringCommandService mentoringCommandService; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private MentorRepository mentorRepository; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private MentoringRepository mentoringRepository; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private SiteUserFixture siteUserFixture; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private MentorFixture mentorFixture; |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private MentoringFixture mentoringFixture; |
| 54 | + |
| 55 | + private SiteUser mentorUser1; |
| 56 | + private SiteUser mentorUser2; |
| 57 | + |
| 58 | + private SiteUser menteeUser; |
| 59 | + private Mentor mentor1; |
| 60 | + private Mentor mentor2; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void setUp() { |
| 64 | + mentorUser1 = siteUserFixture.멘토(1, "mentor1"); |
| 65 | + menteeUser = siteUserFixture.사용자(2, "mentee1"); |
| 66 | + mentorUser2 = siteUserFixture.멘토(3, "mentor2"); |
| 67 | + |
| 68 | + mentor1 = mentorFixture.멘토(mentorUser1.getId(), 1L); |
| 69 | + mentor2 = mentorFixture.멘토(mentorUser2.getId(), 2L); |
| 70 | + } |
| 71 | + |
| 72 | + @Nested |
| 73 | + class 멘토링_신청_테스트 { |
| 74 | + |
| 75 | + @Test |
| 76 | + void 멘토링을_성공적으로_신청한다() { |
| 77 | + // given |
| 78 | + MentoringApplyRequest request = new MentoringApplyRequest(mentor1.getId()); |
| 79 | + |
| 80 | + // when |
| 81 | + MentoringApplyResponse response = mentoringCommandService.applyMentoring(menteeUser.getId(), request); |
| 82 | + |
| 83 | + // then |
| 84 | + Mentoring mentoring = mentoringRepository.findById(response.mentoringId()).orElseThrow(); |
| 85 | + |
| 86 | + assertAll( |
| 87 | + () -> assertThat(mentoring.getMentorId()).isEqualTo(mentor1.getId()), |
| 88 | + () -> assertThat(mentoring.getMenteeId()).isEqualTo(menteeUser.getId()), |
| 89 | + () -> assertThat(mentoring.getVerifyStatus()).isEqualTo(VerifyStatus.PENDING) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void 이미_멘토인_사용자가_신청시_예외를_반환한다() { |
| 95 | + // given |
| 96 | + MentoringApplyRequest request = new MentoringApplyRequest(mentor2.getId()); |
| 97 | + |
| 98 | + // when & then |
| 99 | + assertThatThrownBy(() -> |
| 100 | + mentoringCommandService.applyMentoring(mentorUser1.getId(), request)) |
| 101 | + .isInstanceOf(CustomException.class) |
| 102 | + .hasMessage(ALREADY_MENTOR.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Nested |
| 107 | + class 멘토링_승인_거절_테스트 { |
| 108 | + |
| 109 | + @Test |
| 110 | + void 멘토링을_성공적으로_승인한다() { |
| 111 | + // given |
| 112 | + Mentoring mentoring = mentoringFixture.대기중_멘토링(mentor1.getId(), menteeUser.getId()); |
| 113 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.APPROVED, null); |
| 114 | + int beforeMenteeCount = mentor1.getMenteeCount(); |
| 115 | + |
| 116 | + // when |
| 117 | + MentoringConfirmResponse response = mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); |
| 118 | + |
| 119 | + // then |
| 120 | + Mentoring confirmedMentoring = mentoringRepository.findById(response.mentoringId()).orElseThrow(); |
| 121 | + Mentor mentor = mentorRepository.findById(mentor1.getId()).orElseThrow(); |
| 122 | + |
| 123 | + assertAll( |
| 124 | + () -> assertThat(confirmedMentoring.getVerifyStatus()).isEqualTo(VerifyStatus.APPROVED), |
| 125 | + () -> assertThat(confirmedMentoring.getConfirmedAt()).isNotNull(), |
| 126 | + () -> assertThat(confirmedMentoring.getCheckedAt()).isNotNull(), |
| 127 | + () -> assertThat(mentor.getMenteeCount()).isEqualTo(beforeMenteeCount + 1) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void 멘토링을_성공적으로_거절한다() { |
| 133 | + // given |
| 134 | + Mentoring mentoring = mentoringFixture.대기중_멘토링(mentor1.getId(), menteeUser.getId()); |
| 135 | + String rejectedReason = "멘토링 거절 사유"; |
| 136 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.REJECTED, rejectedReason); |
| 137 | + int beforeMenteeCount = mentor1.getMenteeCount(); |
| 138 | + |
| 139 | + // when |
| 140 | + MentoringConfirmResponse response = mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); |
| 141 | + |
| 142 | + // then |
| 143 | + Mentoring confirmedMentoring = mentoringRepository.findById(response.mentoringId()).orElseThrow(); |
| 144 | + Mentor mentor = mentorRepository.findById(mentor1.getId()).orElseThrow(); |
| 145 | + |
| 146 | + assertAll( |
| 147 | + () -> assertThat(confirmedMentoring.getVerifyStatus()).isEqualTo(VerifyStatus.REJECTED), |
| 148 | + () -> assertThat(confirmedMentoring.getRejectedReason()).isEqualTo(rejectedReason), |
| 149 | + () -> assertThat(confirmedMentoring.getConfirmedAt()).isNotNull(), |
| 150 | + () -> assertThat(confirmedMentoring.getCheckedAt()).isNotNull(), |
| 151 | + () -> assertThat(mentor.getMenteeCount()).isEqualTo(beforeMenteeCount) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void 거절_시_사유가_없으면_예외를_반환한다() { |
| 157 | + // given |
| 158 | + Mentoring mentoring = mentoringFixture.대기중_멘토링(mentor1.getId(), menteeUser.getId()); |
| 159 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.REJECTED, null); |
| 160 | + |
| 161 | + // when & then |
| 162 | + assertThatThrownBy(() -> |
| 163 | + mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request)) |
| 164 | + .isInstanceOf(CustomException.class) |
| 165 | + .hasMessage(REJECTED_REASON_REQUIRED.getMessage()); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void 다른_멘토의_멘토링을_승인할_수_없다() { |
| 170 | + // given |
| 171 | + Mentoring mentoring = mentoringFixture.대기중_멘토링(mentor1.getId(), menteeUser.getId()); |
| 172 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.APPROVED, null); |
| 173 | + |
| 174 | + // when & then |
| 175 | + assertThatThrownBy(() -> |
| 176 | + mentoringCommandService.confirmMentoring(mentorUser2.getId(), mentoring.getId(), request)) |
| 177 | + .isInstanceOf(CustomException.class) |
| 178 | + .hasMessage(UNAUTHORIZED_MENTORING.getMessage()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void 이미_처리된_멘토링은_다시_승인할_수_없다() { |
| 183 | + // given |
| 184 | + Mentoring mentoring = mentoringFixture.승인된_멘토링(mentor1.getId(), menteeUser.getId()); |
| 185 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.APPROVED, null); |
| 186 | + |
| 187 | + // when & then |
| 188 | + assertThatThrownBy(() -> |
| 189 | + mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request)) |
| 190 | + .isInstanceOf(CustomException.class) |
| 191 | + .hasMessage(MENTORING_ALREADY_CONFIRMED.getMessage()); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + void 존재하지_않는_멘토링_아이디로_요청시_예외를_반환한다() { |
| 196 | + // given |
| 197 | + MentoringConfirmRequest request = new MentoringConfirmRequest(VerifyStatus.APPROVED, null); |
| 198 | + Long invalidMentoringId = 9999L; |
| 199 | + |
| 200 | + // when & then |
| 201 | + assertThatThrownBy(() -> mentoringCommandService.confirmMentoring(mentorUser1.getId(), invalidMentoringId, request)) |
| 202 | + .isInstanceOf(CustomException.class) |
| 203 | + .hasMessage(MENTORING_NOT_FOUND.getMessage()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Nested |
| 208 | + class 멘토링_확인_테스트 { |
| 209 | + |
| 210 | + @Test |
| 211 | + void 멘토링을_성공적으로_확인_처리한다() { |
| 212 | + // given |
| 213 | + Mentoring mentoring = mentoringFixture.확인되지_않은_멘토링(mentor1.getId(), menteeUser.getId()); |
| 214 | + |
| 215 | + // when |
| 216 | + MentoringCheckResponse response = mentoringCommandService.checkMentoring(mentorUser1.getId(), mentoring.getId()); |
| 217 | + |
| 218 | + // then |
| 219 | + Mentoring checked = mentoringRepository.findById(response.mentoringId()).orElseThrow(); |
| 220 | + |
| 221 | + assertThat(checked.getCheckedAt()).isNotNull(); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + void 다른_멘토의_멘토링은_확인할_수_없다() { |
| 226 | + // given |
| 227 | + Mentoring mentoring = mentoringFixture.확인되지_않은_멘토링(mentor1.getId(), menteeUser.getId()); |
| 228 | + |
| 229 | + // when & then |
| 230 | + assertThatThrownBy(() -> mentoringCommandService.checkMentoring(mentorUser2.getId(), mentoring.getId())) |
| 231 | + .isInstanceOf(CustomException.class) |
| 232 | + .hasMessage(UNAUTHORIZED_MENTORING.getMessage()); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + void 존재하지_않는_멘토링_아이디로_요청시_예외를_반환한다() { |
| 237 | + // given |
| 238 | + Long invalidMentoringId = 9999L; |
| 239 | + |
| 240 | + // when & then |
| 241 | + assertThatThrownBy(() -> mentoringCommandService.checkMentoring(mentorUser1.getId(), invalidMentoringId)) |
| 242 | + .isInstanceOf(CustomException.class) |
| 243 | + .hasMessage(MENTORING_NOT_FOUND.getMessage()); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments