package dev.fyloz.trial.colorrecipesexplorer.service

import com.nhaarman.mockitokotlin2.*
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.CompanyRepository
import dev.fyloz.trial.colorrecipesexplorer.service.model.RecipeService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class CompanyServiceTest : AbstractExternalNamedModelServiceTest<Company, CompanySaveDto, CompanyUpdateDto, CompanyService, CompanyRepository>() {
    private val recipeService: RecipeService = mock()
    override val repository: CompanyRepository = mock()
    override val service: CompanyService = spy(CompanyServiceImpl(repository, recipeService))

    override val entity: Company = company(id = 0L, name = "company")
    override val anotherEntity: Company = company(id = 1L, name = "another company")
    override val entityWithEntityName: Company = company(id = 2L, name = entity.name)
    override val entitySaveDto: CompanySaveDto = spy(companySaveDto())
    override val entityUpdateDto: CompanyUpdateDto = spy(companyUpdateDto(id = entity.id!!))

    @AfterEach
    override fun afterEach() {
        reset(recipeService)
        super.afterEach()
    }

    @Nested
    inner class IsLinkedToRecipes {
        fun `returns true when a given company is linked to one or more recipes`() {
            whenever(recipeService.existsByCompany(entity)).doReturn(true)

            val found = service.isLinkedToRecipes(entity)

            assertTrue(found)
        }

        fun `returns false when a given company is not linked to any recipe`() {
            whenever(recipeService.existsByCompany(entity)).doReturn(false)

            val found = service.isLinkedToRecipes(entity)

            assertFalse(found)
        }
    }
}
