Skip to content

Commit

Permalink
Merge pull request #287 from MAIF/default-reads-enabled
Browse files Browse the repository at this point in the history
Default reads for enabled
  • Loading branch information
larousso authored Sep 17, 2019
2 parents e5f8653 + f5b7f24 commit 330ca2d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
14 changes: 7 additions & 7 deletions izanami-server/app/domains/feature/instances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object DefaultFeatureInstances {

val reads: Reads[DefaultFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters").readNullable[JsValue].map(_.getOrElse(JsNull))
)(DefaultFeature.apply _)
Expand Down Expand Up @@ -62,7 +62,7 @@ object GlobalScriptFeatureInstances {

private val reads: Reads[GlobalScriptFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters" \ "ref").read[String]
)(GlobalScriptFeature.apply _)
Expand Down Expand Up @@ -108,7 +108,7 @@ object ScriptFeatureInstances {

private val reads: Reads[ScriptFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters").read[Script](ScriptInstances.reads)
)(ScriptFeature.apply _)
Expand Down Expand Up @@ -140,7 +140,7 @@ object DateRangeFeatureInstances {

val reads: Reads[DateRangeFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters" \ "from")
.read[LocalDateTime](localDateTimeReads(pattern)) and
Expand Down Expand Up @@ -186,7 +186,7 @@ object ReleaseDateFeatureInstances {

val reads: Reads[ReleaseDateFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters" \ "releaseDate")
.read[LocalDateTime](
Expand Down Expand Up @@ -223,7 +223,7 @@ object PercentageFeatureInstances {

val reads: Reads[PercentageFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters" \ "percentage").read[Int](min(0) keepAnd max(100))
)(PercentageFeature.apply _)
Expand Down Expand Up @@ -267,7 +267,7 @@ object HourRangeFeatureInstances {

val reads: Reads[HourRangeFeature] = (
(__ \ "id").read[Key] and
(__ \ "enabled").read[Boolean] and
(__ \ "enabled").read[Boolean].orElse(Reads.pure(false)) and
(__ \ "description").readNullable[String] and
(__ \ "parameters" \ "startAt")
.read[LocalTime](localTimeReads(pattern)) and
Expand Down
86 changes: 86 additions & 0 deletions izanami-server/test/domains/feature/FeatureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ class FeatureSpec extends IzanamiSpec with ScalaFutures with IntegrationPatience

}

"Deserialize DefaultFeature without enabled" in {
import FeatureInstances._
val json = Json.parse("""
|{
| "id": "id",
| "activationStrategy": "NO_STRATEGY"
|}
""".stripMargin)

val result = json.validate[Feature]
result mustBe an[JsSuccess[_]]

result.get must be(DefaultFeature(Key("id"), false, None))
}

"Deserialize GlobalScriptFeature" in {
import FeatureInstances._
val json = Json.parse("""
Expand All @@ -102,6 +117,23 @@ class FeatureSpec extends IzanamiSpec with ScalaFutures with IntegrationPatience

}

"Deserialize GlobalScriptFeature without enabled" in {
import FeatureInstances._
val json = Json.parse("""
|{
| "id": "id",
| "activationStrategy": "GLOBAL_SCRIPT",
| "parameters": { "ref": "ref" }
|}
""".stripMargin)

val result = json.validate[Feature]
result mustBe an[JsSuccess[_]]

result.get must be(GlobalScriptFeature(Key("id"), false, None, "ref"))

}

"Deserialize ScriptFeature" in {
import FeatureInstances._
val json = Json.parse("""
Expand All @@ -120,6 +152,23 @@ class FeatureSpec extends IzanamiSpec with ScalaFutures with IntegrationPatience

}

"Deserialize ScriptFeature without enabled" in {
import FeatureInstances._
val json = Json.parse("""
|{
| "id": "id",
| "activationStrategy": "SCRIPT",
| "parameters": { "script": "script" }
|}
""".stripMargin)

val result = json.validate[Feature]
result mustBe an[JsSuccess[_]]

result.get must be(ScriptFeature(Key("id"), false, None, JavascriptScript("script")))

}

"Deserialize ReleaseDateFeature" in {
import FeatureInstances._
val json =
Expand All @@ -139,6 +188,24 @@ class FeatureSpec extends IzanamiSpec with ScalaFutures with IntegrationPatience

}

"Deserialize ReleaseDateFeature without enabled" in {
import FeatureInstances._
val json =
Json.parse("""
|{
| "id": "id",
| "activationStrategy": "RELEASE_DATE",
| "parameters": { "releaseDate": "01/01/2017 12:12:12" }
|}
""".stripMargin)

val result = json.validate[Feature]
result mustBe an[JsSuccess[_]]

result.get must be(ReleaseDateFeature(Key("id"), false, None, LocalDateTime.of(2017, 1, 1, 12, 12, 12)))

}

"Deserialize ReleaseDateFeature other format" in {
import FeatureInstances._
val json =
Expand Down Expand Up @@ -179,6 +246,25 @@ class FeatureSpec extends IzanamiSpec with ScalaFutures with IntegrationPatience

}

"Deserialize HourRangeFeature without enabled" in {
import FeatureInstances._
val json =
Json.parse("""
|{
| "id": "id",
| "activationStrategy": "HOUR_RANGE",
| "parameters": {
| "startAt": "02:15",
| "endAt": "17:30"
| }
|}""".stripMargin)

val result = json.validate[Feature]
result mustBe an[JsSuccess[_]]

result.get must be(HourRangeFeature(Key("id"), false, None, LocalTime.of(2, 15), LocalTime.of(17, 30)))
}

}

"Feature Serialisation" should {
Expand Down

0 comments on commit 330ca2d

Please sign in to comment.