com.producement.test.Id.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-annotations Show documentation
Show all versions of test-annotations Show documentation
Helper annotations for testing Spring Boot applications
The newest version!
package com.producement.test
import java.util.UUID
import kotlin.random.Random
import kotlin.reflect.full.instanceParameter
import kotlin.reflect.full.memberFunctions
private val idCache = hashSetOf()
fun randomId(): Long {
val id = Random.nextLong(0, 10000000)
return if (idCache.add(id)) {
id
} else randomId()
}
fun randomUUID() = UUID.randomUUID()
fun randomString() = randomUUID().toString()
// Returns a new instance of a data class where id is null
@Suppress("UNCHECKED_CAST")
val T.asNew: T
get() = this.also {
val copy = it!!::class.memberFunctions.first { it.name == "copy" }
val idParam = copy.parameters.first { it.name == "id" }
return copy.callBy(mapOf(copy.instanceParameter!! to this, idParam to null)) as T
}