Skip to content

Commit

Permalink
Merge branch 'main' into thomas/adr-api-naming-convention
Browse files Browse the repository at this point in the history
  • Loading branch information
thoomasbro authored Jan 25, 2024
2 parents b4ef53f + 779ffb4 commit 60e71c3
Show file tree
Hide file tree
Showing 42 changed files with 350 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class CanDeleteControlUnit(
private val reportingRepository: IReportingRepository,
) {
fun execute(controlUnitId: Int): Boolean {
val missions = missionRepository.findByControlUnitId(controlUnitId)
val reportings = reportingRepository.findByControlUnitId(controlUnitId)
val nonDeletedMissions = missionRepository.findByControlUnitId(controlUnitId).filter { !it.isDeleted }
val nonDeletedReportings = reportingRepository.findByControlUnitId(controlUnitId).filter { !it.isDeleted }

return missions.isEmpty() && reportings.isEmpty()
return nonDeletedMissions.isEmpty() && nonDeletedReportings.isEmpty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit
import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.exceptions.CouldNotDeleteException
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository

@UseCase
class DeleteControlUnit(
private val controlUnitRepository: IControlUnitRepository,
private val canDeleteControlUnit: CanDeleteControlUnit,
private val missionRepository: IMissionRepository,
private val reportingRepository: IReportingRepository,
) {
fun execute(controlUnitId: Int) {
if (!canDeleteControlUnit.execute(controlUnitId)) {
Expand All @@ -16,6 +20,29 @@ class DeleteControlUnit(
)
}

val deletedMissions = missionRepository.findByControlUnitId(controlUnitId).filter { it.isDeleted }
val deletedReportings = reportingRepository.findByControlUnitId(controlUnitId).filter { it.isDeleted }

deletedMissions.forEach { deletedMission ->
missionRepository.save(
deletedMission.copy(
controlUnits =
deletedMission.controlUnits.filter {
controlUnit ->
controlUnit.id != controlUnitId
},
),
)
}

deletedReportings.forEach { deletedReporting ->
reportingRepository.save(
deletedReporting.copy(
controlUnitId = null,
),
)
}

return controlUnitRepository.deleteById(controlUnitId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ INSERT INTO public.missions (id, open_by, observations_cacem, facade, start_date
INSERT INTO public.missions (id, open_by, observations_cacem, facade, start_datetime_utc, end_datetime_utc, geom, closed_by, deleted, observations_cnsp, mission_source, closed, mission_order, mission_types) VALUES (39, 'KEI', 'Without black box common. More reduce many trial.', 'MED', '2022-03-02 05:42:47.588693', '2022-05-03 09:16:22.588693', NULL, 'ELI', false, NULL, 'MONITORENV', true, NULL, '{LAND}');
INSERT INTO public.missions (id, open_by, observations_cacem, facade, start_datetime_utc, end_datetime_utc, geom, closed_by, deleted, observations_cnsp, mission_source, closed, mission_order, mission_types) VALUES (40, 'TAM', 'Idea tonight interesting value.', 'NAMO', '2022-03-17 13:29:55.588693', '2022-05-27 02:14:48.588693', NULL, 'RAN', false, NULL, 'MONITORENV', true, NULL, '{LAND}');
INSERT INTO public.missions (id, open_by, observations_cacem, facade, start_datetime_utc, end_datetime_utc, geom, closed_by, deleted, observations_cnsp, mission_source, closed, mission_order, mission_types) VALUES (53, 'CDA', 'Idea tonight interesting value.', 'NAMO', '2022-11-21 13:29:55.588693', '2022-11-23 02:14:48.588693', NULL, 'CDA', false, NULL, 'MONITORENV', false, NULL, '{LAND, SEA}');
INSERT INTO public.missions (id, open_by, observations_cacem, facade, start_datetime_utc, end_datetime_utc, geom, closed_by, deleted, observations_cnsp, mission_source, closed, mission_order, mission_types) VALUES (54, 'CDA', 'A deleted mission with a control unit.', 'NAMO', '2020-01-01 00:00:00.000000', '2020-01-01 01:00:00.000000', NULL, 'ABC', true, NULL, 'MONITORENV', false, NULL, '{LAND}');


SELECT pg_catalog.setval('public.missions_id_seq', 53, true);
SELECT pg_catalog.setval('public.missions_id_seq', 54, true);

INSERT INTO missions_control_units
(mission_id, control_unit_id)
Expand Down Expand Up @@ -121,7 +122,8 @@ VALUES
( 47, 10002, 'A Team - Gimme your number'),
( 25, 10002, 'Full contact'),
( 43, 10018, 'Full contact'),
( 53, 10018, 'Full contact');
( 53, 10018, 'Full contact'),
( 54, 10029, 'Full contact');

INSERT INTO missions_control_resources
(mission_id, control_resource_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,59 @@ import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.time.ZonedDateTime

val FAKE_MISSION =
MissionEntity(
id = 1,
missionTypes = listOf(),
controlUnits = listOf(),
openBy = null,
closedBy = null,
observationsCacem = null,
observationsCnsp = null,
facade = null,
geom = null,
startDateTimeUtc = ZonedDateTime.now(),
endDateTimeUtc = null,
envActions = listOf(),
isClosed = false,
isDeleted = false,
isGeometryComputedFromControls = false,
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isUnderJdp = false,
)

val FAKE_REPORTING =
ReportingEntity(
id = 1,
reportingId = null,
sourceType = null,
semaphoreId = null,
controlUnitId = null,
sourceName = null,
targetType = null,
vehicleType = null,
targetDetails = null,
geom = null,
seaFront = null,
description = null,
reportType = null,
themeId = null,
subThemeIds = null,
actionTaken = null,
isControlRequired = null,
hasNoUnitAvailable = null,
createdAt = ZonedDateTime.now(),
validityTime = null,
isArchived = false,
isDeleted = false,
openBy = null,
missionId = null,
attachedToMissionAtUtc = null,
detachedFromMissionAtUtc = null,
attachedEnvActionId = null,
)

@ExtendWith(SpringExtension::class)
class CanDeleteControlUnitUTests {
@MockBean
Expand All @@ -22,95 +75,70 @@ class CanDeleteControlUnitUTests {
private lateinit var reportingRepository: IReportingRepository

@Test
fun `execute should return true when both missions and reportings are empty`() {
fun `execute should return TRUE there are neither missions nor reportings attached to this control unit`() {
val controlUnitId = 1

given(missionRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())
given(reportingRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())

val result = CanDeleteControlUnit(missionRepository, reportingRepository).execute(controlUnitId)

assertThat(result).isTrue
assertThat(result).isTrue()
}

@Test
fun `execute should return FALSE when there are non-deleted missions attached to this control unit`() {
val controlUnitId = 1

given(missionRepository.findByControlUnitId(controlUnitId)).willReturn(
listOf(FAKE_MISSION),
)
given(reportingRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())

val result = CanDeleteControlUnit(missionRepository, reportingRepository).execute(controlUnitId)

assertThat(result).isFalse()
}

@Test
fun `execute should return false when missions are not empty`() {
fun `execute should return TRUE when there are only deleted missions attached to this control unit`() {
val controlUnitId = 1

given(missionRepository.findByControlUnitId(controlUnitId)).willReturn(
listOf(
MissionEntity(
id = 1,
missionTypes = listOf(),
controlUnits = listOf(),
openBy = null,
closedBy = null,
observationsCacem = null,
observationsCnsp = null,
facade = null,
geom = null,
startDateTimeUtc = ZonedDateTime.now(),
endDateTimeUtc = null,
envActions = listOf(),
isClosed = false,
isDeleted = false,
isGeometryComputedFromControls = false,
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isUnderJdp = false,

),
),
listOf(FAKE_MISSION.copy(isDeleted = true)),
)
given(reportingRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())

val result = CanDeleteControlUnit(missionRepository, reportingRepository).execute(controlUnitId)

assertThat(result).isFalse
assertThat(result).isTrue()
}

@Test
fun `execute should return FALSE when there are non-deleted reportings attached to this control unit`() {
val controlUnitId = 1

given(missionRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())
given(reportingRepository.findByControlUnitId(controlUnitId)).willReturn(
listOf(FAKE_REPORTING),
)

val result = CanDeleteControlUnit(missionRepository, reportingRepository).execute(controlUnitId)

assertThat(result).isFalse()
}

@Test
fun `execute should return false when reportings are not empty`() {
fun `execute should return TRUE when there are only deleted reportings attached to this control unit`() {
val controlUnitId = 1

given(missionRepository.findByControlUnitId(controlUnitId)).willReturn(listOf())
given(reportingRepository.findByControlUnitId(controlUnitId)).willReturn(
listOf(
ReportingEntity(
id = 1,
reportingId = null,
sourceType = null,
semaphoreId = null,
controlUnitId = null,
sourceName = null,
targetType = null,
vehicleType = null,
targetDetails = null,
geom = null,
seaFront = null,
description = null,
reportType = null,
themeId = null,
subThemeIds = null,
actionTaken = null,
isControlRequired = null,
hasNoUnitAvailable = null,
createdAt = ZonedDateTime.now(),
validityTime = null,
isArchived = false,
isDeleted = false,
openBy = null,
missionId = null,
attachedToMissionAtUtc = null,
detachedFromMissionAtUtc = null,
attachedEnvActionId = null,

),
),
listOf(FAKE_REPORTING.copy(isDeleted = true)),
)

val result = CanDeleteControlUnit(missionRepository, reportingRepository).execute(controlUnitId)

assertThat(result).isFalse
assertThat(result).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.nhaarman.mockitokotlin2.given
import com.nhaarman.mockitokotlin2.verify
import fr.gouv.cacem.monitorenv.domain.exceptions.CouldNotDeleteException
import fr.gouv.cacem.monitorenv.domain.repositories.IControlUnitRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
Expand All @@ -18,13 +20,24 @@ class DeleteControlUnitUTests {
@MockBean
private lateinit var canDeleteControlUnit: CanDeleteControlUnit

@MockBean
private lateinit var missionRepository: IMissionRepository

@MockBean
private lateinit var reportingRepository: IReportingRepository

@Test
fun `execute should delete control unit when canDeleteControlUnit returns true`() {
val controlUnitId = 1

given(canDeleteControlUnit.execute(controlUnitId)).willReturn(true)

DeleteControlUnit(controlUnitRepository, canDeleteControlUnit).execute(controlUnitId)
DeleteControlUnit(
controlUnitRepository,
canDeleteControlUnit,
missionRepository,
reportingRepository,
).execute(controlUnitId)

verify(controlUnitRepository).deleteById(controlUnitId)
}
Expand All @@ -36,7 +49,12 @@ class DeleteControlUnitUTests {
given(canDeleteControlUnit.execute(controlUnitId)).willReturn(false)

assertThatThrownBy {
DeleteControlUnit(controlUnitRepository, canDeleteControlUnit).execute(controlUnitId)
DeleteControlUnit(
controlUnitRepository,
canDeleteControlUnit,
missionRepository,
reportingRepository,
).execute(controlUnitId)
}
.isInstanceOf(CouldNotDeleteException::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ context('Back Office > Control Unit Table > Row Actions', () => {
})

it('Should show an error dialog when trying to delete a control unit linked to some missions or reportings', () => {
cy.getTableRowById(10000).clickButton('Supprimer cette unité de contrôle')
cy.getTableRowByText('Cultures marines – DDTM 40').clickButton('Supprimer cette unité de contrôle')

cy.get('.Component-Dialog').should('be.visible')
cy.contains('Suppression impossible').should('be.visible')
})

it("Should allow deletion of a control unit when it's only linked to a (soft) deleted mission", () => {
cy.getTableRowByText('Police Municipale Le Marin 972').clickButton('Supprimer cette unité de contrôle')

cy.get('.Component-Dialog').should('be.visible')
cy.contains("Suppression de l'unité").should('be.visible')
})
})
Loading

0 comments on commit 60e71c3

Please sign in to comment.