Skip to content

Commit

Permalink
Add isMeshRequiredForSegment property to gears referential api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Jan 8, 2025
1 parent bf2dcc0 commit a17c229
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class Gear(
val code: String,
val name: String,
val category: String? = null,
var groupId: Int? = null,
val groupId: Int? = null,
val isMeshRequiredForSegment: Boolean? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ interface FleetSegmentRepository {
currentYear: Int,
nextYear: Int,
)

fun findAllSegmentsGearsWithRequiredMesh(year: Int): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,48 @@ package fr.gouv.cnsp.monitorfish.domain.use_cases.gear
import fr.gouv.cnsp.monitorfish.config.UseCase
import fr.gouv.cnsp.monitorfish.domain.entities.gear.Gear
import fr.gouv.cnsp.monitorfish.domain.exceptions.CodeNotFoundException
import fr.gouv.cnsp.monitorfish.domain.repositories.FleetSegmentRepository
import fr.gouv.cnsp.monitorfish.domain.repositories.GearCodeGroupRepository
import fr.gouv.cnsp.monitorfish.domain.repositories.GearRepository
import org.slf4j.LoggerFactory
import java.time.Clock
import java.time.ZonedDateTime

@UseCase
class GetAllGears(
private val gearRepository: GearRepository,
private val GearCodeGroupRepository: GearCodeGroupRepository,
private val fleetSegmentRepository: FleetSegmentRepository,
private val gearCodeGroupRepository: GearCodeGroupRepository,
private val clock: Clock,
) {
private val logger = LoggerFactory.getLogger(GetAllGears::class.java)

fun execute(): List<Gear> {
val currentYear = ZonedDateTime.now(clock).year

val gearsWithRequiredMesh = fleetSegmentRepository.findAllSegmentsGearsWithRequiredMesh(currentYear)
val allGears = gearRepository.findAll()

val allGearsWithGroup =
allGears
.map {
try {
val gearCodeGroup = GearCodeGroupRepository.find(it.code)
it.groupId = gearCodeGroup.groupId
} catch (e: CodeNotFoundException) {
logger.warn(e.message)
}
it
val groupId =
try {
val gearCodeGroup = gearCodeGroupRepository.find(it.code)

gearCodeGroup.groupId
} catch (e: CodeNotFoundException) {
logger.warn(e.message)

null
}

val isMeshRequiredForSegment = gearsWithRequiredMesh.contains(it.code)

return@map it.copy(
groupId = groupId,
isMeshRequiredForSegment = isMeshRequiredForSegment,
)
}

return allGearsWithGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class GearDataOutput(
val name: String,
val category: String? = null,
val groupId: Int? = null,
val isMeshRequiredForSegment: Boolean
) {
companion object {
fun fromGear(gear: Gear): GearDataOutput {
Expand All @@ -15,6 +16,7 @@ data class GearDataOutput(
name = gear.name,
category = gear.category,
groupId = gear.groupId,
isMeshRequiredForSegment = gear.isMeshRequiredForSegment ?: false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CaffeineConfiguration {
// Segments
val currentSegments = "current_segments"
val segmentsByYear = "segments_by_year"
val segmentsWithGearsMeshCondition = "segments_with_gears_mesh_condition"

// Species
val allSpecies = "all_species"
Expand Down Expand Up @@ -140,6 +141,7 @@ class CaffeineConfiguration {
// Segments
val currentSegmentsCache = buildMinutesCache(currentSegments, ticker, 1)
val segmentsByYearCache = buildSecondsCache(segmentsByYear, ticker, 10)
val segmentsWithGearsMeshConditionCache = buildSecondsCache(segmentsWithGearsMeshCondition, ticker, 10)

// Species
val allSpeciesCache = buildMinutesCache(allSpecies, ticker, oneWeek)
Expand Down Expand Up @@ -183,6 +185,7 @@ class CaffeineConfiguration {
controlUnitsCache,
currentSegmentsCache,
segmentsByYearCache,
segmentsWithGearsMeshConditionCache,
districtCache,
faoAreasCache,
faoAreasSortedByUsageCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class JpaFleetSegmentRepository(
}
}

@Cacheable(value = ["segments_with_gears_mesh_condition"])
override fun findAllSegmentsGearsWithRequiredMesh(year: Int): List<String> {
return dbFleetSegmentRepository.findAllSegmentsGearsHavingMinOrMaxMesh(year)
}

@Transactional
override fun update(
segment: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,18 @@ interface DBFleetSegmentRepository : CrudRepository<FleetSegmentEntity, Long> {
currentYear: Int,
nextYear: Int,
)

@Query(
value = """
SELECT DISTINCT
unnest(gears)
FROM
fleet_segments
WHERE
year = :year AND
(min_mesh IS NOT NULL OR max_mesh IS NOT NULL)
""",
nativeQuery = true,
)
fun findAllSegmentsGearsHavingMinOrMaxMesh(year: Int): List<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.gouv.cnsp.monitorfish.domain.use_cases.gear

import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.given
import fr.gouv.cnsp.monitorfish.domain.entities.gear.Gear
import fr.gouv.cnsp.monitorfish.domain.entities.gear.GearCodeGroup
import fr.gouv.cnsp.monitorfish.domain.exceptions.CodeNotFoundException
import fr.gouv.cnsp.monitorfish.domain.repositories.FleetSegmentRepository
import fr.gouv.cnsp.monitorfish.domain.repositories.GearCodeGroupRepository
import fr.gouv.cnsp.monitorfish.domain.repositories.GearRepository
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.time.Clock

@ExtendWith(SpringExtension::class)
class GetAllGearsUTests {
@MockBean
private lateinit var gearRepository: GearRepository

@MockBean
private lateinit var fleetSegmentRepository: FleetSegmentRepository

@MockBean
private lateinit var gearCodeGroupRepository: GearCodeGroupRepository

companion object {
val fixedClock: Clock = Clock.systemUTC()
}

@Test
fun `execute Should return gears augmented with other properties`() {
// Given
given(fleetSegmentRepository.findAllSegmentsGearsWithRequiredMesh(any())).willReturn(
listOf("PTM", "GTN", "PTB", "GNF", "TBB", "TBS", "OTB", "TB", "SDN", "TM", "SSC", "OTM"),
)
given(gearRepository.findAll()).willReturn(
listOf(
Gear("OTB", "Chaluts de fond à panneaux"),
Gear("DRB", "Dragues remorquées par bateau"),
),
)
given(gearCodeGroupRepository.find(eq("OTB"))).willReturn(
GearCodeGroup(
code = "OTB",
groupId = 1,
),
)
given(gearCodeGroupRepository.find(eq("DRB"))).willThrow(CodeNotFoundException("Code not found"))

// When
val gears = GetAllGears(gearRepository, fleetSegmentRepository, gearCodeGroupRepository, fixedClock).execute()

// Then
assertThat(gears).hasSize(2)
assertThat(gears.first().code).isEqualTo("OTB")
assertThat(gears.first().groupId).isEqualTo(1)
assertThat(gears.first().isMeshRequiredForSegment).isTrue()
assertThat(gears.last().code).isEqualTo("DRB")
assertThat(gears.last().groupId).isNull()
assertThat(gears.last().isMeshRequiredForSegment).isFalse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,20 @@ class JpaFleetSegmentRepositoryITests : AbstractDBTests() {
val updatedFleetSegments = jpaFleetSegmentRepository.findAllByYear(currentYear + 1).sortedBy { it.segment }
assertThat(updatedFleetSegments).hasSize(43)
}

@Test
@Transactional
fun `findAllSegmentsGearsWithRequiredMesh Should return all gears having a min or max mesh`() {
// When
cacheManager.getCache("segments_with_gears_mesh_condition")?.clear()
val gears = jpaFleetSegmentRepository.findAllSegmentsGearsWithRequiredMesh(currentYear)

assertThat(gears).hasSize(26)
assertThat(gears).isEqualTo(
listOf(
"PTM", "GTN", "PTB", "GNF", "TBB", "TBS", "OTT", "TB", "SDN", "TM", "SSC", "OTM", "GTR", "GNC", "SX",
"TBN", "PT", "GN", "SV", "TX", "GEN", "SPR", "GNS", "OT", "TMS", "OTB",
),
)
}
}
1 change: 1 addition & 0 deletions frontend/src/domain/types/Gear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export type Gear = {
code: string
// TODO Use an enum to represent the group ID.
groupId: 1 | 2
isMeshRequiredForSegment: boolean
name: string
}

0 comments on commit a17c229

Please sign in to comment.