|
| 1 | +package com.techfork.personalization.integration; |
| 2 | + |
| 3 | +import com.techfork.domain.recommendation.service.RecommendationService; |
| 4 | +import com.techfork.global.common.IntegrationTestBase; |
| 5 | +import com.techfork.personalization.application.event.PersonalizedProfileGeneratedEvent; |
| 6 | +import com.techfork.useraccount.application.query.lookup.UserLookupService; |
| 7 | +import com.techfork.useraccount.domain.User; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.context.ApplicationEventPublisher; |
| 12 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 13 | +import org.springframework.transaction.support.TransactionTemplate; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.argThat; |
| 19 | +import static org.mockito.ArgumentMatchers.eq; |
| 20 | +import static org.mockito.BDDMockito.given; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.never; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 25 | + |
| 26 | +class PersonalizedProfileGeneratedAfterCommitIntegrationTest extends IntegrationTestBase { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ApplicationEventPublisher eventPublisher; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private TransactionTemplate transactionTemplate; |
| 33 | + |
| 34 | + @MockitoBean |
| 35 | + private UserLookupService userLookupService; |
| 36 | + |
| 37 | + @MockitoBean |
| 38 | + private RecommendationService recommendationService; |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("프로필 생성 이벤트는 실제 트랜잭션 커밋 이후 추천 생성을 실행한다") |
| 42 | + void profileGeneratedEvent_CommittedTransaction_GeneratesRecommendationAfterCommit() { |
| 43 | + Long userId = 1L; |
| 44 | + User user = mock(User.class); |
| 45 | + float[] profileVector = new float[]{0.1f, 0.2f}; |
| 46 | + List<String> keyKeywords = List.of("Spring", "JPA"); |
| 47 | + given(userLookupService.getUserOrThrow(userId)).willReturn(user); |
| 48 | + given(recommendationService.generateRecommendationsForUser( |
| 49 | + eq(user), |
| 50 | + argThat(vector -> Arrays.equals(vector, profileVector)), |
| 51 | + eq(keyKeywords) |
| 52 | + )).willReturn(5); |
| 53 | + |
| 54 | + transactionTemplate.executeWithoutResult(status -> { |
| 55 | + eventPublisher.publishEvent(new PersonalizedProfileGeneratedEvent(userId, profileVector, keyKeywords)); |
| 56 | + |
| 57 | + verifyNoInteractions(userLookupService, recommendationService); |
| 58 | + }); |
| 59 | + |
| 60 | + verify(userLookupService).getUserOrThrow(userId); |
| 61 | + verify(recommendationService).generateRecommendationsForUser( |
| 62 | + eq(user), |
| 63 | + argThat(vector -> Arrays.equals(vector, profileVector)), |
| 64 | + eq(keyKeywords) |
| 65 | + ); |
| 66 | + verify(recommendationService, never()).generateRecommendationsForUser(user); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @DisplayName("트랜잭션이 롤백되면 프로필 생성 이벤트의 추천 후처리는 실행되지 않는다") |
| 71 | + void profileGeneratedEvent_RolledBackTransaction_DoesNotGenerateRecommendation() { |
| 72 | + transactionTemplate.executeWithoutResult(status -> { |
| 73 | + eventPublisher.publishEvent(new PersonalizedProfileGeneratedEvent( |
| 74 | + 1L, |
| 75 | + new float[]{0.1f}, |
| 76 | + List.of("Spring") |
| 77 | + )); |
| 78 | + status.setRollbackOnly(); |
| 79 | + }); |
| 80 | + |
| 81 | + verifyNoInteractions(userLookupService, recommendationService); |
| 82 | + } |
| 83 | +} |
0 commit comments