|
| 1 | +package com.loopers.application.strategy; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.JsonNode; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.loopers.application.CatalogEventHandler; |
| 9 | +import com.loopers.domain.metrics.ProductMetrics; |
| 10 | +import com.loopers.infrastructure.metrics.ProductMetricsJpaRepository; |
| 11 | +import com.loopers.domain.event.EventType; |
| 12 | +import com.loopers.support.test.IntegrationTestSupport; |
| 13 | +import java.util.UUID; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Nested; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | + |
| 19 | +@DisplayName("ProductMetrics 집계 통합 테스트") |
| 20 | +class ProductMetricsIntegrationTest extends IntegrationTestSupport { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private CatalogEventHandler catalogEventHandler; |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ProductMetricsJpaRepository productMetricsJpaRepository; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ObjectMapper objectMapper; |
| 30 | + |
| 31 | + @Nested |
| 32 | + @DisplayName("sales_count 집계") |
| 33 | + class SalesCountAggregation { |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("PRODUCT_SOLD 이벤트 처리 시 sales_count가 quantity만큼 증가한다") |
| 37 | + void shouldIncreaseSalesCount_whenProductSoldEventReceived() throws Exception { |
| 38 | + Long productId = 100L; |
| 39 | + int quantity = 3; |
| 40 | + String eventId = UUID.randomUUID().toString(); |
| 41 | + JsonNode payload = objectMapper.readTree("{\"quantity\":" + quantity + ",\"orderId\":1}"); |
| 42 | + |
| 43 | + catalogEventHandler.handle( |
| 44 | + eventId, EventType.PRODUCT_SOLD.getCode(), String.valueOf(productId), System.currentTimeMillis(), payload); |
| 45 | + |
| 46 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 47 | + assertThat(metrics.getSalesCount()).isEqualTo(quantity); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("동일 상품에 여러 번 판매 이벤트 발생 시 sales_count가 누적된다") |
| 52 | + void shouldAccumulateSalesCount_whenMultipleProductSoldEvents() throws Exception { |
| 53 | + Long productId = 101L; |
| 54 | + long now = System.currentTimeMillis(); |
| 55 | + |
| 56 | + catalogEventHandler.handle( |
| 57 | + UUID.randomUUID().toString(), |
| 58 | + EventType.PRODUCT_SOLD.getCode(), |
| 59 | + String.valueOf(productId), |
| 60 | + now, |
| 61 | + objectMapper.readTree("{\"quantity\":2,\"orderId\":1}")); |
| 62 | + |
| 63 | + catalogEventHandler.handle( |
| 64 | + UUID.randomUUID().toString(), |
| 65 | + EventType.PRODUCT_SOLD.getCode(), |
| 66 | + String.valueOf(productId), |
| 67 | + now + 1, |
| 68 | + objectMapper.readTree("{\"quantity\":5,\"orderId\":2}")); |
| 69 | + |
| 70 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 71 | + assertThat(metrics.getSalesCount()).isEqualTo(7); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("quantity 필드가 없으면 예외가 발생한다") |
| 76 | + void shouldThrowException_whenQuantityMissing() throws Exception { |
| 77 | + Long productId = 102L; |
| 78 | + String eventId = UUID.randomUUID().toString(); |
| 79 | + JsonNode payloadWithoutQuantity = objectMapper.readTree("{\"orderId\":1}"); |
| 80 | + |
| 81 | + assertThatThrownBy( |
| 82 | + () -> |
| 83 | + catalogEventHandler.handle( |
| 84 | + eventId, |
| 85 | + EventType.PRODUCT_SOLD.getCode(), |
| 86 | + String.valueOf(productId), |
| 87 | + System.currentTimeMillis(), |
| 88 | + payloadWithoutQuantity)) |
| 89 | + .isInstanceOf(IllegalArgumentException.class) |
| 90 | + .hasMessageContaining("quantity"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Nested |
| 95 | + @DisplayName("like_count 집계") |
| 96 | + class LikeCountAggregation { |
| 97 | + |
| 98 | + @Test |
| 99 | + @DisplayName("PRODUCT_LIKED 이벤트 처리 시 like_count가 1 증가한다") |
| 100 | + void shouldIncreaseLikeCount_whenProductLikedEventReceived() throws Exception { |
| 101 | + Long productId = 200L; |
| 102 | + String eventId = UUID.randomUUID().toString(); |
| 103 | + JsonNode emptyPayload = objectMapper.readTree("{}"); |
| 104 | + |
| 105 | + catalogEventHandler.handle( |
| 106 | + eventId, |
| 107 | + EventType.PRODUCT_LIKED.getCode(), |
| 108 | + String.valueOf(productId), |
| 109 | + System.currentTimeMillis(), |
| 110 | + emptyPayload); |
| 111 | + |
| 112 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 113 | + assertThat(metrics.getLikeCount()).isEqualTo(1); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @DisplayName("PRODUCT_UNLIKED 이벤트 처리 시 like_count가 1 감소한다") |
| 118 | + void shouldDecreaseLikeCount_whenProductUnlikedEventReceived() throws Exception { |
| 119 | + Long productId = 201L; |
| 120 | + long now = System.currentTimeMillis(); |
| 121 | + JsonNode emptyPayload = objectMapper.readTree("{}"); |
| 122 | + |
| 123 | + // 먼저 좋아요 2회 |
| 124 | + catalogEventHandler.handle( |
| 125 | + UUID.randomUUID().toString(), |
| 126 | + EventType.PRODUCT_LIKED.getCode(), |
| 127 | + String.valueOf(productId), |
| 128 | + now, |
| 129 | + emptyPayload); |
| 130 | + catalogEventHandler.handle( |
| 131 | + UUID.randomUUID().toString(), |
| 132 | + EventType.PRODUCT_LIKED.getCode(), |
| 133 | + String.valueOf(productId), |
| 134 | + now + 1, |
| 135 | + emptyPayload); |
| 136 | + |
| 137 | + // 좋아요 취소 1회 |
| 138 | + catalogEventHandler.handle( |
| 139 | + UUID.randomUUID().toString(), |
| 140 | + EventType.PRODUCT_UNLIKED.getCode(), |
| 141 | + String.valueOf(productId), |
| 142 | + now + 2, |
| 143 | + emptyPayload); |
| 144 | + |
| 145 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 146 | + assertThat(metrics.getLikeCount()).isEqualTo(1); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @DisplayName("like_count는 0 미만으로 내려가지 않는다") |
| 151 | + void shouldNotGoBelowZero_whenUnlikedMoreThanLiked() throws Exception { |
| 152 | + Long productId = 202L; |
| 153 | + JsonNode emptyPayload = objectMapper.readTree("{}"); |
| 154 | + |
| 155 | + // 좋아요 없이 취소 시도 |
| 156 | + catalogEventHandler.handle( |
| 157 | + UUID.randomUUID().toString(), |
| 158 | + EventType.PRODUCT_UNLIKED.getCode(), |
| 159 | + String.valueOf(productId), |
| 160 | + System.currentTimeMillis(), |
| 161 | + emptyPayload); |
| 162 | + |
| 163 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 164 | + assertThat(metrics.getLikeCount()).isZero(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Nested |
| 169 | + @DisplayName("view_count 집계") |
| 170 | + class ViewCountAggregation { |
| 171 | + |
| 172 | + @Test |
| 173 | + @DisplayName("PRODUCT_VIEWED 이벤트 처리 시 view_count가 1 증가한다") |
| 174 | + void shouldIncreaseViewCount_whenProductViewedEventReceived() throws Exception { |
| 175 | + Long productId = 300L; |
| 176 | + String eventId = UUID.randomUUID().toString(); |
| 177 | + JsonNode emptyPayload = objectMapper.readTree("{}"); |
| 178 | + |
| 179 | + catalogEventHandler.handle( |
| 180 | + eventId, |
| 181 | + EventType.PRODUCT_VIEWED.getCode(), |
| 182 | + String.valueOf(productId), |
| 183 | + System.currentTimeMillis(), |
| 184 | + emptyPayload); |
| 185 | + |
| 186 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 187 | + assertThat(metrics.getViewCount()).isEqualTo(1); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + @DisplayName("동일 상품에 여러 번 조회 이벤트 발생 시 view_count가 누적된다") |
| 192 | + void shouldAccumulateViewCount_whenMultipleProductViewedEvents() throws Exception { |
| 193 | + Long productId = 301L; |
| 194 | + long now = System.currentTimeMillis(); |
| 195 | + JsonNode emptyPayload = objectMapper.readTree("{}"); |
| 196 | + |
| 197 | + catalogEventHandler.handle( |
| 198 | + UUID.randomUUID().toString(), |
| 199 | + EventType.PRODUCT_VIEWED.getCode(), |
| 200 | + String.valueOf(productId), |
| 201 | + now, |
| 202 | + emptyPayload); |
| 203 | + |
| 204 | + catalogEventHandler.handle( |
| 205 | + UUID.randomUUID().toString(), |
| 206 | + EventType.PRODUCT_VIEWED.getCode(), |
| 207 | + String.valueOf(productId), |
| 208 | + now + 1, |
| 209 | + emptyPayload); |
| 210 | + |
| 211 | + catalogEventHandler.handle( |
| 212 | + UUID.randomUUID().toString(), |
| 213 | + EventType.PRODUCT_VIEWED.getCode(), |
| 214 | + String.valueOf(productId), |
| 215 | + now + 2, |
| 216 | + emptyPayload); |
| 217 | + |
| 218 | + ProductMetrics metrics = productMetricsJpaRepository.findById(productId).orElseThrow(); |
| 219 | + assertThat(metrics.getViewCount()).isEqualTo(3); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments