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

Feature: add orphan removal #245

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
app:
ports:
- "8080"
db:
postgres:
ports:
- "5432"
influx:
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ services:
retries: 5
start_period: 20s
depends_on:
- db
- postgres
- influx
environment:
bss.file-api.url: "http://mock-file-api:8080"
spring.security.user.password: "password"
spring.datasource.url: "jdbc:postgresql://db:5432/postgres?currentSchema=bss_web"
spring.datasource.url: "jdbc:postgresql://postgres:5432/postgres?currentSchema=bss_web"
spring.datasource.username: "postgres"
spring.datasource.password: "postgres"
spring.flyway.default-schema: "bss_web"
Expand All @@ -27,7 +27,7 @@ services:
management.influx.metrics.export.user-name: "user"
management.influx.metrics.export.password: "password"
management.influx.metrics.export.bucket: "bucket"
db:
postgres:
image: "postgres:16.0-alpine3.18"
environment:
POSTGRES_PASSWORD: "postgres"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package hu.bsstudio.bssweb.event.integration
import hu.bsstudio.bssweb.IntegrationTest
import hu.bsstudio.bssweb.event.client.EventClient
import hu.bsstudio.bssweb.event.entity.DetailedEventEntity
import hu.bsstudio.bssweb.eventvideo.entity.EventVideoEntity
import hu.bsstudio.bssweb.video.entity.DetailedVideoEntity
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.longs.shouldBeZero
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatusCode
Expand All @@ -28,4 +31,17 @@ class DeleteEventIntegrationTest(

actual.statusCode shouldBeEqual HttpStatusCode.valueOf(204)
}

