|
| 1 | +package com.back.b2st.domain.performance.service; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | +import static org.mockito.BDDMockito.*; |
| 5 | + |
| 6 | +import java.time.LocalDate; |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.ArgumentCaptor; |
| 14 | +import org.mockito.InjectMocks; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | +import org.springframework.data.domain.PageRequest; |
| 18 | +import org.springframework.data.domain.Pageable; |
| 19 | + |
| 20 | +import com.back.b2st.domain.performance.dto.response.PerformanceCursorPageRes; |
| 21 | +import com.back.b2st.domain.performance.entity.Performance; |
| 22 | +import com.back.b2st.domain.performance.entity.PerformanceStatus; |
| 23 | +import com.back.b2st.domain.performance.mapper.PerformanceMapper; |
| 24 | +import com.back.b2st.domain.performance.repository.PerformanceRepository; |
| 25 | + |
| 26 | +@ExtendWith(MockitoExtension.class) |
| 27 | +class PerformanceServiceTest { |
| 28 | + |
| 29 | + @Mock |
| 30 | + private PerformanceRepository performanceRepository; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private PerformanceMapper performanceMapper; |
| 34 | + |
| 35 | + @InjectMocks |
| 36 | + private PerformanceService performanceService; |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("getActivePerformancesWithCursor(): 종료된 공연을 필터링한다") |
| 40 | + void getActivePerformancesWithCursor_filtersEndedPerformances() { |
| 41 | + // given |
| 42 | + Long cursor = 100L; |
| 43 | + int size = 10; |
| 44 | + LocalDate today = LocalDate.now(); |
| 45 | + LocalDateTime todayStart = today.atStartOfDay(); |
| 46 | + |
| 47 | + given(performanceRepository.findByStatusWithCursor( |
| 48 | + eq(PerformanceStatus.ACTIVE), |
| 49 | + any(LocalDateTime.class), |
| 50 | + eq(cursor), |
| 51 | + any(Pageable.class) |
| 52 | + )).willReturn(List.of()); |
| 53 | + |
| 54 | + // when |
| 55 | + performanceService.getActivePerformancesWithCursor(cursor, size); |
| 56 | + |
| 57 | + // then |
| 58 | + ArgumentCaptor<LocalDateTime> dateCaptor = ArgumentCaptor.forClass(LocalDateTime.class); |
| 59 | + ArgumentCaptor<Pageable> pageableCaptor = ArgumentCaptor.forClass(Pageable.class); |
| 60 | + |
| 61 | + verify(performanceRepository).findByStatusWithCursor( |
| 62 | + eq(PerformanceStatus.ACTIVE), |
| 63 | + dateCaptor.capture(), |
| 64 | + eq(cursor), |
| 65 | + pageableCaptor.capture() |
| 66 | + ); |
| 67 | + |
| 68 | + // todayStart가 오늘 00:00:00인지 확인 |
| 69 | + LocalDateTime capturedDate = dateCaptor.getValue(); |
| 70 | + assertThat(capturedDate).isEqualTo(todayStart); |
| 71 | + assertThat(capturedDate.toLocalDate()).isEqualTo(today); |
| 72 | + assertThat(capturedDate.getHour()).isEqualTo(0); |
| 73 | + assertThat(capturedDate.getMinute()).isEqualTo(0); |
| 74 | + assertThat(capturedDate.getSecond()).isEqualTo(0); |
| 75 | + |
| 76 | + // Pageable이 size+1로 설정되었는지 확인 |
| 77 | + Pageable capturedPageable = pageableCaptor.getValue(); |
| 78 | + assertThat(capturedPageable.getPageSize()).isEqualTo(size + 1); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("searchActivePerformancesWithCursor(): 종료된 공연을 필터링한다") |
| 83 | + void searchActivePerformancesWithCursor_filtersEndedPerformances() { |
| 84 | + // given |
| 85 | + Long cursor = 200L; |
| 86 | + String keyword = "콘서트"; |
| 87 | + int size = 20; |
| 88 | + LocalDate today = LocalDate.now(); |
| 89 | + LocalDateTime todayStart = today.atStartOfDay(); |
| 90 | + |
| 91 | + given(performanceRepository.searchActiveWithCursor( |
| 92 | + eq(PerformanceStatus.ACTIVE), |
| 93 | + any(LocalDateTime.class), |
| 94 | + eq(keyword), |
| 95 | + eq(cursor), |
| 96 | + any(Pageable.class) |
| 97 | + )).willReturn(List.of()); |
| 98 | + |
| 99 | + // when |
| 100 | + performanceService.searchActivePerformancesWithCursor(cursor, keyword, size); |
| 101 | + |
| 102 | + // then |
| 103 | + ArgumentCaptor<LocalDateTime> dateCaptor = ArgumentCaptor.forClass(LocalDateTime.class); |
| 104 | + ArgumentCaptor<Pageable> pageableCaptor = ArgumentCaptor.forClass(Pageable.class); |
| 105 | + |
| 106 | + verify(performanceRepository).searchActiveWithCursor( |
| 107 | + eq(PerformanceStatus.ACTIVE), |
| 108 | + dateCaptor.capture(), |
| 109 | + eq(keyword), |
| 110 | + eq(cursor), |
| 111 | + pageableCaptor.capture() |
| 112 | + ); |
| 113 | + |
| 114 | + // todayStart가 오늘 00:00:00인지 확인 |
| 115 | + LocalDateTime capturedDate = dateCaptor.getValue(); |
| 116 | + assertThat(capturedDate).isEqualTo(todayStart); |
| 117 | + assertThat(capturedDate.toLocalDate()).isEqualTo(today); |
| 118 | + assertThat(capturedDate.getHour()).isEqualTo(0); |
| 119 | + assertThat(capturedDate.getMinute()).isEqualTo(0); |
| 120 | + assertThat(capturedDate.getSecond()).isEqualTo(0); |
| 121 | + |
| 122 | + // Pageable이 size+1로 설정되었는지 확인 |
| 123 | + Pageable capturedPageable = pageableCaptor.getValue(); |
| 124 | + assertThat(capturedPageable.getPageSize()).isEqualTo(size + 1); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @DisplayName("searchActivePerformancesWithCursor(): 키워드가 null이면 일반 목록 조회로 위임") |
| 129 | + void searchActivePerformancesWithCursor_nullKeyword_delegatesToGetActivePerformances() { |
| 130 | + // given |
| 131 | + Long cursor = 300L; |
| 132 | + int size = 15; |
| 133 | + |
| 134 | + given(performanceRepository.findByStatusWithCursor( |
| 135 | + eq(PerformanceStatus.ACTIVE), |
| 136 | + any(LocalDateTime.class), |
| 137 | + eq(cursor), |
| 138 | + any(Pageable.class) |
| 139 | + )).willReturn(List.of()); |
| 140 | + |
| 141 | + // when |
| 142 | + performanceService.searchActivePerformancesWithCursor(cursor, null, size); |
| 143 | + |
| 144 | + // then |
| 145 | + verify(performanceRepository).findByStatusWithCursor( |
| 146 | + eq(PerformanceStatus.ACTIVE), |
| 147 | + any(LocalDateTime.class), |
| 148 | + eq(cursor), |
| 149 | + any(Pageable.class) |
| 150 | + ); |
| 151 | + verify(performanceRepository, never()).searchActiveWithCursor( |
| 152 | + any(), |
| 153 | + any(), |
| 154 | + any(), |
| 155 | + any(), |
| 156 | + any() |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + @DisplayName("searchActivePerformancesWithCursor(): 키워드가 빈 문자열이면 일반 목록 조회로 위임") |
| 162 | + void searchActivePerformancesWithCursor_emptyKeyword_delegatesToGetActivePerformances() { |
| 163 | + // given |
| 164 | + Long cursor = 400L; |
| 165 | + int size = 25; |
| 166 | + |
| 167 | + given(performanceRepository.findByStatusWithCursor( |
| 168 | + eq(PerformanceStatus.ACTIVE), |
| 169 | + any(LocalDateTime.class), |
| 170 | + eq(cursor), |
| 171 | + any(Pageable.class) |
| 172 | + )).willReturn(List.of()); |
| 173 | + |
| 174 | + // when |
| 175 | + performanceService.searchActivePerformancesWithCursor(cursor, " ", size); |
| 176 | + |
| 177 | + // then |
| 178 | + verify(performanceRepository).findByStatusWithCursor( |
| 179 | + eq(PerformanceStatus.ACTIVE), |
| 180 | + any(LocalDateTime.class), |
| 181 | + eq(cursor), |
| 182 | + any(Pageable.class) |
| 183 | + ); |
| 184 | + verify(performanceRepository, never()).searchActiveWithCursor( |
| 185 | + any(), |
| 186 | + any(), |
| 187 | + any(), |
| 188 | + any(), |
| 189 | + any() |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments