feat(recommend): 추천 팔로우 성공 로그를 커밋 후 기록한다

This commit is contained in:
2026-06-01 17:56:50 +09:00
parent da387f43a0
commit bb96f07872
2 changed files with 113 additions and 28 deletions

View File

@@ -14,16 +14,21 @@ import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.system.CapturedOutput
import org.springframework.boot.test.system.OutputCaptureExtension
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.test.context.ContextConfiguration
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.support.TransactionSynchronizationManager
import javax.persistence.EntityManager
@SpringBootTest
@Transactional
@ContextConfiguration(initializers = [EmbeddedRedisInitializer::class])
@ExtendWith(OutputCaptureExtension::class)
class RecommendedCreatorFollowServiceTest @Autowired constructor(
private val service: RecommendedCreatorFollowService,
private val memberRepository: MemberRepository,
@@ -32,7 +37,7 @@ class RecommendedCreatorFollowServiceTest @Autowired constructor(
) {
@Test
@DisplayName("신규 크리에이터만 팔로우 저장하고 이미 팔로우/본인 id는 서버 내부에서 제외한다")
fun shouldFollowOnlyNewCreatorsAndSkipExistingAndSelf() {
fun shouldFollowOnlyNewCreatorsAndSkipExistingAndSelf(output: CapturedOutput) {
val member = saveMember("viewer", MemberRole.USER)
val newCreator = saveMember("new-creator", MemberRole.CREATOR)
val followedCreator = saveMember("followed-creator", MemberRole.CREATOR)
@@ -40,16 +45,39 @@ class RecommendedCreatorFollowServiceTest @Autowired constructor(
entityManager.flush()
entityManager.clear()
val beforeCount = TransactionSynchronizationManager.getSynchronizations().size
service.followCreators(
member = member,
creatorIds = listOf(newCreator.id!!, followedCreator.id!!, member.id!!)
)
entityManager.flush()
entityManager.clear()
TransactionSynchronizationManager.getSynchronizations().drop(beforeCount).forEach { it.afterCommit() }
assertNotNull(creatorFollowingRepository.findByCreatorIdAndMemberId(newCreator.id!!, member.id!!))
assertNotNull(creatorFollowingRepository.findByCreatorIdAndMemberId(followedCreator.id!!, member.id!!))
assertEquals(2, creatorFollowingRepository.findAll().size)
assertTrue(output.out.contains("event=recommended_creator_follow_success"))
assertTrue(output.out.contains("requestedCount=3"))
assertTrue(output.out.contains("savedCount=1"))
}
@Test
@DisplayName("추천 크리에이터 동시 팔로우 성공 로그는 트랜잭션 커밋 후 기록한다")
fun shouldLogFollowSuccessAfterTransactionCommit(output: CapturedOutput) {
val member = saveMember("after-commit-viewer", MemberRole.USER)
val creator = saveMember("after-commit-creator", MemberRole.CREATOR)
entityManager.flush()
entityManager.clear()
val beforeCount = TransactionSynchronizationManager.getSynchronizations().size
service.followCreators(member = member, creatorIds = listOf(creator.id!!))
assertEquals(false, output.out.contains("event=recommended_creator_follow_success"))
TransactionSynchronizationManager.getSynchronizations().drop(beforeCount).forEach { it.afterCommit() }
assertTrue(output.out.contains("event=recommended_creator_follow_success"))
}
@Test
@@ -119,7 +147,7 @@ class RecommendedCreatorFollowServiceTest @Autowired constructor(
@Test
@DisplayName("존재하지 않는 id가 하나라도 포함되면 전체 실패하고 신규 저장하지 않는다")
fun shouldFailAllAndSaveNothingWhenAnyCreatorIdDoesNotExist() {
fun shouldFailAllAndSaveNothingWhenAnyCreatorIdDoesNotExist(output: CapturedOutput) {
val member = saveMember("viewer", MemberRole.USER)
val validCreator = saveMember("valid-creator", MemberRole.CREATOR)
entityManager.flush()
@@ -131,6 +159,7 @@ class RecommendedCreatorFollowServiceTest @Autowired constructor(
assertEquals("member.validation.creator_not_found", exception.messageKey)
assertEquals(0, creatorFollowingRepository.findAll().size)
assertTrue(output.out.contains("event=recommended_creator_follow_failure"))
}
@Test