|
4 | 4 | import static org.mockito.BDDMockito.*; |
5 | 5 |
|
6 | 6 | import java.util.List; |
7 | | -import java.util.Optional; |
8 | 7 |
|
9 | 8 | import org.junit.jupiter.api.DisplayName; |
10 | 9 | import org.junit.jupiter.api.Nested; |
|
13 | 12 | import org.mockito.InjectMocks; |
14 | 13 | import org.mockito.Mock; |
15 | 14 | import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | +import org.springframework.data.domain.Page; |
| 16 | +import org.springframework.data.domain.PageImpl; |
| 17 | +import org.springframework.data.domain.PageRequest; |
| 18 | +import org.springframework.data.domain.Pageable; |
16 | 19 |
|
17 | 20 | import com.back.api.event.dto.request.EventCreateRequest; |
18 | 21 | import com.back.api.event.dto.request.EventUpdateRequest; |
19 | 22 | import com.back.api.event.dto.response.AdminEventDashboardResponse; |
20 | 23 | import com.back.api.event.dto.response.EventResponse; |
21 | 24 | import com.back.domain.event.entity.Event; |
22 | | -import com.back.domain.event.entity.EventCategory; |
23 | | -import com.back.domain.event.entity.EventStatus; |
24 | 25 | import com.back.domain.event.repository.EventRepository; |
25 | 26 | import com.back.domain.preregister.entity.PreRegisterStatus; |
26 | 27 | import com.back.domain.preregister.repository.PreRegisterRepository; |
27 | 28 | import com.back.domain.seat.entity.SeatStatus; |
28 | 29 | import com.back.domain.seat.repository.SeatRepository; |
29 | | -import com.back.global.error.exception.ErrorException; |
30 | 30 | import com.back.support.factory.EventFactory; |
31 | 31 |
|
32 | 32 | @ExtendWith(MockitoExtension.class) |
@@ -123,52 +123,69 @@ void getAllEventsDashboard_Success() { |
123 | 123 | Event event2 = EventFactory.fakeEvent("이벤트2"); |
124 | 124 | List<Event> events = List.of(event1, event2); |
125 | 125 |
|
126 | | - given(eventRepository.findAll()).willReturn(events); |
| 126 | + Pageable pageable = PageRequest.of(0, 20); |
| 127 | + Page<Event> eventPage = new PageImpl<>(events, pageable, events.size()); |
| 128 | + |
| 129 | + |
| 130 | + given(eventRepository.findAll(any(Pageable.class))).willReturn(eventPage); // <- 변경! |
127 | 131 | given(preRegisterRepository.countByEvent_IdAndPreRegisterStatus( |
128 | 132 | any(), eq(PreRegisterStatus.REGISTERED))).willReturn(10L); |
129 | 133 | given(seatRepository.countByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(5L); |
130 | 134 | given(seatRepository.sumPriceByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(50000L); |
131 | 135 |
|
132 | 136 | // when |
133 | | - List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(); |
| 137 | + Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20); |
134 | 138 |
|
135 | 139 | // then |
136 | | - assertThat(results).hasSize(2); |
137 | | - assertThat(results.get(0).preRegisterCount()).isEqualTo(10L); |
138 | | - assertThat(results.get(0).totalSoldSeats()).isEqualTo(5L); |
139 | | - assertThat(results.get(0).totalSalesAmount()).isEqualTo(50000L); |
| 140 | + assertThat(results.getContent()).hasSize(2); |
| 141 | + assertThat(results.getTotalElements()).isEqualTo(2); |
| 142 | + assertThat(results.getTotalPages()).isEqualTo(1); |
| 143 | + assertThat(results.getSize()).isEqualTo(20); |
| 144 | + assertThat(results.getNumber()).isEqualTo(0); |
| 145 | + |
| 146 | + assertThat(results.getContent().get(0).preRegisterCount()).isEqualTo(10L); |
| 147 | + assertThat(results.getContent().get(0).totalSoldSeats()).isEqualTo(5L); |
| 148 | + assertThat(results.getContent().get(0).totalSalesAmount()).isEqualTo(50000L); |
140 | 149 | } |
141 | 150 |
|
142 | 151 | @Test |
143 | 152 | @DisplayName("이벤트가 없으면 빈 리스트를 반환한다") |
144 | 153 | void getAllEventsDashboard_EmptyList() { |
145 | 154 | // given |
146 | | - given(eventRepository.findAll()).willReturn(List.of()); |
| 155 | + Pageable pageable = PageRequest.of(0, 20); |
| 156 | + Page<Event> emptyPage = new PageImpl<>(List.of(), pageable, 0); |
| 157 | + |
| 158 | + given(eventRepository.findAll(any(Pageable.class))).willReturn(emptyPage); |
147 | 159 |
|
148 | 160 | // when |
149 | | - List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(); |
| 161 | + Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20); |
150 | 162 |
|
151 | 163 | // then |
152 | | - assertThat(results).isEmpty(); |
| 164 | + assertThat(results.getContent()).isEmpty(); |
| 165 | + assertThat(results.getTotalElements()).isEqualTo(0); |
| 166 | + assertThat(results.getTotalPages()).isEqualTo(0); |
153 | 167 | } |
154 | 168 |
|
155 | 169 | @Test |
156 | 170 | @DisplayName("총 판매 금액이 null이면 0을 반환한다") |
157 | 171 | void getAllEventsDashboard_NullSalesAmount() { |
158 | 172 | // given |
159 | 173 | Event event = EventFactory.fakeEvent("이벤트"); |
160 | | - given(eventRepository.findAll()).willReturn(List.of(event)); |
| 174 | + Pageable pageable = PageRequest.of(0, 20); |
| 175 | + Page<Event> eventPage = new PageImpl<>(List.of(event), pageable, 1); |
| 176 | + |
| 177 | + given(eventRepository.findAll(any(Pageable.class))).willReturn(eventPage); |
161 | 178 | given(preRegisterRepository.countByEvent_IdAndPreRegisterStatus( |
162 | 179 | any(), eq(PreRegisterStatus.REGISTERED))).willReturn(0L); |
163 | 180 | given(seatRepository.countByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(0L); |
164 | 181 | given(seatRepository.sumPriceByEventIdAndSeatStatus(any(), eq(SeatStatus.SOLD))).willReturn(null); |
165 | 182 |
|
166 | 183 | // when |
167 | | - List<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(); |
| 184 | + Page<AdminEventDashboardResponse> results = adminEventService.getAllEventsDashboard(0, 20); |
168 | 185 |
|
169 | 186 | // then |
170 | | - assertThat(results).hasSize(1); |
171 | | - assertThat(results.get(0).totalSalesAmount()).isEqualTo(0L); |
| 187 | + assertThat(results.getContent()).hasSize(1); |
| 188 | + assertThat(results.getContent().get(0).totalSalesAmount()).isEqualTo(0L); |
172 | 189 | } |
173 | 190 | } |
174 | 191 | } |
0 commit comments