Skip to content

Commit

Permalink
Add missing administration use cases tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 30, 2023
1 parent 541b729 commit 94bf685
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.exceptions.UnarchivedChildException
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.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 ArchiveAdministrationUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@MockBean
private lateinit var canArchiveAdministration: CanArchiveAdministration

@Test
fun `execute should archive when canArchive returns true`() {
val administrationId = 1

given(canArchiveAdministration.execute(administrationId)).willReturn(true)

ArchiveAdministration(administrationRepository, canArchiveAdministration).execute(administrationId)

verify(administrationRepository).archiveById(administrationId)
}

@Test
fun `execute should throw UnarchivedChildException when canArchive() returns false`() {
val administrationId = 1

given(canArchiveAdministration.execute(administrationId)).willReturn(false)

assertThatThrownBy {
ArchiveAdministration(administrationRepository, canArchiveAdministration).execute(administrationId)
}
.isInstanceOf(UnarchivedChildException::class.java)

verifyNoMoreInteractions(administrationRepository)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.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 CanArchiveAdministrationUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@Test
fun `execute should return true when all control units are archived`() {
val administrationId = 1
val fullAdministration = FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 1,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = true,
name = "Control Unit Name",
termsNote = null,
),
),
)

given(administrationRepository.findById(administrationId)).willReturn(fullAdministration)

val result = CanArchiveAdministration(administrationRepository).execute(administrationId)

assertThat(result).isTrue
}

@Test
fun `execute should return false when some control units are not archived`() {
val administrationId = 1
val fullAdministration = FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 1,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = false,
name = "Control Unit Name",
termsNote = null,
),
),
)

given(administrationRepository.findById(administrationId)).willReturn(fullAdministration)

val result = CanArchiveAdministration(administrationRepository).execute(administrationId)

assertThat(result).isFalse
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
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 CanDeleteAdministrationUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@Test
fun `execute should return true when control units are empty`() {
val administrationId = 1
val fullAdministration = FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(),
)

given(administrationRepository.findById(administrationId)).willReturn(fullAdministration)

val result = CanDeleteAdministration(administrationRepository).execute(administrationId)

assertThat(result).isTrue
}

@Test
fun `execute should return false when control units are not empty`() {
val administrationId = 1
val fullAdministration = FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 1,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = true,
name = "Control Unit Name",
termsNote = null,
),
),
)

given(administrationRepository.findById(administrationId)).willReturn(fullAdministration)

val result = CanDeleteAdministration(administrationRepository).execute(administrationId)

assertThat(result).isFalse
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateOrUpdateAdministrationUTests {
private lateinit var administrationRepository: IAdministrationRepository

@Test
fun `execute() should return save() result`() {
fun `execute should return save result`() {
val newAdministration = AdministrationEntity(
isArchived = false,
name = "Administration Name",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import com.nhaarman.mockitokotlin2.verify
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
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 DeleteAdministrationUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@MockBean
private lateinit var canDeleteAdministration: CanDeleteAdministration

@Test
fun `execute should delete when canDeleteAdministration returns true`() {
val administrationId = 1

given(canDeleteAdministration.execute(administrationId)).willReturn(true)

DeleteAdministration(administrationRepository, canDeleteAdministration).execute(administrationId)

verify(administrationRepository).deleteById(administrationId)
}

@Test
fun `execute should throw ForeignKeyConstraintException when canDeleteAdministration returns false`() {
val administrationId = 1

given(canDeleteAdministration.execute(administrationId)).willReturn(false)

assertThatThrownBy {
DeleteAdministration(administrationRepository, canDeleteAdministration).execute(administrationId)
}
.isInstanceOf(ForeignKeyConstraintException::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
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 GetAdministrationByIdUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@Test
fun `execute should return administration by ID`() {
val administrationId = 1
val fullAdministration = FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 1,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = true,
name = "Control Unit Name",
termsNote = null,
),
),
)

given(administrationRepository.findById(administrationId)).willReturn(fullAdministration)

val result = GetAdministrationById(administrationRepository).execute(administrationId)

assertThat(result).isEqualTo(fullAdministration)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.administration

import com.nhaarman.mockitokotlin2.given
import fr.gouv.cacem.monitorenv.domain.entities.administration.AdministrationEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IAdministrationRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.administration.dtos.FullAdministrationDTO
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 GetAdministrationsUTests {
@MockBean
private lateinit var administrationRepository: IAdministrationRepository

@Test
fun `execute should return all administrations`() {
val administrations = listOf(
FullAdministrationDTO(
administration = AdministrationEntity(
id = 1,
name = "Administration Name",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 1,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = true,
name = "Control Unit Name",
termsNote = null,
),
),
),
FullAdministrationDTO(
administration = AdministrationEntity(
id = 2,
name = "Administration Name 2",
isArchived = false,
),
controlUnits = listOf(
ControlUnitEntity(
id = 0,
administrationId = 2,
areaNote = null,
departmentAreaInseeCode = null,
isArchived = true,
name = "Control Unit Name 2",
termsNote = null,
),
),
),
)

given(administrationRepository.findAll()).willReturn(administrations)

val result = GetAdministrations(administrationRepository).execute()

assertThat(result.size).isEqualTo(1)
assertThat(result).isEqualTo(administrations)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateOrUpdateBaseUTests {
private lateinit var baseRepository: IBaseRepository

@Test
fun `execute() should return save() result`() {
fun `execute should return save() result`() {
val newBase = BaseEntity(
latitude = 0.0,
longitude = 0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateOrUpdateControlUnitContactUTests {
private lateinit var controlUnitContactRepository: IControlUnitContactRepository

@Test
fun `execute() should return save() result`() {
fun `execute should return save() result`() {
val newControlUnitContact = ControlUnitContactEntity(
controlUnitId = 2,
email = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CreateOrUpdateControlUnitResourceUTests {
private lateinit var controlUnitResourceRepository: IControlUnitResourceRepository

@Test
fun `execute() should return save() result`() {
fun `execute should return save() result`() {
val newControlUnitResource = ControlUnitResourceEntity(
baseId = 2,
controlUnitId = 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateOrUpdateControlUnitUTests {
private lateinit var controlUnitRepository: IControlUnitRepository

@Test
fun `execute() should return save() result`() {
fun `execute should return save() result`() {
val newControlUnit = ControlUnitEntity(
administrationId = 2,
areaNote = null,
Expand Down

0 comments on commit 94bf685

Please sign in to comment.