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

fix: check actionId is a valid UUID in GetNavActionById.kt #493

Merged
merged 1 commit into from
Jan 13, 2025
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 @@ -5,6 +5,7 @@ import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.MissionNavActionEnti
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.action.INavMissionActionRepository
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.action.GetStatusForAction
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.control.v2.GetControlByActionId2
import fr.gouv.dgampa.rapportnav.domain.utils.isValidUUID
import org.slf4j.LoggerFactory
import java.util.*

Expand All @@ -17,9 +18,9 @@ class GetNavActionById(
private val logger = LoggerFactory.getLogger(GetNavActionById::class.java)

fun execute(actionId: String?): MissionNavActionEntity? {
if (actionId == null) {
logger.error("GetNavActionById received a null actionId")
throw IllegalArgumentException("GetNavActionById should not receive null missionId or actionId null")
if (!isValidUUID(actionId)) {
logger.error("GetNavActionById received an actionId that is not a valid UUID: $actionId, returning null...")
return null
}
return try {
val model = missionActionRepository.findById(UUID.fromString(actionId)).orElse(null) ?: return null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.gouv.dgampa.rapportnav.domain.utils

import java.util.UUID

fun isValidUUID(uuid: String? = null): Boolean =
uuid?.let { runCatching { UUID.fromString(it) }.isSuccess } == true

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.action.v2
import fr.gouv.gmampa.rapportnav.mocks.mission.action.ControlMock
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.Mockito.`when`
import org.mockito.kotlin.anyOrNull
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -34,6 +35,29 @@ class GetNavActionByIdTest {
@MockBean
private lateinit var getStatusForAction: GetStatusForAction

@Test
fun `test execute with null actionId returns null`() {
getNavActionById = GetNavActionById(
missionActionRepository = missionActionRepository,
getStatusForAction = getStatusForAction,
getControlByActionId = getControlByActionId
)

assertThat(getNavActionById.execute(actionId = null)).isNull()
}

@Test
fun `test execute with invalid actionId returns null`() {
getNavActionById = GetNavActionById(
missionActionRepository = missionActionRepository,
getStatusForAction = getStatusForAction,
getControlByActionId = getControlByActionId
)

val invalidActionId = "invalid-uuid"
assertThat(getNavActionById.execute(actionId = invalidActionId)).isNull()
}

@Test
fun `test execute get nav action by id`() {
val missionId = 761
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.gouv.gmampa.rapportnav.domain.utils

import fr.gouv.dgampa.rapportnav.domain.utils.isValidUUID
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class UUIDUtilsTest {

@Test
fun `test valid UUID returns true`() {
val validUUID = "123e4567-e89b-12d3-a456-426614174000"
assertTrue(isValidUUID(validUUID))
}

@Test
fun `test invalid UUID returns false`() {
val invalidUUID = "invalid-uuid"
assertFalse(isValidUUID(invalidUUID))
}

@Test
fun `test null value returns false`() {
assertFalse(isValidUUID(null))
}

@Test
fun `test empty string returns false`() {
val emptyString = ""
assertFalse(isValidUUID(emptyString))
}

@Test
fun `test UUID with incorrect format returns false`() {
val incorrectUUID = "123e4567-e89b-12d3-a456-426614132323232323232400"
assertFalse(isValidUUID(incorrectUUID))
}
}
Loading