From 9ba354407ea6c51941265891ab9a080495b9c5b4 Mon Sep 17 00:00:00 2001 From: Claire Dagan Date: Fri, 29 Nov 2024 09:38:03 +0100 Subject: [PATCH] [Mission] add possibility to search mission by id --- .../repositories/JpaMissionRepository.kt | 40 +++++++++---------- .../JpaMissionRepositoryITests.kt | 32 +++++++++++++-- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepository.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepository.kt index 6d61c69fa..05ed3a4aa 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepository.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepository.kt @@ -8,11 +8,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionContr import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO import fr.gouv.cacem.monitorenv.infrastructure.database.model.MissionModel -import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanSubThemeRepository -import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanTagRepository -import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanThemeRepository -import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlUnitResourceRepository -import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBMissionRepository +import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.* import org.apache.commons.lang3.StringUtils import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Pageable @@ -137,22 +133,26 @@ class JpaMissionRepository( return true } - return mission.envActions?.any { action -> - (action as? EnvActionControlEntity)?.infractions?.any { infraction -> - listOf( - infraction.imo, - infraction.mmsi, - infraction.registrationNumber, - infraction.vesselName, - infraction.companyName, - infraction.controlledPersonIdentity, - ).any { field -> - !field.isNullOrBlank() && - normalizeField(field) - .contains(normalizeField(searchQuery), ignoreCase = true) - } + return mission.id?.toString()?.let { + normalizeField(it) + .contains(normalizeField(searchQuery), ignoreCase = true) + } == true || + mission.envActions?.any { action -> + (action as? EnvActionControlEntity)?.infractions?.any { infraction -> + listOf( + infraction.imo, + infraction.mmsi, + infraction.registrationNumber, + infraction.vesselName, + infraction.companyName, + infraction.controlledPersonIdentity, + ).any { field -> + !field.isNullOrBlank() && + normalizeField(field) + .contains(normalizeField(searchQuery), ignoreCase = true) + } + } ?: false } ?: false - } ?: false } private fun normalizeField(input: String): String { diff --git a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepositoryITests.kt b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepositoryITests.kt index 28289e472..619afba71 100644 --- a/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepositoryITests.kt +++ b/backend/src/test/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/JpaMissionRepositoryITests.kt @@ -35,16 +35,19 @@ import org.springframework.dao.InvalidDataAccessApiUsageException import org.springframework.test.context.junit.jupiter.SpringExtension import org.springframework.transaction.annotation.Transactional import java.time.ZonedDateTime -import java.util.UUID +import java.util.* @ExtendWith(SpringExtension::class) @Import(DataSourceProxyBeanPostProcessor::class) class JpaMissionRepositoryITests : AbstractDBTests() { - @Autowired private val customQueryCountListener: CustomQueryCountListener? = null + @Autowired + private val customQueryCountListener: CustomQueryCountListener? = null - @Autowired private lateinit var jpaMissionRepository: JpaMissionRepository + @Autowired + private lateinit var jpaMissionRepository: JpaMissionRepository - @Autowired private lateinit var jpaControlUnitRepository: JpaControlUnitRepository + @Autowired + private lateinit var jpaControlUnitRepository: JpaControlUnitRepository @Autowired private lateinit var jpaControlUnitResourceRepository: JpaControlUnitResourceRepository @@ -348,6 +351,27 @@ class JpaMissionRepositoryITests : AbstractDBTests() { println("Number of Queries Executed: $queryCount") } + @Test + @Transactional + fun `findAll Should return filtered missions when search query is an id`() { + // When + val missions = + jpaMissionRepository.findAllFullMissions( + startedAfter = ZonedDateTime.parse("2000-01-01T00:01:00Z").toInstant(), + startedBefore = null, + missionTypes = null, + seaFronts = null, + missionStatuses = null, + pageNumber = null, + pageSize = null, + searchQuery = "53", + ) + assertThat(missions).hasSize(1) + + val queryCount = customQueryCountListener!!.getQueryCount() + println("Number of Queries Executed: $queryCount") + } + @Test @Transactional fun `findAll with pagenumber and pagesize Should return subset of missions`() {