|
| 1 | +package com.project.codereview.core.service |
| 2 | + |
| 3 | +import com.project.codereview.client.github.GithubAppTokenProvider |
| 4 | +import com.project.codereview.client.github.GithubDiffClient |
| 5 | +import com.project.codereview.client.github.GithubReviewClient |
| 6 | +import com.project.codereview.client.google.GoogleGeminiClient |
| 7 | +import com.project.codereview.domain.model.WebhookEventStatus |
| 8 | +import com.project.codereview.domain.port.WebhookEventRepository |
| 9 | +import org.junit.jupiter.api.Assertions.* |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import org.springframework.beans.factory.annotation.Autowired |
| 12 | +import org.springframework.boot.test.context.SpringBootTest |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 14 | +import org.springframework.transaction.annotation.Transactional |
| 15 | + |
| 16 | +@SpringBootTest |
| 17 | +@Transactional |
| 18 | +class WebhookEventServiceTest { |
| 19 | + |
| 20 | + @Autowired |
| 21 | + private lateinit var webhookEventService: WebhookEventService |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private lateinit var webhookEventRepository: WebhookEventRepository |
| 25 | + |
| 26 | + @MockBean |
| 27 | + private lateinit var githubAppTokenProvider: GithubAppTokenProvider |
| 28 | + |
| 29 | + @MockBean |
| 30 | + private lateinit var githubDiffClient: GithubDiffClient |
| 31 | + |
| 32 | + @MockBean |
| 33 | + private lateinit var githubReviewClient: GithubReviewClient |
| 34 | + |
| 35 | + @MockBean |
| 36 | + private lateinit var googleGeminiClient: GoogleGeminiClient |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `이벤트를 성공적으로 저장하고 중복 저장을 방지한다`() { |
| 40 | + val deliveryId = "test-delivery-id" |
| 41 | + val payload = "{\"action\": \"opened\"}" |
| 42 | + |
| 43 | + val savedEvent = webhookEventService.saveEvent(deliveryId, "pull_request", payload) |
| 44 | + assertNotNull(savedEvent) |
| 45 | + assertEquals(deliveryId, savedEvent?.deliveryId) |
| 46 | + |
| 47 | + val duplicateEvent = webhookEventService.saveEvent(deliveryId, "pull_request", payload) |
| 48 | + assertNull(duplicateEvent) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `이벤트 상태를 성공적으로 업데이트한다`() { |
| 53 | + val deliveryId = "test-delivery-id-2" |
| 54 | + webhookEventService.saveEvent(deliveryId, "pull_request", "{}") |
| 55 | + |
| 56 | + webhookEventService.updateStatus(deliveryId, WebhookEventStatus.PROCESSING) |
| 57 | + val event = webhookEventRepository.findById(deliveryId).get() |
| 58 | + assertEquals(WebhookEventStatus.PROCESSING, event.status) |
| 59 | + |
| 60 | + webhookEventService.updateStatus(deliveryId, WebhookEventStatus.FAILED, "error occurred") |
| 61 | + val updatedEvent = webhookEventRepository.findById(deliveryId).get() |
| 62 | + assertEquals(WebhookEventStatus.FAILED, updatedEvent.status) |
| 63 | + assertEquals("error occurred", updatedEvent.errorMessage) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `재시도 가능한 실패한 이벤트를 조회한다`() { |
| 68 | + val deliveryId = "test-failed-id" |
| 69 | + webhookEventService.saveEvent(deliveryId, "pull_request", "{}") |
| 70 | + webhookEventService.updateStatus(deliveryId, WebhookEventStatus.FAILED) |
| 71 | + |
| 72 | + val retryEvents = webhookEventService.getEventsToRetry(3) |
| 73 | + assertTrue(retryEvents.any { it.deliveryId == deliveryId }) |
| 74 | + |
| 75 | + webhookEventService.markAsRetrying(deliveryId) |
| 76 | + val eventAfterRetryMark = webhookEventRepository.findById(deliveryId).get() |
| 77 | + assertEquals(1, eventAfterRetryMark.retryCount) |
| 78 | + assertEquals(WebhookEventStatus.PENDING, eventAfterRetryMark.status) |
| 79 | + } |
| 80 | +} |
0 commit comments