|
| 1 | +package org.fastcampus.applicationadmin.dashboard.service |
| 2 | + |
| 3 | +import io.mockk.every |
| 4 | +import io.mockk.mockk |
| 5 | +import io.mockk.verify |
| 6 | +import org.fastcampus.applicationadmin.dashboard.dto.response.OrdersSummaryResponse |
| 7 | +import org.fastcampus.applicationadmin.dashboard.dto.response.SalesSummaryResponse |
| 8 | +import org.fastcampus.applicationadmin.dashboard.dto.response.Type |
| 9 | +import org.fastcampus.applicationadmin.fixture.createOrderFixture |
| 10 | +import org.fastcampus.order.entity.Order |
| 11 | +import org.fastcampus.order.repository.OrderRepository |
| 12 | +import org.fastcampus.store.repository.StoreRepository |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | +import strikt.api.expectThat |
| 15 | +import strikt.assertions.isEqualTo |
| 16 | +import java.time.LocalDate |
| 17 | +import java.time.LocalDateTime |
| 18 | + |
| 19 | +class DashboardServiceTest { |
| 20 | + private val orderRepository: OrderRepository = mockk() |
| 21 | + private val storeRepository: StoreRepository = mockk() |
| 22 | + |
| 23 | + private val dashboardService = DashboardService( |
| 24 | + orderRepository = orderRepository, |
| 25 | + storeRepository = storeRepository, |
| 26 | + ) |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `getDashboard returns correct Sales dashboard response`() { |
| 30 | + // given |
| 31 | + val ownerId = 1L |
| 32 | + val storeId = "store_123" |
| 33 | + val startDate = LocalDate.of(2025, 2, 1) |
| 34 | + val endDate = LocalDate.of(2025, 2, 15) |
| 35 | + val type = Type.SALES.name |
| 36 | + |
| 37 | + every { storeRepository.findByOwnerId(ownerId.toString()) } returns storeId |
| 38 | + |
| 39 | + // 테스트용 Order 데이터 (3건 중 COMPLETED 2건만 SALES 계산 대상) |
| 40 | + val order1 = createOrderFixture( |
| 41 | + id = "order_1", |
| 42 | + storeId = storeId, |
| 43 | + status = Order.Status.COMPLETED, |
| 44 | + orderPrice = 15000L, |
| 45 | + paymentPrice = 15000L, |
| 46 | + orderTime = LocalDateTime.of(2025, 2, 2, 10, 0, 0), |
| 47 | + ) |
| 48 | + val order2 = createOrderFixture( |
| 49 | + id = "order_2", |
| 50 | + storeId = storeId, |
| 51 | + status = Order.Status.WAIT, // WAIT는 제외됨 |
| 52 | + orderPrice = 10000L, |
| 53 | + paymentPrice = 10000L, |
| 54 | + orderTime = LocalDateTime.of(2025, 2, 2, 11, 0, 0), |
| 55 | + ) |
| 56 | + val order3 = createOrderFixture( |
| 57 | + id = "order_3", |
| 58 | + storeId = storeId, |
| 59 | + status = Order.Status.COMPLETED, |
| 60 | + orderPrice = 20000L, |
| 61 | + paymentPrice = 20000L, |
| 62 | + orderTime = LocalDateTime.of(2025, 2, 3, 9, 30, 0), |
| 63 | + ) |
| 64 | + |
| 65 | + every { |
| 66 | + orderRepository.findAllByStoreIdAndOrderTimeBetweenAndStatusNot( |
| 67 | + storeId, |
| 68 | + startDate.atStartOfDay(), |
| 69 | + endDate.atTime(23, 59, 59), |
| 70 | + Order.Status.WAIT, |
| 71 | + ) |
| 72 | + } returns listOf(order1, order2, order3) |
| 73 | + |
| 74 | + // when |
| 75 | + val dashboardResponse = dashboardService.getDashboard(ownerId, startDate, endDate, type) |
| 76 | + ?: error("dashboardResponse is null") |
| 77 | + |
| 78 | + // then |
| 79 | + // SALES 타입은 COMPLETED 주문만 사용함 (order1, order3) |
| 80 | + // totalPrice = 15000 + 20000 = 35000 |
| 81 | + // avgPrice = 35000 / 2 = 17500 |
| 82 | + // 단순 평균 로직이므로, 일별, 시간별 평균도 17500로 계산됨 (예시) |
| 83 | + expectThat(dashboardResponse) { |
| 84 | + get { type } isEqualTo Type.SALES.name |
| 85 | + get { summary } isEqualTo SalesSummaryResponse( |
| 86 | + totalPrice = 35000L, |
| 87 | + avgPrice = 17500L, |
| 88 | + avgPricePerDay = 17500L, |
| 89 | + avgPricePerTime = 17500L, |
| 90 | + ) |
| 91 | + // data에 COMPLETED 주문만 변환되어 들어있으므로 2건이어야 함 |
| 92 | + get { data.size } isEqualTo 2 |
| 93 | + } |
| 94 | + |
| 95 | + verify(exactly = 1) { |
| 96 | + storeRepository.findByOwnerId(ownerId.toString()) |
| 97 | + } |
| 98 | + verify(exactly = 1) { |
| 99 | + orderRepository.findAllByStoreIdAndOrderTimeBetweenAndStatusNot( |
| 100 | + storeId, |
| 101 | + startDate.atStartOfDay(), |
| 102 | + endDate.atTime(23, 59, 59), |
| 103 | + Order.Status.WAIT, |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `getDashboard returns correct Orders dashboard response`() { |
| 110 | + // given |
| 111 | + val ownerId = 1L |
| 112 | + val storeId = "store_123" |
| 113 | + val startDate = LocalDate.of(2025, 2, 1) |
| 114 | + val endDate = LocalDate.of(2025, 2, 15) |
| 115 | + val type = Type.ORDERS.name |
| 116 | + |
| 117 | + every { storeRepository.findByOwnerId(ownerId.toString()) } returns storeId |
| 118 | + |
| 119 | + // 테스트용 Order 데이터 - WAIT 상태 제외, 모두 포함 |
| 120 | + val order1 = createOrderFixture( |
| 121 | + id = "order_1", |
| 122 | + storeId = storeId, |
| 123 | + status = Order.Status.RECEIVE, |
| 124 | + orderPrice = 15000L, |
| 125 | + paymentPrice = 15000L, |
| 126 | + orderTime = LocalDateTime.of(2025, 2, 2, 10, 0, 0), |
| 127 | + ) |
| 128 | + val order2 = createOrderFixture( |
| 129 | + id = "order_2", |
| 130 | + storeId = storeId, |
| 131 | + status = Order.Status.CANCEL, // 취소 주문 |
| 132 | + orderPrice = 10000L, |
| 133 | + paymentPrice = 10000L, |
| 134 | + orderTime = LocalDateTime.of(2025, 2, 2, 11, 0, 0), |
| 135 | + ) |
| 136 | + val order3 = createOrderFixture( |
| 137 | + id = "order_3", |
| 138 | + storeId = storeId, |
| 139 | + status = Order.Status.COMPLETED, |
| 140 | + orderPrice = 20000L, |
| 141 | + paymentPrice = 20000L, |
| 142 | + orderTime = LocalDateTime.of(2025, 2, 3, 9, 30, 0), |
| 143 | + ) |
| 144 | + |
| 145 | + every { |
| 146 | + orderRepository.findAllByStoreIdAndOrderTimeBetweenAndStatusNot( |
| 147 | + storeId, |
| 148 | + startDate.atStartOfDay(), |
| 149 | + endDate.atTime(23, 59, 59), |
| 150 | + Order.Status.WAIT, |
| 151 | + ) |
| 152 | + } returns listOf(order1, order2, order3) |
| 153 | + |
| 154 | + // when |
| 155 | + val dashboardResponse = dashboardService.getDashboard(ownerId, startDate, endDate, type) |
| 156 | + ?: error("dashboardResponse is null") |
| 157 | + |
| 158 | + // then |
| 159 | + // ORDERS 타입은 모든 주문(WAIT 제외)을 사용함: 3건 |
| 160 | + // cancelOrders는 order2가 취소 상태이므로 1건 |
| 161 | + // avgOrderPerDay = totalOrder / days, 여기서는 테스트 환경에 따라 3건/15일 = 0 (정수 나눗셈) |
| 162 | + expectThat(dashboardResponse) { |
| 163 | + get { type } isEqualTo Type.ORDERS.name |
| 164 | + get { summary } isEqualTo OrdersSummaryResponse( |
| 165 | + totalOrder = 3L, |
| 166 | + avgOrderPerDay = 0L, |
| 167 | + cancelOrders = 1L, |
| 168 | + cancelRate = (1.0 / 3.0) * 100.0, |
| 169 | + ) |
| 170 | + get { data.size } isEqualTo 3 |
| 171 | + } |
| 172 | + |
| 173 | + verify(exactly = 1) { |
| 174 | + storeRepository.findByOwnerId(ownerId.toString()) |
| 175 | + } |
| 176 | + verify(exactly = 1) { |
| 177 | + orderRepository.findAllByStoreIdAndOrderTimeBetweenAndStatusNot( |
| 178 | + storeId, |
| 179 | + startDate.atStartOfDay(), |
| 180 | + endDate.atTime(23, 59, 59), |
| 181 | + Order.Status.WAIT, |
| 182 | + ) |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments