Skip to content

Commit

Permalink
add: (#99) 파일 검사 비즈니스 로직
Browse files Browse the repository at this point in the history
  • Loading branch information
khcho0125 committed Oct 11, 2022
1 parent 82350ce commit 544c34a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package team.comit.simtong.domain.file.spi

/**
*
* 파일 확인을 요청하는 IdentifyFilePort
*
* @author Chokyunghyeon
* @date 2022/10/11
* @version 1.0.0
**/
interface IdentifyFilePort {

fun existsPath(path: String): Boolean

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.file.exception.NotFoundFilePathException
import team.comit.simtong.domain.file.spi.IdentifyFilePort
import team.comit.simtong.domain.user.dto.ChangeProfileImageRequest
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.spi.CommandUserPort
Expand All @@ -19,10 +21,15 @@ import team.comit.simtong.global.annotation.UseCase
class ChangeProfileImageUseCase(
private val queryUserPort: QueryUserPort,
private val userSecurityPort: UserSecurityPort,
private val commandUserPort: CommandUserPort
private val commandUserPort: CommandUserPort,
private val identifyFilePort: IdentifyFilePort
) {

fun execute(request: ChangeProfileImageRequest) {
if (!identifyFilePort.existsPath(request.profileImagePath)) {
throw NotFoundFilePathException.EXCEPTION
}

val currentUserId = userSecurityPort.getCurrentUserId()
val user = queryUserPort.queryUserById(currentUserId) ?: throw UserNotFoundException.EXCEPTION

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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.file.exception.NotFoundFilePathException
import team.comit.simtong.domain.file.spi.IdentifyFilePort
import team.comit.simtong.domain.user.dto.ChangeProfileImageRequest
import team.comit.simtong.domain.user.exception.UserNotFoundException
import team.comit.simtong.domain.user.model.Authority
Expand All @@ -29,6 +31,9 @@ class ChangeProfileImageUseCaseTests {
@MockBean
private lateinit var commandUserPort: CommandUserPort

@MockBean
private lateinit var identifyFilePort: IdentifyFilePort

private lateinit var changeProfileImageUseCase: ChangeProfileImageUseCase

private val id = UUID.randomUUID()
Expand Down Expand Up @@ -59,13 +64,17 @@ class ChangeProfileImageUseCaseTests {
changeProfileImageUseCase = ChangeProfileImageUseCase(
queryUserPort,
userSecurityPort,
commandUserPort
commandUserPort,
identifyFilePort
)
}

@Test
fun `프로필 사진 변경 성공`() {
// given
given(identifyFilePort.existsPath(requestStub.profileImagePath))
.willReturn(true)

given(userSecurityPort.getCurrentUserId())
.willReturn(id)

Expand All @@ -78,9 +87,24 @@ class ChangeProfileImageUseCaseTests {
}
}

@Test
fun `알려지지 않은 사진 경로`() {
// given
given(identifyFilePort.existsPath(requestStub.profileImagePath))
.willReturn(false)

// when & then
assertThrows<NotFoundFilePathException> {
changeProfileImageUseCase.execute(requestStub)
}
}

@Test
fun `유저를 찾을 수 없음`() {
// given
given(identifyFilePort.existsPath(requestStub.profileImagePath))
.willReturn(true)

given(userSecurityPort.getCurrentUserId())
.willReturn(id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import org.springframework.stereotype.Component
import team.comit.simtong.domain.file.exception.FileIOInterruptedException
import team.comit.simtong.domain.file.spi.ManageFilePort
import team.comit.simtong.domain.file.spi.IdentifyFilePort
import team.comit.simtong.domain.file.spi.UploadFilePort
import java.io.File
import java.io.IOException

Expand All @@ -23,7 +24,7 @@ import java.io.IOException
class AwsS3Adapter(
private val awsProperties: AwsS3Properties,
private val amazonS3Client: AmazonS3Client
): ManageFilePort {
): UploadFilePort, IdentifyFilePort {

override fun upload(file: File): String {
inputS3(file, file.name)
Expand Down Expand Up @@ -57,4 +58,8 @@ class AwsS3Adapter(
return amazonS3Client.getResourceUrl(awsProperties.bucket, fileName)
}

override fun existsPath(path: String): Boolean {
return amazonS3Client.doesObjectExist(awsProperties.bucket, path.substringAfterLast('/', ""))
}

}

0 comments on commit 544c34a

Please sign in to comment.