Skip to content

Commit 10a544d

Browse files
committed
refactor: 프로필 생성 완료 이벤트로 변경
1 parent 6e7d1b9 commit 10a544d

3 files changed

Lines changed: 23 additions & 67 deletions

File tree

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.techfork.personalization.application;
22

3-
import com.techfork.domain.recommendation.service.RecommendationService;
4-
import com.techfork.useraccount.domain.User;
3+
import com.techfork.personalization.application.event.PersonalizedProfileGeneratedEvent;
54
import com.techfork.personalization.application.generation.PersonalizedProfileGenerator;
6-
import com.techfork.useraccount.application.query.lookup.UserLookupService;
75
import lombok.RequiredArgsConstructor;
86
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.context.ApplicationEventPublisher;
98
import org.springframework.scheduling.annotation.Async;
109
import org.springframework.stereotype.Service;
1110
import org.springframework.transaction.annotation.Transactional;
@@ -16,8 +15,7 @@
1615
public class PersonalizationProfileService {
1716

1817
private final PersonalizedProfileGenerator personalizedProfileGenerator;
19-
private final UserLookupService userLookupService;
20-
private final RecommendationService recommendationService;
18+
private final ApplicationEventPublisher eventPublisher;
2119

2220
@Async
2321
@Transactional
@@ -33,32 +31,13 @@ public void generatePersonalizationProfile(Long userId) {
3331
public void generatePersonalizationProfileSync(Long userId) {
3432
try {
3533
personalizedProfileGenerator.generate(userId);
34+
eventPublisher.publishEvent(new PersonalizedProfileGeneratedEvent(userId));
3635

3736
log.info("Personalization profile generated successfully for userId: {}", userId);
3837

39-
generateRecommendationsAfterProfile(userId);
40-
4138
} catch (Exception e) {
4239
log.error("Failed to generate personalization profile for userId: {}", userId, e);
4340
throw e;
4441
}
4542
}
46-
47-
/**
48-
* 개인화 프로필 생성 완료 후 추천 생성
49-
* 온보딩 또는 관심사 변경 시 새 개인화 프로필 기반으로 추천을 갱신합니다.
50-
*/
51-
private void generateRecommendationsAfterProfile(Long userId) {
52-
try {
53-
User user = userLookupService.getUserOrThrow(userId);
54-
55-
int recommendationCount = recommendationService.generateRecommendationsForUser(user);
56-
57-
log.info("Recommendations generated after personalization profile creation for userId: {} - {} recommendations created",
58-
userId, recommendationCount);
59-
60-
} catch (Exception e) {
61-
log.error("Failed to generate recommendations after personalization profile creation for userId: {}", userId, e);
62-
}
63-
}
6443
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.techfork.personalization.application.event;
2+
3+
public record PersonalizedProfileGeneratedEvent(
4+
Long userId
5+
) {
6+
}
Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.techfork.personalization.application;
22

3-
import com.techfork.domain.recommendation.service.RecommendationService;
4-
import com.techfork.useraccount.domain.User;
5-
import com.techfork.useraccount.domain.enums.SocialType;
3+
import com.techfork.personalization.application.event.PersonalizedProfileGeneratedEvent;
64
import com.techfork.personalization.application.generation.PersonalizedProfileGenerator;
7-
import com.techfork.useraccount.application.query.lookup.UserLookupService;
85
import org.junit.jupiter.api.DisplayName;
96
import org.junit.jupiter.api.Test;
107
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.ArgumentCaptor;
119
import org.mockito.InjectMocks;
1210
import org.mockito.Mock;
1311
import org.mockito.junit.jupiter.MockitoExtension;
14-
import org.springframework.test.util.ReflectionTestUtils;
12+
import org.springframework.context.ApplicationEventPublisher;
1513

16-
import static org.assertj.core.api.Assertions.assertThatCode;
14+
import static org.assertj.core.api.Assertions.assertThat;
1715
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1816
import static org.mockito.BDDMockito.given;
1917
import static org.mockito.Mockito.verify;
@@ -26,32 +24,27 @@ class PersonalizationProfileServiceTest {
2624
private PersonalizedProfileGenerator personalizedProfileGenerator;
2725

2826
@Mock
29-
private UserLookupService userLookupService;
30-
31-
@Mock
32-
private RecommendationService recommendationService;
27+
private ApplicationEventPublisher eventPublisher;
3328

3429
@InjectMocks
3530
private PersonalizationProfileService personalizationProfileService;
3631

3732
@Test
38-
@DisplayName("개인화 프로필 생성을 요청한 뒤 추천을 생성한다")
39-
void generatePersonalizationProfileSync_GeneratesProfileAndRecommendations() {
33+
@DisplayName("개인화 프로필 생성 성공 후 프로필 생성 이벤트를 발행한다")
34+
void generatePersonalizationProfileSync_PublishesProfileGeneratedEventAfterProfileGeneration() {
4035
Long userId = 1L;
41-
User user = createUser(userId);
42-
given(userLookupService.getUserOrThrow(userId)).willReturn(user);
43-
given(recommendationService.generateRecommendationsForUser(user)).willReturn(5);
4436

4537
personalizationProfileService.generatePersonalizationProfileSync(userId);
4638

39+
ArgumentCaptor<PersonalizedProfileGeneratedEvent> eventCaptor = ArgumentCaptor.forClass(PersonalizedProfileGeneratedEvent.class);
4740
verify(personalizedProfileGenerator).generate(userId);
48-
verify(userLookupService).getUserOrThrow(userId);
49-
verify(recommendationService).generateRecommendationsForUser(user);
41+
verify(eventPublisher).publishEvent(eventCaptor.capture());
42+
assertThat(eventCaptor.getValue().userId()).isEqualTo(userId);
5043
}
5144

5245
@Test
53-
@DisplayName("개인화 프로필 생성이 실패하면 예외를 전파하고 추천을 시도하지 않는다")
54-
void generatePersonalizationProfileSync_ProfileGenerationFailurePropagatesException() {
46+
@DisplayName("개인화 프로필 생성이 실패하면 예외를 전파하고 이벤트를 발행하지 않는다")
47+
void generatePersonalizationProfileSync_ProfileGenerationFailurePropagatesExceptionAndDoesNotPublishEvent() {
5548
Long userId = 2L;
5649
RuntimeException failure = new RuntimeException("profile generation failure");
5750
given(personalizedProfileGenerator.generate(userId)).willThrow(failure);
@@ -60,28 +53,6 @@ void generatePersonalizationProfileSync_ProfileGenerationFailurePropagatesExcept
6053
.isSameAs(failure);
6154

6255
verify(personalizedProfileGenerator).generate(userId);
63-
verifyNoInteractions(userLookupService, recommendationService);
64-
}
65-
66-
@Test
67-
@DisplayName("추천 생성이 실패해도 개인화 프로필 저장은 유지된다")
68-
void generatePersonalizationProfileSync_RecommendationFailureDoesNotBreakProfileSave() {
69-
Long userId = 3L;
70-
User user = createUser(userId);
71-
given(userLookupService.getUserOrThrow(userId)).willReturn(user);
72-
given(recommendationService.generateRecommendationsForUser(user))
73-
.willThrow(new RuntimeException("recommendation failure"));
74-
75-
assertThatCode(() -> personalizationProfileService.generatePersonalizationProfileSync(userId))
76-
.doesNotThrowAnyException();
77-
78-
verify(personalizedProfileGenerator).generate(userId);
79-
verify(recommendationService).generateRecommendationsForUser(user);
80-
}
81-
82-
private User createUser(Long userId) {
83-
User user = User.createSocialUser(SocialType.KAKAO, "social-" + userId, "user" + userId + "@example.com", null);
84-
ReflectionTestUtils.setField(user, "id", userId);
85-
return user;
56+
verifyNoInteractions(eventPublisher);
8657
}
8758
}

0 commit comments

Comments
 (0)