From ddc701ea51b9189dd9e029caac84e1c285fe142e Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Mon, 3 Feb 2025 22:05:51 +0100 Subject: [PATCH] add valid account annotation - add AccountValidator - add ValidAccount --- .../ltd/hlaeja/validator/AccountValidator.kt | 19 +++ .../ltd/hlaeja/validator/ValidAccount.kt | 15 +++ .../hlaeja/validator/AccountValidatorTest.kt | 109 ++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt create mode 100644 src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt create mode 100644 src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt diff --git a/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt b/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt new file mode 100644 index 0000000..52ec38f --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt @@ -0,0 +1,19 @@ +package ltd.hlaeja.validator + +import jakarta.validation.ConstraintValidator +import jakarta.validation.ConstraintValidatorContext +import ltd.hlaeja.library.accountRegistry.Account + +class AccountValidator : ConstraintValidator { + + override fun isValid(value: Any?, context: ConstraintValidatorContext): Boolean { + return when (value) { + is Account.Request -> value.validate() + else -> true // Default to valid if the type is not a list + } + } + + private fun Account.Request.validate(): Boolean = username.isNotBlank() + && password?.isNotBlank() ?: true + && roles.isNotEmpty() +} diff --git a/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt b/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt new file mode 100644 index 0000000..3803c1f --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt @@ -0,0 +1,15 @@ +package ltd.hlaeja.validator + +import jakarta.validation.Constraint +import jakarta.validation.Payload +import kotlin.annotation.AnnotationRetention.RUNTIME +import kotlin.reflect.KClass + +@Constraint(validatedBy = [AccountValidator::class]) +@Retention(RUNTIME) +@Target(AnnotationTarget.VALUE_PARAMETER) +annotation class ValidAccount( + val message: String = "Roles must not be empty", + val groups: Array> = [], + val payload: Array> = [] +) diff --git a/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt b/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt new file mode 100644 index 0000000..6a36c49 --- /dev/null +++ b/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt @@ -0,0 +1,109 @@ +package ltd.hlaeja.validator + + +import io.mockk.mockk +import ltd.hlaeja.library.accountRegistry.Account +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class AccountValidatorTest { + + val validator = AccountValidator() + + @Test + fun `validate account - success all values`() { + // given + val request = Account.Request( + username = "validUser", + password = "strongPassword", + enabled = true, + roles = listOf("USER", "TEST") + ) + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isTrue + } + + @Test + fun `validate account - success password null`() { + // given + val request = Account.Request( + username = "validUser", + password = null, + enabled = true, + roles = listOf("USER") + ) + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isTrue + } + + @Test + fun `validate account - failed username empty`() { + // given + val request = Account.Request( + username = "", + password = "strongPassword", + enabled = true, + roles = listOf("USER") + ) + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isFalse + } + + @Test + fun `validate account - failed password empty`() { + // given + val request = Account.Request( + username = "validUser", + password = "", + enabled = true, + roles = listOf("USER") + ) + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isFalse + } + + @Test + fun `validate account - failed roles empty`() { + // given + val request = Account.Request( + username = "validUser", + password = "", + enabled = true, + roles = emptyList() + ) + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isFalse + } + + @Test + fun `validate account - success wrong data type`() { + // given + val request = "A string" + + // when + val result = validator.isValid(request, mockk()) + + // then + assertThat(result).isTrue + } +}