Skip to content

Commit

Permalink
refactor: (#78) DomainService로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Sep 29, 2022
1 parent 8f35587 commit d116bf2
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team.comit.simtong.domain.auth.usecase

import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
import team.comit.simtong.domain.auth.spi.QueryAuthCodePort
import team.comit.simtong.global.annotation.UseCase
Expand All @@ -17,6 +17,7 @@ import team.comit.simtong.global.annotation.UseCase
@UseCase
class CheckAuthCodeUseCase(
private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort,
private val constructAuthCodeLimitService: ConstructAuthCodeLimitService,
private val queryAuthCodePort: QueryAuthCodePort
) {

Expand All @@ -28,12 +29,7 @@ class CheckAuthCodeUseCase(
}

commandAuthCodeLimitPort.save(
AuthCodeLimit(
key = email,
expirationTime = AuthCodeLimit.VERIFIED_EXPIRED,
attemptCount = 0,
isVerified = true
)
constructAuthCodeLimitService.verified(email)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package team.comit.simtong.domain.auth.usecase

import team.comit.simtong.domain.auth.exception.CertifiedEmailException
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService
import team.comit.simtong.domain.auth.service.ConstructAuthCodeService
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
import team.comit.simtong.domain.auth.spi.CommandAuthCodePort
import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort
Expand All @@ -22,20 +22,24 @@ class SendAuthCodeUseCase(
private val commandAuthCodeLimitPort: CommandAuthCodeLimitPort,
private val commandAuthCodePort: CommandAuthCodePort,
private val queryAuthCodeLimitPort: QueryAuthCodeLimitPort,
private val constructAuthCodeLimitService: ConstructAuthCodeLimitService,
private val constructAuthCodeService: ConstructAuthCodeService,
private val sendEmailPort: SendEmailPort
) {

fun execute(email: String) {
val authCodeLimit = queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email)
?: AuthCodeLimit(email)
?: constructAuthCodeLimitService.construct(email)

if(authCodeLimit.isVerified) {
throw CertifiedEmailException.EXCEPTION
}

commandAuthCodeLimitPort.save(authCodeLimit.increaseCount())

val authCode = commandAuthCodePort.save(AuthCode(email))
val authCode = commandAuthCodePort.save(
constructAuthCodeService.construct(email)
)

sendEmailPort.sendAuthCode(authCode.code, email)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package team.comit.simtong.domain.auth.usecase

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.auth.exception.AuthCodeMismatchException
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
import team.comit.simtong.domain.auth.spi.QueryAuthCodePort

Expand All @@ -18,6 +21,9 @@ class CheckAuthCodeUseCaseTests {
@MockBean
private lateinit var queryAuthCodePort: QueryAuthCodePort

@MockBean
private lateinit var constructAuthCodeLimitService: ConstructAuthCodeLimitService

@MockBean
private lateinit var commandAuthCodeLimitPort: CommandAuthCodeLimitPort

Expand All @@ -27,6 +33,15 @@ class CheckAuthCodeUseCaseTests {

private val code = "123456"

private val verifiedAuthCodeLimitStub: AuthCodeLimit by lazy {
AuthCodeLimit(
key = email,
expirationTime = AuthCodeLimit.VERIFIED_EXPIRED,
attemptCount = 0,
isVerified = true
)
}

private val authCodeStub: AuthCode by lazy {
AuthCode(
key = email,
Expand All @@ -45,7 +60,11 @@ class CheckAuthCodeUseCaseTests {

@BeforeEach
fun setUp() {
checkAuthCodeUseCase = CheckAuthCodeUseCase(commandAuthCodeLimitPort, queryAuthCodePort)
checkAuthCodeUseCase = CheckAuthCodeUseCase(
commandAuthCodeLimitPort,
constructAuthCodeLimitService,
queryAuthCodePort
)
}

@Test
Expand All @@ -54,8 +73,13 @@ class CheckAuthCodeUseCaseTests {
given(queryAuthCodePort.queryAuthCodeByEmail(email))
.willReturn(authCodeStub)

// when
checkAuthCodeUseCase.execute(email, code)
given(constructAuthCodeLimitService.verified(email))
.willReturn(verifiedAuthCodeLimitStub)

// when & then
assertDoesNotThrow {
checkAuthCodeUseCase.execute(email, code)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.willDoNothing
import org.mockito.kotlin.any
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import team.comit.simtong.domain.auth.exception.CertifiedEmailException
import team.comit.simtong.domain.auth.exception.ExceededSendAuthCodeRequestException
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.service.ConstructAuthCodeLimitService
import team.comit.simtong.domain.auth.service.ConstructAuthCodeService
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
import team.comit.simtong.domain.auth.spi.CommandAuthCodePort
import team.comit.simtong.domain.auth.spi.QueryAuthCodeLimitPort
Expand All @@ -34,12 +35,27 @@ class SendAuthCodeUseCaseTests {
@MockBean
private lateinit var sendEmailPort: SendEmailPort

@MockBean
private lateinit var constructAuthCodeLimitService: ConstructAuthCodeLimitService

@MockBean
private lateinit var constructAuthCodeService: ConstructAuthCodeService

private lateinit var sendAuthCodeUseCase: SendAuthCodeUseCase

private val email = "[email protected]"

private val code = "123456"

private val authCodeLimitStub by lazy {
AuthCodeLimit(
key = email,
expirationTime = AuthCodeLimit.EXPIRED,
attemptCount = 0,
isVerified = false
)
}

private val verifiedAuthCodeLimitStub by lazy {
AuthCodeLimit(
key = email,
Expand Down Expand Up @@ -72,6 +88,8 @@ class SendAuthCodeUseCaseTests {
commandAuthCodeLimitPort,
commandAuthCodePort,
queryAuthCodeLimitPort,
constructAuthCodeLimitService,
constructAuthCodeService,
sendEmailPort
)
}
Expand All @@ -82,7 +100,13 @@ class SendAuthCodeUseCaseTests {
given(queryAuthCodeLimitPort.queryAuthCodeLimitByEmail(email))
.willReturn(null)

given(commandAuthCodePort.save(any()))
given(constructAuthCodeLimitService.construct(email))
.willReturn(authCodeLimitStub)

given(constructAuthCodeService.construct(email))
.willReturn(authCodeStub)

given(commandAuthCodePort.save(authCodeStub))
.willReturn(authCodeStub)

willDoNothing().given(sendEmailPort).sendAuthCode(code, email)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team.comit.simtong.domain.auth.model

import net.bytebuddy.utility.RandomString
import team.comit.simtong.global.annotation.Aggregate

/**
Expand All @@ -24,10 +23,4 @@ class AuthCode(
const val EXPIRED = 180
}

constructor(email: String) : this(
key = email,
code = RandomString(6).nextString(),
expirationTime = EXPIRED
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class AuthCodeLimit(
const val VERIFIED_EXPIRED = 2700
}

constructor(email: String) : this(
key = email,
expirationTime = EXPIRED,
attemptCount = 0,
isVerified = false
)

fun increaseCount(): AuthCodeLimit {
if (attemptCount >= MAX_ATTEMPT_COUNT) {
throw ExceededSendAuthCodeRequestException.EXCEPTION
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package team.comit.simtong.domain.auth.service

import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.global.annotation.DomainService

/**
*
* AuthCodeLimit을 생성하는 ConstructAuthCodeLimitService
*
* @author Chokyunghyeon
* @date 2022/09/29
* @version 1.0.0
**/
@DomainService
class ConstructAuthCodeLimitService {

fun construct(email: String): AuthCodeLimit {
return AuthCodeLimit(
key = email,
expirationTime = AuthCodeLimit.EXPIRED,
attemptCount = 0,
isVerified = false
)
}

fun verified(email: String): AuthCodeLimit {
return AuthCodeLimit(
key = email,
expirationTime = AuthCodeLimit.VERIFIED_EXPIRED,
attemptCount = AuthCodeLimit.MAX_ATTEMPT_COUNT,
isVerified = true
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package team.comit.simtong.domain.auth.service

import net.bytebuddy.utility.RandomString
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.global.annotation.DomainService

/**
*
* AuthCode를 생성하는 ConstructAuthCodeService
*
* @author Chokyunghyeon
* @date 2022/09/29
* @version 1.0.0
**/
@DomainService
class ConstructAuthCodeService {

fun construct(email: String): AuthCode {
return AuthCode(
key = email,
code = RandomString(6).nextString(),
expirationTime = AuthCode.EXPIRED
)
}

}

0 comments on commit d116bf2

Please sign in to comment.