From 773bf4263a7c9c1e0f24baaee7661071dc08e443 Mon Sep 17 00:00:00 2001 From: Ivan Gabriele Date: Mon, 30 Oct 2023 20:30:38 +0100 Subject: [PATCH] Add missing base use cases tests --- .../use_cases/base/CanDeleteBaseUTests.kt | 71 +++++++++++++++++++ .../domain/use_cases/base/DeleteBaseUTests.kt | 43 +++++++++++ .../use_cases/base/GetBaseByIdUTests.kt | 37 ++++++++++ .../domain/use_cases/base/GetBasesUTests.kt | 48 +++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/CanDeleteBaseUTests.kt create mode 100644 backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/DeleteBaseUTests.kt create mode 100644 backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBaseByIdUTests.kt create mode 100644 backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBasesUTests.kt diff --git a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/CanDeleteBaseUTests.kt b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/CanDeleteBaseUTests.kt new file mode 100644 index 0000000000..00e00e4a89 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/CanDeleteBaseUTests.kt @@ -0,0 +1,71 @@ +package fr.gouv.cacem.monitorenv.domain.use_cases.base + +import com.nhaarman.mockitokotlin2.given +import fr.gouv.cacem.monitorenv.domain.entities.base.BaseEntity +import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceEntity +import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceType +import fr.gouv.cacem.monitorenv.domain.repositories.IBaseRepository +import fr.gouv.cacem.monitorenv.domain.use_cases.base.dtos.FullBaseDTO +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.test.context.junit.jupiter.SpringExtension + +@ExtendWith(SpringExtension::class) +class CanDeleteBaseUTests { + @MockBean + private lateinit var baseRepository: IBaseRepository + + @Test + fun `execute should return true when control unit resources are empty`() { + val baseId = 1 + val fullBase = FullBaseDTO( + base = BaseEntity( + id = 1, + latitude = 0.0, + longitude = 0.0, + name = "Base Name", + ), + controlUnitResources = listOf(), + ) + + given(baseRepository.findById(baseId)).willReturn(fullBase) + + val result = CanDeleteBase(baseRepository).execute(baseId) + + assertThat(result).isTrue + } + + @Test + fun `execute should return false when control unit resources are not empty`() { + val baseId = 1 + val fullBase = FullBaseDTO( + base = BaseEntity( + id = 1, + latitude = 0.0, + longitude = 0.0, + name = "Base Name", + ), + controlUnitResources = listOf( + ControlUnitResourceEntity( + id = 0, + baseId = 1, + controlUnitId = 0, + isArchived = false, + name = "Control Unit Resource Name", + note = null, + photo = null, + type = ControlUnitResourceType.BARGE, + + ), + ), + ) + + given(baseRepository.findById(baseId)).willReturn(fullBase) + + val result = CanDeleteBase(baseRepository).execute(baseId) + + assertThat(result).isFalse + } +} diff --git a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/DeleteBaseUTests.kt b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/DeleteBaseUTests.kt new file mode 100644 index 0000000000..62c58bcad2 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/DeleteBaseUTests.kt @@ -0,0 +1,43 @@ +package fr.gouv.cacem.monitorenv.domain.use_cases.base + +import com.nhaarman.mockitokotlin2.given +import com.nhaarman.mockitokotlin2.verify +import fr.gouv.cacem.monitorenv.domain.repositories.IBaseRepository +import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.exceptions.ForeignKeyConstraintException +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.test.context.junit.jupiter.SpringExtension + +@ExtendWith(SpringExtension::class) +class DeleteBaseUTests { + @MockBean + private lateinit var baseRepository: IBaseRepository + + @MockBean + private lateinit var canDeleteBase: CanDeleteBase + + @Test + fun `execute should delete when canDeleteBase returns true`() { + val baseId = 1 + + given(canDeleteBase.execute(baseId)).willReturn(true) + + DeleteBase(baseRepository, canDeleteBase).execute(baseId) + + verify(baseRepository).deleteById(baseId) + } + + @Test + fun `execute should throw ForeignKeyConstraintException when canDeleteBase returns false`() { + val baseId = 1 + + given(canDeleteBase.execute(baseId)).willReturn(false) + + assertThatThrownBy { + DeleteBase(baseRepository, canDeleteBase).execute(baseId) + } + .isInstanceOf(ForeignKeyConstraintException::class.java) + } +} diff --git a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBaseByIdUTests.kt b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBaseByIdUTests.kt new file mode 100644 index 0000000000..bad084dca7 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBaseByIdUTests.kt @@ -0,0 +1,37 @@ +package fr.gouv.cacem.monitorenv.domain.use_cases.base + +import com.nhaarman.mockitokotlin2.given +import fr.gouv.cacem.monitorenv.domain.entities.base.BaseEntity +import fr.gouv.cacem.monitorenv.domain.repositories.IBaseRepository +import fr.gouv.cacem.monitorenv.domain.use_cases.base.dtos.FullBaseDTO +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.test.context.junit.jupiter.SpringExtension + +@ExtendWith(SpringExtension::class) +class GetBaseByIdUTests { + @MockBean + private lateinit var baseRepository: IBaseRepository + + @Test + fun `execute should return a base by its ID`() { + val baseId = 1 + val fullBase = FullBaseDTO( + base = BaseEntity( + id = 1, + latitude = 0.0, + longitude = 0.0, + name = "Base Name", + ), + controlUnitResources = listOf(), + ) + + given(baseRepository.findById(baseId)).willReturn(fullBase) + + val result = GetBaseById(baseRepository).execute(baseId) + + assertThat(result).isEqualTo(fullBase) + } +} diff --git a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBasesUTests.kt b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBasesUTests.kt new file mode 100644 index 0000000000..93ed2afa1a --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/base/GetBasesUTests.kt @@ -0,0 +1,48 @@ +package fr.gouv.cacem.monitorenv.domain.use_cases.base + +import com.nhaarman.mockitokotlin2.given +import fr.gouv.cacem.monitorenv.domain.entities.base.BaseEntity +import fr.gouv.cacem.monitorenv.domain.repositories.IBaseRepository +import fr.gouv.cacem.monitorenv.domain.use_cases.base.dtos.FullBaseDTO +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.test.context.junit.jupiter.SpringExtension + +@ExtendWith(SpringExtension::class) +class GetBasesUTests { + @MockBean + private lateinit var baseRepository: IBaseRepository + + @Test + fun `execute should return all bases`() { + val bases = listOf( + FullBaseDTO( + base = BaseEntity( + id = 1, + latitude = 0.0, + longitude = 0.0, + name = "Base Name", + ), + controlUnitResources = listOf(), + ), + FullBaseDTO( + base = BaseEntity( + id = 2, + latitude = 0.0, + longitude = 0.0, + name = "Base Name 2", + ), + controlUnitResources = listOf(), + ), + ) + + given(baseRepository.findAll()).willReturn(bases) + + val result = GetBases(baseRepository).execute() + + assertThat(result.size).isEqualTo(1) + assertThat(result).isEqualTo(bases) + } +}