|
| 1 | +package com.somemore.facade.volunteerapply; |
| 2 | + |
| 3 | +import static com.somemore.auth.oauth.OAuthProvider.NAVER; |
| 4 | +import static com.somemore.common.fixture.RecruitBoardFixture.createCompletedRecruitBoard; |
| 5 | +import static com.somemore.global.exception.ExceptionMessage.RECRUIT_BOARD_ID_MISMATCH; |
| 6 | +import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_RECRUIT_BOARD; |
| 7 | +import static com.somemore.global.exception.ExceptionMessage.VOLUNTEER_APPLY_LIST_MISMATCH; |
| 8 | +import static com.somemore.recruitboard.domain.VolunteerCategory.COUNSELING; |
| 9 | +import static com.somemore.volunteerapply.domain.ApplyStatus.APPROVED; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 12 | + |
| 13 | +import com.somemore.IntegrationTestSupport; |
| 14 | +import com.somemore.global.exception.BadRequestException; |
| 15 | +import com.somemore.recruitboard.domain.RecruitBoard; |
| 16 | +import com.somemore.recruitboard.repository.RecruitBoardRepository; |
| 17 | +import com.somemore.volunteer.domain.Volunteer; |
| 18 | +import com.somemore.volunteer.repository.VolunteerRepository; |
| 19 | +import com.somemore.volunteerapply.domain.VolunteerApply; |
| 20 | +import com.somemore.volunteerapply.dto.request.VolunteerApplySettleRequestDto; |
| 21 | +import com.somemore.volunteerapply.repository.VolunteerApplyRepository; |
| 22 | +import java.util.List; |
| 23 | +import java.util.UUID; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | + |
| 28 | +class SettleVolunteerApplyFacadeServiceTest extends IntegrationTestSupport { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private SettleVolunteerApplyFacadeService settleVolunteerApplyFacadeService; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private RecruitBoardRepository recruitBoardRepository; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private VolunteerApplyRepository volunteerApplyRepository; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private VolunteerRepository volunteerRepository; |
| 41 | + |
| 42 | + @DisplayName("봉사 활동 지원을 정산할 수 있다.") |
| 43 | + @Test |
| 44 | + void settleVolunteerApplies() { |
| 45 | + // given |
| 46 | + UUID centerId = UUID.randomUUID(); |
| 47 | + |
| 48 | + Volunteer volunteer1 = volunteerRepository.save(createVolunteer()); |
| 49 | + Volunteer volunteer2 = volunteerRepository.save(createVolunteer()); |
| 50 | + Volunteer volunteer3 = volunteerRepository.save(createVolunteer()); |
| 51 | + |
| 52 | + RecruitBoard board = createCompletedRecruitBoard(centerId, COUNSELING); |
| 53 | + recruitBoardRepository.save(board); |
| 54 | + |
| 55 | + VolunteerApply apply1 = createApply(volunteer1.getId(), board.getId()); |
| 56 | + VolunteerApply apply2 = createApply(volunteer2.getId(), board.getId()); |
| 57 | + VolunteerApply apply3 = createApply(volunteer3.getId(), board.getId()); |
| 58 | + volunteerApplyRepository.saveAll(List.of(apply1, apply2, apply3)); |
| 59 | + |
| 60 | + int hour = board.getVolunteerHours().getHour(); |
| 61 | + VolunteerApplySettleRequestDto dto = VolunteerApplySettleRequestDto.builder() |
| 62 | + .ids(List.of(apply1.getId(), apply2.getId(), apply3.getId())) |
| 63 | + .build(); |
| 64 | + |
| 65 | + // when |
| 66 | + settleVolunteerApplyFacadeService.settleVolunteerApplies(dto, centerId); |
| 67 | + |
| 68 | + // then |
| 69 | + VolunteerApply findApply1 = volunteerApplyRepository.findById(apply1.getId()).orElseThrow(); |
| 70 | + VolunteerApply findApply2 = volunteerApplyRepository.findById(apply2.getId()).orElseThrow(); |
| 71 | + VolunteerApply findApply3 = volunteerApplyRepository.findById(apply3.getId()).orElseThrow(); |
| 72 | + |
| 73 | + assertThat(findApply1.getAttended()).isTrue(); |
| 74 | + assertThat(findApply2.getAttended()).isTrue(); |
| 75 | + assertThat(findApply3.getAttended()).isTrue(); |
| 76 | + |
| 77 | + Volunteer findVolunteer1 = volunteerRepository.findById(volunteer1.getId()).orElseThrow(); |
| 78 | + Volunteer findVolunteer2 = volunteerRepository.findById(volunteer2.getId()).orElseThrow(); |
| 79 | + Volunteer findVolunteer3 = volunteerRepository.findById(volunteer3.getId()).orElseThrow(); |
| 80 | + |
| 81 | + assertThat(findVolunteer1.getTotalVolunteerHours()).isEqualTo(hour); |
| 82 | + assertThat(findVolunteer2.getTotalVolunteerHours()).isEqualTo(hour); |
| 83 | + assertThat(findVolunteer3.getTotalVolunteerHours()).isEqualTo(hour); |
| 84 | + |
| 85 | + assertThat(findVolunteer1.getTotalVolunteerCount()).isEqualTo(1); |
| 86 | + assertThat(findVolunteer2.getTotalVolunteerCount()).isEqualTo(1); |
| 87 | + assertThat(findVolunteer3.getTotalVolunteerCount()).isEqualTo(1); |
| 88 | + } |
| 89 | + |
| 90 | + @DisplayName("정산시, 지원 리스트에 존재하지 않는 지원이 있는 경우 에러가 발생한다.") |
| 91 | + @Test |
| 92 | + void settleVolunteerAppliesWhenNotExistId() { |
| 93 | + // given |
| 94 | + UUID centerId = UUID.randomUUID(); |
| 95 | + |
| 96 | + Volunteer volunteer = volunteerRepository.save(createVolunteer()); |
| 97 | + |
| 98 | + RecruitBoard board = createCompletedRecruitBoard(centerId, COUNSELING); |
| 99 | + recruitBoardRepository.save(board); |
| 100 | + |
| 101 | + VolunteerApply apply = createApply(volunteer.getId(), board.getId()); |
| 102 | + volunteerApplyRepository.save(apply); |
| 103 | + |
| 104 | + VolunteerApplySettleRequestDto dto = VolunteerApplySettleRequestDto.builder() |
| 105 | + .ids(List.of(apply.getId(), 100L)) |
| 106 | + .build(); |
| 107 | + |
| 108 | + // when |
| 109 | + // then |
| 110 | + assertThatThrownBy( |
| 111 | + () -> settleVolunteerApplyFacadeService.settleVolunteerApplies(dto, centerId) |
| 112 | + ).isInstanceOf(BadRequestException.class) |
| 113 | + .hasMessage(VOLUNTEER_APPLY_LIST_MISMATCH.getMessage()); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @DisplayName("정산시, 모집글이 다른 지원이 있는 경우 에러가 발생한다.") |
| 118 | + @Test |
| 119 | + void settleVolunteerAppliesWhenMismatchApplyAndRecruitBoard() { |
| 120 | + // given |
| 121 | + UUID centerId = UUID.randomUUID(); |
| 122 | + |
| 123 | + Volunteer volunteer1 = volunteerRepository.save(createVolunteer()); |
| 124 | + Volunteer volunteer2 = volunteerRepository.save(createVolunteer()); |
| 125 | + |
| 126 | + RecruitBoard board = createCompletedRecruitBoard(centerId, COUNSELING); |
| 127 | + recruitBoardRepository.save(board); |
| 128 | + |
| 129 | + VolunteerApply apply1 = createApply(volunteer1.getId(), board.getId()); |
| 130 | + VolunteerApply apply2 = createApply(volunteer2.getId(), 100L); |
| 131 | + volunteerApplyRepository.saveAll(List.of(apply1, apply2)); |
| 132 | + |
| 133 | + VolunteerApplySettleRequestDto dto = VolunteerApplySettleRequestDto.builder() |
| 134 | + .ids(List.of(apply1.getId(), apply2.getId())) |
| 135 | + .build(); |
| 136 | + |
| 137 | + // when |
| 138 | + // then |
| 139 | + assertThatThrownBy( |
| 140 | + () -> settleVolunteerApplyFacadeService.settleVolunteerApplies(dto, centerId) |
| 141 | + ).isInstanceOf(BadRequestException.class) |
| 142 | + .hasMessage(RECRUIT_BOARD_ID_MISMATCH.getMessage()); |
| 143 | + } |
| 144 | + |
| 145 | + @DisplayName("정산시, 기관이 작성한 모집글이 아닌 경우 에러가 발생한다.") |
| 146 | + @Test |
| 147 | + void settleVolunteerAppliesWhenNoAuthCenter() { |
| 148 | + // given |
| 149 | + UUID wrongId = UUID.randomUUID(); |
| 150 | + |
| 151 | + Volunteer volunteer1 = volunteerRepository.save(createVolunteer()); |
| 152 | + Volunteer volunteer2 = volunteerRepository.save(createVolunteer()); |
| 153 | + |
| 154 | + RecruitBoard board = createCompletedRecruitBoard(UUID.randomUUID(), COUNSELING); |
| 155 | + recruitBoardRepository.save(board); |
| 156 | + |
| 157 | + VolunteerApply apply1 = createApply(volunteer1.getId(), board.getId()); |
| 158 | + VolunteerApply apply2 = createApply(volunteer2.getId(), board.getId()); |
| 159 | + volunteerApplyRepository.saveAll(List.of(apply1, apply2)); |
| 160 | + |
| 161 | + VolunteerApplySettleRequestDto dto = VolunteerApplySettleRequestDto.builder() |
| 162 | + .ids(List.of(apply1.getId(), apply2.getId())) |
| 163 | + .build(); |
| 164 | + |
| 165 | + // when |
| 166 | + // then |
| 167 | + assertThatThrownBy( |
| 168 | + () -> settleVolunteerApplyFacadeService.settleVolunteerApplies(dto, wrongId) |
| 169 | + ).isInstanceOf(BadRequestException.class) |
| 170 | + .hasMessage(UNAUTHORIZED_RECRUIT_BOARD.getMessage()); |
| 171 | + } |
| 172 | + |
| 173 | + private static Volunteer createVolunteer() { |
| 174 | + return Volunteer.createDefault(NAVER, "naver"); |
| 175 | + } |
| 176 | + |
| 177 | + private static VolunteerApply createApply(UUID volunteerId, Long recruitId) { |
| 178 | + return VolunteerApply.builder() |
| 179 | + .volunteerId(volunteerId) |
| 180 | + .recruitBoardId(recruitId) |
| 181 | + .status(APPROVED) |
| 182 | + .attended(false) |
| 183 | + .build(); |
| 184 | + } |
| 185 | +} |
0 commit comments