generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3) Unit Test Validations on Request Models (#78)
* Unit test validations * add gender test * Refactors * Fixes * Fixes * Fixes * Fixes --------- Co-authored-by: Ewan Donovan <ewan.donovan@digital.justice.com>
- Loading branch information
1 parent
aece714
commit c23ac81
Showing
8 changed files
with
328 additions
and
12 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/models/request/Gender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package uk.gov.justice.digital.hmpps.learnerrecordsapi.models.request | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
|
||
enum class Gender(val description: String, val code: Int) { | ||
MALE("MALE", 1), | ||
FEMALE("FEMALE", 2), | ||
NOT_KNOWN("NOT_KNOWN", 0), | ||
NOT_SPECIFIED("NOT_SPECIFIED", 9), | ||
; | ||
|
||
@JsonValue | ||
override fun toString(): String = description | ||
|
||
companion object { | ||
@JsonCreator | ||
@JvmStatic | ||
fun fromString(value: String): Gender = entries.find { it.description.equals(value, ignoreCase = true) } | ||
?: throw IllegalArgumentException("Invalid gender value: $value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/models/request/GenderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package uk.gov.justice.digital.hmpps.learnerrecordsapi.models.request | ||
|
||
import com.fasterxml.jackson.databind.exc.ValueInstantiationException | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class GenderTest { | ||
|
||
private val objectMapper = jacksonObjectMapper() | ||
|
||
@Test | ||
fun `valid gender should parse`() { | ||
objectMapper.readValue("\"MALE\"", Gender::class.java) | ||
objectMapper.readValue("\"FEMALE\"", Gender::class.java) | ||
objectMapper.readValue("\"NOT_KNOWN\"", Gender::class.java) | ||
objectMapper.readValue("\"NOT_SPECIFIED\"", Gender::class.java) | ||
} | ||
|
||
@Test | ||
fun `invalid gender should fail at runtime with an illegal argument exception`() { | ||
assertThrows<ValueInstantiationException> { | ||
objectMapper.readValue("\"STRAWBERRY\"", Gender::class.java) | ||
} | ||
|
||
assertThrows<ValueInstantiationException> { | ||
objectMapper.readValue("\"2\"", Gender::class.java) | ||
} | ||
|
||
assertThrows<ValueInstantiationException> { | ||
objectMapper.readValue("\"1\"", Gender::class.java) | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...uk/gov/justice/digital/hmpps/learnerrecordsapi/models/request/LearnerEventsRequestTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package uk.gov.justice.digital.hmpps.learnerrecordsapi.models.request | ||
|
||
import jakarta.validation.Validation | ||
import jakarta.validation.ValidatorFactory | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import java.time.LocalDate | ||
|
||
class LearnerEventsRequestTest { | ||
|
||
private val factory: ValidatorFactory = Validation.buildDefaultValidatorFactory() | ||
private val validator = factory.validator | ||
|
||
@Test | ||
fun `valid request should pass validation`() { | ||
val request = LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
assertTrue(validator.validate(request).isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `invalid givenNames and familyNames should fail validation`() { | ||
val requestWithSymbolsInNames = LearnerEventsRequest( | ||
givenName = "Firstname!", | ||
familyName = "L@stname", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
val requestWithTooShortNames = LearnerEventsRequest( | ||
givenName = "Fi", | ||
familyName = "La", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
val requestWithTooLongNames = LearnerEventsRequest( | ||
givenName = "F" + "i".repeat(35), | ||
familyName = "L" + "a".repeat(35), | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
assertTrue(validator.validate(requestWithSymbolsInNames).size == 2) | ||
assertTrue(validator.validate(requestWithTooShortNames).size == 2) | ||
assertTrue(validator.validate(requestWithTooLongNames).size == 2) | ||
} | ||
|
||
@Test | ||
fun `invalid uln should fail validation`() { | ||
val requestWithLettersInUln = LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "I2E4S67B90", | ||
dateOfBirth = LocalDate.of(2000, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
val requestWithTooLongUln = LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "1".repeat(50), | ||
dateOfBirth = LocalDate.of(2000, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
val requestWithEmptyUln = LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "", | ||
dateOfBirth = LocalDate.of(2000, 1, 1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
assertTrue(validator.validate(requestWithLettersInUln).size == 1) | ||
assertTrue(validator.validate(requestWithTooLongUln).size == 1) | ||
assertTrue(validator.validate(requestWithEmptyUln).size == 1) | ||
} | ||
|
||
@Test | ||
fun `dateOfBirth in the future should fail validation`() { | ||
val requestWithDateInFuture = LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.now().plusDays(1), | ||
gender = Gender.MALE, | ||
) | ||
|
||
assertTrue(validator.validate(requestWithDateInFuture).size == 1) | ||
} | ||
|
||
@Test | ||
fun `invalid gender should fail at runtime with an illegal argument exception`() { | ||
assertThrows<IllegalArgumentException> { | ||
LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.valueOf("ENGLAND"), | ||
) | ||
} | ||
assertThrows<IllegalArgumentException> { | ||
LearnerEventsRequest( | ||
givenName = "Firstname", | ||
familyName = "Lastname", | ||
uln = "1234567890", | ||
dateOfBirth = LocalDate.of(1990, 1, 1), | ||
gender = Gender.valueOf("1"), | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.