-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f0eaf0
commit 4b49708
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
domain/src/main/kotlin/com/threedays/domain/user/entity/UserProfileImage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.threedays.domain.user.entity | ||
|
||
import com.threedays.domain.user.exception.UserException | ||
import com.threedays.support.common.base.domain.DomainEntity | ||
import com.threedays.support.common.base.domain.UUIDTypeId | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import java.net.URL | ||
import java.util.* | ||
|
||
class UserProfileImage( | ||
override val id: Id, | ||
val extension: Extension, | ||
val url: URL, | ||
) : DomainEntity<UserProfileImage, UserProfileImage.Id>() { | ||
|
||
data class Id(override val value: UUID) : UUIDTypeId(value) | ||
|
||
enum class Extension(val value: String) { | ||
PNG("png"), | ||
} | ||
|
||
companion object { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
fun create( | ||
extension: Extension, | ||
getProfileImageUrlAction: (Id, Extension) -> URL, | ||
): UserProfileImage { | ||
val imageId = Id(UUID.randomUUID()) | ||
val imageUrl: URL = try { | ||
getProfileImageUrlAction(imageId, extension) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Failed to upload profile image" } | ||
throw UserException.ProfileImageUploadFailedException() | ||
} | ||
|
||
return UserProfileImage( | ||
id = imageId, | ||
extension = extension, | ||
url = imageUrl, | ||
) | ||
} | ||
} | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
domain/src/main/kotlin/com/threedays/domain/user/exception/UserException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.threedays.domain.user.exception | ||
|
||
import com.threedays.support.common.base.exception.CustomException | ||
|
||
sealed class UserException( | ||
codeNumber: Int, | ||
override val message: String = DEFAULT_MESSAGE, | ||
) : CustomException("USER", codeNumber, message) { | ||
|
||
data class ProfileImageUploadFailedException( | ||
override val message: String = "프로필 이미지 업로드에 실패했습니다.", | ||
) : UserException(2010, message) | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
domain/src/test/kotlin/com/threedays/domain/user/entity/UserProfileImageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.threedays.domain.user.entity | ||
|
||
import com.threedays.domain.user.exception.UserException | ||
import io.kotest.core.spec.DisplayName | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
import java.net.URI | ||
import java.net.URL | ||
|
||
@DisplayName("[도메인][유저] 프로필 이미지") | ||
class UserProfileImageTest : DescribeSpec({ | ||
|
||
describe("프로필 이미지 생성") { | ||
UserProfileImage.Extension.entries.forEach { extension -> | ||
it("확장자가 ${extension.value}인 프로필 이미지를 생성한다") { | ||
// arrange | ||
val getProfileImageUrlAction: (UserProfileImage.Id, UserProfileImage.Extension) -> URL = | ||
{ _, _ -> URI("https://example.com").toURL() } | ||
|
||
// act | ||
val userProfileImage: UserProfileImage = | ||
UserProfileImage.create(extension, getProfileImageUrlAction) | ||
|
||
// assert | ||
userProfileImage.extension shouldBe extension | ||
} | ||
} | ||
|
||
context("URL 생성작업에서 예외가 발생하는 경우") { | ||
it("UserException.ProfileImageUploadFailedException 예외를 던진다") { | ||
// arrange | ||
val getProfileImageUrlAction: (UserProfileImage.Id, UserProfileImage.Extension) -> URL = | ||
{ _, _ -> throw Exception() } | ||
|
||
// act & assert | ||
runCatching { | ||
UserProfileImage.create( | ||
UserProfileImage.Extension.PNG, | ||
getProfileImageUrlAction | ||
) | ||
}.onFailure { it shouldBe UserException.ProfileImageUploadFailedException() } | ||
} | ||
} | ||
} | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters