Skip to content

Commit

Permalink
Merge pull request #76 from whatever-mentoring/feature/service-test
Browse files Browse the repository at this point in the history
Write test code of GoalService
  • Loading branch information
mkSpace authored Jan 17, 2024
2 parents 20381b0 + cbdc90c commit bd2be06
Show file tree
Hide file tree
Showing 32 changed files with 820 additions and 187 deletions.
1 change: 1 addition & 0 deletions raisedragon-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tasks.jar {
val swaggerVersion: String by project.extra

dependencies {
implementation(project(":raisedragon-common"))
implementation(project(":raisedragon-core"))
implementation(project(":raisedragon-external"))
api("org.springframework.boot:spring-boot-starter-data-jpa:3.0.4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BettingApplicationService(

private fun Betting.validateStartDate() {
val now = LocalDateTime.now()
val goalStartDate = goalService.loadById(goalId).startDate
val goalStartDate = goalService.findById(goalId).startDate
if (now.toLocalDate() >= goalStartDate.toLocalDate()) {
throw BaseException.of(ExceptionCode.E400_BAD_REQUEST, "이미 시작한 내기 입니다.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.whatever.raisedragon.domain.goal.*
import com.whatever.raisedragon.domain.goalgifticon.GoalGifticonService
import com.whatever.raisedragon.domain.goalproof.GoalProofService
import com.whatever.raisedragon.domain.user.UserService
import com.whatever.raisedragon.domain.user.fromDto
import com.whatever.raisedragon.domain.winner.WinnerService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -33,7 +32,7 @@ class GoalApplicationService(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 생성할 수 있는 다짐 갯수를 초과하였습니다."
)
if (goalService.existsByUserIdAndAnyResult(request.userId, GoalResult.PROCEEDING)) throw BaseException.of(
if (goalService.existsByUserIdAndAnyGoalResult(request.userId, GoalResult.PROCEEDING)) throw BaseException.of(
exceptionCode = ExceptionCode.E409_CONFLICT,
executionMessage = "다짐을 생성하는 중, 이미 생성한 다짐이 있어 생성이 불가합니다."
)
Expand All @@ -58,13 +57,13 @@ class GoalApplicationService(
}

fun retrieveGoal(goalId: Long): GoalResponse {
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
return GoalResponse.of(goal, hostUser.nickname.value)
}

fun retrieveGoalDetail(goalId: Long, userId: Long): GoalWithBettingResponse {
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
val betting = bettingService.loadUserAndGoal(userId, goalId)
val goalProofs = goalProofService.findAllByGoalIdAndUserId(goalId, userId)
Expand All @@ -80,7 +79,7 @@ class GoalApplicationService(
}

fun retrieveAllByUserId(userId: Long): List<GoalResponse> {
val goals = goalService.loadAllByUserId(userId)
val goals = goalService.findAllByUserId(userId)
val users = userService.findAllByIdInIds(goals.map { it.userId }.toSet())

return goals.map { goal ->
Expand All @@ -95,9 +94,9 @@ class GoalApplicationService(
fun retrieveGoalBettingParticipant(
goalId: Long
): GoalRetrieveParticipantResponse {
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
val hostUser = userService.loadById(goal.userId)
val bettingList = bettingService.loadAllByGoalId(goalId)
val bettingList = bettingService.findAllByGoalId(goalId)

val hostDto = GoalBettingHostResponse(
id = hostUser.id!!,
Expand Down Expand Up @@ -125,9 +124,9 @@ class GoalApplicationService(
fun retrieveGoalBettingParticipantNoAuth(
goalId: Long
): GoalRetrieveParticipantResponse {
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
val goalHostUser = userService.loadById(goal.userId)
val bettingList = bettingService.loadAllByGoalId(goalId)
val bettingList = bettingService.findAllByGoalId(goalId)

val hostDto = GoalBettingHostResponse(
id = goalHostUser.id!!,
Expand Down Expand Up @@ -178,13 +177,12 @@ class GoalApplicationService(

@Transactional
fun modifyGoal(request: GoalModifyServiceRequest): GoalResponse {
val goal = goalService.loadById(request.goalId)
val goal = goalService.findById(request.goalId)
isNotUsersGoal(goal, request.userId)
isAlreadyStarted(goal)

val modifiedGoal = goalService.modify(
goal = goal,
userEntity = userService.loadById(request.userId).fromDto(),
val modifiedGoal = goalService.updateContent(
goalId = goal.id,
content = request.content
)

Expand All @@ -194,14 +192,11 @@ class GoalApplicationService(

@Transactional
fun deleteGoal(request: GoalDeleteServiceRequest) {
val goal = goalService.loadById(request.goalId)
val goal = goalService.findById(request.goalId)
isNotUsersGoal(goal, request.userId)
isAlreadyStarted(goal)

goalService.softDelete(
goal = goal,
userEntity = userService.loadById(request.userId).fromDto(),
)
goalService.softDelete(goal.id)
}

private fun isNotUsersGoal(goal: Goal, userId: Long) {
Expand All @@ -223,6 +218,6 @@ class GoalApplicationService(
}

private fun isNumberOfGoalUnderOneHundred(userId: Long): Boolean {
return goalService.loadAllByUserId(userId).size > 99
return goalService.findAllByUserId(userId).size > 99
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GoalGifticonApplicationService(

@Transactional
fun createAndUploadGifticon(request: GoalGifticonCreateServiceRequest): GoalGifticonResponse {
val goal = goalService.loadById(request.goalId)
val goal = goalService.findById(request.goalId)
if (isNotBettingTypeBilling(goal.type)) throw BaseException.of(
exceptionCode = ExceptionCode.E400_BAD_REQUEST,
executionMessage = "기프티콘을 업르도하는 중, 무료 다짐에는 기프티콘을 업로드할 수 없습니다."
Expand Down Expand Up @@ -65,7 +65,7 @@ class GoalGifticonApplicationService(
userId: Long
): GifticonResponse {
val user = userService.loadById(userId)
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
isBrokenTiming(goal)

val goalGifticon = if (userId != goal.userId) {
Expand Down Expand Up @@ -93,7 +93,7 @@ class GoalGifticonApplicationService(
@Transactional
fun updateGifticonURLByGoalId(request: GoalGifticonUpdateServiceRequest): GoalGifticonResponse {
val userEntity = userService.loadById(request.userId).fromDto()
val goal = goalService.loadById(request.goalId).fromDto(userEntity).toDto()
val goal = goalService.findById(request.goalId).fromDto(userEntity).toDto()
val goalGifticon = goalGifticonService.loadByGoalAndUserEntity(
goal = goal,
userEntity = userEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import com.whatever.raisedragon.domain.goalproof.Comment
import com.whatever.raisedragon.domain.goalproof.GoalProof
import com.whatever.raisedragon.domain.goalproof.GoalProofService
import com.whatever.raisedragon.domain.user.UserService
import com.whatever.raisedragon.domain.user.fromDto
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import kotlin.math.abs

@Service
@Transactional(readOnly = true)
Expand All @@ -27,7 +25,7 @@ class GoalProofApplicationService(

@Transactional
fun create(request: GoalProofCreateServiceRequest): GoalProofCreateUpdateResponse {
val goal = goalService.loadById(request.goalId)
val goal = goalService.findById(request.goalId)
val user = userService.loadById(request.userId)

isGoalProofAlreadyExists(request.goalId)
Expand All @@ -39,7 +37,7 @@ class GoalProofApplicationService(
url = URL(request.url),
comment = request.comment
)
goalService.increaseThreshold(goal, user.fromDto())
goalService.increaseThreshold(goal.id)
return GoalProofCreateUpdateResponse(GoalProofRetrieveResponse.of(goalProof))
}

Expand All @@ -63,7 +61,7 @@ class GoalProofApplicationService(

fun retrieveAll(goalId: Long, userId: Long): GoalProofListRetrieveResponse {
val goalProofs = goalProofService.findAllByGoalIdAndUserId(goalId, userId)
val goalStartDateTime = goalService.loadById(goalId).startDate
val goalStartDateTime = goalService.findById(goalId).startDate

val progressDays = goalProofs.map {
it.createdAt!!.dayOfMonth.minus(goalStartDateTime.dayOfMonth) + 1
Expand Down Expand Up @@ -126,7 +124,7 @@ class GoalProofApplicationService(
}

private fun GoalProof.validateEndDate() {
if (goalService.loadById(goalId).endDate > LocalDateTime.now()) {
if (goalService.findById(goalId).endDate > LocalDateTime.now()) {
throw BaseException.of(ExceptionCode.E400_BAD_REQUEST, "이미 끝난 내기입니다")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class TestApplicationService(

@Transactional
fun confirmGoalResult(goalId: Long) {
val goal = goalService.loadById(goalId)
val goal = goalService.findById(goalId)
val goalProofCount = goalProofService.countAllByGoalId(goal.id)
// TODO: using 7 instead of goal's threshold. must be changed to goal's threshold after confirming business requirements
val goalResult = if (goalProofCount >= 7) GoalResult.SUCCESS else GoalResult.FAIL
goalService.updateResult(goal.id, goalResult)

val bettingList = bettingService.findAllByGoalIdAndNotDeleted(goal.id)
val bettingList = bettingService.findAllByGoalId(goal.id)
val failedBettingList = mutableListOf<Betting>()
val succeedBettingList = mutableListOf<Betting>()
bettingList.forEach { betting ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class UserApplicationService(

@Transactional
fun delete(id: Long) {
val user = userService.loadById(id)
if (goalService.findProceedingGoalIsExistsByUser(user)) {
if (goalService.existsByUserAndEndDateIsAfterThanNow(id)) {
throw BaseException.of(
exceptionCode = ExceptionCode.E400_BAD_REQUEST,
executionMessage = "아직 진행중인 다짐이 있어 회원탈퇴에 실패했습니다."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.context.request.WebRequest
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException

@RestControllerAdvice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GoalResultScheduler(
val now = LocalDateTime.now()
logger.info("Start adjusting result of goals at {}", now)
val endDate = LocalDateTime.of(now.year, now.month, now.dayOfMonth, 23, 59, 59, 59)
val betGoalList = goalService.findAllByEndDateLessThanEqualAndResultIsProceeding(endDate)
val betGoalList = goalService.findAllByEndDateLessThanEqualAndGoalResultIsProceeding(endDate)
betGoalList.forEach(::confirmGoalResult)
logger.info("Done adjusting result for {} goals, at {}", betGoalList.size, LocalDateTime.now())
}
Expand All @@ -45,7 +45,7 @@ class GoalResultScheduler(
val goalResult = if (goalProofCount >= 7) GoalResult.SUCCESS else GoalResult.FAIL
goalService.updateResult(goal.id, goalResult)

val bettingList = bettingService.findAllByGoalIdAndNotDeleted(goal.id)
val bettingList = bettingService.findAllByGoalId(goal.id)
val failedBettingList = mutableListOf<Betting>()
val succeedBettingList = mutableListOf<Betting>()
bettingList.forEach { betting ->
Expand Down
2 changes: 2 additions & 0 deletions raisedragon-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.whatever.raisedragon.common.exception

import com.whatever.raisedragon.common.ErrorResponse
import org.springframework.http.HttpStatus

enum class ExceptionCode(
Expand Down Expand Up @@ -38,11 +37,4 @@ fun ExceptionCode.throwAsException() {
exceptionCode = this,
executionMessage = message
)
}

fun ExceptionCode.toErrorResponse(): ErrorResponse {
return ErrorResponse(
code = code,
detailMessage = message
)
}
1 change: 1 addition & 0 deletions raisedragon-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dependencies {
implementation(project(":raisedragon-common"))
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.0.4")
runtimeOnly("com.mysql:mysql-connector-j:8.0.32")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.whatever.raisedragon.common.config

import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.function.Supplier

@Configuration
class ExceptionSuppliers {

@Bean
@Qualifier("notFoundExceptionSupplier")
fun notFoundExceptionSupplier(): Supplier<BaseException> {
return Supplier<BaseException> { BaseException.of(ExceptionCode.E404_NOT_FOUND) }
}

@Bean
@Qualifier("InternalExceptionSupplier")
fun internalExceptionSupplier(): Supplier<BaseException> {
return Supplier<BaseException> { BaseException.of(ExceptionCode.E500_INTERNAL_SERVER_ERROR) }
}
}
Loading

0 comments on commit bd2be06

Please sign in to comment.