Skip to content

Commit

Permalink
fix: check actionId is a valid UUID in GetNavActionById.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Jan 13, 2025
1 parent 7fde903 commit f8243fa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
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 throws exception`() {
getNavActionById = GetNavActionById(
missionActionRepository = missionActionRepository,
getStatusForAction = getStatusForAction,
getControlByActionId = getControlByActionId
)

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

@Test
fun `test execute with invalid actionId throws exception`() {
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))
}
}

0 comments on commit f8243fa

Please sign in to comment.