|
| 1 | +package com.techfork.personalization.integration; |
| 2 | + |
| 3 | +import com.techfork.domain.recommendation.listener.PersonalizedProfileGeneratedEventListener; |
| 4 | +import com.techfork.domain.recommendation.service.RecommendationService; |
| 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.BeforeEach; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Tag; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.context.ApplicationEventPublisher; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.context.annotation.Configuration; |
| 16 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 17 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 18 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 19 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 20 | +import org.springframework.transaction.PlatformTransactionManager; |
| 21 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 22 | +import org.springframework.transaction.support.TransactionTemplate; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import javax.sql.DataSource; |
| 28 | + |
| 29 | +import static org.mockito.ArgumentMatchers.argThat; |
| 30 | +import static org.mockito.ArgumentMatchers.eq; |
| 31 | +import static org.mockito.BDDMockito.given; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.never; |
| 34 | +import static org.mockito.Mockito.reset; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 37 | + |
| 38 | +@Tag("integration") |
| 39 | +@SpringJUnitConfig(PersonalizedProfileGeneratedAfterCommitIntegrationTest.TestConfig.class) |
| 40 | +class PersonalizedProfileGeneratedAfterCommitIntegrationTest { |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private ApplicationEventPublisher eventPublisher; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private TransactionTemplate transactionTemplate; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private UserLookupService userLookupService; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private RecommendationService recommendationService; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setUp() { |
| 56 | + reset(userLookupService, recommendationService); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("프로필 생성 이벤트는 실제 트랜잭션 커밋 이후 추천 생성을 실행한다") |
| 61 | + void profileGeneratedEvent_CommittedTransaction_GeneratesRecommendationAfterCommit() { |
| 62 | + Long userId = 1L; |
| 63 | + User user = mock(User.class); |
| 64 | + float[] profileVector = new float[]{0.1f, 0.2f}; |
| 65 | + List<String> keyKeywords = List.of("Spring", "JPA"); |
| 66 | + given(userLookupService.getUserOrThrow(userId)).willReturn(user); |
| 67 | + given(recommendationService.generateRecommendationsForUser( |
| 68 | + eq(user), |
| 69 | + argThat(vector -> Arrays.equals(vector, profileVector)), |
| 70 | + eq(keyKeywords) |
| 71 | + )).willReturn(5); |
| 72 | + |
| 73 | + transactionTemplate.executeWithoutResult(status -> { |
| 74 | + eventPublisher.publishEvent(new PersonalizedProfileGeneratedEvent(userId, profileVector, keyKeywords)); |
| 75 | + |
| 76 | + verifyNoInteractions(userLookupService, recommendationService); |
| 77 | + }); |
| 78 | + |
| 79 | + verify(userLookupService).getUserOrThrow(userId); |
| 80 | + verify(recommendationService).generateRecommendationsForUser( |
| 81 | + eq(user), |
| 82 | + argThat(vector -> Arrays.equals(vector, profileVector)), |
| 83 | + eq(keyKeywords) |
| 84 | + ); |
| 85 | + verify(recommendationService, never()).generateRecommendationsForUser(user); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @DisplayName("트랜잭션이 롤백되면 프로필 생성 이벤트의 추천 후처리는 실행되지 않는다") |
| 90 | + void profileGeneratedEvent_RolledBackTransaction_DoesNotGenerateRecommendation() { |
| 91 | + transactionTemplate.executeWithoutResult(status -> { |
| 92 | + eventPublisher.publishEvent(new PersonalizedProfileGeneratedEvent( |
| 93 | + 1L, |
| 94 | + new float[]{0.1f}, |
| 95 | + List.of("Spring") |
| 96 | + )); |
| 97 | + status.setRollbackOnly(); |
| 98 | + }); |
| 99 | + |
| 100 | + verifyNoInteractions(userLookupService, recommendationService); |
| 101 | + } |
| 102 | + |
| 103 | + @Configuration |
| 104 | + @EnableTransactionManagement |
| 105 | + static class TestConfig { |
| 106 | + |
| 107 | + @Bean |
| 108 | + PersonalizedProfileGeneratedEventListener personalizedProfileGeneratedEventListener( |
| 109 | + UserLookupService userLookupService, |
| 110 | + RecommendationService recommendationService |
| 111 | + ) { |
| 112 | + return new PersonalizedProfileGeneratedEventListener(userLookupService, recommendationService); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + UserLookupService userLookupService() { |
| 117 | + return mock(UserLookupService.class); |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + RecommendationService recommendationService() { |
| 122 | + return mock(RecommendationService.class); |
| 123 | + } |
| 124 | + |
| 125 | + @Bean |
| 126 | + DataSource dataSource() { |
| 127 | + return new EmbeddedDatabaseBuilder() |
| 128 | + .generateUniqueName(true) |
| 129 | + .setType(EmbeddedDatabaseType.H2) |
| 130 | + .build(); |
| 131 | + } |
| 132 | + |
| 133 | + @Bean |
| 134 | + PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 135 | + return new DataSourceTransactionManager(dataSource); |
| 136 | + } |
| 137 | + |
| 138 | + @Bean |
| 139 | + TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) { |
| 140 | + return new TransactionTemplate(transactionManager); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments