-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
345f406
commit aff2b85
Showing
6 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...rc/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package fr.gouv.cacem.monitorenv.domain.validators.reporting | ||
|
||
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingEntity | ||
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum | ||
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode | ||
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException | ||
import fr.gouv.cacem.monitorenv.domain.validators.Validator | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
|
||
private const val NB_CHAR_MAX = 3 | ||
|
||
@Component | ||
class ReportingValidator : Validator<ReportingEntity> { | ||
private val logger = LoggerFactory.getLogger(ReportingValidator::class.java) | ||
|
||
override fun validate(reporting: ReportingEntity) { | ||
logger.info("Validating reporting: ${reporting.id}") | ||
|
||
if (reporting.openBy !== null && reporting.openBy.length != NB_CHAR_MAX) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"Le trigramme \"ouvert par\" doit avoir 3 lettres", | ||
) | ||
} | ||
if (reporting.reportingSources.isEmpty()) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"Une source du signalement est obligatoire", | ||
) | ||
} | ||
if (reporting.validityTime == 0) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"La validité du signalement doit être supérieur à 0", | ||
) | ||
} | ||
reporting.reportingSources.forEach { source -> | ||
when (source.sourceType) { | ||
SourceTypeEnum.SEMAPHORE -> { | ||
if (source.semaphoreId === null || source.controlUnitId !== null || source.sourceName !== null) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"La source du signalement est invalide", | ||
) | ||
} | ||
} | ||
|
||
SourceTypeEnum.CONTROL_UNIT -> { | ||
if (source.semaphoreId !== null || source.controlUnitId === null || source.sourceName !== null) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"La source du signalement est invalide", | ||
) | ||
} | ||
} | ||
|
||
SourceTypeEnum.OTHER -> { | ||
if (source.semaphoreId !== null || source.controlUnitId !== null || source.sourceName === null) { | ||
throw BackendUsageException( | ||
BackendUsageErrorCode.UNVALID_PROPERTY, | ||
"La source du signalement est invalide", | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...st/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidatorUTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package fr.gouv.cacem.monitorenv.domain.validators.reporting | ||
|
||
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException | ||
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReporting | ||
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingSourceControlUnit | ||
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingSourceOther | ||
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingSourceSemaphore | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
class ReportingValidatorUTest { | ||
private val reportingValidator = ReportingValidator() | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["A", "AA", "AAAA"]) | ||
fun `validate should throw an exception if there is a control with openBy is not a trigram`(openBy: String) { | ||
val reporting = aReporting(openBy = openBy) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("Le trigramme \"ouvert par\" doit avoir 3 lettres") | ||
} | ||
|
||
@Test | ||
fun `validate should throw an exception if reportingSource is empty`() { | ||
val reporting = aReporting(reportingSources = listOf()) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("Une source du signalement est obligatoire") | ||
} | ||
|
||
@Test | ||
fun `validate should throw an exception if reportingSource from control unit is invalid`() { | ||
val reporting = aReporting(reportingSources = listOf(aReportingSourceControlUnit().copy(semaphoreId = 1))) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("La source du signalement est invalide") | ||
} | ||
|
||
@Test | ||
fun `validate should throw an exception if reportingSource from semaphore is invalid`() { | ||
val reporting = aReporting(reportingSources = listOf(aReportingSourceSemaphore().copy(sourceName = "test"))) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("La source du signalement est invalide") | ||
} | ||
|
||
@Test | ||
fun `validate should throw an exception if reportingSource from other is invalid`() { | ||
val reporting = aReporting(reportingSources = listOf(aReportingSourceOther().copy(controlUnitId = 1))) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("La source du signalement est invalide") | ||
} | ||
|
||
@Test | ||
fun `validate should throw an exception if validityTime is less than 1`() { | ||
val reporting = aReporting(validityTime = 0) | ||
|
||
val assertThrows = assertThrows(BackendUsageException::class.java) { reportingValidator.validate(reporting) } | ||
assertThat(assertThrows.message).isEqualTo("La validité du signalement doit être supérieur à 0") | ||
} | ||
|
||
@Test | ||
fun `validate should pass for a valid ReportingEntity`() { | ||
val reporting = aReporting() | ||
|
||
reportingValidator.validate(reporting) | ||
} | ||
} |