|
| 1 | +package com.loopers.interfaces.consumer; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.never; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.loopers.application.inbox.EventInboxService; |
| 11 | +import com.loopers.application.metrics.ProductMetricsService; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 15 | +import org.junit.jupiter.api.DisplayName; |
| 16 | +import org.junit.jupiter.api.Nested; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | + |
| 22 | +/** |
| 23 | + * CatalogEventConsumer 단위 테스트 |
| 24 | + * |
| 25 | + * 목적: |
| 26 | + * 1. Consumer가 각 이벤트 타입을 올바르게 처리하는지 검증 |
| 27 | + * 2. Inbox 패턴으로 중복 이벤트를 방지하는지 검증 |
| 28 | + * 3. 비즈니스 로직(ProductMetricsService)이 올바르게 호출되는지 검증 |
| 29 | + */ |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +@DisplayName("Catalog 이벤트 Consumer") |
| 32 | +class CatalogEventConsumerTest { |
| 33 | + |
| 34 | + @Mock |
| 35 | + private EventInboxService eventInboxService; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private ProductMetricsService productMetricsService; |
| 39 | + |
| 40 | + private CatalogEventConsumer catalogEventConsumer; |
| 41 | + |
| 42 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 43 | + |
| 44 | + @org.junit.jupiter.api.BeforeEach |
| 45 | + void setUp() { |
| 46 | + // ObjectMapper는 실제 인스턴스 사용, DeadLetterQueueService는 null (단위 테스트에서 불필요) |
| 47 | + catalogEventConsumer = new CatalogEventConsumer( |
| 48 | + eventInboxService, |
| 49 | + productMetricsService, |
| 50 | + null, // DeadLetterQueueService - 단위 테스트에서 사용하지 않음 |
| 51 | + objectMapper |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Nested |
| 56 | + @DisplayName("LikeCreatedEvent 처리") |
| 57 | + class LikeCreatedEventTest { |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("좋아요 생성 이벤트를 받으면 ProductMetrics의 좋아요 수를 증가시킨다") |
| 61 | + void incrementLikeCount() throws Exception { |
| 62 | + // Given: 좋아요 생성 이벤트 |
| 63 | + Long productId = 1L; |
| 64 | + String eventId = "event-001"; |
| 65 | + Map<String, Object> event = createEvent(eventId, "LikeCreatedEvent", "LIKE", productId.toString()); |
| 66 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 67 | + |
| 68 | + when(eventInboxService.isDuplicate(eventId)).thenReturn(false); |
| 69 | + |
| 70 | + // When: Consumer가 이벤트 처리 |
| 71 | + boolean result = catalogEventConsumer.processEvent(record); |
| 72 | + |
| 73 | + // Then: Inbox에 저장하고, 좋아요 수 증가 |
| 74 | + assertThat(result).isTrue(); |
| 75 | + verify(eventInboxService).save(eventId, "LIKE", productId.toString(), "LikeCreatedEvent"); |
| 76 | + verify(productMetricsService).incrementLikeCount(productId); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("중복된 좋아요 이벤트는 무시한다 (멱등성 보장)") |
| 81 | + void ignoreDuplicateEvent() throws Exception { |
| 82 | + // Given: 이미 처리된 이벤트 (Inbox에 존재) |
| 83 | + String eventId = "event-001"; |
| 84 | + Map<String, Object> event = createEvent(eventId, "LikeCreatedEvent", "LIKE", "1"); |
| 85 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 86 | + |
| 87 | + when(eventInboxService.isDuplicate(eventId)).thenReturn(true); |
| 88 | + |
| 89 | + // When: 같은 이벤트를 다시 수신 |
| 90 | + boolean result = catalogEventConsumer.processEvent(record); |
| 91 | + |
| 92 | + // Then: 처리하지 않고 스킵 (false 반환) |
| 93 | + assertThat(result).isFalse(); |
| 94 | + verify(eventInboxService, never()).save(any(), any(), any(), any()); |
| 95 | + verify(productMetricsService, never()).incrementLikeCount(any()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Nested |
| 100 | + @DisplayName("LikeDeletedEvent 처리") |
| 101 | + class LikeDeletedEventTest { |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("좋아요 삭제 이벤트를 받으면 ProductMetrics의 좋아요 수를 감소시킨다") |
| 105 | + void decrementLikeCount() throws Exception { |
| 106 | + // Given: 좋아요 삭제 이벤트 |
| 107 | + Long productId = 2L; |
| 108 | + String eventId = "event-002"; |
| 109 | + Map<String, Object> event = createEvent(eventId, "LikeDeletedEvent", "LIKE", productId.toString()); |
| 110 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 111 | + |
| 112 | + when(eventInboxService.isDuplicate(eventId)).thenReturn(false); |
| 113 | + |
| 114 | + // When: Consumer가 이벤트 처리 |
| 115 | + boolean result = catalogEventConsumer.processEvent(record); |
| 116 | + |
| 117 | + // Then: Inbox에 저장하고, 좋아요 수 감소 |
| 118 | + assertThat(result).isTrue(); |
| 119 | + verify(eventInboxService).save(eventId, "LIKE", productId.toString(), "LikeDeletedEvent"); |
| 120 | + verify(productMetricsService).decrementLikeCount(productId); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Nested |
| 125 | + @DisplayName("ProductViewedEvent 처리") |
| 126 | + class ProductViewedEventTest { |
| 127 | + |
| 128 | + @Test |
| 129 | + @DisplayName("상품 조회 이벤트를 받으면 ProductMetrics의 조회 수를 증가시킨다") |
| 130 | + void incrementViewCount() throws Exception { |
| 131 | + // Given: 상품 조회 이벤트 |
| 132 | + Long productId = 3L; |
| 133 | + String eventId = "event-003"; |
| 134 | + Map<String, Object> event = createEvent(eventId, "ProductViewedEvent", "PRODUCT", productId.toString()); |
| 135 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 136 | + |
| 137 | + when(eventInboxService.isDuplicate(eventId)).thenReturn(false); |
| 138 | + |
| 139 | + // When: Consumer가 이벤트 처리 |
| 140 | + boolean result = catalogEventConsumer.processEvent(record); |
| 141 | + |
| 142 | + // Then: Inbox에 저장하고, 조회 수 증가 |
| 143 | + assertThat(result).isTrue(); |
| 144 | + verify(eventInboxService).save(eventId, "PRODUCT", productId.toString(), "ProductViewedEvent"); |
| 145 | + verify(productMetricsService).incrementViewCount(productId); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Nested |
| 150 | + @DisplayName("예외 상황 처리") |
| 151 | + class ExceptionHandlingTest { |
| 152 | + |
| 153 | + @Test |
| 154 | + @DisplayName("eventId가 없는 메시지는 false를 반환하고 처리하지 않는다") |
| 155 | + void handleMissingEventId() throws Exception { |
| 156 | + // Given: eventId가 없는 잘못된 메시지 |
| 157 | + Map<String, Object> event = new HashMap<>(); |
| 158 | + event.put("eventType", "LikeCreatedEvent"); |
| 159 | + event.put("aggregateType", "LIKE"); |
| 160 | + event.put("aggregateId", "1"); |
| 161 | + // id 필드 없음 |
| 162 | + |
| 163 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 164 | + |
| 165 | + // When: Consumer가 이벤트 처리 시도 |
| 166 | + boolean result = catalogEventConsumer.processEvent(record); |
| 167 | + |
| 168 | + // Then: 처리하지 않고 false 반환 |
| 169 | + assertThat(result).isFalse(); |
| 170 | + verify(eventInboxService, never()).isDuplicate(any()); |
| 171 | + verify(eventInboxService, never()).save(any(), any(), any(), any()); |
| 172 | + verify(productMetricsService, never()).incrementLikeCount(any()); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + @DisplayName("알 수 없는 이벤트 타입은 Inbox에만 저장하고 비즈니스 로직은 실행하지 않는다") |
| 177 | + void handleUnknownEventType() throws Exception { |
| 178 | + // Given: 알 수 없는 이벤트 타입 |
| 179 | + String eventId = "event-999"; |
| 180 | + Map<String, Object> event = createEvent(eventId, "UnknownEvent", "UNKNOWN", "1"); |
| 181 | + ConsumerRecord<Object, Object> record = createConsumerRecord("catalog-events", event); |
| 182 | + |
| 183 | + when(eventInboxService.isDuplicate(eventId)).thenReturn(false); |
| 184 | + |
| 185 | + // When: Consumer가 이벤트 처리 |
| 186 | + boolean result = catalogEventConsumer.processEvent(record); |
| 187 | + |
| 188 | + // Then: Inbox에는 저장하지만, ProductMetrics는 업데이트하지 않음 |
| 189 | + assertThat(result).isTrue(); |
| 190 | + verify(eventInboxService).save(eventId, "UNKNOWN", "1", "UnknownEvent"); |
| 191 | + verify(productMetricsService, never()).incrementLikeCount(any()); |
| 192 | + verify(productMetricsService, never()).decrementLikeCount(any()); |
| 193 | + verify(productMetricsService, never()).incrementViewCount(any()); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // Helper methods |
| 198 | + private Map<String, Object> createEvent(String id, String eventType, String aggregateType, String aggregateId) { |
| 199 | + Map<String, Object> event = new HashMap<>(); |
| 200 | + event.put("id", id); |
| 201 | + event.put("eventType", eventType); |
| 202 | + event.put("aggregateType", aggregateType); |
| 203 | + event.put("aggregateId", aggregateId); |
| 204 | + return event; |
| 205 | + } |
| 206 | + |
| 207 | + private ConsumerRecord<Object, Object> createConsumerRecord(String topic, Map<String, Object> event) throws Exception { |
| 208 | + String payload = objectMapper.writeValueAsString(event); |
| 209 | + return new ConsumerRecord<>(topic, 0, 0L, "key", payload); |
| 210 | + } |
| 211 | +} |
0 commit comments