Skip to content

Commit

Permalink
Add deletion check for control unit resources in Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 30, 2023
1 parent f9b9873 commit 1bd88c5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ interface IMissionRepository {

fun findByControlUnitId(controlUnitId: Int): List<MissionEntity>

fun findByControlUnitResourceId(controlUnitResourceId: Int): List<MissionEntity>

fun save(mission: MissionEntity): MissionDTO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository

@UseCase
class CanDeleteControlUnitResource(
private val missionRepository: IMissionRepository,
) {
fun execute(controlUnitId: Int): Boolean {
val missions = missionRepository.findByControlUnitId(controlUnitId)

return missions.isEmpty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitResourceRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.exceptions.ForeignKeyConstraintException

@UseCase
class DeleteControlUnitResource(private val controlUnitResourceRepository: IControlUnitResourceRepository) {
class DeleteControlUnitResource(
private val canDeleteControlUnitResource: CanDeleteControlUnitResource,
private val controlUnitResourceRepository: IControlUnitResourceRepository,
) {
fun execute(controlUnitResourceId: Int) {
if (!canDeleteControlUnitResource.execute(controlUnitResourceId)) {
throw ForeignKeyConstraintException(
"Cannot delete control unit resource (ID=$controlUnitResourceId) due to existing relationships.",
)
}

return controlUnitResourceRepository.deleteById(controlUnitResourceId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.gouv.cacem.monitorenv.infrastructure.api.endpoints.publicapi

import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.*
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.inputs.CreateOrUpdateControlUnitResourceDataInput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.BooleanDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.ControlUnitResourceDataOutput
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.publicapi.outputs.FullControlUnitResourceDataOutput
import io.swagger.v3.oas.annotations.Operation
Expand All @@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.*
@Tag(name = "Control Unit Resources")
class ApiControlUnitResourcesController(
private val archiveControlUnitResource: ArchiveControlUnitResource,
private val canDeleteControlUnitResource: CanDeleteControlUnitResource,
private val createOrUpdateControlUnitResource: CreateOrUpdateControlUnitResource,
private val deleteControlUnitResource: DeleteControlUnitResource,
private val getControlUnitResources: GetControlUnitResources,
Expand All @@ -30,6 +32,16 @@ class ApiControlUnitResourcesController(
archiveControlUnitResource.execute(controlUnitResourceId)
}

@GetMapping("/{controlUnitResourceId}/can_delete")
@Operation(summary = "Can this control unit resource be deleted?")
fun canDelete(
@PathParam("Control unit resource ID")
@PathVariable(name = "controlUnitResourceId")
controlUnitResourceId: Int,
): BooleanDataOutput {
return canDeleteControlUnitResource.execute(controlUnitResourceId).let { BooleanDataOutput.get(it) }
}

@PostMapping("", consumes = ["application/json"])
@Operation(summary = "Create a control unit resource")
@ResponseStatus(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class JpaMissionRepository(
}
}

override fun findByControlUnitResourceId(controlUnitResourceId: Int): List<MissionEntity> {
return dbMissionRepository.findByControlUnitResourceId(controlUnitResourceId).map {
it.toMissionEntity(mapper)
}
}

override fun findAll(
startedAfter: Instant,
startedBefore: Instant?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ interface IDBMissionRepository : JpaRepository<MissionModel, Int> {

@Query("SELECT mm FROM MissionModel mm JOIN mm.controlUnits mmcu WHERE mmcu.unit.id = :controlUnitId")
fun findByControlUnitId(controlUnitId: Int): List<MissionModel>

@Query(
"SELECT mm FROM MissionModel mm JOIN mm.controlResources mmcr WHERE mmcr.resource.id = :controlUnitResourceId",
)
fun findByControlUnitResourceId(controlUnitResourceId: Int): List<MissionModel>
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ApiControlUnitResourcesControllerITests {
@MockBean
private lateinit var archiveControlUnitResource: ArchiveControlUnitResource

@MockBean
private lateinit var canDeleteControlUnitResource: CanDeleteControlUnitResource

@MockBean
private lateinit var createOrUpdateControlUnitResource: CreateOrUpdateControlUnitResource

Expand All @@ -51,7 +54,7 @@ class ApiControlUnitResourcesControllerITests {
private lateinit var objectMapper: ObjectMapper

@Test
fun `archive() should archive a control unit resource`() {
fun `archive should archive a control unit resource`() {
val controlUnitResourceId = 1

mockMvc.perform(
Expand All @@ -63,7 +66,20 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `create() should create a resource`() {
fun `canDelete should check if a control unit resource can be deleted`() {
val controlUnitResourceId = 1

given(canDeleteControlUnitResource.execute(controlUnitResourceId)).willReturn(true)

mockMvc.perform(get("/api/v1/control_unit_resources/$controlUnitResourceId/can_delete"))
.andExpect(status().isOk)
.andExpect(MockMvcResultMatchers.jsonPath("$.value").value(true))

BDDMockito.verify(canDeleteControlUnitResource).execute(controlUnitResourceId)
}

@Test
fun `create should create a control unit resource`() {
val expectedCreatedControlUnitResource = ControlUnitResourceEntity(
id = 1,
baseId = 0,
Expand Down Expand Up @@ -100,7 +116,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `delete() should delete a control unit resource`() {
fun `delete should delete a control unit resource`() {
val controlUnitResourceId = 1

mockMvc.perform(
Expand All @@ -112,7 +128,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `get() should get a control unit resource by its ID`() {
fun `get should get a control unit resource by its ID`() {
val expectedFullControlUnitResource = FullControlUnitResourceDTO(
base = BaseEntity(
id = 0,
Expand Down Expand Up @@ -152,7 +168,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `getAll() should get all control unit resources`() {
fun `getAll should get all control unit resources`() {
val expectedFullControlUnitResources = listOf(
FullControlUnitResourceDTO(
base = BaseEntity(
Expand Down Expand Up @@ -221,7 +237,7 @@ class ApiControlUnitResourcesControllerITests {
}

@Test
fun `update() should update a control unit resource`() {
fun `update should update a control unit resource`() {
val expectedUpdatedControlUnitResource = ControlUnitResourceEntity(
id = 1,
baseId = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

@Test
@Transactional
fun `findByControlUnitId() should find the matching missions`() {
fun `findByControlUnitId should find the matching missions`() {
val foundMissions = jpaMissionRepository.findByControlUnitId(10002)

assertThat(foundMissions).hasSize(18)
}

@Test
@Transactional
fun `findByControlUnitResourceId should find the matching missions`() {
val foundMissions = jpaMissionRepository.findByControlUnitResourceId(8)

assertThat(foundMissions).hasSize(3)
}

@Test
@Transactional
fun `save should create a new mission`() {
Expand Down

0 comments on commit 1bd88c5

Please sign in to comment.