Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wow890209 committed Nov 4, 2023
1 parent dc79d3b commit 9d23eaa
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@ import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import tw.waterballsa.utopia.utopiagamification.quest.domain.Mission
import tw.waterballsa.utopia.utopiagamification.quest.domain.Player
import tw.waterballsa.utopia.utopiagamification.quest.domain.exception.AssignedQuestException
import tw.waterballsa.utopia.utopiagamification.quest.domain.quests.QuestIds.Companion.unlockAcademyQuestId
import tw.waterballsa.utopia.utopiagamification.quest.usecase.AssignPlayerQuestUsecase
import tw.waterballsa.utopia.utopiagamification.repositories.MissionRepository
import tw.waterballsa.utopia.utopiagamification.repositories.PlayerRepository
import tw.waterballsa.utopia.utopiagamification.repositories.exceptions.NotFoundException
import tw.waterballsa.utopia.utopiatestkit.annotations.UtopiaTest

private const val UNLOCK_QUEST_ID = 1

@UtopiaTest
class AssignPlayerQuestUsecaseTest @Autowired constructor(
private val assignPlayerQuestUsecase: AssignPlayerQuestUsecase,
private val playerRepository: PlayerRepository,
private val missionRepository: MissionRepository,
private val mongoTemplate: MongoTemplate
private val missionRepository: MissionRepository
) {

private val playerA = Player("A", "A")
private val request = AssignPlayerQuestUsecase.Request(playerId = playerA.id, questId = UNLOCK_QUEST_ID)
private val request = AssignPlayerQuestUsecase.Request(playerId = playerA.id, questId = unlockAcademyQuestId)

@BeforeEach
fun setup() {
Expand All @@ -35,7 +32,7 @@ class AssignPlayerQuestUsecaseTest @Autowired constructor(
@Test
fun `success assign a unlock quest to player A`() {
//Given
val presenter = AssignPlayerQuestTestPresenter(playerA.id, UNLOCK_QUEST_ID)
val presenter = AssignPlayerQuestTestPresenter(playerA.id, unlockAcademyQuestId)

//when
assertThatNoException().isThrownBy {
Expand All @@ -47,15 +44,15 @@ class AssignPlayerQuestUsecaseTest @Autowired constructor(
assertThat(missions).hasSize(1)
val mission = missions.first()
assertThat(mission.player.id).isEqualTo(playerA.id)
assertThat(mission.quest.id).isEqualTo(UNLOCK_QUEST_ID)
assertThat(mission.quest.id).isEqualTo(unlockAcademyQuestId)
assertThat(presenter.isExecuted).isTrue
}

@Test
fun `given playerA has first quest when playerA assign first quest then playerA can't get same quest repeatedly`() {
//given

val presenter = AssignPlayerQuestTestPresenter(playerA.id, UNLOCK_QUEST_ID)
val presenter = AssignPlayerQuestTestPresenter(playerA.id, unlockAcademyQuestId)

//when
assertThatNoException().isThrownBy {
Expand All @@ -72,11 +69,12 @@ class AssignPlayerQuestUsecaseTest @Autowired constructor(
fun `when assign a quest to the non-exist player, then should be fail`() {
//given
val nonExistPlayerId = "B"
val presenter = AssignPlayerQuestTestPresenter(nonExistPlayerId, UNLOCK_QUEST_ID)
val failRequest = AssignPlayerQuestUsecase.Request(playerId = nonExistPlayerId, questId = unlockAcademyQuestId)
val presenter = AssignPlayerQuestTestPresenter(nonExistPlayerId, unlockAcademyQuestId)

//then
assertThatExceptionOfType(NotFoundException::class.java).isThrownBy {
assignPlayerQuestUsecase.execute(request, presenter)
assignPlayerQuestUsecase.execute(failRequest, presenter)
}
}

Expand Down

0 comments on commit 9d23eaa

Please sign in to comment.