diff --git a/domain/src/main/kotlin/com/threedays/domain/user/entity/UserProfileImage.kt b/domain/src/main/kotlin/com/threedays/domain/user/entity/UserProfileImage.kt new file mode 100644 index 0000000..162e2e3 --- /dev/null +++ b/domain/src/main/kotlin/com/threedays/domain/user/entity/UserProfileImage.kt @@ -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() { + + 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, + ) + } + } + + +} diff --git a/domain/src/main/kotlin/com/threedays/domain/user/exception/UserException.kt b/domain/src/main/kotlin/com/threedays/domain/user/exception/UserException.kt new file mode 100644 index 0000000..b6e4d25 --- /dev/null +++ b/domain/src/main/kotlin/com/threedays/domain/user/exception/UserException.kt @@ -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) + +} diff --git a/domain/src/test/kotlin/com/threedays/domain/user/entity/UserProfileImageTest.kt b/domain/src/test/kotlin/com/threedays/domain/user/entity/UserProfileImageTest.kt new file mode 100644 index 0000000..69306c5 --- /dev/null +++ b/domain/src/test/kotlin/com/threedays/domain/user/entity/UserProfileImageTest.kt @@ -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() } + } + } + } + +}) diff --git a/domain/src/test/kotlin/com/threedays/domain/user/entity/UserTest.kt b/domain/src/test/kotlin/com/threedays/domain/user/entity/UserTest.kt index 94e3020..d3ff520 100644 --- a/domain/src/test/kotlin/com/threedays/domain/user/entity/UserTest.kt +++ b/domain/src/test/kotlin/com/threedays/domain/user/entity/UserTest.kt @@ -18,7 +18,7 @@ import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import java.time.Year -@DisplayName("[도메인][유저] - 유저") +@DisplayName("[도메인][유저] 유저") class UserTest : DescribeSpec({ val fixtureMonkey: FixtureMonkey = FixtureMonkey.builder()