From ebbf4d6c43c34f110b405da0e5174174d9a9bd93 Mon Sep 17 00:00:00 2001 From: Loup Theron Date: Thu, 22 Aug 2024 09:19:30 +0200 Subject: [PATCH] Fix positions parsing when network type vary --- .../domain/entities/position/NetworkType.kt | 19 +++++++++++++------ .../entities/position/NetworkTypeUTests.kt | 19 +++++++++++++++++++ .../domain/mappers/NAFMessageMapperUTests.kt | 12 ++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkTypeUTests.kt diff --git a/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkType.kt b/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkType.kt index 0c1409546a..feb65f871c 100644 --- a/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkType.kt +++ b/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkType.kt @@ -1,16 +1,23 @@ package fr.gouv.cnsp.monitorfish.domain.entities.position -enum class NetworkType(val code: String) { - CELLULAR("CEL"), - SATELLITE("SAT"), +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +enum class NetworkType(val codes: List) { + CELLULAR(listOf("CEL", "GSM")), + SATELLITE(listOf("SAT")), ; companion object { - infix fun from(code: String): NetworkType { + private val logger: Logger = LoggerFactory.getLogger(NetworkType::class.java) + + infix fun from(code: String): NetworkType? { return try { - NetworkType.entries.first { it.code == code } + NetworkType.entries.first { it.codes.contains(code) } } catch (e: NoSuchElementException) { - throw NoSuchElementException("NetworkType $code not found.", e) + logger.error("NAF Message parsing : NetworkType $code not found.", e) + + null } } } diff --git a/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkTypeUTests.kt b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkTypeUTests.kt new file mode 100644 index 0000000000..6f1b297b55 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/position/NetworkTypeUTests.kt @@ -0,0 +1,19 @@ +package fr.gouv.cnsp.monitorfish.domain.entities.position + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.test.context.junit.jupiter.SpringExtension + +@ExtendWith(SpringExtension::class) +class CountryCodeUTests { + + @Test + fun `from Should return null if not found`() { + // When + val result = NetworkType.from("INCORRECT") + + // Then + assertThat(result).isNull() + } +} diff --git a/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/mappers/NAFMessageMapperUTests.kt b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/mappers/NAFMessageMapperUTests.kt index cd06993776..cea2ec8f74 100644 --- a/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/mappers/NAFMessageMapperUTests.kt +++ b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/mappers/NAFMessageMapperUTests.kt @@ -218,4 +218,16 @@ internal class NAFMessageMapperUTests { // Then assertThat(position.networkType).isEqualTo(NetworkType.SATELLITE) } + + @Test + internal fun `init Should not throw Whe nthe network type is incorrect`() { + // Given + val naf = "//SR//TM/POS//IR/FRA000123456//NA/MANUEL//RC/FT6951//FS/FRA//XR/TL326095//DA/20200814//TI/0911//LT/+43.0789//LG/+006.1549//SP/000//CO/0//FR/FRA//RD/20200814//RT/0912//MS/INCORRECT//ER//" + + // When + val position = NAFMessageMapper(naf).toPosition() + + // Then + assertThat(position.networkType).isNull() + } }