|
| 1 | +package dmu.dasom.api.domain.activity; |
| 2 | + |
| 3 | +import dmu.dasom.api.domain.activity.dto.ActivityRequestDto; |
| 4 | +import dmu.dasom.api.domain.activity.dto.ActivityResponseDto; |
| 5 | +import dmu.dasom.api.domain.activity.entity.Activity; |
| 6 | +import dmu.dasom.api.domain.activity.entity.Section; |
| 7 | +import dmu.dasom.api.domain.activity.repository.ActivityRepository; |
| 8 | +import dmu.dasom.api.domain.activity.repository.SectionRepository; |
| 9 | +import dmu.dasom.api.domain.activity.service.ActivityServiceImpl; |
| 10 | +import dmu.dasom.api.domain.common.exception.CustomException; |
| 11 | +import dmu.dasom.api.domain.common.exception.ErrorCode; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.InjectMocks; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | + |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.BDDMockito.given; |
| 27 | +import static org.mockito.Mockito.never; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class ActivityServiceTest { |
| 32 | + |
| 33 | + @InjectMocks |
| 34 | + private ActivityServiceImpl activityService; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private ActivityRepository activityRepository; |
| 38 | + |
| 39 | + @Mock |
| 40 | + private SectionRepository sectionRepository; |
| 41 | + |
| 42 | + @Test |
| 43 | + @DisplayName("활동 전체 조회 성공") |
| 44 | + void getActivities() { |
| 45 | + // given |
| 46 | + Section section1 = createSection(1L, "교내 경진대회"); |
| 47 | + Section section2 = createSection(2L, "외부 경진대회"); |
| 48 | + Activity activity1 = createActivity(10L, "캡스톤 디자인", section1, LocalDate.of(2024, 5, 10)); |
| 49 | + Activity activity2 = createActivity(11L, "구글 해커톤", section2, LocalDate.of(2024, 8, 20)); |
| 50 | + |
| 51 | + given(activityRepository.findAll()).willReturn(List.of(activity1, activity2)); |
| 52 | + |
| 53 | + // when |
| 54 | + List<ActivityResponseDto> response = activityService.getActivities(); |
| 55 | + |
| 56 | + // then |
| 57 | + assertThat(response).hasSize(1); |
| 58 | + assertThat(response.get(0).getYear()).isEqualTo(2024); |
| 59 | + assertThat(response.get(0).getSections()).hasSize(2); |
| 60 | + verify(activityRepository).findAll(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("활동 생성 성공") |
| 65 | + void createActivity() { |
| 66 | + // given |
| 67 | + Section section = createSection(1L, "교내 경진대회"); |
| 68 | + ActivityRequestDto request = createRequestDto(section); // Section 객체로 DTO 생성 |
| 69 | + |
| 70 | + // findByName 대신 findBySection 사용 가정 (혹은 그 반대) |
| 71 | + given(sectionRepository.findByName("교내 경진대회")).willReturn(Optional.of(section)); |
| 72 | + |
| 73 | + // when |
| 74 | + activityService.createActivity(request); |
| 75 | + |
| 76 | + // then |
| 77 | + verify(activityRepository).save(any(Activity.class)); |
| 78 | + verify(sectionRepository).findByName("교내 경진대회"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("활동 수정 성공") |
| 83 | + void updateActivity() { |
| 84 | + // given |
| 85 | + long activityId = 1L; |
| 86 | + Section oldSection = createSection(1L, "교내 경진대회"); |
| 87 | + Section updatedSection = createSection(2L, "외부 경진대회"); |
| 88 | + ActivityRequestDto request = createRequestDto(updatedSection); // Section 객체로 DTO 생성 |
| 89 | + Activity existingActivity = createActivity(activityId, "기존 제목", oldSection, LocalDate.now()); |
| 90 | + |
| 91 | + given(activityRepository.findById(activityId)).willReturn(Optional.of(existingActivity)); |
| 92 | + given(sectionRepository.findByName("외부 경진대회")).willReturn(Optional.of(updatedSection)); |
| 93 | + |
| 94 | + // when |
| 95 | + activityService.updateActivity(activityId, request); |
| 96 | + |
| 97 | + // then |
| 98 | + assertThat(existingActivity.getSection()).isEqualTo(updatedSection); |
| 99 | + assertThat(existingActivity.getTitle()).isEqualTo(request.getTitle()); |
| 100 | + verify(activityRepository).findById(activityId); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("활동 수정 실패 - 존재하지 않는 ID") |
| 105 | + void updateActivity_whenNotFound_thenThrowException() { |
| 106 | + // given |
| 107 | + long nonExistentId = 99L; |
| 108 | + Section dummySection = createSection(99L, "아무 섹션"); |
| 109 | + ActivityRequestDto request = createRequestDto(dummySection); // Section 객체로 DTO 생성 |
| 110 | + given(activityRepository.findById(nonExistentId)).willReturn(Optional.empty()); |
| 111 | + |
| 112 | + // when & then |
| 113 | + CustomException exception = assertThrows(CustomException.class, |
| 114 | + () -> activityService.updateActivity(nonExistentId, request)); |
| 115 | + assertThat(exception.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND); |
| 116 | + verify(activityRepository).findById(nonExistentId); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @DisplayName("활동 삭제 성공") |
| 121 | + void deleteActivity() { |
| 122 | + // given |
| 123 | + long activityId = 1L; |
| 124 | + given(activityRepository.existsById(activityId)).willReturn(true); |
| 125 | + |
| 126 | + // when |
| 127 | + activityService.deleteActivity(activityId); |
| 128 | + |
| 129 | + // then |
| 130 | + verify(activityRepository).existsById(activityId); |
| 131 | + verify(activityRepository).deleteById(activityId); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @DisplayName("활동 삭제 실패 - 존재하지 않는 ID") |
| 136 | + void deleteActivity_whenNotFound_thenThrowException() { |
| 137 | + // given |
| 138 | + long nonExistentId = 99L; |
| 139 | + given(activityRepository.existsById(nonExistentId)).willReturn(false); |
| 140 | + |
| 141 | + // when & then |
| 142 | + CustomException exception = assertThrows(CustomException.class, |
| 143 | + () -> activityService.deleteActivity(nonExistentId)); |
| 144 | + assertThat(exception.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND); |
| 145 | + verify(activityRepository).existsById(nonExistentId); |
| 146 | + verify(activityRepository, never()).deleteById(nonExistentId); |
| 147 | + } |
| 148 | + |
| 149 | + // --- Helper Methods --- |
| 150 | + |
| 151 | + // [수정] 파라미터 타입을 String에서 Section으로 변경 |
| 152 | + private ActivityRequestDto createRequestDto(Section section) { |
| 153 | + return ActivityRequestDto.builder() |
| 154 | + .activityDate(LocalDate.of(2024, 5, 10)) |
| 155 | + .section(section.getName()) // Section 객체를 직접 전달 |
| 156 | + .title("새로운 활동 제목") |
| 157 | + .award("최우수상") |
| 158 | + .build(); |
| 159 | + } |
| 160 | + |
| 161 | + private Section createSection(Long id, String sectionName) { |
| 162 | + return Section.builder() |
| 163 | + .id(id) |
| 164 | + .name(sectionName) |
| 165 | + .build(); |
| 166 | + } |
| 167 | + |
| 168 | + private Activity createActivity(Long id, String title, Section section, LocalDate date) { |
| 169 | + return Activity.builder() |
| 170 | + .id(id) |
| 171 | + .activityDate(date) |
| 172 | + .section(section) |
| 173 | + .title(title) |
| 174 | + .award("테스트 상") |
| 175 | + .build(); |
| 176 | + } |
| 177 | +} |
0 commit comments