test(backend): add failing tests for step 3 — repositories
This commit is contained in:
@@ -0,0 +1,91 @@
|
|||||||
|
package com.condado.newsletter.repository
|
||||||
|
|
||||||
|
import com.condado.newsletter.model.DispatchLog
|
||||||
|
import com.condado.newsletter.model.DispatchStatus
|
||||||
|
import com.condado.newsletter.model.VirtualEntity
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
class RepositoryTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
lateinit var entityManager: TestEntityManager
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
lateinit var virtualEntityRepository: VirtualEntityRepository
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
lateinit var dispatchLogRepository: DispatchLogRepository
|
||||||
|
|
||||||
|
private lateinit var activeEntity: VirtualEntity
|
||||||
|
private lateinit var inactiveEntity: VirtualEntity
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun setUp() {
|
||||||
|
activeEntity = entityManager.persistAndFlush(
|
||||||
|
VirtualEntity(name = "Active One", email = "active@condado.com", jobTitle = "CEO", active = true)
|
||||||
|
)
|
||||||
|
inactiveEntity = entityManager.persistAndFlush(
|
||||||
|
VirtualEntity(name = "Inactive One", email = "inactive@condado.com", jobTitle = "CTO", active = false)
|
||||||
|
)
|
||||||
|
entityManager.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_returnOnlyActiveEntities_when_findAllByActiveTrueCalled() {
|
||||||
|
val result = virtualEntityRepository.findAllByActiveTrue()
|
||||||
|
|
||||||
|
assertThat(result).hasSize(1)
|
||||||
|
assertThat(result[0].email).isEqualTo("active@condado.com")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_findEntityByEmail_when_emailExists() {
|
||||||
|
val result = virtualEntityRepository.findByEmail("active@condado.com")
|
||||||
|
|
||||||
|
assertThat(result).isPresent
|
||||||
|
assertThat(result.get().name).isEqualTo("Active One")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_returnEmptyOptional_when_emailNotFound() {
|
||||||
|
val result = virtualEntityRepository.findByEmail("nobody@condado.com")
|
||||||
|
|
||||||
|
assertThat(result).isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_returnAllLogsForEntity_when_findAllByVirtualEntityCalled() {
|
||||||
|
val entity = entityManager.find(VirtualEntity::class.java, activeEntity.id)
|
||||||
|
entityManager.persistAndFlush(DispatchLog(virtualEntity = entity, status = DispatchStatus.SENT))
|
||||||
|
entityManager.persistAndFlush(DispatchLog(virtualEntity = entity, status = DispatchStatus.FAILED))
|
||||||
|
entityManager.clear()
|
||||||
|
|
||||||
|
val result = dispatchLogRepository.findAllByVirtualEntity(entity)
|
||||||
|
|
||||||
|
assertThat(result).hasSize(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_returnMostRecentLog_when_findTopByVirtualEntityOrderByDispatchedAtDescCalled() {
|
||||||
|
val entity = entityManager.find(VirtualEntity::class.java, activeEntity.id)
|
||||||
|
val firstLog = entityManager.persistAndFlush(
|
||||||
|
DispatchLog(virtualEntity = entity, status = DispatchStatus.SENT)
|
||||||
|
)
|
||||||
|
Thread.sleep(10)
|
||||||
|
val latestLog = entityManager.persistAndFlush(
|
||||||
|
DispatchLog(virtualEntity = entity, status = DispatchStatus.FAILED)
|
||||||
|
)
|
||||||
|
entityManager.clear()
|
||||||
|
|
||||||
|
val result = dispatchLogRepository.findTopByVirtualEntityOrderByDispatchedAtDesc(entity)
|
||||||
|
|
||||||
|
assertThat(result).isPresent
|
||||||
|
assertThat(result.get().id).isEqualTo(latestLog.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user