From 8849135689440a9bc9ac71b2619fbb7f3b965062 Mon Sep 17 00:00:00 2001 From: Loup Theron Date: Wed, 14 Feb 2024 10:17:10 +0100 Subject: [PATCH] Handle other flag states for national identifier --- .../domain/entities/vessel/Vessel.kt | 52 +++++++++++++- .../domain/entities/vessel/VesselUTests.kt | 71 +++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/VesselUTests.kt diff --git a/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/Vessel.kt b/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/Vessel.kt index bcfbed59b7..5eb9c33ee8 100644 --- a/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/Vessel.kt +++ b/backend/src/main/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/Vessel.kt @@ -37,8 +37,58 @@ data class Vessel( val underCharter: Boolean? = null, ) { fun getNationalIdentifier(): String { - val identifier = internalReferenceNumber?.replace("FRA000", "") ?: "" + val internalReferenceNumberCountryCode = LIKELY_CONTROLLED_COUNTRY_CODES.find { countryAlpha3 -> internalReferenceNumber?.contains(countryAlpha3) ?: false } + val identifier = internalReferenceNumber?.replace("${internalReferenceNumberCountryCode}000", "") ?: "" + + if (districtCode.isNullOrEmpty()) { + return identifier + } return "$districtCode$identifier" } } + +val LIKELY_CONTROLLED_COUNTRY_CODES = listOf( + CountryCode.FR.alpha3, + CountryCode.RU.alpha3, + CountryCode.KR.alpha3, + CountryCode.JP.alpha3, + CountryCode.LY.alpha3, + CountryCode.VE.alpha3, + CountryCode.SC.alpha3, + CountryCode.AU.alpha3, + CountryCode.CN.alpha3, + CountryCode.MG.alpha3, + CountryCode.BR.alpha3, + CountryCode.PL.alpha3, + CountryCode.VU.alpha3, + CountryCode.KM.alpha3, + CountryCode.MC.alpha3, + CountryCode.BW.alpha3, + CountryCode.AI.alpha3, + CountryCode.LK.alpha3, + CountryCode.TW.alpha3, + CountryCode.TT.alpha3, + CountryCode.FO.alpha3, + CountryCode.GY.alpha3, + CountryCode.PA.alpha3, + CountryCode.SX.alpha3, + CountryCode.TN.alpha3, + CountryCode.TR.alpha3, + CountryCode.AL.alpha3, + CountryCode.GD.alpha3, + CountryCode.DZ.alpha3, + CountryCode.SX.alpha3, + CountryCode.BE.alpha3, + CountryCode.DK.alpha3, + CountryCode.DE.alpha3, + CountryCode.EE.alpha3, + CountryCode.IE.alpha3, + CountryCode.ES.alpha3, + CountryCode.IT.alpha3, + CountryCode.NL.alpha3, + CountryCode.NO.alpha3, + CountryCode.PT.alpha3, + CountryCode.SE.alpha3, + CountryCode.GB.alpha3, +) diff --git a/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/VesselUTests.kt b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/VesselUTests.kt new file mode 100644 index 0000000000..4542dd2163 --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/cnsp/monitorfish/domain/entities/vessel/VesselUTests.kt @@ -0,0 +1,71 @@ +package fr.gouv.cnsp.monitorfish.domain.entities.vessel + +import com.neovisionaries.i18n.CountryCode +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assertions.* +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 VesselUTests { + @Test + fun `getNationalIdentifier should return the identifier for a FR vessel`() { + // Given + val vessel = Vessel( + id = 1, + internalReferenceNumber = "FRA00022680", + vesselName = "MY AWESOME VESSEL", + flagState = CountryCode.FR, + declaredFishingGears = listOf("Trémails"), + vesselType = "Fishing", + districtCode = "AY", + ) + + // When + val nationalIdentifier = vessel.getNationalIdentifier() + + // Then + assertThat(nationalIdentifier).isEqualTo("AY22680") + } + + @Test + fun `getNationalIdentifier should return the identifier for a FR vessel When district code is null`() { + // Given + val vessel = Vessel( + id = 1, + internalReferenceNumber = "FRA00022680", + vesselName = "MY AWESOME VESSEL", + flagState = CountryCode.FR, + declaredFishingGears = listOf("Trémails"), + vesselType = "Fishing", + districtCode = "", + ) + + // When + val nationalIdentifier = vessel.getNationalIdentifier() + + // Then + assertThat(nationalIdentifier).isEqualTo("22680") + } + + @Test + fun `getNationalIdentifier should return the identifier for a GB vessel`() { + // Given + val vessel = Vessel( + id = 1, + internalReferenceNumber = "GBR00022680", + vesselName = "MY AWESOME VESSEL", + flagState = CountryCode.GB, + declaredFishingGears = listOf("Trémails"), + vesselType = "Fishing", + districtCode = "AY", + ) + + // When + val nationalIdentifier = vessel.getNationalIdentifier() + + // Then + assertThat(nationalIdentifier).isEqualTo("AY22680") + } +}