Skip to content

Commit

Permalink
store 정의 추가 및 test 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou committed Sep 29, 2023
1 parent 0005779 commit 2672cce
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 60 deletions.
17 changes: 17 additions & 0 deletions src/main/kotlin/com/mjucow/eatda/domain/store/entity/Category.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
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
Expand All @@ -20,6 +25,18 @@ class Category() : BaseEntity() {
field = value
}

@ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST, CascadeType.MERGE])
@JoinTable(
name = "store_category",
joinColumns = [JoinColumn(name = "category_id")],
inverseJoinColumns = [JoinColumn(name = "store_id")],
)
protected val mutableStores: MutableSet<Store> = mutableSetOf()

fun getStores(): Set<Store> {
return mutableStores.toSet()

Check warning on line 37 in src/main/kotlin/com/mjucow/eatda/domain/store/entity/Category.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/domain/store/entity/Category.kt#L37

Added line #L37 was not covered by tests
}

private fun validateName(name: String) {
require(name.isNotBlank() && name.trim().length <= MAX_NAME_LENGTH)
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt
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

Check warning on line 32 in src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt#L32

Added line #L32 was not covered by tests
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()

Check warning on line 49 in src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt#L49

Added line #L49 was not covered by tests
}

fun addCategory(category: Category) {
mutableCategories.add(category)
}

Check warning on line 54 in src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/com/mjucow/eatda/domain/store/entity/Store.kt#L53-L54

Added lines #L53 - L54 were not covered by tests

private fun validateName(name: String) {
require(name.isNotBlank() && name.trim().length <= MAX_NAME_LENGTH)
}

companion object {
const val MAX_NAME_LENGTH = 31
}
}
18 changes: 18 additions & 0 deletions src/main/resources/db/changelog/230929-store.sql
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()
);
4 changes: 3 additions & 1 deletion src/main/resources/db/changelog/changelog-master.yml
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
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 src/test/kotlin/com/mjucow/eatda/domain/store/entity/StoreTest.kt
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Import
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -25,20 +24,6 @@ class CategoryCommandServiceDataTest : AbstractDataTest() {
@Autowired
lateinit var repository: CategoryRepository

@DisplayName("이름이 빈 값일 경우 예외를 던진다")
@ParameterizedTest
@EmptySource
fun throwExceptionWhenNameIsEmpty(name: String) {
// given
val command = CreateCommand(name)

// when
val throwable = catchThrowable { categoryCommandService.create(command) }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("이름이 중복될 경우 예외를 던진다")
@Test
fun throwExceptionWhenDuplicatedName() {
Expand All @@ -54,20 +39,6 @@ class CategoryCommandServiceDataTest : AbstractDataTest() {
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("이름이 최소 길이 이상일 경우 에외를 던진다")
@Test
fun throwExceptionWhenNameLengthGreaterThanMaxLength() {
// given
val name = "x".repeat(Category.MAX_NAME_LENGTH + 1)
val command = CreateCommand(name)

// when
val throwable = catchThrowable { categoryCommandService.create(command) }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("정상 입력이라면 객체가 생성된다")
@Test
fun createCategoryWhenValidInput() {
Expand All @@ -82,36 +53,6 @@ class CategoryCommandServiceDataTest : AbstractDataTest() {
assertThat(domain).isNotNull
}

@DisplayName("새로운 이름이 빈 값일 경우 예외를 던진다")
@ParameterizedTest
@EmptySource
fun throwExceptionWhenNewNameIsEmpty(newName: String) {
// given
val id = 1L
val command = UpdateNameCommand(newName)

// when
val throwable = catchThrowable { categoryCommandService.updateName(id, command) }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("새로운 이름이 최소 길이 이상일 경우 에외를 던진다")
@Test
fun throwExceptionWhenNewNameLengthGreaterThanMaxLength() {
// given
val id = 1L
val newName = "x".repeat(Category.MAX_NAME_LENGTH + 1)
val command = UpdateNameCommand(newName)

// when
val throwable = catchThrowable { categoryCommandService.updateName(id, command) }

// then
assertThat(throwable).isInstanceOf(RuntimeException::class.java)
}

@DisplayName("새로운 이름을 수정하려는 대상의 조회되지 않을 경우 예외를 던진다")
@Test
fun throwExceptionWhenNotFoundTarget() {
Expand Down

0 comments on commit 2672cce

Please sign in to comment.