166 lines
6.3 KiB
Kotlin
166 lines
6.3 KiB
Kotlin
package solutions.bitbadger.documents
|
|
|
|
import org.junit.jupiter.api.DisplayName
|
|
import org.junit.jupiter.api.Test
|
|
import org.junit.jupiter.api.assertThrows
|
|
import kotlin.test.assertEquals
|
|
import kotlin.test.assertFalse
|
|
import kotlin.test.assertNotEquals
|
|
import kotlin.test.assertTrue
|
|
|
|
/**
|
|
* Unit tests for the `AutoId` enum
|
|
*/
|
|
@DisplayName("Kotlin | AutoId")
|
|
class AutoIdTest {
|
|
|
|
@Test
|
|
@DisplayName("Generates a UUID string")
|
|
fun generateUUID() =
|
|
assertEquals(32, AutoId.generateUUID().length, "The UUID should have been a 32-character string")
|
|
|
|
@Test
|
|
@DisplayName("Generates a random hex character string of an even length")
|
|
fun generateRandomStringEven() {
|
|
val result = AutoId.generateRandomString(8)
|
|
assertEquals(8, result.length, "There should have been 8 characters in $result")
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Generates a random hex character string of an odd length")
|
|
fun generateRandomStringOdd() {
|
|
val result = AutoId.generateRandomString(11)
|
|
assertEquals(11, result.length, "There should have been 11 characters in $result")
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Generates different random hex character strings")
|
|
fun generateRandomStringIsRandom() {
|
|
val result1 = AutoId.generateRandomString(16)
|
|
val result2 = AutoId.generateRandomString(16)
|
|
assertNotEquals(result1, result2, "There should have been 2 different strings generated")
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId fails for null document")
|
|
fun needsAutoIdFailsForNullDocument() {
|
|
assertThrows<DocumentException> { AutoId.needsAutoId(AutoId.DISABLED, null, "id") }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId fails for missing ID property")
|
|
fun needsAutoIdFailsForMissingId() {
|
|
assertThrows<DocumentException> { AutoId.needsAutoId(AutoId.UUID, IntIdClass(0), "Id") }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false if disabled")
|
|
fun needsAutoIdFalseIfDisabled() =
|
|
assertFalse(AutoId.needsAutoId(AutoId.DISABLED, "", ""), "Disabled Auto ID should always return false")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for Number strategy and byte ID of 0")
|
|
fun needsAutoIdTrueForByteWithZero() =
|
|
assertTrue(AutoId.needsAutoId(AutoId.NUMBER, ByteIdClass(0), "id"), "Number Auto ID with 0 should return true")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for Number strategy and byte ID of non-0")
|
|
fun needsAutoIdFalseForByteWithNonZero() =
|
|
assertFalse(
|
|
AutoId.needsAutoId(AutoId.NUMBER, ByteIdClass(77), "id"),
|
|
"Number Auto ID with 77 should return false"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for Number strategy and short ID of 0")
|
|
fun needsAutoIdTrueForShortWithZero() =
|
|
assertTrue(AutoId.needsAutoId(AutoId.NUMBER, ShortIdClass(0), "id"), "Number Auto ID with 0 should return true")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for Number strategy and short ID of non-0")
|
|
fun needsAutoIdFalseForShortWithNonZero() =
|
|
assertFalse(
|
|
AutoId.needsAutoId(AutoId.NUMBER, ShortIdClass(31), "id"),
|
|
"Number Auto ID with 31 should return false"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for Number strategy and int ID of 0")
|
|
fun needsAutoIdTrueForIntWithZero() =
|
|
assertTrue(AutoId.needsAutoId(AutoId.NUMBER, IntIdClass(0), "id"), "Number Auto ID with 0 should return true")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for Number strategy and int ID of non-0")
|
|
fun needsAutoIdFalseForIntWithNonZero() =
|
|
assertFalse(AutoId.needsAutoId(AutoId.NUMBER, IntIdClass(6), "id"), "Number Auto ID with 6 should return false")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for Number strategy and long ID of 0")
|
|
fun needsAutoIdTrueForLongWithZero() =
|
|
assertTrue(AutoId.needsAutoId(AutoId.NUMBER, LongIdClass(0), "id"), "Number Auto ID with 0 should return true")
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for Number strategy and long ID of non-0")
|
|
fun needsAutoIdFalseForLongWithNonZero() =
|
|
assertFalse(
|
|
AutoId.needsAutoId(AutoId.NUMBER, LongIdClass(2), "id"),
|
|
"Number Auto ID with 2 should return false"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId fails for Number strategy and non-number ID")
|
|
fun needsAutoIdFailsForNumberWithStringId() {
|
|
assertThrows<DocumentException> { AutoId.needsAutoId(AutoId.NUMBER, StringIdClass(""), "id") }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for UUID strategy and blank ID")
|
|
fun needsAutoIdTrueForUUIDWithBlank() =
|
|
assertTrue(
|
|
AutoId.needsAutoId(AutoId.UUID, StringIdClass(""), "id"),
|
|
"UUID Auto ID with blank should return true"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for UUID strategy and non-blank ID")
|
|
fun needsAutoIdFalseForUUIDNotBlank() =
|
|
assertFalse(
|
|
AutoId.needsAutoId(AutoId.UUID, StringIdClass("howdy"), "id"),
|
|
"UUID Auto ID with non-blank should return false"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId fails for UUID strategy and non-string ID")
|
|
fun needsAutoIdFailsForUUIDNonString() {
|
|
assertThrows<DocumentException> { AutoId.needsAutoId(AutoId.UUID, IntIdClass(5), "id") }
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns true for Random String strategy and blank ID")
|
|
fun needsAutoIdTrueForRandomWithBlank() =
|
|
assertTrue(
|
|
AutoId.needsAutoId(AutoId.RANDOM_STRING, StringIdClass(""), "id"),
|
|
"Random String Auto ID with blank should return true"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId returns false for Random String strategy and non-blank ID")
|
|
fun needsAutoIdFalseForRandomNotBlank() =
|
|
assertFalse(
|
|
AutoId.needsAutoId(AutoId.RANDOM_STRING, StringIdClass("full"), "id"),
|
|
"Random String Auto ID with non-blank should return false"
|
|
)
|
|
|
|
@Test
|
|
@DisplayName("needsAutoId fails for Random String strategy and non-string ID")
|
|
fun needsAutoIdFailsForRandomNonString() {
|
|
assertThrows<DocumentException> { AutoId.needsAutoId(AutoId.RANDOM_STRING, ShortIdClass(55), "id") }
|
|
}
|
|
}
|
|
|
|
data class ByteIdClass(val id: Byte)
|
|
data class ShortIdClass(val id: Short)
|
|
data class IntIdClass(val id: Int)
|
|
data class LongIdClass(val id: Long)
|
|
data class StringIdClass(val id: String)
|