-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
7 changed files
with
287 additions
and
60 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.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,63 @@ | ||
package com.mjucow.eatda.domain.store.entity | ||
|
||
import com.mjucow.eatda.domain.common.BaseEntity | ||
import jakarta.persistence.CascadeType | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.FetchType | ||
import jakarta.persistence.JoinColumn | ||
import jakarta.persistence.JoinTable | ||
import jakarta.persistence.ManyToMany | ||
import jakarta.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "store") | ||
class Store() : BaseEntity() { | ||
constructor(name: String, displayName: String? = null) : this() { | ||
this.name = name.also { validateName(name) } | ||
this.displayName = displayName?.let { | ||
validateName(displayName) | ||
displayName.trim() | ||
} | ||
} | ||
|
||
@Column(nullable = false, unique = true) | ||
var name: String = "" | ||
set(value) { | ||
validateName(value) | ||
field = value.trim() | ||
} | ||
|
||
@Column(nullable = true) | ||
var displayName: String? = null | ||
set(value) { | ||
field = value?.let { | ||
validateName(it) | ||
it.trim() | ||
} | ||
} | ||
|
||
@ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST, CascadeType.MERGE]) | ||
@JoinTable( | ||
name = "store_category", | ||
joinColumns = [JoinColumn(name = "store_id")], | ||
inverseJoinColumns = [JoinColumn(name = "category_id")], | ||
) | ||
protected val mutableCategories: MutableSet<Category> = mutableSetOf() | ||
|
||
fun getCategories(): Set<Category> { | ||
return mutableCategories.toSet() | ||
} | ||
|
||
fun addCategory(category: Category) { | ||
mutableCategories.add(category) | ||
} | ||
|
||
private fun validateName(name: String) { | ||
require(name.isNotBlank() && name.trim().length <= MAX_NAME_LENGTH) | ||
} | ||
|
||
companion object { | ||
const val MAX_NAME_LENGTH = 31 | ||
} | ||
} |
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,18 @@ | ||
-- liquibase formatted sql | ||
|
||
-- changeset liquibase:2 | ||
CREATE TABLE store ( | ||
id bigserial NOT NULL PRIMARY KEY , | ||
name varchar(31) NOT NULL, | ||
display_name varchar(31) NULL, | ||
created_at timestamp NOT NULL DEFAULT NOW(), | ||
updated_at timestamp NOT NULL | ||
); | ||
CREATE UNIQUE INDEX idx_store_name ON store(name); | ||
|
||
CREATE TABLE store_category ( | ||
id bigserial NOT NULL PRIMARY KEY, | ||
store_id bigint NOT NULL REFERENCES store, | ||
category_id bigint NOT NULL REFERENCES category, | ||
created_at timestamp NOT NULL DEFAULT NOW() | ||
); |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
databaseChangeLog: | ||
- include: | ||
file: /db/changelog/230925-init.sql | ||
file: /db/changelog/230925-init.sql | ||
- include: | ||
file: /db/changelog/230929-store.sql |
79 changes: 79 additions & 0 deletions
79
src/test/kotlin/com/mjucow/eatda/domain/store/entity/CategoryTest.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,79 @@ | ||
package com.mjucow.eatda.domain.store.entity | ||
|
||
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.EmptySource | ||
|
||
class CategoryTest { | ||
@DisplayName("이름이 빈 값일 경우 예외를 던진다") | ||
@ParameterizedTest | ||
@EmptySource | ||
fun throwExceptionWhenNameIsEmpty(name: String) { | ||
// given | ||
|
||
// when | ||
val throwable = catchThrowable { Category(name) } | ||
|
||
// then | ||
assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
|
||
@DisplayName("이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenNameLengthGreaterThanMaxLength() { | ||
// given | ||
val name = "x".repeat(Category.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = catchThrowable { Category(name) } | ||
|
||
// then | ||
assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
|
||
@DisplayName("새로운 이름이 빈 값일 경우 예외를 던진다") | ||
@ParameterizedTest | ||
@EmptySource | ||
fun throwExceptionWhenNewNameIsEmpty(newName: String) { | ||
// given | ||
val category = Category("validName") | ||
|
||
// when | ||
val throwable = catchThrowable { category.name = newName } | ||
|
||
// then | ||
assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
@DisplayName("새로운 이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenNewNameLengthGreaterThanMaxLength() { | ||
// given | ||
val category = Category("validName") | ||
val newName = "x".repeat(Category.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = catchThrowable { category.name = newName } | ||
|
||
// then | ||
assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
@DisplayName("정상적인 경우 객체가 생성된다") | ||
@Test | ||
fun createInstantWhenValidInput() { | ||
// given | ||
val name = "validName" | ||
|
||
// when | ||
val category = Category(name) | ||
|
||
// then | ||
assertThat(category).isNotNull | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/test/kotlin/com/mjucow/eatda/domain/store/entity/StoreTest.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,107 @@ | ||
package com.mjucow.eatda.domain.store.entity | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.EmptySource | ||
|
||
class StoreTest { | ||
@DisplayName("이름이 빈 값일 경우 예외를 던진다") | ||
@ParameterizedTest | ||
@EmptySource | ||
fun throwExceptionWhenNameIsEmpty(name: String) { | ||
// given | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { Store(name) } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
|
||
@DisplayName("이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenNameLengthGreaterThanMaxLength() { | ||
// given | ||
val name = "x".repeat(Store.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { Store(name) } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
|
||
@DisplayName("보이는 이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenDisplayNameLengthGreaterThanMaxLength() { | ||
// given | ||
val name = "validName" | ||
val displayName = "x".repeat(Store.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { Store(name, displayName) } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
|
||
@DisplayName("새로운 이름이 빈 값일 경우 예외를 던진다") | ||
@ParameterizedTest | ||
@EmptySource | ||
fun throwExceptionWhenNewNameIsEmpty(newName: String) { | ||
// given | ||
val store = Store("validName") | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { store.name = newName } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
@DisplayName("새로운 이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenNewNameLengthGreaterThanMaxLength() { | ||
// given | ||
val store = Store("validName") | ||
val newName = "x".repeat(Store.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { store.name = newName } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
@DisplayName("새로운 보이는 이름이 최소 길이 이상일 경우 에외를 던진다") | ||
@Test | ||
fun throwExceptionWhenNewDisplayNameLengthGreaterThanMaxLength() { | ||
// given | ||
val store = Store("validName", "validDisplayName") | ||
val newDisplayName = "x".repeat(Store.MAX_NAME_LENGTH + 1) | ||
|
||
// when | ||
val throwable = Assertions.catchThrowable { store.displayName = newDisplayName } | ||
|
||
// then | ||
Assertions.assertThat(throwable).isInstanceOf(RuntimeException::class.java) | ||
} | ||
|
||
@DisplayName("정상적인 경우 객체가 생성된다") | ||
@Test | ||
fun createInstantWhenValidInput() { | ||
// given | ||
val name = "validName" | ||
|
||
// when | ||
val category = Store(name) | ||
|
||
// then | ||
Assertions.assertThat(category).isNotNull | ||
} | ||
} |
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