Skip to content

Commit

Permalink
add test in reporting + fix delete actions linked to reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
thoomasbro committed Nov 2, 2023
1 parent fe2f7fe commit ec9c481
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface IReportingRepository {

fun attachReportingsToMission(reportingIds: List<Int>, missionId: Int)

fun detachDanglingEnvActions(missionId: Int, envActionIds: List<UUID>)

fun count(): Long

fun delete(reportingId: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class CreateOrUpdateMission(
) {
@Throws(IllegalArgumentException::class)
fun execute(
mission: MissionEntity?,
mission: MissionEntity,
): MissionEntity {
require(mission != null) { "No mission to create or update" }
val envActions =
mission.envActions?.map {
when (it.actionType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.inputs.missions.EnvActionAttachedToReportingIds
import java.util.UUID

@UseCase
class CreateOrUpdateMissionWithAttachedReporting(
Expand All @@ -17,10 +18,15 @@ class CreateOrUpdateMissionWithAttachedReporting(
) {
@Throws(IllegalArgumentException::class)
fun execute(
mission: MissionEntity?,
mission: MissionEntity,
attachedReportingIds: List<Int>,
envActionsAttachedToReportingIds: List<EnvActionAttachedToReportingIds>,
): MissionDTO {

if (mission.id != null) {
reportingRepository.detachDanglingEnvActions(mission.id, getListOfEnvActionIds(mission))
}

val savedMission = createOrUpdateMission.execute(mission)
require(savedMission.id != null) { "The mission id is null" }

Expand All @@ -32,4 +38,8 @@ class CreateOrUpdateMissionWithAttachedReporting(

return missionRepository.findFullMissionById(savedMission.id)
}

private fun getListOfEnvActionIds(mission: MissionEntity?) : List<UUID>{
return mission?.envActions?.map { it.id } ?: emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,47 @@ data class EnvActionModel(
@JdbcType(UUIDJdbcType::class)
@Column(name = "id", nullable = false, updatable = false, columnDefinition = "uuid")
val id: UUID,

@Column(name = "action_start_datetime_utc") val actionStartDateTime: Instant? = null,

@Column(name = "action_end_datetime_utc") val actionEndDateTime: Instant? = null,

@JsonSerialize(using = GeometrySerializer::class)
@JsonDeserialize(contentUsing = GeometryDeserializer::class)
@Column(name = "geom")
val geom: Geometry? = null,

@Column(name = "action_type") @Enumerated(EnumType.STRING) val actionType: ActionTypeEnum,

@Type(JsonBinaryType::class)
@Column(name = "value", columnDefinition = "jsonb")
val value: String,

@Column(name = "facade") val facade: String? = null,

@Column(name = "department") val department: String? = null,

@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "mission_id")
@JsonBackReference
val mission: MissionModel,
@Column(name = "is_administrative_control") val isAdministrativeControl: Boolean? = null,

@Column(name = "is_administrative_control")
val isAdministrativeControl: Boolean? = null,

@Column(name = "is_compliance_with_water_regulations_control")
val isComplianceWithWaterRegulationsControl: Boolean? = null,

@Column(name = "is_safety_equipment_and_standards_compliance_control")
val isSafetyEquipmentAndStandardsComplianceControl: Boolean? = null,
@Column(name = "is_seafarers_control") val isSeafarersControl: Boolean? = null,
@OneToMany(fetch = FetchType.EAGER, mappedBy = "attachedEnvAction")

@Column(name = "is_seafarers_control")
val isSeafarersControl: Boolean? = null,

@OneToMany(
fetch = FetchType.EAGER,
mappedBy = "attachedEnvAction",
)
@JsonManagedReference
val attachedReporting: List<ReportingModel>? = listOf(),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ data class MissionModel(
@Column(name = "is_geometry_computed_from_controls", nullable = false)
val isGeometryComputedFromControls: Boolean,
@Column(name = "is_under_jdp", nullable = false) val isUnderJdp: Boolean,

@OneToMany(
mappedBy = "mission",
cascade = [CascadeType.ALL],
Expand All @@ -75,6 +76,7 @@ data class MissionModel(
@JsonManagedReference
@Fetch(value = FetchMode.SUBSELECT)
val envActions: MutableList<EnvActionModel>? = ArrayList(),

@OneToMany(
mappedBy = "mission",
cascade = [CascadeType.ALL],
Expand All @@ -84,6 +86,7 @@ data class MissionModel(
@JsonManagedReference
@Fetch(value = FetchMode.SUBSELECT)
val controlResources: MutableList<MissionControlResourceModel>? = ArrayList(),

@OneToMany(
mappedBy = "mission",
cascade = [CascadeType.ALL],
Expand All @@ -93,6 +96,7 @@ data class MissionModel(
@JsonManagedReference
@Fetch(value = FetchMode.SUBSELECT)
val controlUnits: MutableList<MissionControlUnitModel>? = ArrayList(),

@OneToMany(mappedBy = "mission")
@JsonManagedReference
@Fetch(value = FetchMode.SUBSELECT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class JpaMissionRepository(

val missionModel = MissionModel.fromMissionEntity(mission, mapper, controlUnitResourceModelMap)

return dbMissionRepository.save(missionModel).toMissionDTO(mapper)
return dbMissionRepository.saveAndFlush(missionModel).toMissionDTO(mapper)
}

private fun convertToPGArray(array: List<String>?): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ class JpaReportingRepository(
) : IReportingRepository {

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun attachEnvActionsToReportings(envActionId: UUID, reportingIds: List<Int>) {
dbReportingRepository.attachEnvActionsToReportings(envActionId, reportingIds)
}

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun attachReportingsToMission(reportingIds: List<Int>, missionId: Int) {
dbReportingRepository.attachReportingsToMission(reportingIds, missionId)
}

@Transactional
override fun detachDanglingEnvActions(missionId: Int, envActionIds: List<UUID>) {
dbReportingRepository.detachDanglingEnvActions(missionId, envActionIds)
}

override fun findById(reportingId: Int): ReportingDTO {
return dbReportingRepository.findById(reportingId).get().toReportingDTO(mapper)
}
Expand Down Expand Up @@ -136,7 +139,6 @@ class JpaReportingRepository(
}

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun delete(reportingId: Int) {
dbReportingRepository.delete(reportingId)
}
Expand All @@ -146,19 +148,16 @@ class JpaReportingRepository(
}

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun archiveOutdatedReportings(): Int {
return dbReportingRepository.archiveOutdatedReportings()
}

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun archiveReportings(ids: List<Int>) {
dbReportingRepository.archiveReportings(ids)
}

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
override fun deleteReportings(ids: List<Int>) {
dbReportingRepository.deleteReportings(ids)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.Instant
import java.util.UUID

interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
@Modifying(clearAutomatically = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
Expand All @@ -21,7 +21,7 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun archiveOutdatedReportings(): Int

@Modifying(clearAutomatically = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
Expand All @@ -33,7 +33,7 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun archiveReportings(ids: List<Int>)

@Modifying(clearAutomatically = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
Expand All @@ -60,7 +60,7 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun attachEnvActionsToReportings(envActionId: UUID, reportingIds: List<Int>)

@Modifying(clearAutomatically = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
Expand All @@ -72,7 +72,7 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun delete(id: Int)

@Modifying(clearAutomatically = true)
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
Expand All @@ -84,6 +84,18 @@ interface IDBReportingRepository : JpaRepository<ReportingModel, Int> {
)
fun deleteReportings(ids: List<Int>)

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value =
"""
UPDATE reportings
SET attached_env_action_id = NULL
WHERE mission_id = :missionId AND (:envActionIds IS NULL OR attached_env_action_id NOT IN (:envActionIds))
""",
nativeQuery = true,
)
fun detachDanglingEnvActions(missionId: Int, envActionIds: List<UUID>)

@Query(
value =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ INSERT INTO public.env_actions VALUES ('475d2887-5344-46cd-903b-8cb5e42f9a9c', 4
INSERT INTO public.env_actions VALUES ('16eeb9e8-f30c-430e-b36b-32b4673f81ce', 49, 'NOTE', '{"observations": "Note libre"}', NULL, NULL);
INSERT INTO public.env_actions VALUES ('6d4b7d0a-79ce-47cf-ac26-2024d2b27f28', 49, 'CONTROL', '{"themes": [{"theme": "AMP sans réglementation particulière", "subThemes": ["Contrôle dans une AMP sans réglementation particulière"], "protectedSpecies": []}], "infractions": [{"id": "e56648c1-6ca3-4d5e-a5d2-114aa7c17126", "natinf": ["10231", "10228"], "toProcess": true, "vesselSize": null, "vesselType": null, "companyName": null, "formalNotice": "PENDING", "observations": "RAS", "relevantCourt": "PRE", "infractionType": "WAITING", "registrationNumber": null, "controlledPersonIdentity": "M DURAND"}], "vehicleType": null, "actionTargetType": "INDIVIDUAL", "actionNumberOfControls": 1}', NULL, '0104000020E61000000100000001010000003B0DADC6D4BB01C0A8387A2964714740');

INSERT INTO public.env_actions VALUES ('b8007c8a-5135-4bc3-816f-c69c7b75d807', 34, 'SURVEILLANCE', '{"themes": [{"theme": "Police des espèces protégées et de leurs habitats (faune et flore)", "subThemes": ["Destruction, capture, arrachage", "Atteinte aux habitats d''espèces protégées"], "protectedSpecies": ["FLORA", "BIRDS"]}, {"theme": "Police des mouillages", "subThemes": ["Mouillage individuel", "ZMEL"], "protectedSpecies": []}], "duration": 0.0, "observations": "RAS", "coverMissionZone": true, "protectedSpecies": []}', '2022-07-16 10:03:12.588693', NULL, NULL, NULL, '2022-07-16 12:03:12.588693');
INSERT INTO public.env_actions VALUES ('c52c6f20-e495-4b29-b3df-d7edfb67fdd7', 34, 'CONTROL', '{"themes": [{"theme": "Police des mouillages", "subThemes": ["Mouillage individuel", "ZMEL"], "protectedSpecies": []}], "observations": "RAS", "infractions": [{"id": "5d5b7829-68cd-4436-8c0b-1cc8db7788a0", "natinf": ["10038","10231"], "toProcess": false, "vesselSize": "FROM_24_TO_46m", "vesselType": "COMMERCIAL", "companyName": null, "formalNotice": "PENDING", "observations": "Pas d''observations", "relevantCourt": "LOCAL_COURT", "infractionType": "WITH_REPORT", "registrationNumber": "BALTIK", "controlledPersonIdentity": "John Doe"}], "vehicleType": "VESSEL", "actionTargetType": "VEHICLE", "actionNumberOfControls": 1}', '2022-07-16 09:03:12.588693', '0104000020E610000001000000010100000047A07E6651E3DEBF044620AB65C54840', NULL, NULL, '2022-07-16 12:03:12.588693');
INSERT INTO public.env_actions VALUES ('c52c6f20-e495-4b29-b3df-d7edfb67fdd7', 34, 'SURVEILLANCE', '{"themes": [{"theme": "Police des espèces protégées et de leurs habitats (faune et flore)", "subThemes": ["Destruction, capture, arrachage", "Atteinte aux habitats d''espèces protégées"], "protectedSpecies": ["FLORA", "BIRDS"]}, {"theme": "Police des mouillages", "subThemes": ["Mouillage individuel", "ZMEL"], "protectedSpecies": []}], "duration": 0.0, "observations": "RAS", "coverMissionZone": true, "protectedSpecies": []}', '2022-07-16 10:03:12.588693', NULL, NULL, NULL, '2022-07-16 12:03:12.588693');
INSERT INTO public.env_actions VALUES ('b8007c8a-5135-4bc3-816f-c69c7b75d807', 34, 'CONTROL', '{"themes": [{"theme": "Police des mouillages", "subThemes": ["Mouillage individuel", "ZMEL"], "protectedSpecies": []}], "observations": "RAS", "infractions": [{"id": "5d5b7829-68cd-4436-8c0b-1cc8db7788a0", "natinf": ["10038","10231"], "toProcess": false, "vesselSize": "FROM_24_TO_46m", "vesselType": "COMMERCIAL", "companyName": null, "formalNotice": "PENDING", "observations": "Pas d''observations", "relevantCourt": "LOCAL_COURT", "infractionType": "WITH_REPORT", "registrationNumber": "BALTIK", "controlledPersonIdentity": "John Doe"}], "vehicleType": "VESSEL", "actionTargetType": "VEHICLE", "actionNumberOfControls": 1}', '2022-07-16 09:03:12.588693', '0104000020E610000001000000010100000047A07E6651E3DEBF044620AB65C54840', NULL, NULL, '2022-07-16 12:03:12.588693');

INSERT INTO public.env_actions VALUES ('4d9a3139-6c60-49a5-b443-0e6238a6a120', 41, 'CONTROL', '{"themes": [{"theme": "Police des mouillages", "subThemes": ["Contrôle administratif"], "protectedSpecies": []}], "infractions": [], "vehicleType": null, "observations": "", "actionTargetType": null, "actionNumberOfControls": null}','2022-07-01 02:44:16.588693', NULL, NULL, NULL, NULL, TRUE, TRUE, TRUE, TRUE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@ class CreateOrUpdateMissionUTests {

@MockBean private lateinit var facadeAreasRepository: IFacadeAreasRepository

@Test
fun `execute Should throw an exception when input mission is null`() {
// When
val throwable =
Assertions.catchThrowable {
CreateOrUpdateMission(
departmentRepository = departmentRepository,
missionRepository = missionRepository,
facadeRepository = facadeAreasRepository,
)
.execute(null)
}

// Then
assertThat(throwable).isInstanceOf(IllegalArgumentException::class.java)
assertThat(throwable.message).contains("No mission to create or update")
}

@Test
fun `should return the mission to create or update with computed facade and department info`() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class CreateOrUpdateMissionWithAttachedReportingUTests {

val missionToCreate =
MissionEntity(
id = 100,
missionTypes = listOf(MissionTypeEnum.LAND),
facade = "Outre-Mer",
geom = polygon,
Expand Down Expand Up @@ -156,7 +157,7 @@ class CreateOrUpdateMissionWithAttachedReportingUTests {
)
val envActionAttachedToReportingIds = Pair(envActionControl.id, listOf(1))

given(createOrUpdateMission.execute(anyOrNull())).willReturn(missionToCreate.copy(id = 100))
given(createOrUpdateMission.execute(anyOrNull())).willReturn(missionToCreate)
given(missionRepository.save(anyOrNull()))
.willReturn(MissionDTO(mission = missionToCreate.copy(id = 100)))
given(missionRepository.findFullMissionById(100)).willReturn(expectedCreatedMission)
Expand All @@ -178,7 +179,7 @@ class CreateOrUpdateMissionWithAttachedReportingUTests {
)

// Then

verify(reportingRepository,times(1)).detachDanglingEnvActions(missionId = 100, envActionIds = listOf(envActionControl.id))
verify(reportingRepository, times(1)).attachReportingsToMission(attachedReportingIds, 100)
verify(
reportingRepository,
Expand Down
Loading

0 comments on commit ec9c481

Please sign in to comment.