From 442579b27ccbac82cb001a5b02402a593d005977 Mon Sep 17 00:00:00 2001 From: Kseniya Tomskikh Date: Mon, 14 Aug 2017 14:15:14 +0600 Subject: PDSUI-2188 Created tests for export and dictionary API --- .../json/sprayformats/bridgeuploadqueue.scala | 68 +++++++++++ .../formats/json/sprayformats/export.scala | 78 +++++++++++++ .../formats/json/sprayformats/hypothesis.scala | 12 ++ .../json/sprayformats/patientcriterion.scala | 6 +- .../formats/json/sprayformats/studydesign.scala | 12 ++ .../sprayformats/BridgeUploadQueueFormat.scala | 32 ++++++ .../json/sprayformats/ExportFormatSuite.scala | 124 +++++++++++++++++++++ .../json/sprayformats/HypothesisFormatSuite.scala | 28 +++++ .../sprayformats/PatientCriterionFormatSuite.scala | 55 +++++++++ .../PatientEligibleTrialFormatSuite.scala | 62 +++++++++++ .../json/sprayformats/StudyDesignFormatSuite.scala | 24 ++++ 11 files changed, 500 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/export.scala create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/hypothesis.scala create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/BridgeUploadQueueFormat.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExportFormatSuite.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/HypothesisFormatSuite.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientCriterionFormatSuite.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientEligibleTrialFormatSuite.scala create mode 100644 src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala new file mode 100644 index 0000000..77fb4d2 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala @@ -0,0 +1,68 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue +import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue.Item + +object bridgeuploadqueue { + import DefaultJsonProtocol._ + import common._ + + implicit val queueUploadItemFormat: RootJsonFormat[BridgeUploadQueue.Item] = new RootJsonFormat[Item] { + override def write(obj: Item) = + JsObject( + "kind" -> obj.kind.toJson, + "tag" -> obj.tag.toJson, + "created" -> obj.created.toJson, + "attempts" -> obj.attempts.toJson, + "nextAttempt" -> obj.nextAttempt.toJson, + "completed" -> obj.completed.toJson + ) + + override def read(json: JsValue): Item = json match { + case JsObject(fields) => + val kind = fields + .get("kind") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `kind` field: $json")) + + val tag = fields + .get("tag") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `tag` field: $json")) + + val created = fields + .get("created") + .map(_.convertTo[LocalDateTime]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `created` field: $json")) + + val attempts = fields + .get("attempts") + .map(_.convertTo[Int]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `attempts` field: $json")) + + val nextAttempt = fields + .get("nextAttempt") + .map(_.convertTo[LocalDateTime]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `nextAttempt` field: $json")) + + BridgeUploadQueue.Item( + kind = kind, + tag = tag, + created = created, + attempts = attempts, + nextAttempt = nextAttempt, + completed = true, + dependencyKind = None, + dependencyTag = None + ) + + case _ => deserializationError(s"Expected Json Object as BridgeUploadQueue.Item, but got $json") + } + } +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/export.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/export.scala new file mode 100644 index 0000000..20b6ed0 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/export.scala @@ -0,0 +1,78 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import xyz.driver.pdsuidomain.entities.export.patient._ +import xyz.driver.pdsuidomain.entities.export.trial.{ExportTrialArm, ExportTrialLabelCriterion, ExportTrialWithLabels} + +object export { + import DefaultJsonProtocol._ + import common._ + import record._ + + implicit val patientLabelEvidenceDocumentFormat: RootJsonFormat[ExportPatientLabelEvidenceDocument] = jsonFormat5( + ExportPatientLabelEvidenceDocument.apply) + + implicit val patientLabelEvidenceWriter: JsonWriter[ExportPatientLabelEvidence] = + new JsonWriter[ExportPatientLabelEvidence] { + override def write(obj: ExportPatientLabelEvidence): JsValue = + JsObject( + "evidenceId" -> obj.id.toJson, + "labelValue" -> obj.value.toJson, + "evidenceText" -> obj.evidenceText.toJson, + "document" -> obj.document.toJson + ) + } + + implicit val patientLabelWriter: JsonWriter[ExportPatientLabel] = new JsonWriter[ExportPatientLabel] { + override def write(obj: ExportPatientLabel): JsValue = + JsObject( + "labelId" -> obj.id.toJson, + "evidence" -> obj.evidences.map(_.toJson).toJson + ) + } + + implicit val patientWithLabelsWriter: JsonWriter[ExportPatientWithLabels] = new JsonWriter[ExportPatientWithLabels] { + override def write(obj: ExportPatientWithLabels): JsValue = + JsObject( + "patientId" -> obj.patientId.toJson, + "labelVersion" -> obj.labelVersion.toJson, + "labels" -> obj.labels.map(_.toJson).toJson + ) + } + + implicit val trialArmFormat: RootJsonFormat[ExportTrialArm] = jsonFormat2(ExportTrialArm.apply) + + implicit val trialLabelCriterionWriter: JsonWriter[ExportTrialLabelCriterion] = + new JsonWriter[ExportTrialLabelCriterion] { + override def write(obj: ExportTrialLabelCriterion): JsValue = + JsObject( + "value" -> obj.value + .map { + case true => "Yes" + case false => "No" + } + .getOrElse("Unknown") + .toJson, + "labelId" -> obj.labelId.toJson, + "criterionId" -> obj.criterionId.toJson, + "criterionText" -> obj.criteria.toJson, + "armIds" -> obj.armIds.toJson, + "isCompound" -> obj.isCompound.toJson, + "isDefining" -> obj.isDefining.toJson + ) + } + + implicit val trialWithLabelsWriter: JsonWriter[ExportTrialWithLabels] = new JsonWriter[ExportTrialWithLabels] { + override def write(obj: ExportTrialWithLabels) = + JsObject( + "nctId" -> obj.nctId.toJson, + "trialId" -> obj.trialId.toJson, + "disease" -> obj.condition.toJson, + "lastReviewed" -> obj.lastReviewed.toJson, + "labelVersion" -> obj.labelVersion.toJson, + "arms" -> obj.arms.toJson, + "criteria" -> obj.criteria.map(_.toJson).toJson + ) + } + +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/hypothesis.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/hypothesis.scala new file mode 100644 index 0000000..c05ff23 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/hypothesis.scala @@ -0,0 +1,12 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import xyz.driver.pdsuidomain.entities._ + +object hypothesis { + import DefaultJsonProtocol._ + import common._ + + implicit val hypothesisFormat: RootJsonFormat[Hypothesis] = jsonFormat4(Hypothesis.apply) + +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala index ff7593f..53e927d 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientcriterion.scala @@ -31,7 +31,11 @@ object patientcriterion { case _ => deserializationError(s"Expected Json Object as partial PatientCriterion, but got $json") } - implicit val patientCriterionFormat: RootJsonFormat[DraftPatientCriterion] = jsonFormat3(DraftPatientCriterion.apply) + implicit val draftPatientCriterionFormat: RootJsonFormat[DraftPatientCriterion] = jsonFormat3( + DraftPatientCriterion.apply) + implicit val draftPatientCriterionListReader = new JsonReader[List[DraftPatientCriterion]] { + override def read(json: JsValue) = json.convertTo[List[JsValue]].map(_.convertTo[DraftPatientCriterion]) + } implicit val patientCriterionWriter: JsonWriter[(PatientCriterion, LongId[Label], List[PatientCriterionArm])] = new JsonWriter[(PatientCriterion, LongId[Label], List[PatientCriterionArm])] { diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala new file mode 100644 index 0000000..e801666 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala @@ -0,0 +1,12 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import xyz.driver.pdsuidomain.entities._ + +object studydesign { + import DefaultJsonProtocol._ + import common._ + + implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = jsonFormat2(StudyDesign.apply) + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/BridgeUploadQueueFormat.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/BridgeUploadQueueFormat.scala new file mode 100644 index 0000000..854f51f --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/BridgeUploadQueueFormat.scala @@ -0,0 +1,32 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue + +class BridgeUploadQueueFormat extends FlatSpec with Matchers { + import bridgeuploadqueue._ + + "Json format for BridgeUploadQueue.Item" should "read and write correct JSON" in { + val item = BridgeUploadQueue.Item( + kind = "kind", + tag = "tag", + created = LocalDateTime.parse("2017-08-10T18:00:00"), + attempts = 0, + nextAttempt = LocalDateTime.parse("2017-08-10T18:10:00"), + completed = false, + dependencyKind = Some("dependency king"), + dependencyTag = None + ) + val writtenJson = queueUploadItemFormat.write(item) + + writtenJson should be( + """{"kind":"kind","tag":"tag","created":"2017-08-10T18:00Z","attempts":0,"nextAttempt":"2017-08-10T18:10Z","completed":false}""".parseJson) + + val parsedItem = queueUploadItemFormat.read(writtenJson) + parsedItem should be(item.copy(dependencyKind = None, completed = true)) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExportFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExportFormatSuite.scala new file mode 100644 index 0000000..d0d4d1a --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExportFormatSuite.scala @@ -0,0 +1,124 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.{LocalDate, LocalDateTime} +import java.util.UUID + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId} +import xyz.driver.pdsuidomain.entities.RecordRequestId + +class ExportFormatSuite extends FlatSpec with Matchers { + import export._ + + "Json format for ExportPatientWithLabels" should "read and write correct JSON" in { + import xyz.driver.pdsuidomain.entities.export.patient._ + val document = ExportPatientLabelEvidenceDocument( + documentId = LongId(101), + requestId = RecordRequestId(UUID.fromString("7b54a75d-4197-4b27-9045-b9b6cb131be9")), + documentType = "document type", + providerType = "provider type", + date = LocalDate.parse("2017-08-10") + ) + val labels = List( + ExportPatientLabel( + id = LongId(1), + evidences = List( + ExportPatientLabelEvidence( + id = LongId(11), + value = FuzzyValue.Yes, + evidenceText = "evidence text 11", + document = document + ), + ExportPatientLabelEvidence( + id = LongId(12), + value = FuzzyValue.No, + evidenceText = "evidence text 12", + document = document + ) + ) + ), + ExportPatientLabel( + id = LongId(2), + evidences = List( + ExportPatientLabelEvidence( + id = LongId(12), + value = FuzzyValue.Yes, + evidenceText = "evidence text 12", + document = document + ), + ExportPatientLabelEvidence( + id = LongId(13), + value = FuzzyValue.Yes, + evidenceText = "evidence text 13", + document = document + ) + ) + ) + ) + val patientWithLabels = ExportPatientWithLabels( + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + labelVersion = 1L, + labels = labels + ) + + val writtenJson = patientWithLabelsWriter.write(patientWithLabels) + writtenJson should be( + """{"patientId":"748b5884-3528-4cb9-904b-7a8151d6e343","labelVersion":1,"labels":[{"labelId":1,"evidence":[{"evidenceId":11, + "labelValue":"Yes","evidenceText":"evidence text 11","document":{"documentId":101,"requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9", + "documentType":"document type","providerType":"provider type","date":"2017-08-10"}},{"evidenceId":12,"labelValue":"No", + "evidenceText":"evidence text 12","document":{"documentId":101,"requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9", + "documentType":"document type","providerType":"provider type","date":"2017-08-10"}}]}, + {"labelId":2,"evidence":[{"evidenceId":12,"labelValue":"Yes","evidenceText":"evidence text 12","document": + {"documentId":101,"requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9","documentType":"document type", + "providerType":"provider type","date":"2017-08-10"}},{"evidenceId":13,"labelValue":"Yes","evidenceText":"evidence text 13", + "document":{"documentId":101,"requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9","documentType":"document type", + "providerType":"provider type","date":"2017-08-10"}}]}]}""".parseJson) + } + + "Json format for ApiExportTrialWithLabels" should "read and write correct JSON" in { + import xyz.driver.pdsuidomain.entities.export.trial._ + val arms = List( + ExportTrialArm(armId = LongId(1), armName = "arm 1"), + ExportTrialArm(armId = LongId(2), armName = "arm 2") + ) + val criteriaList = List( + ExportTrialLabelCriterion( + criterionId = LongId(10), + value = Some(true), + labelId = LongId(21), + armIds = Set(LongId(1), LongId(2)), + criteria = "criteria 10 text", + isCompound = false, + isDefining = false + ), + ExportTrialLabelCriterion( + criterionId = LongId(11), + value = None, + labelId = LongId(21), + armIds = Set(LongId(2)), + criteria = "criteria 11 text", + isCompound = true, + isDefining = false + ) + ) + val trialWithLabels = ExportTrialWithLabels( + nctId = StringId("NCT000001"), + trialId = UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), + condition = "Breast", + lastReviewed = LocalDateTime.parse("2017-08-10T18:00:00"), + labelVersion = 1L, + arms = arms, + criteria = criteriaList + ) + + val writtenJson = trialWithLabelsWriter.write(trialWithLabels) + writtenJson should be( + """{"nctId":"NCT000001","trialId":"40892a07-c638-49d2-9795-1edfefbbcc7c","disease":"Breast","lastReviewed":"2017-08-10T18:00Z", + "labelVersion":1,"arms":[{"armId":1,"armName":"arm 1"},{"armId":2,"armName":"arm 2"}],"criteria":[ + {"value":"Yes","labelId":21,"criterionId":10,"criterionText":"criteria 10 text","armIds":[1,2],"isCompound":false,"isDefining":false}, + {"value":"Unknown","labelId":21,"criterionId":11,"criterionText":"criteria 11 text","armIds":[2],"isCompound":true,"isDefining":false}]}""".parseJson) + } + + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/HypothesisFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/HypothesisFormatSuite.scala new file mode 100644 index 0000000..306fb17 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/HypothesisFormatSuite.scala @@ -0,0 +1,28 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.UuidId +import xyz.driver.pdsuidomain.entities.Hypothesis + +class HypothesisFormatSuite extends FlatSpec with Matchers { + import hypothesis._ + + "Json format for Hypothesis" should "read and write correct JSON" in { + val hypothesis = Hypothesis( + id = UuidId("3b80b2e2-5372-4cf5-a342-6e4ebe10fafd"), + name = "hypothesis name", + treatmentType = "treatment type", + description = "descr" + ) + val writtenJson = hypothesisFormat.write(hypothesis) + + writtenJson should be( + """{"id":"3b80b2e2-5372-4cf5-a342-6e4ebe10fafd","name":"hypothesis name", + "treatmentType":"treatment type","description":"descr"}""".parseJson) + + val parsedHypothesis = hypothesisFormat.read(writtenJson) + parsedHypothesis should be(hypothesis) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientCriterionFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientCriterionFormatSuite.scala new file mode 100644 index 0000000..0f5e4e2 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientCriterionFormatSuite.scala @@ -0,0 +1,55 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId} +import xyz.driver.pdsuidomain.entities.{PatientCriterion, PatientCriterionArm} +import xyz.driver.pdsuidomain.services.PatientCriterionService.DraftPatientCriterion + +class PatientCriterionFormatSuite extends FlatSpec with Matchers { + import patientcriterion._ + + "Json format for PatientCriterion" should "read and write correct JSON" in { + val orig = PatientCriterion( + id = LongId(1), + patientLabelId = LongId(1), + trialId = 0L, + nctId = StringId("NCT00001"), + criterionId = LongId(101), + criterionText = "criterion text", + criterionValue = Some(true), + criterionIsDefining = false, + eligibilityStatus = Some(FuzzyValue.Yes), + verifiedEligibilityStatus = None, + isVisible = true, + isVerified = true, + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val arms = List( + PatientCriterionArm(patientCriterionId = LongId(1), armId = LongId(31), armName = "arm 31"), + PatientCriterionArm(patientCriterionId = LongId(1), armId = LongId(32), armName = "arm 32") + ) + val writtenJson = patientCriterionWriter.write((orig, LongId(21), arms)) + + writtenJson should be ( + """{"id":1,"labelId":21,"nctId":"NCT00001","criterionId":101,"criterionText":"criterion text","criterionValue":"Yes", + "criterionIsDefining":false,"criterionIsCompound":false,"eligibilityStatus":"Yes","verifiedEligibilityStatus":null, + "isVisible":true,"isVerified":true,"lastUpdate":"2017-08-10T18:00Z","arms":["arm 31","arm 32"]}""".parseJson) + + val updatePatientCriterionJson = """{"verifiedEligibilityStatus":"No"}""".parseJson + val expectedUpdatedPatientCriterion = orig.copy(verifiedEligibilityStatus = Some(FuzzyValue.No)) + val parsedUpdatePatientCriterion = applyUpdateToPatientCriterion(updatePatientCriterionJson, orig) + parsedUpdatePatientCriterion should be(expectedUpdatedPatientCriterion) + + val updateBulkPatientCriterionJson = """[{"id":1,"eligibilityStatus":"No"},{"id":2,"isVerified":false}]""".parseJson + val expectedDraftPatientCriterionList = List( + DraftPatientCriterion(id = LongId(1), eligibilityStatus = Some(FuzzyValue.No), isVerified = None), + DraftPatientCriterion(id = LongId(2), eligibilityStatus = None, isVerified = Some(false)) + ) + val parsedDraftPatientCriterionList = draftPatientCriterionListReader.read(updateBulkPatientCriterionJson) + parsedDraftPatientCriterionList should be(expectedDraftPatientCriterionList) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientEligibleTrialFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientEligibleTrialFormatSuite.scala new file mode 100644 index 0000000..ad54946 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientEligibleTrialFormatSuite.scala @@ -0,0 +1,62 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId} +import xyz.driver.pdsuidomain.entities.{PatientCriterionArm, PatientTrialArmGroupView, Trial} +import xyz.driver.pdsuidomain.services.PatientEligibleTrialService.RichPatientEligibleTrial + +class PatientEligibleTrialFormatSuite extends FlatSpec with Matchers { + import patienteligibletrial._ + + "Json format for PatientEligibleTrial" should "read and write correct JSON" in { + val trial = Trial( + id = StringId("NCT000001"), + externalId = UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), + status = Trial.Status.Done, + assignee = None, + previousStatus = None, + previousAssignee = None, + lastActiveUserId = None, + lastUpdate = LocalDateTime.parse("2017-08-10T18:16:19"), + condition = Trial.Condition.Breast, + phase = "", + hypothesisId = Some(UuidId("e76e2fc4-a29c-44fb-a81b-8856d06bb1d4")), + studyDesignId = Some(LongId(321)), + originalStudyDesign = None, + isPartner = false, + overview = None, + overviewTemplate = "", + isUpdated = false, + title = "trial title", + originalTitle = "orig trial title" + ) + val group = PatientTrialArmGroupView( + id = LongId(1), + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + trialId = StringId("NCT000001"), + hypothesisId = UuidId("e76e2fc4-a29c-44fb-a81b-8856d06bb1d4"), + eligibilityStatus = Some(FuzzyValue.Yes), + verifiedEligibilityStatus = Some(FuzzyValue.Yes), + isVerified = false + ) + val arms = List( + PatientCriterionArm(patientCriterionId = LongId(1), armId = LongId(31), armName = "arm 31"), + PatientCriterionArm(patientCriterionId = LongId(1), armId = LongId(32), armName = "arm 32") + ) + val orig = RichPatientEligibleTrial(trial, group, arms) + val writtenJson = patientEligibleTrialWriter.write(orig) + + writtenJson should be ( + """{"id":1,"patientId":"748b5884-3528-4cb9-904b-7a8151d6e343","trialId":"NCT000001","trialTitle":"trial title", + "hypothesisId":"e76e2fc4-a29c-44fb-a81b-8856d06bb1d4","verifiedEligibilityStatus":"Yes","isVerified":false,"arms":["arm 31","arm 32"]}""".parseJson) + + val updatePatientEligibleTrialJson = """{"isVerified":true}""".parseJson + val expectedUpdatedPatientTrialArmGroup = group.copy(isVerified = true) + val parsedUpdatePatientTrialArmGroup = applyUpdateToTrialArmGroup(updatePatientEligibleTrialJson, group) + parsedUpdatePatientTrialArmGroup should be(expectedUpdatedPatientTrialArmGroup) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala new file mode 100644 index 0000000..9cf9b5f --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala @@ -0,0 +1,24 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.LongId +import xyz.driver.pdsuidomain.entities.StudyDesign + +class StudyDesignFormatSuite extends FlatSpec with Matchers { + import studydesign._ + + "Json format for StudyDesign" should "read and write correct JSON" in { + val studyDesign = StudyDesign( + id = LongId(10), + name = "study design name" + ) + val writtenJson = studyDesignFormat.write(studyDesign) + + writtenJson should be("""{"id":10,"name":"study design name"}""".parseJson) + + val parsedStudyDesign = studyDesignFormat.read(writtenJson) + parsedStudyDesign should be(studyDesign) + } + +} -- cgit v1.2.3