From b6abe63cae41e3414e07ec7550b8a498df72fa62 Mon Sep 17 00:00:00 2001 From: larousso Date: Tue, 17 Sep 2019 09:50:12 +0200 Subject: [PATCH 1/2] Default value false for enabled --- izanami-server/app/domains/feature/instances.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/izanami-server/app/domains/feature/instances.scala b/izanami-server/app/domains/feature/instances.scala index 08a03332e..bdc4d1aa7 100644 --- a/izanami-server/app/domains/feature/instances.scala +++ b/izanami-server/app/domains/feature/instances.scala @@ -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 _) @@ -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 _) @@ -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 _) @@ -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 @@ -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]( @@ -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 _) @@ -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 From f5b7f243b35c6a51aa01cd23d45f708e4401f228 Mon Sep 17 00:00:00 2001 From: larousso Date: Tue, 17 Sep 2019 10:22:33 +0200 Subject: [PATCH 2/2] tests --- .../test/domains/feature/FeatureSpec.scala | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/izanami-server/test/domains/feature/FeatureSpec.scala b/izanami-server/test/domains/feature/FeatureSpec.scala index 8ea1c3ffa..46eaed6e0 100644 --- a/izanami-server/test/domains/feature/FeatureSpec.scala +++ b/izanami-server/test/domains/feature/FeatureSpec.scala @@ -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(""" @@ -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(""" @@ -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 = @@ -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 = @@ -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 {