|
| 1 | +package apptive.team5.booth.service; |
| 2 | + |
| 3 | +import apptive.team5.booth.dto.PublicKillingPartResponse; |
| 4 | +import apptive.team5.booth.dto.PublicUserResponse; |
| 5 | +import apptive.team5.diary.domain.DiaryEntity; |
| 6 | +import apptive.team5.diary.domain.DiaryScope; |
| 7 | +import apptive.team5.diary.service.DiaryLowService; |
| 8 | +import apptive.team5.global.exception.NotFoundEntityException; |
| 9 | +import apptive.team5.user.domain.UserEntity; |
| 10 | +import apptive.team5.user.service.UserLowService; |
| 11 | +import apptive.team5.util.TestUtil; |
| 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 | +import org.springframework.data.domain.Page; |
| 19 | +import org.springframework.data.domain.PageImpl; |
| 20 | +import org.springframework.data.domain.PageRequest; |
| 21 | +import org.springframework.test.util.ReflectionTestUtils; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 27 | +import static org.mockito.BDDMockito.given; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +public class BoothServiceTest { |
| 33 | + |
| 34 | + @InjectMocks |
| 35 | + private BoothService boothService; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private UserLowService userLowService; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private DiaryLowService diaryLowService; |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("태그로 유저 조회 - 성공") |
| 45 | + void findUserByTag_success() { |
| 46 | + // given |
| 47 | + UserEntity user = TestUtil.makeUserEntityWithId(); |
| 48 | + String tag = user.getTag(); |
| 49 | + |
| 50 | + given(userLowService.findByTag(tag)).willReturn(user); |
| 51 | + |
| 52 | + // when |
| 53 | + PublicUserResponse response = boothService.findUserByTag(tag); |
| 54 | + |
| 55 | + // then |
| 56 | + assertThat(response.userId()).isEqualTo(user.getId()); |
| 57 | + assertThat(response.username()).isEqualTo(user.getUsername()); |
| 58 | + assertThat(response.tag()).isEqualTo(tag); |
| 59 | + assertThat(response.profileImageUrl()).contains(user.getProfileImage()); |
| 60 | + |
| 61 | + verify(userLowService).findByTag(tag); |
| 62 | + verifyNoMoreInteractions(userLowService, diaryLowService); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("태그로 유저 조회 - 태그 없으면 NotFoundEntityException") |
| 67 | + void findUserByTag_notFound() { |
| 68 | + // given |
| 69 | + String tag = "MISSING_TAG"; |
| 70 | + given(userLowService.findByTag(tag)) |
| 71 | + .willThrow(new NotFoundEntityException("존재하지 않는 회원입니다.")); |
| 72 | + |
| 73 | + // when / then |
| 74 | + assertThatThrownBy(() -> boothService.findUserByTag(tag)) |
| 75 | + .isInstanceOf(NotFoundEntityException.class); |
| 76 | + |
| 77 | + verify(userLowService).findByTag(tag); |
| 78 | + verifyNoMoreInteractions(userLowService, diaryLowService); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("플레이리스트 조회 - PUBLIC/KILLING_PART 스코프 필터로 호출") |
| 83 | + void getUserPlaylist_filtersScopes() { |
| 84 | + // given |
| 85 | + UserEntity owner = TestUtil.makeUserEntityWithId(); |
| 86 | + Long userId = owner.getId(); |
| 87 | + |
| 88 | + DiaryEntity publicDiary = TestUtil.makeDiaryEntityWithScope(owner, DiaryScope.PUBLIC); |
| 89 | + ReflectionTestUtils.setField(publicDiary, "id", 10L); |
| 90 | + DiaryEntity killingPartDiary = TestUtil.makeDiaryEntityWithScope(owner, DiaryScope.KILLING_PART); |
| 91 | + ReflectionTestUtils.setField(killingPartDiary, "id", 11L); |
| 92 | + |
| 93 | + Page<DiaryEntity> diaryPage = new PageImpl<>(List.of(publicDiary, killingPartDiary)); |
| 94 | + PageRequest pageRequest = PageRequest.of(0, 5); |
| 95 | + List<DiaryScope> visibleScopes = List.of(DiaryScope.PUBLIC, DiaryScope.KILLING_PART); |
| 96 | + |
| 97 | + given(diaryLowService.findDiaryByUserAndScopeIn(userId, visibleScopes, pageRequest)) |
| 98 | + .willReturn(diaryPage); |
| 99 | + |
| 100 | + // when |
| 101 | + Page<PublicKillingPartResponse> result = boothService.getUserPlaylist(userId, pageRequest); |
| 102 | + |
| 103 | + // then |
| 104 | + assertThat(result.getContent()).hasSize(2); |
| 105 | + |
| 106 | + PublicKillingPartResponse publicDto = result.getContent().get(0); |
| 107 | + assertThat(publicDto.diaryId()).isEqualTo(10L); |
| 108 | + assertThat(publicDto.musicTitle()).isEqualTo("Test Music"); |
| 109 | + assertThat(publicDto.artist()).isEqualTo("Test Artist"); |
| 110 | + assertThat(publicDto.albumImageUrl()).isEqualTo("image.url"); |
| 111 | + assertThat(publicDto.videoUrl()).isEqualTo("video.url"); |
| 112 | + assertThat(publicDto.totalDuration()).isEqualTo("PT2M58S"); |
| 113 | + assertThat(publicDto.start()).isEqualTo("PT1M1S"); |
| 114 | + assertThat(publicDto.end()).isEqualTo("PT1M31S"); |
| 115 | + |
| 116 | + PublicKillingPartResponse killingPartDto = result.getContent().get(1); |
| 117 | + assertThat(killingPartDto.diaryId()).isEqualTo(11L); |
| 118 | + |
| 119 | + verify(diaryLowService).findDiaryByUserAndScopeIn(userId, visibleScopes, pageRequest); |
| 120 | + verifyNoMoreInteractions(userLowService, diaryLowService); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("플레이리스트 조회 - 다이어리 없으면 빈 페이지") |
| 125 | + void getUserPlaylist_emptyResult() { |
| 126 | + // given |
| 127 | + Long userId = 999L; |
| 128 | + PageRequest pageRequest = PageRequest.of(0, 5); |
| 129 | + List<DiaryScope> visibleScopes = List.of(DiaryScope.PUBLIC, DiaryScope.KILLING_PART); |
| 130 | + |
| 131 | + given(diaryLowService.findDiaryByUserAndScopeIn(userId, visibleScopes, pageRequest)) |
| 132 | + .willReturn(new PageImpl<>(List.of())); |
| 133 | + |
| 134 | + // when |
| 135 | + Page<PublicKillingPartResponse> result = boothService.getUserPlaylist(userId, pageRequest); |
| 136 | + |
| 137 | + // then |
| 138 | + assertThat(result.getContent()).isEmpty(); |
| 139 | + verify(diaryLowService).findDiaryByUserAndScopeIn(userId, visibleScopes, pageRequest); |
| 140 | + } |
| 141 | +} |
0 commit comments