Skip to content

Commit

Permalink
Merge pull request #401 from MTES-MCT/398-trier-liste-équipage-selon-…
Browse files Browse the repository at this point in the history
…grade

bugfix(398) sort agent by role priority
  • Loading branch information
lwih authored Oct 15, 2024
2 parents 909fe1e + ac6d8a7 commit eaeaf03
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,25 @@ import fr.gouv.dgampa.rapportnav.domain.repositories.mission.crew.IMissionCrewRe
class GetAgentsCrewByMissionId(private val agentCrewRepository: IMissionCrewRepository) {

fun execute(missionId: Int, commentDefaultsToString: Boolean? = false): List<MissionCrewEntity> {
val rolePriority = listOf(
"Commandant",
"Second capitaine",
"Second",
"Chef mécanicien",
"Second mécanicien",
"Mécanicien électricien",
"Mécanicien",
"Chef de quart",
"Maître d’équipage",
"Agent pont",
"Agent machine",
"Agent mécanicien",
"Électricien",
"Cuisinier",
)

return agentCrewRepository.findByMissionId(missionId = missionId)
.map { it.toMissionCrewEntity(commentDefaultsToString) }
.sortedBy { it.id }
.sortedBy { rolePriority.indexOf(it.role.title) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MissionCrewModel(
var missionId: Int,

@Column(name = "comment", nullable = true)
var comment: String?,
var comment: String? = null,

@ManyToOne
@JoinColumn(name = "agent_role_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.crew

import fr.gouv.dgampa.rapportnav.domain.repositories.mission.crew.IMissionCrewRepository
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.crew.GetAgentsCrewByMissionId
import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.AgentModel
import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.AgentRoleModel
import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.MissionCrewModel
import junit.framework.TestCase.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean

@SpringBootTest(classes = [GetAgentsCrewByMissionId::class])
class GetAgentsCrewByMissionIdTest {

@Autowired
private lateinit var getAgentsCrewByMissionId: GetAgentsCrewByMissionId

@MockBean
private lateinit var agentCrewRepository: IMissionCrewRepository


@Test
fun `execute should return sorted list of crew members by role priority`() {

val missionId = 1

val johnDoe = AgentModel(
firstName = "John",
lastName = "Doe",
id = 1
)

val janeDoe = AgentModel(
firstName = "Jane",
lastName = "Doe",
id = 2
)

val alfredDeMusset = AgentModel(
firstName = "Alfred",
lastName = "de Musset",
id = 3
)

val guyDeMaupassant = AgentModel(
firstName = "Guy",
lastName = "de Maupassant",
id = 4
)

val chefMecano = AgentRoleModel(
title = "Chef mécanicien",
id = 1
)

val secondCapitaine = AgentRoleModel(
title = "Second capitaine",
id = 2
)

val cuisinier = AgentRoleModel(
title = "Cuisinier",
id = 3
)

val commandant = AgentRoleModel(
title = "Commandant",
id = 4
)

val crewMembers = listOf(
MissionCrewModel(role = chefMecano, agent = janeDoe, missionId = missionId, id = 1),
MissionCrewModel(role = secondCapitaine, agent = johnDoe, missionId = missionId, id = 2),
MissionCrewModel(role = cuisinier, agent = alfredDeMusset, missionId = missionId, id = 3),
MissionCrewModel(role = commandant, agent = guyDeMaupassant, missionId = missionId, id = 4),
)

`when`(agentCrewRepository.findByMissionId(missionId)).thenReturn(crewMembers)

val sortedCrew = getAgentsCrewByMissionId.execute(missionId, commentDefaultsToString = false)

// Assert
val expectedRoles = listOf("Commandant", "Second capitaine", "Chef mécanicien", "Cuisinier")
assertEquals(expectedRoles, sortedCrew.map { it.role.title })
}
}

0 comments on commit eaeaf03

Please sign in to comment.