|
| 1 | +package org.gitanimals.quiz.domain.context |
| 2 | + |
| 3 | +import com.ninjasquad.springmockk.MockkBean |
| 4 | +import io.kotest.assertions.nondeterministic.eventually |
| 5 | +import io.kotest.core.spec.DisplayName |
| 6 | +import io.kotest.core.spec.style.StringSpec |
| 7 | +import io.kotest.matchers.shouldBe |
| 8 | +import io.mockk.verify |
| 9 | +import org.gitanimals.quiz.app.DomainEventHolder |
| 10 | +import org.gitanimals.quiz.app.IdentityApi |
| 11 | +import org.gitanimals.quiz.app.InboxApi |
| 12 | +import org.gitanimals.quiz.domain.quiz.quiz |
| 13 | +import org.gitanimals.quiz.infra.hibernate.* |
| 14 | +import org.gitanimals.quiz.infra.hibernate.QuizSolveContextDoneLogicDelegator.QuizSolveContextDone |
| 15 | +import org.springframework.boot.autoconfigure.domain.EntityScan |
| 16 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest |
| 17 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories |
| 18 | +import org.springframework.data.repository.findByIdOrNull |
| 19 | +import org.springframework.test.context.ContextConfiguration |
| 20 | +import org.springframework.test.context.TestPropertySource |
| 21 | +import org.springframework.transaction.annotation.Propagation |
| 22 | +import org.springframework.transaction.annotation.Transactional |
| 23 | +import kotlin.time.Duration.Companion.seconds |
| 24 | + |
| 25 | +@DataJpaTest |
| 26 | +@Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 27 | +@ContextConfiguration( |
| 28 | + classes = [ |
| 29 | + DomainEventHolder::class, |
| 30 | + QuizDeletedHibernateEventListener::class, |
| 31 | + HibernateEventListenerConfiguration::class, |
| 32 | + QuizSolveContextDoneHibernateEventListener::class, |
| 33 | + NewQuizCreatedInsertHibernateEventListener::class, |
| 34 | + QuizSolveContextDoneLogicDelegator::class, |
| 35 | + QuizSolveContextService::class, |
| 36 | + ] |
| 37 | +) |
| 38 | +@EntityScan(basePackages = ["org.gitanimals.quiz.domain"]) |
| 39 | +@EnableJpaRepositories(basePackages = ["org.gitanimals.quiz.domain"]) |
| 40 | +@TestPropertySource("classpath:test.properties") |
| 41 | +@DisplayName("QuizSolveContextService 클래스의") |
| 42 | +class QuizSolveContextServiceTest( |
| 43 | + private val quizSolveContextService: QuizSolveContextService, |
| 44 | + private val quizSolveContextRepository: QuizSolveContextRepository, |
| 45 | + private val domainEventHolder: DomainEventHolder, |
| 46 | + @MockkBean(relaxed = true) private val inboxApi: InboxApi, |
| 47 | + @MockkBean(relaxed = true) private val identityApi: IdentityApi, |
| 48 | +) : StringSpec({ |
| 49 | + |
| 50 | + "solveQuiz 메소드는 퀴즈가 DONE으로 변경된 경우 유저에게 포인트를 지급한다" { |
| 51 | + // given |
| 52 | + val userId = 1L |
| 53 | + val quiz = quiz() |
| 54 | + val quizSolveContext: QuizSolveContext = quizSolveContext(userId = userId, quizs = listOf(quiz)) |
| 55 | + quizSolveContextRepository.save(quizSolveContext).id |
| 56 | + |
| 57 | + quizSolveContextService.getAndStartSolveQuizContext( |
| 58 | + id = quizSolveContext.id, |
| 59 | + userId = userId, |
| 60 | + ) |
| 61 | + |
| 62 | + // when |
| 63 | + quizSolveContextService.solveQuiz( |
| 64 | + id = quizSolveContext.id, |
| 65 | + userId = userId, |
| 66 | + answer = "YES", |
| 67 | + ) |
| 68 | + |
| 69 | + val result = quizSolveContextRepository.findByIdOrNull(quizSolveContext.id)!! |
| 70 | + // then |
| 71 | + |
| 72 | + result.getStatus() shouldBe QuizSolveContextStatus.DONE |
| 73 | + eventually(5.seconds) { |
| 74 | + domainEventHolder.eventsShouldBe(QuizSolveContextDone::class, 1) |
| 75 | + } |
| 76 | + verify(exactly = 1) { |
| 77 | + identityApi.increaseUserPointsById(any(), any(), any()) |
| 78 | + } |
| 79 | + } |
| 80 | +}) { |
| 81 | + |
| 82 | +} |
0 commit comments