Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] (Point,PhoneNumber)primitive value -> VO Class #76

Merged
merged 16 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/kotlin/com/mjucow/eatda/common/vo/Latitude.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mjucow.eatda.common.vo

import com.fasterxml.jackson.annotation.JsonProperty

@JvmInline
value class Latitude(
@JsonProperty(value = "latitude")
val value: Double,

Check warning on line 8 in src/main/kotlin/com/mjucow/eatda/common/vo/Latitude.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/common/vo/Latitude.kt#L8

Added line #L8 was not covered by tests
) {
init {
if (LATITUDE_MIN > value || LATITUDE_MAX < value) {
throw IllegalArgumentException()
}
}

companion object {
private const val LATITUDE_MIN = -90.0
private const val LATITUDE_MAX = 90.0
0702Yoon marked this conversation as resolved.
Show resolved Hide resolved
}
}
19 changes: 19 additions & 0 deletions src/main/kotlin/com/mjucow/eatda/common/vo/Longitude.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mjucow.eatda.common.vo

import com.fasterxml.jackson.annotation.JsonProperty

@JvmInline
value class Longitude(
@JsonProperty(value = "longitude")
val value: Double,

Check warning on line 8 in src/main/kotlin/com/mjucow/eatda/common/vo/Longitude.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/common/vo/Longitude.kt#L8

Added line #L8 was not covered by tests
) {
init {
if (LONGITUDE_MIN > value || LONGITUDE_MAX < value) {
throw IllegalArgumentException()
}
}
companion object {
private const val LONGITUDE_MIN = 0.0
private const val LONGITUDE_MAX = 180.0
}
}
8 changes: 2 additions & 6 deletions src/main/kotlin/com/mjucow/eatda/common/vo/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import jakarta.persistence.Embeddable
example = """{"latitude": 37.5802219, "longitude": 126.9226047}"""
)
data class Point(

@Column(name = "location_latitude")
val latitude: Double,

@Column(name = "location_longitude")
val longitude: Double,
@Column(name = "location_latitude") val latitude: Latitude,
@Column(name = "location_longitude") val longitude: Longitude,
)
48 changes: 48 additions & 0 deletions src/test/kotlin/com/mjucow/eatda/common/vo/LatitudeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mjucow.eatda.common.vo

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class LatitudeTest : VOTest {

@DisplayName("위도의 범위가 올바르면 객체가 생성된다.")
@ParameterizedTest
@ValueSource(doubles = [-90.0, -89.0, 89.0, 90.0])
fun createInstanceWhenValidLatitude(value: Double) {
// given

0702Yoon marked this conversation as resolved.
Show resolved Hide resolved
// when
val sut = Latitude(value)
// then
assertThat(sut).isNotNull
}

@DisplayName("위도의 범위가 올바르면 객체가 생성된다.")
@ParameterizedTest
@ValueSource(doubles = [-Double.MAX_VALUE, -91.0, 91.0, Double.MAX_VALUE])
fun throwExceptionWhenInvalidNumber(value: Double) {
// given

// when
val throwable = catchThrowable { Latitude(value) }
// then
assertThat(throwable).isNotNull
}

@Test
override fun returnEqualsTrueAndSameHashcodeWhenSameValue() {
// given
val value = 20.0
val standardInstance = Latitude(value)
val sameValueInstance = Latitude(value)
// when
val isEqaulsAndSameHashcode = (standardInstance == sameValueInstance) &&
(sameValueInstance.hashCode() == sameValueInstance.hashCode())
// then
assertThat(isEqaulsAndSameHashcode).isTrue()
}
}
48 changes: 48 additions & 0 deletions src/test/kotlin/com/mjucow/eatda/common/vo/LongitudeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mjucow.eatda.common.vo

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class LongitudeTest : VOTest {

@DisplayName("경도의 범위가 올바르면 객체가 생성된다.")
@ParameterizedTest
@ValueSource(doubles = [0.0, 1.0, 179.0, 180.0])
fun createInstanceWhenValidLatitude(value: Double) {
// given

// when
val sut = Longitude(value)
// then
assertThat(sut).isNotNull
}

@DisplayName("위도의 범위가 올바르면 객체가 생성된다.")
0702Yoon marked this conversation as resolved.
Show resolved Hide resolved
@ParameterizedTest
@ValueSource(doubles = [-Double.MAX_VALUE, -1.0, 181.0, Double.MAX_VALUE])
fun throwExceptionWhenInvalidNumber(value: Double) {
// given

// when
val throwable = catchThrowable { Longitude(value) }
// then
assertThat(throwable).isNotNull
}

@Test
override fun returnEqualsTrueAndSameHashcodeWhenSameValue() {
// given
val value = 20.0
val standardInstance = Longitude(value)
val sameValueInstance = Longitude(value)
// when
val isEqaulsAndSameHashcode = (standardInstance == sameValueInstance) &&
(sameValueInstance.hashCode() == sameValueInstance.hashCode())
// then
assertThat(isEqaulsAndSameHashcode).isTrue()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mjucow.eatda.domain.store.entity.objectmother

import com.mjucow.eatda.common.objectmother.EntityMother
import com.mjucow.eatda.common.vo.Latitude
import com.mjucow.eatda.common.vo.Longitude
import com.mjucow.eatda.common.vo.PhoneNumber
import com.mjucow.eatda.common.vo.Point
import com.mjucow.eatda.domain.store.entity.Store
Expand All @@ -11,7 +13,7 @@ object StoreMother : EntityMother<Store>() {
const val DISPLAY_NAME = "띵지대"
const val IMAGE_ADDRESS = "/eatda/public/store/232D8241-C6A9-4AD9-B0EA-56F6DD24BADF.jpg"
val PHONE_NUMBER = PhoneNumber("02-300-1656")
val LOCATION = Point(latitude = 37.5802219, longitude = 126.9226047)
val LOCATION = Point(latitude = Latitude(37.5802219), Longitude(126.9226047))

override fun createDefaultInstance() = Store(NAME, ADDRESS)

Expand Down