Skip to content

Commit

Permalink
tech: add case for envaction dates
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault committed Jan 16, 2025
1 parent 4025640 commit c7d02db
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.gouv.cacem.monitorenv.domain.validators.mission

import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.ActionTargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
Expand Down Expand Up @@ -87,15 +88,14 @@ class MissionValidator : Validator<MissionEntity> {
}

val sumOfNbTarget = control.infractions?.sumOf { infraction -> infraction.nbTarget }
if (sumOfNbTarget != null) {
if (control.actionNumberOfControls == null || sumOfNbTarget > control.actionNumberOfControls) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"Le nombre de cibles excède le nombre total de contrôles",
)
}
if (sumOfNbTarget != 0 && sumOfNbTarget != null && (control.actionNumberOfControls != null && sumOfNbTarget > control.actionNumberOfControls)) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"Le nombre de cibles excède le nombre total de contrôles",
)
}


control.infractions?.forEach { infraction ->
if (infraction.infractionType !== InfractionTypeEnum.WAITING && infraction.natinf?.isEmpty() == true) {
throw BackendUsageException(
Expand Down Expand Up @@ -130,28 +130,33 @@ class MissionValidator : Validator<MissionEntity> {
envAction: EnvActionEntity,
mission: MissionEntity,
) {
if (envAction.actionStartDateTimeUtc?.isBefore(mission.startDateTimeUtc) == true) {
val actionType = if (envAction.actionType === ActionTypeEnum.CONTROL) "du contrôle" else "de la surveillance"
if (envAction.actionStartDateTimeUtc?.isAfter(mission.startDateTimeUtc) == false
&& envAction.actionStartDateTimeUtc?.isEqual(mission.startDateTimeUtc) == false
) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"La date de début du contrôle doit être postérieure à celle du début de mission",
"La date de début $actionType doit être postérieure à celle du début de mission",
)
}
if (envAction.actionStartDateTimeUtc?.isAfter(mission.endDateTimeUtc) == true) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"La date de début du contrôle doit être antérieure à celle de fin de mission",
"La date de début $actionType doit être antérieure à celle de fin de mission",
)
}
if (envAction.actionEndDateTimeUtc?.isAfter(mission.endDateTimeUtc) == true) {
if (envAction.actionEndDateTimeUtc?.isBefore(mission.endDateTimeUtc) == false &&
envAction.actionEndDateTimeUtc?.isEqual(mission.endDateTimeUtc) == false
) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"La date de fin du contrôle doit être antérieure à celle de fin de mission",
"La date de fin $actionType doit être antérieure à celle de fin de mission",
)
}
if (envAction.actionEndDateTimeUtc?.isBefore(mission.startDateTimeUtc) == true) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"La date de fin du contrôle doit être postérieure à celle du début de mission",
"La date de fin $actionType doit être postérieure à celle du début de mission",
)
}
if (envAction.openBy?.length != NB_CHAR_MAX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class EnvActionFixture {
fun anEnvActionSurveillance(
startTime: ZonedDateTime? = null,
endTime: ZonedDateTime? = null,
openBy: String? = null,
openBy: String? = "CDA",
): EnvActionSurveillanceEntity {
return EnvActionSurveillanceEntity(
id = UUID.randomUUID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ class MissionValidatorUTest {
assertThat(assertThrows.message).isEqualTo("Le type de mission est requis")
}


@ParameterizedTest
@ValueSource(strings = ["A", "AA", "AAAA"])
fun `validate should throw an exception if openBy is not a trigram`(openBy: String) {
val mission = aMissionEntity(openBy = openBy)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(assertThrows.message).isEqualTo("Le trigramme \"ouvert par\" doit avoir 3 lettres")
}

@ParameterizedTest
@ValueSource(strings = ["A", "AA", "AAAA"])
fun `validate should throw an exception if completedBy is not a trigram`(completedBy: String) {
val mission = aMissionEntity(completedBy = completedBy)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(assertThrows.message).isEqualTo("Le trigramme \"complété par\" doit avoir 3 lettres")
}

@Test
fun `validate should throw an exception if there is a control with a start date before mission starting date`() {
val startDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
Expand Down Expand Up @@ -98,15 +117,35 @@ class MissionValidatorUTest {
@Test
fun `validate should throw an exception if there is a control with an end date before mission starting date`() {
val startDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
val endDateTimeUtc = ZonedDateTime.parse("2021-03-04T00:00:00.000Z")

val anEnvActionControl = anEnvActionControl(endTime = startDateTimeUtc.minusSeconds(1))
val mission = aMissionEntity(startDateTimeUtc = startDateTimeUtc, envAction = listOf(anEnvActionControl))
val mission = aMissionEntity(
startDateTimeUtc = startDateTimeUtc,
endDateTimeUtc = endDateTimeUtc,
envAction = listOf(anEnvActionControl)
)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(
assertThrows.message,
).isEqualTo("La date de fin du contrôle doit être postérieure à celle du début de mission")
}

@Test
fun `validate should pass if there is a control with an date equal to mission's`() {
val startDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
val endDateTimeUtc = ZonedDateTime.parse("2021-03-04T00:00:00.000Z")
val anEnvActionControl = anEnvActionControl(startTime = startDateTimeUtc, endTime = endDateTimeUtc)
val mission = aMissionEntity(
startDateTimeUtc = startDateTimeUtc,
endDateTimeUtc = endDateTimeUtc,
envAction = listOf(anEnvActionControl)
)

missionValidator.validate(mission)
}

@ParameterizedTest
@EnumSource(value = InfractionTypeEnum::class, names = ["WAITING"], mode = EnumSource.Mode.EXCLUDE)
fun `validate should throw an exception if there is a control with infractionType other than WAITING that doesnt have a NATINF`(
Expand Down Expand Up @@ -143,6 +182,18 @@ class MissionValidatorUTest {
assertThat(assertThrows.message).isEqualTo("Le nombre de cibles excède le nombre total de contrôles")
}

@Test
fun `validate should pass if there is a control with infractions that got less nbTarget than the mission actionNumberOfControls `() {
val anEnvActionControl =
anEnvActionControl(
actionNumberOfControls = 2,
infractions = listOf(anInfraction(nbTarget = 1)),
)
val mission = aMissionEntity(envAction = listOf(anEnvActionControl))

missionValidator.validate(mission)
}

@Test
fun `validate should pass if there is a control with infractionType = WAITING that doesnt have a NATINF`() {
val anEnvActionControl =
Expand Down Expand Up @@ -195,7 +246,7 @@ class MissionValidatorUTest {
val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(
assertThrows.message,
).isEqualTo("La date de début du contrôle doit être postérieure à celle du début de mission")
).isEqualTo("La date de début de la surveillance doit être postérieure à celle du début de mission")
}

@Test
Expand All @@ -213,7 +264,7 @@ class MissionValidatorUTest {
val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(
assertThrows.message,
).isEqualTo("La date de fin du contrôle doit être antérieure à celle de fin de mission")
).isEqualTo("La date de fin de la surveillance doit être antérieure à celle de fin de mission")
}

@Test
Expand All @@ -231,37 +282,39 @@ class MissionValidatorUTest {
val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(
assertThrows.message,
).isEqualTo("La date de fin du contrôle doit être antérieure à celle de fin de mission")
).isEqualTo("La date de fin de la surveillance doit être antérieure à celle de fin de mission")
}

@Test
fun `validate should throw an exception if there is a surveillance with an end date before mission starting date`() {
val startDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
val anEnvActionControl = anEnvActionSurveillance(endTime = startDateTimeUtc.minusSeconds(1))
val mission = aMissionEntity(startDateTimeUtc = startDateTimeUtc, envAction = listOf(anEnvActionControl))
val endDateTimeUtc = ZonedDateTime.parse("2021-03-04T00:00:00.000Z")

val anEnvActionSurveillance = anEnvActionSurveillance(endTime = startDateTimeUtc.minusSeconds(1))
val mission = aMissionEntity(
startDateTimeUtc = startDateTimeUtc,
endDateTimeUtc = endDateTimeUtc,
envAction = listOf(anEnvActionSurveillance)
)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(
assertThrows.message,
).isEqualTo("La date de fin du contrôle doit être postérieure à celle du début de mission")
}

@ParameterizedTest
@ValueSource(strings = ["A", "AA", "AAAA"])
fun `validate should throw an exception if openBy is not a trigram`(openBy: String) {
val mission = aMissionEntity(openBy = openBy)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(assertThrows.message).isEqualTo("Le trigramme \"ouvert par\" doit avoir 3 lettres")
).isEqualTo("La date de fin de la surveillance doit être postérieure à celle du début de mission")
}

@ParameterizedTest
@ValueSource(strings = ["A", "AA", "AAAA"])
fun `validate should throw an exception if completedBy is not a trigram`(completedBy: String) {
val mission = aMissionEntity(completedBy = completedBy)
@Test
fun `validate should pass if there is a surveillance with an date equal to mission's`() {
val startDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
val endDateTimeUtc = ZonedDateTime.parse("2020-03-04T00:00:00.000Z")
val anEnvActionSurveillance = anEnvActionSurveillance(startTime = startDateTimeUtc, endTime = endDateTimeUtc)
val mission = aMissionEntity(
startDateTimeUtc = startDateTimeUtc,
endDateTimeUtc = endDateTimeUtc,
envAction = listOf(anEnvActionSurveillance)
)

val assertThrows = assertThrows(BackendUsageException::class.java) { missionValidator.validate(mission) }
assertThat(assertThrows.message).isEqualTo("Le trigramme \"complété par\" doit avoir 3 lettres")
missionValidator.validate(mission)
}

@Test
Expand Down

0 comments on commit c7d02db

Please sign in to comment.