@Test
fun `it should return 204 when event has video attached`() {
val eventEntity = eventRepository.save(DetailedEventEntity(url = "url", title = "title"))
val videoEntity = videoRepository.save(DetailedVideoEntity(url = "url", title = "title"))
eventVideoRepository.save(EventVideoEntity(eventId = eventEntity.id, videoId = videoEntity.id))

val actual = client.deleteEvent(eventEntity.id)

actual.statusCode shouldBeEqual HttpStatusCode.valueOf(204)
videoRepository.count().shouldBeEqual(1)
eventVideoRepository.count().shouldBeZero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import org.hibernate.Hibernate
@Table(name = "crew")
data class DetailedVideoCrewEntity(
@EmbeddedId
var id: VideoCrewEntityId,
var id: VideoCrewEntityId
) {

@ManyToOne
@JoinColumn(insertable = false, updatable = false)
var member: SimpleMemberEntity
) {
lateinit var member: SimpleMemberEntity

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hu.bsstudio.bssweb

import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager

inline fun <reified T> TestEntityManager.find(primaryKey: Any?): T {
return this.find(T::class.java, primaryKey)
}

inline fun <reified T> TestEntityManager.persistAndGetId(entity: Any): T {
return this.persistAndGetId(entity, T::class.java)
}

inline fun <reified T> TestEntityManager.getId(entity: Any): T {
return this.getId(entity, T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package hu.bsstudio.bssweb.event.repository
import hu.bsstudio.bssweb.event.entity.DetailedEventEntity
import hu.bsstudio.bssweb.event.entity.SimpleEventEntity
import hu.bsstudio.bssweb.eventvideo.entity.EventVideoEntity
import hu.bsstudio.bssweb.eventvideo.repository.EventVideoRepository
import hu.bsstudio.bssweb.find
import hu.bsstudio.bssweb.persistAndGetId
import hu.bsstudio.bssweb.video.entity.SimpleVideoEntity
import hu.bsstudio.bssweb.video.repository.SimpleVideoRepository
import io.kotest.matchers.equality.shouldBeEqualToComparingFields
import io.kotest.matchers.longs.shouldBeZero
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.optional.shouldBeEmpty
import io.kotest.matchers.optional.shouldBePresent
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
Expand All @@ -23,11 +23,9 @@ import java.util.UUID
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class DetailedEventRepositoryTest(
@Autowired private val underTest: DetailedEventRepository,
@Autowired private val simpleEventRepository: SimpleEventRepository,
@Autowired private val simpleVideoRepository: SimpleVideoRepository,
@Autowired private val eventVideoRepository: EventVideoRepository,
@Autowired private val entityManager: TestEntityManager
) {

@Test
internal fun `create read delete`() {
underTest.count().shouldBeZero()
Expand All @@ -40,24 +38,26 @@ class DetailedEventRepositoryTest(
underTest.findById(id) shouldBePresent { it shouldBeEqualToComparingFields expected }

underTest.deleteById(id)
entityManager.flush()

underTest.findById(id).shouldBeEmpty()
}

@Test
internal fun `create read delete with video`() {
val eventId = simpleEventRepository.save(SimpleEventEntity(url = URL, title = TITLE)).id
val video = simpleVideoRepository.save(SimpleVideoEntity(url = "url", title = "title"))
val eventId = entityManager.persistAndGetId<UUID>(SimpleEventEntity(url = URL, title = TITLE))
val video = entityManager.persist(SimpleVideoEntity(url = "url", title = "title"))
entityManager.persistAndFlush(EventVideoEntity(eventId = eventId, videoId = video.id))

eventVideoRepository.save(EventVideoEntity(eventId = eventId, videoId = video.id))
entityManager.run { flush(); clear() }

val actual = underTest.findById(eventId).orElseThrow()
val actual = underTest.findById(eventId)
val expected = createExpected(eventId, listOf(video))
actual.shouldBeEqualToComparingFields(expected)
actual shouldBePresent { it shouldBeEqualToComparingFields expected }

underTest.deleteById(eventId)
entityManager.flush()

underTest.findById(eventId).shouldBeEmpty()
simpleVideoRepository.count().shouldBe(1L)
entityManager.find<SimpleVideoEntity>(video.id).shouldNotBeNull()
}

private fun createExpected(id: UUID, videos: List<SimpleVideoEntity> = emptyList()) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import java.time.LocalDate

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class EventRepositoryTest(
@Autowired private val underTest: SimpleEventRepository
@Autowired private val underTest: SimpleEventRepository,
@Autowired private val entityManager: TestEntityManager
) {

@Test
Expand All @@ -36,6 +38,8 @@ class EventRepositoryTest(
}

underTest.deleteById(id)
entityManager.flush()

underTest.findById(id).shouldBeEmpty()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
package hu.bsstudio.bssweb.eventvideo.repository

import hu.bsstudio.bssweb.event.entity.SimpleEventEntity
import hu.bsstudio.bssweb.event.repository.SimpleEventRepository
import hu.bsstudio.bssweb.eventvideo.entity.EventVideoEntity
import hu.bsstudio.bssweb.find
import hu.bsstudio.bssweb.persistAndGetId
import hu.bsstudio.bssweb.video.entity.SimpleVideoEntity
import hu.bsstudio.bssweb.video.repository.SimpleVideoRepository
import io.kotest.matchers.equality.shouldBeEqualToComparingFields
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.optional.shouldBeEmpty
import io.kotest.matchers.optional.shouldBePresent
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import java.util.UUID

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class EventVideoRepositoryTest(
@Autowired private val eventRepository: SimpleEventRepository,
@Autowired private val videoRepository: SimpleVideoRepository,
@Autowired private val underTest: EventVideoRepository
@Autowired private val underTest: EventVideoRepository,
@Autowired private val entityManager: TestEntityManager
) {

@Test
fun `create read delete`() {
val videoId = videoRepository.save(SimpleVideoEntity(url = "url", title = "title")).id
val eventId = eventRepository.save(SimpleEventEntity(url = "url", title = "title")).id
val videoId = entityManager.persistAndGetId<UUID>(SimpleVideoEntity(url = "url", title = "title"))
val eventId = entityManager.persistAndGetId<UUID>(SimpleEventEntity(url = "url", title = "title"))

val entity = EventVideoEntity(eventId, videoId)
underTest.save(entity)

underTest.findById(entity) shouldBePresent { it shouldBeEqualToComparingFields entity }

underTest.deleteById(entity)
entityManager.flush()

underTest.findById(entity).shouldBeEmpty()
videoRepository.count().shouldBe(1L)
eventRepository.count().shouldBe(1L)
entityManager.find<SimpleVideoEntity>(videoId).shouldNotBeNull()
entityManager.find<SimpleEventEntity>(eventId).shouldNotBeNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import java.time.LocalDate

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class MemberRepositoryTest(
@Autowired private val underTest: MemberRepository
@Autowired private val underTest: MemberRepository,
@Autowired private val entityManager: TestEntityManager
) {
@Test
fun `create read delete`() {
Expand All @@ -40,6 +42,8 @@ class MemberRepositoryTest(
}

underTest.deleteById(id)
entityManager.flush()

underTest.findById(id).shouldBeEmpty()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package hu.bsstudio.bssweb.video.repository

import hu.bsstudio.bssweb.member.entity.DetailedMemberEntity
import hu.bsstudio.bssweb.member.entity.SimpleMemberEntity
import hu.bsstudio.bssweb.member.repository.MemberRepository
import hu.bsstudio.bssweb.video.entity.DetailedVideoEntity
import hu.bsstudio.bssweb.video.entity.SimpleVideoEntity
import hu.bsstudio.bssweb.videocrew.entity.DetailedVideoCrewEntity
import hu.bsstudio.bssweb.videocrew.entity.VideoCrewEntity
import hu.bsstudio.bssweb.videocrew.entity.VideoCrewEntityId
import hu.bsstudio.bssweb.videocrew.repository.VideoCrewRepository
import io.kotest.matchers.equality.shouldBeEqualToComparingFields
import io.kotest.matchers.longs.shouldBeZero
import io.kotest.matchers.optional.shouldBeEmpty
Expand All @@ -25,9 +17,6 @@ import java.util.UUID
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class DetailedVideoRepositoryTest(
@Autowired private val underTest: DetailedVideoRepository,
@Autowired private val memberRepository: MemberRepository,
@Autowired private val simpleVideoRepository: SimpleVideoRepository,
@Autowired private val videoCrewRepository: VideoCrewRepository,
@Autowired private val entityManager: TestEntityManager
) {
@Test
Expand All @@ -42,28 +31,12 @@ class DetailedVideoRepositoryTest(
underTest.findById(id) shouldBePresent { it shouldBeEqualToComparingFields expected }

underTest.deleteById(id)
underTest.findById(id).shouldBeEmpty()
}

@Test
internal fun `create read delete with crew`() {
val memberId = DetailedMemberEntity(name = MEMBER_NAME, url = MEMBER_URL, nickname = MEMBER_NICKNAME)
.let { this.memberRepository.save(it) }
.id
val videoId = SimpleVideoEntity(url = URL, title = TITLE)
.let { this.simpleVideoRepository.save(it) }
.id
val videoCrewId = VideoCrewEntityId(videoId, "cameraman", memberId)
this.videoCrewRepository.save(VideoCrewEntity(videoCrewId))
entityManager.run { flush(); clear() }
entityManager.flush()

val expected = createExpected(videoId, listOf(DetailedVideoCrewEntity(videoCrewId, SimpleMemberEntity(MEMBER_NAME, MEMBER_NICKNAME).apply { id = memberId })))
underTest.findById(videoId)
.shouldBePresent()
.shouldBeEqualToComparingFields(expected)
underTest.findById(id).shouldBeEmpty()
}

private fun createExpected(id: UUID, videoCrew: List<DetailedVideoCrewEntity> = emptyList()) =
private fun createExpected(id: UUID) =
DetailedVideoEntity(
url = URL,
title = TITLE,
Expand All @@ -72,14 +45,11 @@ class DetailedVideoRepositoryTest(
visible = false
).apply {
this.id = id
this.videoCrew = videoCrew
this.videoCrew = emptyList()
}

private companion object {
private const val URL = "szobakommando"
private const val TITLE = "Szobakommando"
private const val MEMBER_NAME = "Bence Csik"
private const val MEMBER_URL = "bcsik"
private const val MEMBER_NICKNAME = "CséBé"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import java.time.LocalDate

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class SimpleVideoRepositoryTest(
@Autowired private val underTest: SimpleVideoRepository
@Autowired private val underTest: SimpleVideoRepository,
@Autowired private val entityManager: TestEntityManager
) {

@Test
Expand All @@ -36,6 +38,8 @@ class SimpleVideoRepositoryTest(
}

underTest.deleteById(id)
entityManager.flush()

underTest.findById(id).shouldBeEmpty()
}

Expand Down
Loading