Skip to content

Commit

Permalink
feat: baseEntity 생성 & FeedEntity 골격만 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Aug 21, 2023
1 parent 8728279 commit 29f03dc
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package joryu.sns_service

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaAuditing

@EnableJpaAuditing
@SpringBootApplication
class SnsServiceApplication

fun main(args: Array<String>) {
runApplication<SnsServiceApplication>(*args)
runApplication<SnsServiceApplication>(*args)
}
30 changes: 30 additions & 0 deletions sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package joryu.sns_service.feed.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Table
import joryu.sns_service.profile.entity.BaseEntity

@Table(name = "feed")
@Entity
class Feed(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

content: String,
) : BaseEntity() {
constructor() : this(0, "")
constructor(content: String) : this(0, content)

@Column(name = "view_count", nullable = false)
var viewCount: Long = 0
private set

@Column(name = "content", nullable = false, length = 1000)
var content: String = content
private set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package joryu.sns_service.feed.repository

import joryu.sns_service.feed.entity.Feed
import org.springframework.data.jpa.repository.JpaRepository

interface FeedRepository : JpaRepository<Feed, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package joryu.sns_service.profile.entity

import jakarta.persistence.Column
import jakarta.persistence.EntityListeners
import jakarta.persistence.MappedSuperclass
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {

@CreatedDate
@Column(name = "create_at", nullable = false, updatable = false)
var createAt: LocalDateTime = LocalDateTime.now()
private set

@LastModifiedDate
@Column(name = "update_at", nullable = false)
var updateAt: LocalDateTime = LocalDateTime.now()
private set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package joryu.sns_service.feed.entity

import jakarta.persistence.EntityManager
import joryu.sns_service.feed.repository.FeedRepository
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Transactional
@SpringBootTest
@Disabled("프로젝트 초기 kotlin entity 테스트용도. springBootTest라 속도가 느려서 disabled.")
class FeedEntityTest {

@Autowired
lateinit var em: EntityManager

@Autowired
lateinit var feedRepository: FeedRepository

@Test
fun `BaseEntity에 createAt, updateAt이 잘 들어가야 한다`() {
val before = LocalDateTime.now()
val feedEntity = feedRepository.save(Feed("123"))

em.flush()
em.clear()

val findFeedEntity = feedRepository.getReferenceById(feedEntity.id)

assertThat(findFeedEntity.createAt).isNotNull()
assertThat(findFeedEntity.createAt).isAfter(before)
assertThat(findFeedEntity.createAt).isBefore(LocalDateTime.now())
assertThat(findFeedEntity.updateAt).isNotNull()
assertThat(findFeedEntity.updateAt).isAfter(before)
assertThat(findFeedEntity.updateAt).isBefore(LocalDateTime.now())
}

@Test
fun `BaseEntity의 Id는 auto increment 되어야 한다`() {
val feedEntity1 = feedRepository.save(Feed("123"))
val feedEntity2 = feedRepository.save(Feed("123"))
val feedEntity3 = feedRepository.save(Feed("123"))

assertThat(feedEntity1.id).isLessThan(feedEntity2.id)
assertThat(feedEntity2.id).isLessThan(feedEntity3.id)
}
}

0 comments on commit 29f03dc

Please sign in to comment.