|
| 1 | +package org.mybatis.jpetstore.catalog.service; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.mybatis.jpetstore.catalog.CatalogServiceApplication; |
| 6 | +import org.mybatis.jpetstore.catalog.controller.CatalogController; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.boot.test.context.TestConfiguration; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 12 | +import org.springframework.kafka.core.KafkaTemplate; |
| 13 | +import org.springframework.kafka.core.ProducerFactory; |
| 14 | +import org.springframework.kafka.support.serializer.JsonSerializer; |
| 15 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 16 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 17 | +import org.springframework.kafka.test.utils.ContainerTestUtils; |
| 18 | +import org.springframework.kafka.config.KafkaListenerEndpointRegistry; |
| 19 | +import org.springframework.kafka.listener.MessageListenerContainer; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.springframework.test.context.ActiveProfiles; |
| 22 | +import org.springframework.boot.test.mock.mockito.SpyBean; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 29 | +import static org.mockito.ArgumentMatchers.any; |
| 30 | +import static org.mockito.Mockito.timeout; |
| 31 | +import static org.mockito.Mockito.verify; |
| 32 | + |
| 33 | +/** |
| 34 | + * Saga 패턴의 보상 트랜잭션 수신 측(Catalog Service) 통합 테스트입니다. |
| 35 | + */ |
| 36 | +@SpringBootTest(classes = CatalogServiceApplication.class, properties = { |
| 37 | + "eureka.client.enabled=false", |
| 38 | + "spring.cloud.discovery.enabled=false", |
| 39 | + "spring.cloud.compatibility-verifier.enabled=false", |
| 40 | + "spring.sql.init.mode=always", |
| 41 | + "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.mybatis.jpetstore.common.config.CommonAutoConfiguration", |
| 42 | + "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer", |
| 43 | + "spring.kafka.consumer.properties.spring.json.trusted.packages=*", |
| 44 | + "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer", |
| 45 | + "kafka.bootstrap-servers=${spring.kafka.bootstrap-servers}", |
| 46 | + "grpc.server.port=0" |
| 47 | +}) |
| 48 | +@EmbeddedKafka(partitions = 1, topics = {"product_compensation"}, bootstrapServersProperty = "spring.kafka.bootstrap-servers") |
| 49 | +@ActiveProfiles("test") |
| 50 | +public class RollbackIntegrationTest { |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private KafkaTemplate<String, Object> kafkaTemplate; |
| 54 | + |
| 55 | + @SpyBean |
| 56 | + private CatalogService catalogService; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private CatalogController catalogController; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private EmbeddedKafkaBroker embeddedKafkaBroker; |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + public void setUp() { |
| 69 | + for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry.getListenerContainers()) { |
| 70 | + ContainerTestUtils.waitForAssignment(messageListenerContainer, embeddedKafkaBroker.getPartitionsPerTopic()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("보상 트랜잭션 통합 테스트: Catalog 서비스는 Kafka 보상 이벤트를 받으면 실제 DB 재고를 복구해야 한다") |
| 76 | + void verifyCatalogRollbackByKafkaEvent() { |
| 77 | + // Given: 보상 이벤트 데이터 (아이템 EST-1, 수량 10개 복구) |
| 78 | + Map<String, Object> data = new HashMap<>(); |
| 79 | + data.put("EST-1", 10); |
| 80 | + |
| 81 | + // 테스트 시작 전 EST-1 의 현재 재고 확인 (데이터로드 SQL 에 의해 10000개로 초기화됨) |
| 82 | + Integer initialQuantity = catalogService.getItemQuantity("EST-1"); |
| 83 | + assertNotNull(initialQuantity); |
| 84 | + System.out.println("=== [TEST] Initial DB Quantity for EST-1: " + initialQuantity + " ==="); |
| 85 | + |
| 86 | + // When: 실제 Kafka 토픽으로 메시지 전송 |
| 87 | + System.out.println("=== [TEST] SENDING COMPENSATION EVENT TO EMBEDDED KAFKA ==="); |
| 88 | + kafkaTemplate.send("product_compensation", data); |
| 89 | + kafkaTemplate.flush(); |
| 90 | + |
| 91 | + // Then: CatalogController의 @KafkaListener가 동작하여 catalogService.rollbackInventory를 호출하는지 대기 |
| 92 | + verify(catalogService, timeout(15000).times(1)).rollbackInventory(any()); |
| 93 | + |
| 94 | + // Kafka 메시지 처리로 인해 트랜잭션이 커밋되는 시간을 약간 대기 (필요시) |
| 95 | + try { |
| 96 | + Thread.sleep(500); |
| 97 | + } catch (InterruptedException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + |
| 101 | + // DB 재고가 실제로 복구되었는지 확인 |
| 102 | + Integer updatedQuantity = catalogService.getItemQuantity("EST-1"); |
| 103 | + System.out.println("=== [TEST] Updated DB Quantity for EST-1: " + updatedQuantity + " ==="); |
| 104 | + |
| 105 | + assertEquals(initialQuantity + 10, updatedQuantity); |
| 106 | + |
| 107 | + System.out.println("=== [TEST] VERIFIED: Catalog Service DB Rollback Triggered and Executed by REAL Kafka Event! ==="); |
| 108 | + } |
| 109 | +} |
0 commit comments