package dev.fyloz.trial.colorrecipesexplorer.repository

import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType
import dev.fyloz.trial.colorrecipesexplorer.model.materialType
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import org.junit.jupiter.api.Test
import kotlin.test.*

class MaterialTypeRepositoryTest @Autowired constructor(materialTypeRepository: MaterialTypeRepository, entityManager: TestEntityManager) :
        AbstractNamedJpaRepositoryTest<MaterialType, MaterialTypeRepository>(materialTypeRepository, entityManager) {
    override fun entity(name: String): MaterialType = entity(name = name, prefix = "MAT")
    private fun entity(name: String = "materialType", prefix: String = "MAT"): MaterialType = materialType(name = name, prefix = prefix)

    @Test
    fun `existsByPrefix() returns true when a material type with the given prefix exists`() {
        val materialType = entity()
        entityManager.persist(materialType)

        val found = repository.existsByPrefix(materialType.prefix)

        assertTrue(found)
    }

    @Test
    fun `existsByPrefix() returns false when no material type with the given prefix exists`() {
        val materialType = entity()
        val anotherMaterialType = entity(prefix = "ANO")
        entityManager.persist(anotherMaterialType)

        val found = repository.existsByPrefix(materialType.prefix)

        assertFalse(found)
    }

    @Test
    fun `findAllBySystemTypeIs() returns all system types`() {
        val materialType = entity()
        val systemType = materialType(name = "system type", prefix = "SYT", systemType = true)
        val anotherSystemType = materialType(name = "another system type", prefix = "ASY", systemType = true)

        entityManager.persist(materialType)
        entityManager.persist(systemType)
        entityManager.persist(anotherSystemType)

        val found = repository.findAllBySystemTypeIs(true)

        assertTrue(found.contains(systemType))
        assertTrue(found.contains(anotherSystemType))
        assertFalse(found.contains(materialType))
    }

    @Test
    fun `findAllBySystemTypeIs() returns all material types which are not system types`() {
        val materialType = entity()
        val anotherMaterialType = entity(name = "another material type", prefix = "AMT")
        val systemType = materialType(name = "system type", prefix = "SYT", systemType = true)

        entityManager.persist(materialType)
        entityManager.persist(anotherMaterialType)
        entityManager.persist(systemType)

        val found = repository.findAllBySystemTypeIs(false)

        assertTrue(found.contains(anotherMaterialType))
        assertTrue(found.contains(materialType))
        assertFalse(found.contains(systemType))
    }

    @Test
    fun `findByPrefix() returns the material type with the given prefix`() {
        val materialType = entity()
        entityManager.persist(materialType)

        val found = repository.findByPrefix(materialType.prefix)

        assertNotNull(found)
        assertEquals(materialType, found)
    }

    @Test
    fun `findByPrefix() returns null when no material type with the given prefix exists`() {
        entityManager.persist(entity(prefix = "ANO"))

        val found = repository.findByPrefix(entity().prefix)

        assertNull(found)
    }
}
