Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(398) sort agent by role priority #401

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
}
}
Loading