Skip to content

Commit

Permalink
Merge pull request #162 from KUSITMS-29th-TEAM-B/test/flight-132
Browse files Browse the repository at this point in the history
test: Token, User 도메인 테스트 코드 작성(#132)
  • Loading branch information
isprogrammingfun authored May 23, 2024
2 parents 6a87b8c + a72147a commit b7a83fc
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ inline fun <reified T> generateFixture(): T {
// 기본 타입의 임의의 객체
inline fun <reified T> generateBasicTypeFixture(length: Int): T {
return if (T::class == String::class) {
FixtureMonkey.builder()
val generatedString = FixtureMonkey.builder()
.defaultNotNull(true)
.plugin(KotlinPlugin())
.build()
.giveMeBuilder<T>()
.sample()
.toString()
.take(length) as T
val paddedString = generatedString.padEnd(length, ' ')
paddedString.take(length) as T
} else {
FixtureMonkey.builder()
.defaultNotNull(true)
Expand All @@ -45,5 +46,4 @@ inline fun <reified T> generateBasicTypeFixture(length: Int): T {
.giveMeBuilder<T>()
.sample()
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bamyanggang.domainmodule.domain.user.service

import com.bamyanggang.domainmodule.domain.user.aggregate.Token
import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository

class TokenRemover(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bamyanggang.domainmodule.domain.user.aggregate

import com.bamyanggang.commonmodule.fixture.generateFixture
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.util.UUID

class TokenTest : FunSpec({

test("Token 생성") {
val token: Token = generateFixture {
it.set("userId", UUID.randomUUID())
it.set("value", "token")
}
token.value shouldBe "token"
}

test("Token 업데이트") {
val userId = UUID.randomUUID()
val token: Token = generateFixture {
it.set("userId", userId)
it.set("value", "token")
}
val newToken = "newToken"
val updatedToken = token.update(userId, newToken)
updatedToken.value shouldBe newToken
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.bamyanggang.domainmodule.domain.user.aggregate

import com.bamyanggang.commonmodule.fixture.generateBasicTypeFixture
import com.bamyanggang.commonmodule.fixture.generateFixture
import com.bamyanggang.domainmodule.domain.user.enums.SocialLoginProvider
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime

class UserTest : FunSpec({

test("User 생성") {
val socialId = "socialId"
val profileImgUrl = "profileImgUrl"
val provider: SocialLoginProvider = SocialLoginProvider.KAKAO
val email = "email"
val nickName = "nickName"
val jobSearchStatus = null
val desiredJob = null
val goal = null
val dream = null

val user = User.create(
socialId = socialId,
profileImgUrl = profileImgUrl,
provider = provider,
email = email,
nickName = nickName,
jobSearchStatus = jobSearchStatus,
desiredJob = desiredJob,
goal = goal,
dream = dream
)

user.socialId shouldBe socialId
user.profileImgUrl shouldBe profileImgUrl
user.provider shouldBe provider
user.email shouldBe email
user.nickName shouldBe nickName
user.jobSearchStatus shouldBe jobSearchStatus
user.desiredJob shouldBe desiredJob
user.goal shouldBe goal
user.dream shouldBe dream

}

test("User 업데이트") {
val user: User = generateFixture {
it.set("socialId", "socialId")
it.set("profileImgUrl", "profileImgUrl")
it.set("provider", SocialLoginProvider.KAKAO)
it.set("email", "email")
it.set("nickName", "nickName")
it.set("createdAt", LocalDateTime.now())
it.set("updatedAt", LocalDateTime.now())
}
val newNickName = "newNick"
val updatedUser = user.update(
nickName = newNickName,
profileImgUrl = null,
jobSearchStatus = null,
desiredJob = null,
goal = null,
dream = null
)
updatedUser.nickName shouldBe newNickName
}

test("필수 항목이 누락되었을 경우 에러 반환") {
// arrange
val socialId = "socialId"
val profileImgUrl = ""
val provider: SocialLoginProvider = SocialLoginProvider.KAKAO
val email = "email"
val nickName = "nickName"
val jobSearchStatus = null
val desiredJob = null
val goal = null
val dream = null

// act, assert
shouldThrow<IllegalArgumentException> {
User.create(
socialId = socialId,
profileImgUrl = profileImgUrl,
provider = provider,
email = email,
nickName = nickName,
jobSearchStatus = jobSearchStatus,
desiredJob = desiredJob,
goal = goal,
dream = dream
)
}
}

test("닉네임이 10자를 초과할 경우 에러 반환") {
// arrange
val socialId = "socialId"
val profileImgUrl= "profileImgUrl"
val provider: SocialLoginProvider = SocialLoginProvider.KAKAO
val email = "email"
val nickName = "nickName12345"
val jobSearchStatus = null
val desiredJob = null
val goal = null
val dream = null

// act, assert
shouldThrow<IllegalArgumentException> {
User.create(
socialId = socialId,
profileImgUrl = profileImgUrl,
provider = provider,
email = email,
nickName = nickName,
jobSearchStatus = jobSearchStatus,
desiredJob = desiredJob,
goal = goal,
dream = dream
)
}
}

test("희망 직무가 50자를 초과할 경우 에러 반환") {
// arrange
val socialId = "socialId"
val profileImgUrl = "profileImgUrl"
val provider: SocialLoginProvider = SocialLoginProvider.KAKAO
val email = "email"
val nickName = "nickName"
val jobSearchStatus = null
val desiredJob: String = generateBasicTypeFixture(51)
val goal = null
val dream = null

// act, assert
shouldThrow<IllegalArgumentException> {
User.create(
socialId = socialId,
profileImgUrl = profileImgUrl,
provider = provider,
email = email,
nickName = nickName,
jobSearchStatus = jobSearchStatus,
desiredJob = desiredJob,
goal = goal,
dream = dream
)
}
}

test("발전시키고 싶은 역량이 400자를 초과할 경우 에러 반환") {
// arrange
val socialId = "socialId"
val profileImgUrl = "profileImgUrl"
val provider: SocialLoginProvider = SocialLoginProvider.KAKAO
val email = "email"
val nickName = "nickName"
val jobSearchStatus = null
val desiredJob = null
val goal: String = generateBasicTypeFixture(401)
val dream = null

// act, assert
shouldThrow<IllegalArgumentException> {
User.create(
socialId = socialId,
profileImgUrl = profileImgUrl,
provider = provider,
email = email,
nickName = nickName,
jobSearchStatus = jobSearchStatus,
desiredJob = desiredJob,
goal = goal,
dream = dream
)
}
}

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.bamyanggang.domainmodule.domain.user.service

import com.bamyanggang.domainmodule.domain.user.aggregate.Token
import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.util.UUID

class TokenAppenderTest : BehaviorSpec({
val mockTokenRepository = mockk<TokenRepository>(relaxed = true)
val tokenAppender = TokenAppender(mockTokenRepository)
val userId = UUID.randomUUID()
val refreshToken = "refreshToken"

Given("a user id and a refresh token, and the user does not have an existing token") {
every { mockTokenRepository.findByUserId(userId) } returns null

When("appendToken is called") {
tokenAppender.appendToken(userId, refreshToken)

Then("a new token should be created and saved") {
verify { mockTokenRepository.findByUserId(userId) }
verify { mockTokenRepository.save(any()) }
}
}
}

Given("a user id and a refresh token, and the user has an existing token") {
val mockToken = mockk<Token>(relaxed = true)
every { mockTokenRepository.findByUserId(userId) } returns mockToken

When("appendToken is called") {
tokenAppender.appendToken(userId, refreshToken)

Then("the existing token should be updated and saved") {
verify { mockTokenRepository.findByUserId(userId) }
verify { mockToken.update(userId, refreshToken) }
verify { mockTokenRepository.save(mockToken) }
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bamyanggang.domainmodule.domain.user.service

import com.bamyanggang.domainmodule.domain.user.aggregate.Token
import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.util.UUID

class TokenModifierTest : BehaviorSpec({
val mockTokenRepository = mockk<TokenRepository>(relaxed = true)
val tokenModifier = TokenModifier(mockTokenRepository)
val userId = UUID.randomUUID()
val refreshToken = "refreshToken"
val newRefreshToken = "newRefreshToken"

Given("a user id, a refresh token, and a new refresh token") {
val mockToken = mockk<Token>(relaxed = true)
every { mockTokenRepository.findByValue(refreshToken) } returns mockToken

When("modifyToken is called") {
tokenModifier.modifyToken(userId, refreshToken, newRefreshToken)

Then("the token should be updated and saved") {
verify { mockTokenRepository.findByValue(refreshToken) }
verify { mockToken.update(userId, newRefreshToken) }
verify { mockTokenRepository.save(any()) }
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bamyanggang.domainmodule.domain.user.service

import com.bamyanggang.domainmodule.domain.user.repository.TokenRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.mockk
import io.mockk.verify

class TokenRemoverTest : BehaviorSpec({
val mockTokenRepository = mockk<TokenRepository>(relaxed = true)
val tokenRemover = TokenRemover(mockTokenRepository)
val token = "token"

Given("a token") {
When("removeToken is called") {
tokenRemover.removeToken(token)

Then("the token should be removed") {
verify { mockTokenRepository.deleteByValue(token) }
}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.bamyanggang.domainmodule.domain.user.service

import com.bamyanggang.domainmodule.domain.user.aggregate.User
import com.bamyanggang.domainmodule.domain.user.repository.UserRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.util.UUID

class UserModifierTest : BehaviorSpec({
val mockUserRepository = mockk<UserRepository>(relaxed = true)
val userModifier = UserModifier(mockUserRepository)
val userId = UUID.randomUUID()
val nickName = "nickName"
val profileImgUrl = "profileImgUrl"
val jobSearchStatus = "jobSearchStatus"
val desiredJob = "desiredJob"
val goal = "goal"
val dream = "dream"

Given("a user id, nickname, profile image url, job search status, desired job, goal, and dream") {
val mockUser = mockk<User>(relaxed = true)
every { mockUserRepository.findById(userId) } returns mockUser

When("modifyUserInfo is called") {
userModifier.modifyUserInfo(userId, nickName, profileImgUrl, jobSearchStatus, desiredJob, goal, dream)

Then("the user info should be updated and saved") {
verify { mockUserRepository.findById(userId) }
verify { mockUser.update(nickName, profileImgUrl, jobSearchStatus, desiredJob, goal, dream) }
verify { mockUserRepository.save(any()) }
}
}
}
})
Loading

0 comments on commit b7a83fc

Please sign in to comment.