|
| 1 | +package com.example.log4u.domain.user.mypage.service; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 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.PageRequest; |
| 19 | +import org.springframework.data.domain.SliceImpl; |
| 20 | + |
| 21 | +import com.example.log4u.common.dto.PageResponse; |
| 22 | +import com.example.log4u.domain.diary.dto.DiaryResponseDto; |
| 23 | +import com.example.log4u.domain.diary.service.DiaryService; |
| 24 | +import com.example.log4u.domain.follow.repository.FollowQuerydsl; |
| 25 | +import com.example.log4u.domain.subscription.PaymentProvider; |
| 26 | +import com.example.log4u.domain.subscription.PaymentStatus; |
| 27 | +import com.example.log4u.domain.subscription.dto.SubscriptionResponseDto; |
| 28 | +import com.example.log4u.domain.subscription.entity.Subscription; |
| 29 | +import com.example.log4u.domain.subscription.repository.SubscriptionRepository; |
| 30 | +import com.example.log4u.domain.user.dto.UserThumbnailResponseDto; |
| 31 | +import com.example.log4u.fixture.DiaryFixture; |
| 32 | + |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +public class MyPageServiceTest { |
| 35 | + @InjectMocks |
| 36 | + private MyPageService myPageService; |
| 37 | + |
| 38 | + @Mock |
| 39 | + private DiaryService diaryService; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private FollowQuerydsl followQuerydsl; |
| 43 | + |
| 44 | + @Mock |
| 45 | + private SubscriptionRepository subscriptionRepository; |
| 46 | + |
| 47 | + private final Long userId = 1L; |
| 48 | + private final Long cursorId = 10L; |
| 49 | + |
| 50 | + private List<DiaryResponseDto> diaries; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() { |
| 54 | + diaries = DiaryFixture.createDiariesFixture() |
| 55 | + .stream() |
| 56 | + .map(diary -> DiaryResponseDto.of(diary, new ArrayList<>())) |
| 57 | + .toList(); |
| 58 | + } |
| 59 | + |
| 60 | + @DisplayName("성공 테스트 : 내 다이어리 조회") |
| 61 | + @Test |
| 62 | + void getMyDiariesByCursor_returnsCorrectData() { |
| 63 | + PageResponse<DiaryResponseDto> mockResponse = PageResponse.of( |
| 64 | + new SliceImpl<>(diaries), null |
| 65 | + ); |
| 66 | + |
| 67 | + when(diaryService.getDiariesByCursor(userId, userId, cursorId, 6)).thenReturn(mockResponse); |
| 68 | + |
| 69 | + PageResponse<DiaryResponseDto> result = myPageService.getMyDiariesByCursor(userId, cursorId); |
| 70 | + |
| 71 | + assertThat(result).isNotNull(); |
| 72 | + verify(diaryService).getDiariesByCursor(userId, userId, cursorId, 6); |
| 73 | + } |
| 74 | + |
| 75 | + @DisplayName("성공 테스트 : 좋아요한 다이어리 조회") |
| 76 | + @Test |
| 77 | + void getLikeDiariesByCursor_returnsCorrectData() { |
| 78 | + PageResponse<DiaryResponseDto> mockResponse = PageResponse.of( |
| 79 | + new SliceImpl<>(diaries), null |
| 80 | + ); |
| 81 | + |
| 82 | + when(diaryService.getLikeDiariesByCursor(userId, userId, cursorId, 6)).thenReturn(mockResponse); |
| 83 | + |
| 84 | + PageResponse<DiaryResponseDto> result = myPageService.getLikeDiariesByCursor(userId, cursorId); |
| 85 | + |
| 86 | + assertThat(result).isNotNull(); |
| 87 | + verify(diaryService).getLikeDiariesByCursor(userId, userId, cursorId, 6); |
| 88 | + } |
| 89 | + |
| 90 | + @DisplayName("성공 테스트 : 내 팔로워 조회") |
| 91 | + @Test |
| 92 | + void getMyFollowers_returnsCorrectData() { |
| 93 | + var slice = new SliceImpl<>(List.of(new UserThumbnailResponseDto(userId, "nick", "image"))); |
| 94 | + |
| 95 | + when(followQuerydsl.getFollowerSliceByUserId(eq(userId), eq(cursorId), any(PageRequest.class))) |
| 96 | + .thenReturn(slice); |
| 97 | + |
| 98 | + PageResponse<UserThumbnailResponseDto> result = myPageService.getMyFollowers(userId, cursorId); |
| 99 | + |
| 100 | + assertThat(result).isNotNull(); |
| 101 | + } |
| 102 | + |
| 103 | + @DisplayName("성공 테스트 : 내 팔로잉 조회") |
| 104 | + @Test |
| 105 | + void getMyFollowings_returnsCorrectData() { |
| 106 | + var slice = new SliceImpl<>(List.of(new UserThumbnailResponseDto(userId, "nick", "image"))); |
| 107 | + |
| 108 | + when(followQuerydsl.getFollowingSliceByUserId(eq(userId), eq(cursorId), any(PageRequest.class))) |
| 109 | + .thenReturn(slice); |
| 110 | + |
| 111 | + PageResponse<UserThumbnailResponseDto> result = myPageService.getMyFollowings(userId, cursorId); |
| 112 | + |
| 113 | + assertThat(result).isNotNull(); |
| 114 | + } |
| 115 | + |
| 116 | + @DisplayName("구독 정보 조회 - 구독이 있을 때") |
| 117 | + @Test |
| 118 | + void getMySubscription_whenExists_returnsActiveSubscription() { |
| 119 | + LocalDateTime createdAt = LocalDateTime.now().minusDays(1); |
| 120 | + Subscription subscription = Subscription.builder() |
| 121 | + .id(1L) |
| 122 | + .userId(userId) |
| 123 | + .createdAt(createdAt) |
| 124 | + .paymentProvider(PaymentProvider.KAKAO) |
| 125 | + .paymentStatus(PaymentStatus.SUCCESS) |
| 126 | + .build(); |
| 127 | + |
| 128 | + when(subscriptionRepository.findByUserIdAndCreatedAtBeforeAndPaymentStatusOrderByCreatedAtDesc( |
| 129 | + eq(userId), any(LocalDateTime.class), eq(PaymentStatus.SUCCESS))) |
| 130 | + .thenReturn(Optional.of(subscription)); |
| 131 | + |
| 132 | + SubscriptionResponseDto result = myPageService.getMySubscription(userId); |
| 133 | + |
| 134 | + assertThat(result.isSubscriptionActive()).isTrue(); |
| 135 | + assertThat(result.startDate()).isEqualTo(createdAt); |
| 136 | + assertThat(result.paymentProvider()).isEqualTo(PaymentProvider.KAKAO); |
| 137 | + } |
| 138 | + |
| 139 | + @DisplayName("구독 정보 조회 - 구독이 없을 때") |
| 140 | + @Test |
| 141 | + void getMySubscription_whenNotExists_returnsInactive() { |
| 142 | + when(subscriptionRepository.findByUserIdAndCreatedAtBeforeAndPaymentStatusOrderByCreatedAtDesc( |
| 143 | + anyLong(), any(), eq(PaymentStatus.SUCCESS))) |
| 144 | + .thenReturn(Optional.empty()); |
| 145 | + |
| 146 | + SubscriptionResponseDto result = myPageService.getMySubscription(userId); |
| 147 | + |
| 148 | + assertThat(result.isSubscriptionActive()).isFalse(); |
| 149 | + } |
| 150 | +} |
0 commit comments