Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

NINOA-83 refactored BirthDateVerification to not throw exception when invalid… #23

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions app/v1/models/request/BirthDateVerification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@
package v1.models.request

import play.api.Logger
import play.api.libs.json.{JsString, Reads, Writes, __}
import play.api.libs.json._

sealed trait BirthDateVerification{
sealed trait BirthDateVerification {
val value: String
}

object BirthDateVerification {
private def valueCheck(input: String): BirthDateVerification = {
input match {

private[models] def birthDateValidation: String => Boolean = {
case Unverified.value => true
case Verified.value => true
case VerificationNotKnown.value => true
case CoegConfirmed.value => true
case _ =>
Logger.warn("[BirthDateVerification][valueCheck] birthDateVerification field is invalid")
false
}

implicit val reads: Reads[BirthDateVerification] = for {
birthDateValue <- __.read[String].filter(JsonValidationError("Invalid Birth Date verification"))(birthDateValidation)
} yield {
birthDateValue match {
case Unverified.value => Unverified
case Verified.value => Verified
case VerificationNotKnown.value => VerificationNotKnown
case CoegConfirmed.value => CoegConfirmed
case _ =>
Logger.debug(s"[BirthDateVerification][valueCheck] birthDateVerification field is invalid: $input")
Logger.warn("[BirthDateVerification][valueCheck] birthDateVerification field is invalid")
throw new IllegalArgumentException(s"birthDateVerification field is invalid")
}
}

implicit val reads: Reads[BirthDateVerification] = __.read[String] map valueCheck

implicit val writes: Writes[BirthDateVerification] = Writes[BirthDateVerification] ( verificationValue => JsString(verificationValue.value) )
implicit val writes: Writes[BirthDateVerification] = Writes[BirthDateVerification](verificationValue => JsString(verificationValue.value))
}

case object Unverified extends BirthDateVerification {
Expand Down
51 changes: 47 additions & 4 deletions test/v1/models/request/BirthDateVerificationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,85 @@ class BirthDateVerificationSpec extends UnitSpec {
val coegConfirmedString = JsString("COEG CONFIRMED")

"BirthDateVerification" should {

"correctly parse from Json" when {

"value provided is VERIFIED" in {
verifiedString.as[BirthDateVerification] shouldBe Verified
}

"value provided is UNVERIFIED" in {
unverifiedString.as[BirthDateVerification] shouldBe Unverified
}

"value provided is NOT KNOWN" in {
notKnownString.as[BirthDateVerification] shouldBe VerificationNotKnown
}

"value provided is COEG CONFIRMED" in {
coegConfirmedString.as[BirthDateVerification] shouldBe CoegConfirmed
}
}

"correctly parse to Json" when {
"value provided is VERIFIED" in {
Json.toJson(Verified) shouldBe verifiedString
}

"value provided is UNVERIFIED" in {
Json.toJson(Unverified) shouldBe unverifiedString
}

"value provided is NOT KNOWN" in {
Json.toJson(VerificationNotKnown) shouldBe notKnownString
}

"value provided is COEG CONFIRMED" in {
Json.toJson(CoegConfirmed) shouldBe coegConfirmedString
}
}

"fail to parse from json" when {
"the value provided is not one of the valid values" in {
val expectedException = intercept[IllegalArgumentException] {
JsString("I SAID SO").as[BirthDateVerification]
}
JsString("Incorrect value").validate[BirthDateVerification].isError shouldBe true
}
}
}

"BirthDateVerification .birthDateValidation" when {

"provided with VERIFIED" should {

"return true" in {
BirthDateVerification.birthDateValidation(Verified.value) shouldBe true
}
}

"provided with UNVERIFIED" should {

"return true" in {
BirthDateVerification.birthDateValidation(Unverified.value) shouldBe true
}
}

"provided with NOT KNOWN" should {

"return true" in {
BirthDateVerification.birthDateValidation(VerificationNotKnown.value) shouldBe true
}
}

"provided with COEG CONFIRMED" should {

"return true" in {
BirthDateVerification.birthDateValidation(CoegConfirmed.value) shouldBe true
}
}

"provided with an invalid value" should {

expectedException.getMessage shouldBe "birthDateVerification field is invalid"
"return false" in {
BirthDateVerification.birthDateValidation("Invalid value") shouldBe false
}
}
}
Expand Down