From 9e60edb6216fce615b13f9bcc68d8f86258b85c3 Mon Sep 17 00:00:00 2001 From: Kseniya Tomskikh Date: Thu, 10 Aug 2017 19:25:00 +0600 Subject: PDSUI-2188 Created and fixed test for json formats of TriC --- .../formats/json/sprayformats/common.scala | 11 ++++- .../formats/json/sprayformats/criterion.scala | 48 ++++++++++++++++++---- .../formats/json/sprayformats/trial.scala | 18 ++++---- 3 files changed, 61 insertions(+), 16 deletions(-) (limited to 'src/main/scala') diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala index fbb0258..4dadb11 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala @@ -1,6 +1,7 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats -import java.time.{LocalDate, LocalDateTime} +import java.time.format.DateTimeFormatter +import java.time.{LocalDate, LocalDateTime, ZonedDateTime} import spray.json._ import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId} @@ -39,6 +40,14 @@ object common { } } + implicit def zonedDateTimeFormat = new RootJsonFormat[ZonedDateTime] { + override def write(date: ZonedDateTime): JsString = JsString(date.toString) + override def read(json: JsValue): ZonedDateTime = json match { + case JsString(value) => ZonedDateTime.parse(value) + case _ => deserializationError(s"Expected date as ZonedDateTime, but got $json") + } + } + implicit def dateFormat = new RootJsonFormat[LocalDate] { override def write(date: LocalDate): JsString = JsString(date.toString) override def read(json: JsValue): LocalDate = json match { diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala index 83657f5..732bcad 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala @@ -57,6 +57,45 @@ object criterion { case _ => deserializationError(s"Expected Json Object as CriterionLabel, but got $json") } + def applyUpdateToCriterion(json: JsValue, orig: RichCriterion): RichCriterion = json match { + case JsObject(fields) => + val text = fields + .get("text") + .map(_.convertTo[String]) + + val isCompound = fields + .get("isCompound") + .exists(_.convertTo[Boolean]) + + val meta = fields + .get("meta") + .map(_.convertTo[Option[String]].getOrElse("{}")) + .getOrElse(orig.criterion.meta) + + val arms = fields + .get("arms") + .map(_.convertTo[Option[List[LongId[Arm]]]].getOrElse(List.empty[LongId[Arm]])) + .getOrElse(orig.armIds) + + val labels = fields + .get("labels") + .map(_.convertTo[Option[List[JsValue]]].getOrElse(List.empty[JsValue])) + .map(_.map(l => jsValueToCriterionLabel(l, orig.criterion.id))) + .getOrElse(orig.labels) + + orig.copy( + criterion = orig.criterion.copy( + meta = meta, + text = text, + isCompound = isCompound + ), + armIds = arms, + labels = labels + ) + + case _ => deserializationError(s"Expected Json Object as partial Criterion, but got $json") + } + val richCriterionFormat = new RootJsonFormat[RichCriterion] { override def write(obj: RichCriterion): JsValue = JsObject( @@ -71,11 +110,6 @@ object criterion { override def read(json: JsValue): RichCriterion = json match { case JsObject(fields) => - val id = fields - .get("id") - .map(_.convertTo[LongId[Criterion]]) - .getOrElse(LongId[Criterion](0)) - val trialId = fields .get("trialId") .map(_.convertTo[StringId[Trial]]) @@ -102,12 +136,12 @@ object criterion { val labels = fields .get("labels") .map(_.convertTo[List[JsValue]]) - .map(_.map(l => jsValueToCriterionLabel(l, id))) + .map(_.map(l => jsValueToCriterionLabel(l, LongId(0)))) .getOrElse(List.empty[CriterionLabel]) RichCriterion( criterion = Criterion( - id = id, + id = LongId(0), trialId = trialId, text = text, isCompound = isCompound, diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala index e7b6d54..c1751bf 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala @@ -1,5 +1,7 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats +import java.time.{ZoneId, ZonedDateTime} + import spray.json._ import xyz.driver.core.json.EnumJsonFormat import xyz.driver.pdsuicommon.domain.{LongId, UuidId} @@ -33,7 +35,7 @@ object trial { JsObject( "id" -> obj.id.toJson, "externalid" -> obj.externalId.toJson, - "lastUpdate" -> obj.lastUpdate.toJson, + "lastUpdate" -> ZonedDateTime.of(obj.lastUpdate, ZoneId.of("Z")).toJson, "status" -> obj.status.toJson, "assignee" -> obj.assignee.toJson, "previousStatus" -> obj.previousStatus.toJson, @@ -57,22 +59,22 @@ object trial { case JsObject(fields) => val hypothesisId = fields .get("hypothesisId") - .map(_.convertTo[UuidId[Hypothesis]]) - .orElse(orig.hypothesisId) + .map(_.convertTo[Option[UuidId[Hypothesis]]]) + .getOrElse(orig.hypothesisId) val studyDesignId = fields .get("studyDesignId") - .map(_.convertTo[LongId[StudyDesign]]) - .orElse(orig.studyDesignId) + .map(_.convertTo[Option[LongId[StudyDesign]]]) + .getOrElse(orig.studyDesignId) val overview = fields .get("overview") - .map(_.convertTo[String]) - .orElse(orig.overview) + .map(_.convertTo[Option[String]]) + .getOrElse(orig.overview) val title = fields .get("title") - .map(_.convertTo[String]) + .map(_.convertTo[Option[String]].getOrElse("")) .getOrElse(orig.title) orig.copy( -- cgit v1.2.3