diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-08-10 19:25:00 +0600 |
---|---|---|
committer | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-08-11 14:41:30 +0600 |
commit | 9e60edb6216fce615b13f9bcc68d8f86258b85c3 (patch) | |
tree | 04ca0ec035cabd617bc215f470166d507cdc4f27 | |
parent | ef9517e1b8f599fbdd15c474cf7dfea61e803c2f (diff) | |
download | rest-query-9e60edb6216fce615b13f9bcc68d8f86258b85c3.tar.gz rest-query-9e60edb6216fce615b13f9bcc68d8f86258b85c3.tar.bz2 rest-query-9e60edb6216fce615b13f9bcc68d8f86258b85c3.zip |
PDSUI-2188 Created and fixed test for json formats of TriC
7 files changed, 278 insertions, 16 deletions
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( diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatTestSuite.scala new file mode 100644 index 0000000..4a4b3c4 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatTestSuite.scala @@ -0,0 +1,36 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.Arm + +class ArmFormatTestSuite extends FlatSpec with Matchers { + import arm._ + + "Json format for Arm" should "read and write correct JSON" in { + val arm = Arm( + id = LongId(10), + trialId = StringId("NCT000001"), + name = "arm name", + originalName = "orig arm name" + ) + val writtenJson = armFormat.write(arm) + + writtenJson should be("""{"id":10,"trialId":"NCT000001","name":"arm name","originalName":"orig arm name"}""".parseJson) + + val createArmJson = """{"trialId":"NCT000001","name":"arm name"}""".parseJson + val parsedArm = armFormat.read(createArmJson) + val expectedCreatedArm = arm.copy( + id = LongId(0), + originalName = "arm name" + ) + parsedArm should be(expectedCreatedArm) + + val updateArmJson = """{"name":"new arm name"}""".parseJson + val expectedUpdatedArm = arm.copy(name = "new arm name") + val parsedUpdateArm = applyUpdateToArm(updateArmJson, arm) + parsedUpdateArm should be(expectedUpdatedArm) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatTestSuite.scala new file mode 100644 index 0000000..0167620 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatTestSuite.scala @@ -0,0 +1,68 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.{Arm, Criterion, CriterionLabel} +import xyz.driver.pdsuidomain.services.CriterionService.RichCriterion + +class CriterionFormatTestSuite extends FlatSpec with Matchers { + import criterion._ + + "Json format for Criterion" should "read and write correct JSON" in { + val criterion = Criterion( + id = LongId(10), + trialId = StringId("NCT000001"), + text = Some("text"), + isCompound = false, + meta = "{}" + ) + val labels = List( + CriterionLabel( + id = LongId(1L), + labelId = Some(LongId(101)), + criterionId = criterion.id, + categoryId = Some(LongId(3)), + value = Some(true), + isDefining = true + ), + CriterionLabel( + id = LongId(2L), + labelId = Some(LongId(102)), + criterionId = criterion.id, + categoryId = Some(LongId(3)), + value = Some(false), + isDefining = true + ) + ) + val arms = List(LongId[Arm](20), LongId[Arm](21), LongId[Arm](21)) + val richCriterion = RichCriterion( + criterion = criterion, + armIds = arms, + labels = labels + ) + val writtenJson = richCriterionFormat.write(richCriterion) + + writtenJson should be( + """{"text":"text","isCompound":false,"trialId":"NCT000001","arms":[20,21,21],"id":10,"meta":"{}", + "labels":[{"labelId":101,"categoryId":3,"value":"Yes","isDefining":true}, + {"labelId":102,"categoryId":3,"value":"No","isDefining":true}]}""".parseJson) + + val createCriterionJson = + """{"text":"text","isCompound":false,"trialId":"NCT000001", + "arms":[20,21,21],"meta":"{}","labels":[{"labelId":101,"categoryId":3,"value":"Yes","isDefining":true}, + {"labelId":102,"categoryId":3,"value":"No","isDefining":true}]}""".parseJson + val parsedRichCriterion = richCriterionFormat.read(createCriterionJson) + val expectedRichCriterion = richCriterion.copy( + criterion = criterion.copy(id = LongId(0)), + labels = labels.map(_.copy(id = LongId(0), criterionId = LongId(0))) + ) + parsedRichCriterion should be(expectedRichCriterion) + + val updateCriterionJson = """{"meta":null,"text":"new text","isCompound":true}""".parseJson + val expectedUpdatedCriterion = richCriterion.copy(criterion = criterion.copy(text = Some("new text"), isCompound = true, meta = "{}")) + val parsedUpdateCriterion = applyUpdateToCriterion(updateCriterionJson, richCriterion) + parsedUpdateCriterion should be(expectedUpdatedCriterion) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatTestSuite.scala new file mode 100644 index 0000000..280b2a7 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatTestSuite.scala @@ -0,0 +1,63 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.{Intervention, InterventionArm, InterventionType, InterventionWithArms} + +class InterventionFormatTestSuite extends FlatSpec with Matchers { + import intervention._ + + "Json format for Intervention" should "read and write correct JSON" in { + val intervention = Intervention( + id = LongId(1), + trialId = StringId("NCT000001"), + name = "intervention name", + originalName = "orig name", + typeId = Some(LongId(10)), + originalType = Some("orig type"), + description = "", + originalDescription = "", + isActive = true + ) + val arms = List( + InterventionArm(interventionId = intervention.id, armId = LongId(20)), + InterventionArm(interventionId = intervention.id, armId = LongId(21)), + InterventionArm(interventionId = intervention.id, armId = LongId(22)) + ) + val orig = InterventionWithArms( + intervention = intervention, + arms = arms + ) + val writtenJson = interventionWriter.write(orig) + + writtenJson should be( + """{"id":1,"name":"intervention name","typeId":10,"description":"","isActive":true,"arms":[20,21,22], + "trialId":"NCT000001","originalName":"orig name","originalDescription":"","originalType":"orig type"}""".parseJson) + + val updateInterventionJson = """{"description":"descr","arms":[21,22]}""".parseJson + val expectedUpdatedIntervention = orig.copy( + intervention = intervention.copy(description = "descr"), + arms = List( + InterventionArm(interventionId = intervention.id, armId = LongId(21)), + InterventionArm(interventionId = intervention.id, armId = LongId(22)) + ) + ) + val parsedUpdateIntervention = applyUpdateToInterventionWithArms(updateInterventionJson, orig) + parsedUpdateIntervention should be(expectedUpdatedIntervention) + } + + "Json format for InterventionType" should "read and write correct JSON" in { + val interventionType = InterventionType( + id = LongId(10), + name = "type name" + ) + val writtenJson = interventionTypeFormat.write(interventionType) + + writtenJson should be("""{"id":10,"name":"type name"}""".parseJson) + + val parsedInterventionType = interventionTypeFormat.read(writtenJson) + parsedInterventionType should be(interventionType) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatTestSuite.scala new file mode 100644 index 0000000..397d2aa --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatTestSuite.scala @@ -0,0 +1,50 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, StringId, UuidId} +import xyz.driver.pdsuidomain.entities.Trial + +class TrialFormatTestSuite extends FlatSpec with Matchers { + import trial._ + + "Json format for Trial" should "read and write correct JSON" in { + val orig = Trial( + id = StringId("NCT000001"), + externalId = UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), + status = Trial.Status.New, + assignee = None, + previousStatus = None, + previousAssignee = None, + lastActiveUserId = None, + lastUpdate = LocalDateTime.parse("2017-08-10T18:16:19"), + condition = Trial.Condition.Breast, + phase = "", + hypothesisId = Some(UuidId("3b80b2e2-5372-4cf5-a342-6e4ebe10fafd")), + studyDesignId = Some(LongId(321)), + originalStudyDesign = None, + isPartner = false, + overview = None, + overviewTemplate = "", + isUpdated = false, + title = "trial title", + originalTitle = "orig trial title" + ) + val writtenJson = trialWriter.write(orig) + + writtenJson should be ( + """{"isPartner":false,"assignee":null,"lastUpdate":"2017-08-10T18:16:19Z","previousStatus":null, + "isUpdated":false,"overviewTemplate":"","phase":"","originalStudyDesignId":null, + "hypothesisId":"3b80b2e2-5372-4cf5-a342-6e4ebe10fafd","originalTitle":"orig trial title", + "studyDesignId":321,"lastActiveUser":null,"externalid":"40892a07-c638-49d2-9795-1edfefbbcc7c", + "id":"NCT000001","condition":"Breast","status":"New","overview":null,"previousAssignee":null,"title":"trial title"}""".parseJson) + + val updateTrialJson = """{"hypothesisId":null,"overview":"new overview"}""".parseJson + val expectedUpdatedTrial = orig.copy(hypothesisId = None, overview = Some("new overview")) + val parsedUpdateTrial = applyUpdateToTrial(updateTrialJson, orig) + parsedUpdateTrial should be(expectedUpdatedTrial) + + } +} |