Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write test codes of GifticonService #78

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class GoalGifticonApplicationService(
ExceptionCode.E403_FORBIDDEN,
"접근할 수 없는 기프티콘입니다."
)
val gifticon = gifticonService.loadById(actualGifticonId)
val gifticon = gifticonService.findById(actualGifticonId)

return GifticonResponse(
goalId = goalId,
Expand All @@ -98,7 +98,7 @@ class GoalGifticonApplicationService(
goal = goal,
userEntity = userEntity
) ?: throw BaseException.of(ExceptionCode.E404_NOT_FOUND, "다짐에 등록된 기프티콘을 찾을 수 없습니다.")
val gifticon = gifticonService.loadById(goalGifticon.gifticonId)
val gifticon = gifticonService.findById(goalGifticon.gifticonId)

validateIsRequestUserHasUpdateAuthority(goal, request.userId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GifticonService(
return gifticon.toDto()
}

fun loadById(gifticonId: Long): Gifticon {
fun findById(gifticonId: Long): Gifticon {
return gifticonRepository.findById(gifticonId).orElseThrow(notFoundExceptionSupplier).toDto()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.whatever.raisedragon.domain.gifticon

import com.whatever.raisedragon.IntegrationTestSupport
import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.domain.user.Nickname
import com.whatever.raisedragon.domain.user.UserEntity
import com.whatever.raisedragon.domain.user.UserRepository
import jakarta.transaction.Transactional
import org.assertj.core.api.Assertions.*
import org.assertj.core.api.ObjectAssert
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.repository.findByIdOrNull

@Transactional
class GifticonServiceTest : IntegrationTestSupport {

@Autowired
private lateinit var gifticonService: GifticonService

@Autowired
private lateinit var gifticonRepositry: GifticonRepository

@Autowired
private lateinit var userRepository: UserRepository

@DisplayName("userId와 url를 입력받아 Gifticon을 생성한다.")
@Test
fun create1() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = "url"

// when
val foundGifticon = gifticonService.create(user.id, url)

// then
assertThat(foundGifticon)
.isInstanceOfGifticon()
.extracting("userId", "url")
.contains(user.id, URL(url))
}

@DisplayName("userId와 url를 입력받아 Gifticon을 생성할 때 요청한 userId에 해당하는 유저가 없는 경우 예외가 발생한다.")
@Test
fun create2() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = "url"

// when // then
assertThatThrownBy { gifticonService.create(-1L, url) }
.isInstanceOf(BaseException::class.java)
.hasMessage(ExceptionCode.E404_NOT_FOUND.message)
}

@DisplayName("요청한 id에 해당하는 Gifticon을 조회한다.")
@Test
fun findById1() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = URL("url1")
val gifticon1 = GifticonEntity(userEntity = user, url = url)
gifticonRepositry.save(gifticon1)

// when
val foundGifticon = gifticonService.findById(gifticon1.id)

// then
assertThat(foundGifticon)
.isInstanceOfGifticon()
.extracting("userId", "url")
.contains(user.id, url)
}

@DisplayName("요청한 id에 해당하는 Gifticon을 조회하는 중 해당하는 Gifticon이 없는 경우 예외가 발생한다.")
@Test
fun findById2() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = URL("url1")
val gifticon1 = GifticonEntity(userEntity = user, url = url)
gifticonRepositry.save(gifticon1)

// when // then
assertThatThrownBy { gifticonService.findById(-1L) }
.isInstanceOf(BaseException::class.java)
.hasMessage(ExceptionCode.E404_NOT_FOUND.message)
}

@DisplayName("요청한 userId를 갖는 Gifticon을 hard-delete한다.")
@Test
fun hardDeleteByUserId1() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = URL("url1")
val gifticon1 = GifticonEntity(userEntity = user, url = url)
gifticonRepositry.save(gifticon1)

// when
gifticonService.hardDeleteByUserId(user.id)

// then
assertThat(gifticonRepositry.findByIdOrNull(gifticon1.id))
.isNull()
}

@DisplayName("요청한 userId를 갖는 Gifticon을 hard-delete 하는 중 userId에 해당하는 User가 없으면 예외가 발생한다.")
@Test
fun hardDeleteByUserId2() {
// given
val user = UserEntity(
oauthTokenPayload = null,
fcmTokenPayload = null,
nickname = Nickname("UserNickname1")
)
userRepository.save(user)

val url = URL("url1")
val gifticon1 = GifticonEntity(userEntity = user, url = url)
gifticonRepositry.save(gifticon1)

// when // then
assertThatThrownBy { gifticonService.hardDeleteByUserId(-1L) }
.isInstanceOf(BaseException::class.java)
.hasMessage(ExceptionCode.E404_NOT_FOUND.message)
}

private fun ObjectAssert<*>.isInstanceOfGifticon() = isInstanceOf(Gifticon::class.java)

}