diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/betting/BettingApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/betting/BettingApplicationService.kt index 18fa16a..3e03ae0 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/betting/BettingApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/betting/BettingApplicationService.kt @@ -55,7 +55,8 @@ class BettingApplicationService( val betting = findByIdOrThrowException(request.bettingId) betting.validateOwnerId(request.userId) betting.validateStartDate() - return BettingRetrieveResponse.of(bettingService.updatePredictionType(request.bettingId, request.bettingPredictionType)) + return BettingRetrieveResponse.of(bettingService.updatePredictionType(request.bettingId, + request.bettingPredictionType)) } @Transactional diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goal/GoalApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goal/GoalApplicationService.kt index 3071225..af1981f 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goal/GoalApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goal/GoalApplicationService.kt @@ -48,8 +48,7 @@ class GoalApplicationService( val gifticon = gifticonService.create(request.userId, request.gifticonUrl) goalGifticonService.create( goalId = goal.id, - gifticonId = gifticon.id, - userId = request.userId + gifticonId = gifticon.id ) } val hostUser = userService.loadById(request.userId) diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goalgifticon/GoalGifticonApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goalgifticon/GoalGifticonApplicationService.kt index c4daddf..981f631 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goalgifticon/GoalGifticonApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/goalgifticon/GoalGifticonApplicationService.kt @@ -45,12 +45,10 @@ class GoalGifticonApplicationService( executionMessage = "기프티콘을 업로드하는 중, 이미 다짐 수행이 시작되어 업로드할 수 없습니다." ) - val gifticon = gifticonService.create(request.userId, request.uploadedURL) val goalGifticon = goalGifticonService.create( goalId = request.goalId, - gifticonId = gifticon.id, - userId = request.userId + gifticonId = gifticon.id ) return GoalGifticonResponse( goalGifticonId = goalGifticon.id, @@ -64,17 +62,13 @@ class GoalGifticonApplicationService( goalId: Long, userId: Long ): GifticonResponse { - val user = userService.loadById(userId) val goal = goalService.findById(goalId) isBrokenTiming(goal) val goalGifticon = if (userId != goal.userId) { null } else { - goalGifticonService.loadByGoalAndUserEntity( - goal = goal, - userEntity = user.fromDto() - ) + goalGifticonService.findByGoalId(goal.id) } val winnerGifticonId = winnerService.findByGoalIdAndUserId(goalId, userId)?.gifticonId val actualGifticonId = goalGifticon?.gifticonId ?: winnerGifticonId ?: throw BaseException.of( @@ -94,10 +88,8 @@ class GoalGifticonApplicationService( fun updateGifticonURLByGoalId(request: GoalGifticonUpdateServiceRequest): GoalGifticonResponse { val userEntity = userService.loadById(request.userId).fromDto() val goal = goalService.findById(request.goalId).fromDto(userEntity).toDto() - val goalGifticon = goalGifticonService.loadByGoalAndUserEntity( - goal = goal, - userEntity = userEntity - ) ?: throw BaseException.of(ExceptionCode.E404_NOT_FOUND, "다짐에 등록된 기프티콘을 찾을 수 없습니다.") + val goalGifticon = goalGifticonService.findByGoalId(goal.id) + ?: throw BaseException.of(ExceptionCode.E404_NOT_FOUND, "다짐에 등록된 기프티콘을 찾을 수 없습니다.") val gifticon = gifticonService.findById(goalGifticon.gifticonId) validateIsRequestUserHasUpdateAuthority(goal, request.userId) diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/test/TestApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/test/TestApplicationService.kt index deb7c21..835ee79 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/test/TestApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/test/TestApplicationService.kt @@ -60,7 +60,10 @@ class TestApplicationService( loserBettingList.map { betting -> betting.id }.toSet(), com.whatever.raisedragon.domain.betting.BettingResult.NO_GIFTICON ) - bettingService.updateResult(winnerBetting.id, com.whatever.raisedragon.domain.betting.BettingResult.GET_GIFTICON) + bettingService.updateResult( + bettingId = winnerBetting.id, + bettingResult = com.whatever.raisedragon.domain.betting.BettingResult.GET_GIFTICON + ) winnerService.create( goalId = goal.id, userId = winnerBetting.userId, diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/user/UserApplicationService.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/user/UserApplicationService.kt index 795fd65..8531e11 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/user/UserApplicationService.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/applicationservice/user/UserApplicationService.kt @@ -52,8 +52,7 @@ class UserApplicationService( exceptionCode = ExceptionCode.E400_BAD_REQUEST, executionMessage = "아직 진행중인 다짐이 있어 회원탈퇴에 실패했습니다." ) - } - else if (bettingService.existsBettingParticipantUser(id)) { + } else if (bettingService.existsBettingParticipantUser(id)) { throw BaseException.of( exceptionCode = ExceptionCode.E400_BAD_REQUEST, executionMessage = "아직 진행중인 다짐에 대한 내기가 있어 회원탈퇴에 실패했습니다." diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/common/Response.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/common/Response.kt index 712f3ad..88f313e 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/common/Response.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/common/Response.kt @@ -2,7 +2,6 @@ package com.whatever.raisedragon.common import com.whatever.raisedragon.common.exception.BaseException import com.whatever.raisedragon.common.exception.ExceptionCode -import org.springframework.http.HttpStatus data class Response( val isSuccess: Boolean, diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/goalproof/GoalProofDto.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/goalproof/GoalProofDto.kt index faa1350..7b175c6 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/goalproof/GoalProofDto.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/goalproof/GoalProofDto.kt @@ -35,10 +35,12 @@ data class GoalProofUpdateRequest( val comment: String? = null ) -fun GoalProofUpdateRequest.toServiceRequest(userId: Long, goalProofId: Long): GoalProofUpdateServiceRequest = - GoalProofUpdateServiceRequest( - userId = userId, - goalProofId = goalProofId, - url = url, - comment = comment?.let { Comment(it) } +fun GoalProofUpdateRequest.toServiceRequest( + userId: Long, + goalProofId: Long +): GoalProofUpdateServiceRequest = GoalProofUpdateServiceRequest( + userId = userId, + goalProofId = goalProofId, + url = url, + comment = comment?.let { Comment(it) } ) \ No newline at end of file diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/jwt/JwtAgentImpl.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/jwt/JwtAgentImpl.kt index df83b14..ac00403 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/jwt/JwtAgentImpl.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/jwt/JwtAgentImpl.kt @@ -97,7 +97,8 @@ class JwtAgentImpl( companion object { const val BEARER_PREFIX = "Bearer" -// private const val ACCESS_TOKEN_EXPIRE_TIME = (1000 * 60 * 30).toLong() + + // private const val ACCESS_TOKEN_EXPIRE_TIME = (1000 * 60 * 30).toLong() private const val ACCESS_TOKEN_EXPIRE_TIME = (1000 * 60 * 60 * 24 * 7).toLong() // 임시 JWT 만료기간 private const val REFRESH_TOKEN_EXPIRE_TIME = (1000 * 60 * 60 * 24 * 7).toLong() private const val CLAIM_INFO_KEY = "claim-info" diff --git a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/resolver/UserInfoArgumentResolver.kt b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/resolver/UserInfoArgumentResolver.kt index 3038e73..ae3e885 100644 --- a/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/resolver/UserInfoArgumentResolver.kt +++ b/raisedragon-api/src/main/kotlin/com/whatever/raisedragon/security/resolver/UserInfoArgumentResolver.kt @@ -15,7 +15,7 @@ class UserInfoArgumentResolver : HandlerMethodArgumentResolver { override fun supportsParameter(parameter: MethodParameter): Boolean { return parameter.hasParameterAnnotation(GetAuth::class.java) - && parameter.parameter.type == UserInfo::class.java + && parameter.parameter.type == UserInfo::class.java } override fun resolveArgument( diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonService.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonService.kt index 6ef6380..b458098 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonService.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonService.kt @@ -2,10 +2,7 @@ package com.whatever.raisedragon.domain.goalgifticon import com.whatever.raisedragon.common.exception.BaseException import com.whatever.raisedragon.domain.gifticon.GifticonRepository -import com.whatever.raisedragon.domain.goal.Goal import com.whatever.raisedragon.domain.goal.GoalRepository -import com.whatever.raisedragon.domain.goal.fromDto -import com.whatever.raisedragon.domain.user.UserEntity import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -23,8 +20,7 @@ class GoalGifticonService( @Transactional fun create( goalId: Long, - gifticonId: Long, - userId: Long, + gifticonId: Long ): GoalGifticon { val goalGifticon = goalGifticonRepository.save( GoalGifticonEntity( @@ -35,18 +31,6 @@ class GoalGifticonService( return goalGifticon.toDto() } - fun loadById(id: Long): GoalGifticon { - return goalGifticonRepository.findById(id).orElseThrow(notFoundExceptionSupplier).toDto() - } - - fun loadByGoalAndUserEntity( - goal: Goal, - userEntity: UserEntity - ): GoalGifticon? { - return goalGifticonRepository.findByGoalEntity(goal.fromDto(userEntity))?.toDto() - ?: throw notFoundExceptionSupplier.get() - } - fun findByGoalId(goalId: Long): GoalGifticon? { val goalEntity = goalRepository.findById(goalId).orElseThrow(notFoundExceptionSupplier) return goalGifticonRepository.findByGoalEntity(goalEntity)?.toDto() diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/RandomWordsNickname.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/RandomWordsNickname.kt index 85e293f..33a4cb6 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/RandomWordsNickname.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/user/RandomWordsNickname.kt @@ -3,31 +3,31 @@ package com.whatever.raisedragon.domain.user object RandomWordsNickname { internal val adjectives = listOf( - "반장인","청순한","선물사는","미소짓는","달콤한","밤샘하는 ","슬기로운","인기많은","노래하는","귀여운", - "요가하는","라식한","용감한","공부하는","서핑하는","지혜로운","당돌한","산책하는","거울보는","작곡하는", - "돈많은","코딩하는","소원비는","사연있는","결혼한","씨름하는 ","여행하는","당찬","캠핑하는","팀플하는", - "한약먹는","깔끔한","휴가간","배부른","비행중인","기분좋은 ","조깅하는","회의하는","양치하는","고기굽는", - "휴가나온","행복한","알바하는","늦잠잔","면도하는","칼퇴한","요리하는","수줍은","이국적인","순박한", - "상큼한","똑똑한","명랑한","발랄한","활발한","친절한","부지런한","바른","배짱있는","밝은","빼어난", - "부드러운","반듯한","친숙한","차분한","침착한","청초한","착한","쾌활한","편안한","포근한","활달한", - "화끈한","화사한","훌륭한","털털한","튼튼한","강직한","고운","기특한","깜찍한","관대한","다정한", - "당당한","든든한","또렷한","단호한","대담한","놀라운","넉넉한","느긋한","맑은","멋진","어여쁜", - "우아한","위대한","순한","수려한","산뜻한","순수한","섬세한","상냥한","소탈한","기운찬","공손한", - "검소한","겸손한","꾸준한","고요한","솔직한","신중한","싹싹한","지적인","정직한","조용한","재밌는", - "존귀한","성실한","순진한","원만한","유능한","유쾌한", + "반장인", "청순한", "선물사는", "미소짓는", "달콤한", "밤샘하는 ", "슬기로운", "인기많은", "노래하는", "귀여운", + "요가하는", "라식한", "용감한", "공부하는", "서핑하는", "지혜로운", "당돌한", "산책하는", "거울보는", "작곡하는", + "돈많은", "코딩하는", "소원비는", "사연있는", "결혼한", "씨름하는 ", "여행하는", "당찬", "캠핑하는", "팀플하는", + "한약먹는", "깔끔한", "휴가간", "배부른", "비행중인", "기분좋은 ", "조깅하는", "회의하는", "양치하는", "고기굽는", + "휴가나온", "행복한", "알바하는", "늦잠잔", "면도하는", "칼퇴한", "요리하는", "수줍은", "이국적인", "순박한", + "상큼한", "똑똑한", "명랑한", "발랄한", "활발한", "친절한", "부지런한", "바른", "배짱있는", "밝은", "빼어난", + "부드러운", "반듯한", "친숙한", "차분한", "침착한", "청초한", "착한", "쾌활한", "편안한", "포근한", "활달한", + "화끈한", "화사한", "훌륭한", "털털한", "튼튼한", "강직한", "고운", "기특한", "깜찍한", "관대한", "다정한", + "당당한", "든든한", "또렷한", "단호한", "대담한", "놀라운", "넉넉한", "느긋한", "맑은", "멋진", "어여쁜", + "우아한", "위대한", "순한", "수려한", "산뜻한", "순수한", "섬세한", "상냥한", "소탈한", "기운찬", "공손한", + "검소한", "겸손한", "꾸준한", "고요한", "솔직한", "신중한", "싹싹한", "지적인", "정직한", "조용한", "재밌는", + "존귀한", "성실한", "순진한", "원만한", "유능한", "유쾌한", ) internal val nouns = listOf( - "고양이","강아지","거북이","토끼","사자","호랑이","표범","기린","코끼리","코뿔소","하마","악어", - "펭귄","부엉이","올빼미 ","고릴라","침팬지","원숭이","코알라","캥거루","앵무새","판다","개구리", - "너구리","두더지","당나귀","수달","북극곰","미어캣","기러기","비둘기","돌고래","알파카","다람쥐", - "오소리","청설모","여우","사슴","낙타","비버","치타","문어","생쥐","병아리","물고기","햄스터", - "독수리","도롱뇽","해태","유니콘","그리핀","봉황","용","드래곤","방울새","파랑새","봉황새","코요테", - "퓨마","벨루가","고라니","노루","스컹크","암탉","얼룩말","족제비","칠면조","타조","염소","수탉", - "물소","자라","오징어","닭","소","개미","거머리","영양","맘모스","고래","나방","박쥐","올빼미", - "말","나비","칼리브","사향소","브롱혼","순록","양","맥","돼지","무스","엘크","와피티","가젤","곰", - "팬더","상어","도마뱀","쥐","늑대","이리","담비","매","솔개","재규어","왈라비","리드벅","리스트", - "리카온","해파리","지렁이","진드기","진딧물","고니","큰고니","살쾡이","공룡","재칼","밍크","비비", - "호저","문차크","에란드","몽구스","파리","강치","누","모기","라이거","오카피", + "고양이", "강아지", "거북이", "토끼", "사자", "호랑이", "표범", "기린", "코끼리", "코뿔소", "하마", "악어", + "펭귄", "부엉이", "올빼미 ", "고릴라", "침팬지", "원숭이", "코알라", "캥거루", "앵무새", "판다", "개구리", + "너구리", "두더지", "당나귀", "수달", "북극곰", "미어캣", "기러기", "비둘기", "돌고래", "알파카", "다람쥐", + "오소리", "청설모", "여우", "사슴", "낙타", "비버", "치타", "문어", "생쥐", "병아리", "물고기", "햄스터", + "독수리", "도롱뇽", "해태", "유니콘", "그리핀", "봉황", "용", "드래곤", "방울새", "파랑새", "봉황새", "코요테", + "퓨마", "벨루가", "고라니", "노루", "스컹크", "암탉", "얼룩말", "족제비", "칠면조", "타조", "염소", "수탉", + "물소", "자라", "오징어", "닭", "소", "개미", "거머리", "영양", "맘모스", "고래", "나방", "박쥐", "올빼미", + "말", "나비", "칼리브", "사향소", "브롱혼", "순록", "양", "맥", "돼지", "무스", "엘크", "와피티", "가젤", "곰", + "팬더", "상어", "도마뱀", "쥐", "늑대", "이리", "담비", "매", "솔개", "재규어", "왈라비", "리드벅", "리스트", + "리카온", "해파리", "지렁이", "진드기", "진딧물", "고니", "큰고니", "살쾡이", "공룡", "재칼", "밍크", "비비", + "호저", "문차크", "에란드", "몽구스", "파리", "강치", "누", "모기", "라이거", "오카피", ) } diff --git a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/winner/WinnerEntity.kt b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/winner/WinnerEntity.kt index 76d5c9e..55f8e2c 100644 --- a/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/winner/WinnerEntity.kt +++ b/raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/winner/WinnerEntity.kt @@ -10,7 +10,7 @@ import org.hibernate.annotations.SQLRestriction @Table(name = "winner") @Entity @SQLRestriction("deleted_at IS NULL") -class WinnerEntity ( +class WinnerEntity( @OneToOne @JoinColumn(name = "goal_id") val goalEntity: GoalEntity, @@ -26,4 +26,4 @@ class WinnerEntity ( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = 0L -): BaseEntity() \ No newline at end of file +) : BaseEntity() \ No newline at end of file diff --git a/raisedragon-core/src/test/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonServiceTest.kt b/raisedragon-core/src/test/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonServiceTest.kt new file mode 100644 index 0000000..4eac19f --- /dev/null +++ b/raisedragon-core/src/test/kotlin/com/whatever/raisedragon/domain/goalgifticon/GoalGifticonServiceTest.kt @@ -0,0 +1,228 @@ +package com.whatever.raisedragon.domain.goalgifticon + +import com.whatever.raisedragon.IntegrationTestSupport +import com.whatever.raisedragon.common.exception.BaseException +import com.whatever.raisedragon.common.exception.ExceptionCode +import com.whatever.raisedragon.domain.gifticon.GifticonEntity +import com.whatever.raisedragon.domain.gifticon.GifticonRepository +import com.whatever.raisedragon.domain.gifticon.URL +import com.whatever.raisedragon.domain.goal.* +import com.whatever.raisedragon.domain.goal.GoalResult.* +import com.whatever.raisedragon.domain.goal.GoalType.* +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.Assertions.* +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import java.time.LocalDateTime + +@Transactional +class GoalGifticonServiceTest : IntegrationTestSupport { + + @Autowired + private lateinit var goalGifticonService: GoalGifticonService + + @Autowired + private lateinit var userRepository: UserRepository + + @Autowired + private lateinit var goalGifticonRepository: GoalGifticonRepository + + @Autowired + private lateinit var goalRepository: GoalRepository + + @Autowired + private lateinit var gifticonRepository: GifticonRepository + + @DisplayName("goalId, gifticonId, userId를 입력받아 GoalGifticon을 생성한다.") + @Test + fun create1() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goal = createGoalEntity(user, BILLING, SUCCESS) + goalRepository.save(goal) + + val gifticon = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.save(gifticon) + + // when + val foundGoalGifticon = goalGifticonService.create(goalId = goal.id, gifticonId = gifticon.id) + + // then + assertThat(foundGoalGifticon) + .isInstanceOfGoalGifticon() + .extracting("goalId", "gifticonId") + .contains(goal.id, gifticon.id) + } + + @DisplayName("goalId, gifticonId를 입력받아 GoalGifticon을 생성하는 중 요청한 goalId에 해당하는 Goal이 없으면 예외가 발생한다.") + @Test + fun create2() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goal = createGoalEntity(user, BILLING, SUCCESS) + goalRepository.save(goal) + + val gifticon = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.save(gifticon) + + // when // then + assertThatThrownBy { goalGifticonService.create(goalId = -1L, gifticonId = gifticon.id) } + .isInstanceOf(BaseException::class.java) + .hasMessage(ExceptionCode.E404_NOT_FOUND.message) + } + + @DisplayName("goalId, gifticonId를 입력받아 GoalGifticon을 생성하는 중 요청한 gifticonId 해당하는 Gifticon 없으면 예외가 발생한다.") + @Test + fun create3() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goal = createGoalEntity(user, BILLING, SUCCESS) + goalRepository.save(goal) + + val gifticon = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.save(gifticon) + + // when // then + assertThatThrownBy { goalGifticonService.create(goalId = goal.id, gifticonId = -1L) } + .isInstanceOf(BaseException::class.java) + .hasMessage(ExceptionCode.E404_NOT_FOUND.message) + } + + @DisplayName("goalId에 해당하는 GoalGifticon을 조회한다.") + @Test + fun findByGoalId1() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goalEntity1 = createGoalEntity(user, BILLING, SUCCESS) + val goalEntity2 = createGoalEntity(user, FREE, FAIL) + goalRepository.saveAll(listOf(goalEntity1, goalEntity2)) + + val gifticon1 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon2 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon3 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon4 = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.saveAll(listOf(gifticon1, gifticon2, gifticon3, gifticon4)) + + val goalGifticonEntity1 = GoalGifticonEntity(goalEntity = goalEntity1, gifticonEntity = gifticon1) + val goalGifticonEntity2 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon2) + val goalGifticonEntity3 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon3) + val goalGifticonEntity4 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon4) + goalGifticonRepository.saveAll( + listOf( + goalGifticonEntity1, + goalGifticonEntity2, + goalGifticonEntity3, + goalGifticonEntity4) + ) + // when + val foundGoalGifticon = goalGifticonService.findByGoalId(goalEntity1.id) + + // then + assertThat(foundGoalGifticon) + .isInstanceOfGoalGifticon() + .extracting("goalId", "gifticonId") + .contains(goalEntity1.id, gifticon1.id) + } + + @DisplayName("goalId에 해당하는 GoalGifticon이 없으면 null을 반환한다.") + @Test + fun findByGoalId2() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goalEntity1 = createGoalEntity(user, BILLING, SUCCESS) + val goalEntity2 = createGoalEntity(user, FREE, FAIL) + goalRepository.saveAll(listOf(goalEntity1, goalEntity2)) + + val gifticon1 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon2 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon3 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon4 = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.saveAll(listOf(gifticon1, gifticon2, gifticon3, gifticon4)) + + val goalGifticonEntity1 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon1) + val goalGifticonEntity2 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon2) + val goalGifticonEntity3 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon3) + val goalGifticonEntity4 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon4) + goalGifticonRepository.saveAll( + listOf( + goalGifticonEntity1, + goalGifticonEntity2, + goalGifticonEntity3, + goalGifticonEntity4) + ) + // when + val foundGoalGifticon = goalGifticonService.findByGoalId(goalEntity1.id) + + // then + assertThat(foundGoalGifticon).isNull() + } + + @DisplayName("goalId에 해당하는 GoalGifticon을 조회하는 중 goalId에 해당하는 Goal이 없는 경우 예외가 발생한다.") + @Test + fun findByGoalId3() { + // given + val user = UserEntity(nickname = Nickname("user")) + userRepository.save(user) + + val goalEntity1 = createGoalEntity(user, BILLING, SUCCESS) + val goalEntity2 = createGoalEntity(user, FREE, FAIL) + goalRepository.saveAll(listOf(goalEntity1, goalEntity2)) + + val gifticon1 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon2 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon3 = GifticonEntity(userEntity = user, url = URL("url")) + val gifticon4 = GifticonEntity(userEntity = user, url = URL("url")) + gifticonRepository.saveAll(listOf(gifticon1, gifticon2, gifticon3, gifticon4)) + + val goalGifticonEntity1 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon1) + val goalGifticonEntity2 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon2) + val goalGifticonEntity3 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon3) + val goalGifticonEntity4 = GoalGifticonEntity(goalEntity = goalEntity2, gifticonEntity = gifticon4) + goalGifticonRepository.saveAll( + listOf( + goalGifticonEntity1, + goalGifticonEntity2, + goalGifticonEntity3, + goalGifticonEntity4) + ) + // when // then + assertThatThrownBy { goalGifticonService.findByGoalId(-1L) } + .isInstanceOf(BaseException::class.java) + .hasMessage(ExceptionCode.E404_NOT_FOUND.message) + } + + private fun ObjectAssert<*>.isInstanceOfGoalGifticon() = isInstanceOf(GoalGifticon::class.java) + + private fun createGoalEntity( + userEntity: UserEntity, + goalType: GoalType, + goalResult: GoalResult, + startDateTime: LocalDateTime = LocalDateTime.now(), + endDateTime: LocalDateTime = startDateTime.plusDays(7) + ): GoalEntity { + return GoalEntity( + userEntity = userEntity, + goalType = goalType, + content = Content("sampleContent"), + goalResult = goalResult, + startDate = startDateTime, + endDate = endDateTime + ) + } +} \ No newline at end of file