add valid account annotation
- add AccountValidator - add ValidAccount
This commit is contained in:
19
src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt
Normal file
19
src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt
Normal file
@@ -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<ValidAccount, Any> {
|
||||
|
||||
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()
|
||||
}
|
||||
15
src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt
Normal file
15
src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt
Normal file
@@ -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<KClass<out Any>> = [],
|
||||
val payload: Array<KClass<out Payload>> = []
|
||||
)
|
||||
109
src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt
Normal file
109
src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user