From bfac6a54dcf37e0280cc8f2ec6ff3802dc8e8dfe Mon Sep 17 00:00:00 2001 From: Kseniya Tomskikh Date: Fri, 11 Aug 2017 14:40:19 +0600 Subject: PDSUI-2188 Created and fixed test for json formats for ReP and TM --- .../formats/json/sprayformats/common.scala | 7 +- .../formats/json/sprayformats/document.scala | 64 +++++------ .../formats/json/sprayformats/documentissue.scala | 8 +- .../formats/json/sprayformats/extracteddata.scala | 101 +++++++++-------- .../formats/json/sprayformats/patientissue.scala | 6 +- .../formats/json/sprayformats/patientlabel.scala | 20 ++-- .../formats/json/sprayformats/record.scala | 124 +++++++++++++++++++-- .../formats/json/sprayformats/recordissue.scala | 8 +- 8 files changed, 210 insertions(+), 128 deletions(-) (limited to 'src/main/scala/xyz') 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 4dadb11..dbd0a43 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,7 +1,6 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats -import java.time.format.DateTimeFormatter -import java.time.{LocalDate, LocalDateTime, ZonedDateTime} +import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime} import spray.json._ import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId} @@ -33,9 +32,9 @@ object common { } implicit def dateTimeFormat = new RootJsonFormat[LocalDateTime] { - override def write(date: LocalDateTime): JsString = JsString(date.toString) + override def write(date: LocalDateTime): JsString = JsString(ZonedDateTime.of(date, ZoneId.of("Z")).toString) override def read(json: JsValue): LocalDateTime = json match { - case JsString(value) => LocalDateTime.parse(value) + case JsString(value) => ZonedDateTime.parse(value).toLocalDateTime case _ => deserializationError(s"Expected date as LocalDateTime, but got $json") } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala index 20cf6af..baf1b4e 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala @@ -39,41 +39,35 @@ object document { .get("physician") .map(_.convertTo[String]) - val typeId = if (fields.contains("typeId")) { - fields - .get("typeId") - .map(_.convertTo[LongId[DocumentType]]) - } else orig.typeId - - val provider = if (fields.contains("provider")) { - fields - .get("provider") - .map(_.convertTo[String]) - } else orig.providerName - - val providerTypeId = if (fields.contains("providerTypeId")) { - fields - .get("providerTypeId") - .map(_.convertTo[LongId[ProviderType]]) - } else orig.providerTypeId - - val meta = if (fields.contains("meta")) { - fields - .get("meta") - .map(_.convertTo[TextJson[Meta]]) - } else orig.meta - - val startDate = if (fields.contains("startDate")) { - fields - .get("startDate") - .map(_.convertTo[LocalDate]) - } else orig.startDate - - val endDate = if (fields.contains("endDate")) { - fields - .get("endDate") - .map(_.convertTo[LocalDate]) - } else orig.endDate + val typeId = fields + .get("typeId") + .map(_.convertTo[Option[LongId[DocumentType]]]) + .getOrElse(orig.typeId) + + val provider = fields + .get("provider") + .map(_.convertTo[Option[String]]) + .getOrElse(orig.providerName) + + val providerTypeId = fields + .get("providerTypeId") + .map(_.convertTo[Option[LongId[ProviderType]]]) + .getOrElse(orig.providerTypeId) + + val meta = fields + .get("meta") + .map(_.convertTo[Option[TextJson[Meta]]]) + .getOrElse(orig.meta) + + val startDate = fields + .get("startDate") + .map(_.convertTo[Option[LocalDate]]) + .getOrElse(orig.startDate) + + val endDate = fields + .get("endDate") + .map(_.convertTo[Option[LocalDate]]) + .getOrElse(orig.endDate) orig.copy( physician = physician.orElse(orig.physician), diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala index 9ba3614..28b2a5e 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala @@ -44,12 +44,6 @@ object documentissue { .map(_.convertTo[String]) .getOrElse(deserializationError(s"DocumentIssue json object does not contain `text` field: $json")) - val archiveRequired = fields - .get("archiveRequired") - .map(_.convertTo[Boolean]) - .getOrElse( - deserializationError(s"DocumentIssue json object does not contain `archiveRequired` field: $json")) - val startPage = fields.get("startPage").map(_.convertTo[Double]) val endPage = fields.get("endPage").map(_.convertTo[Double]) DocumentIssue( @@ -59,7 +53,7 @@ object documentissue { lastUpdate = LocalDateTime.MIN, isDraft = true, text = text, - archiveRequired = archiveRequired, + archiveRequired = false, startPage = startPage, endPage = endPage ) diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/extracteddata.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/extracteddata.scala index ab27b67..42473bc 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/extracteddata.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/extracteddata.scala @@ -21,7 +21,7 @@ object extracteddata { override def read(json: JsValue): TextJson[Meta] = TextJson(extractedDataMetaFormat.read(json)) } - implicit val extractedDataLabelFormat: RootJsonFormat[ExtractedDataLabel] = new RootJsonFormat[ExtractedDataLabel] { + implicit val extractedDataLabelWriter: JsonWriter[ExtractedDataLabel] = new JsonWriter[ExtractedDataLabel] { override def write(label: ExtractedDataLabel): JsObject = { JsObject( "id" -> label.labelId.toJson, @@ -29,59 +29,57 @@ object extracteddata { "value" -> label.value.toJson ) } + } - override def read(json: JsValue): ExtractedDataLabel = json match { - case JsObject(fields) => - val labelId = fields - .get("id") - .map(_.convertTo[LongId[Label]]) - - val categoryId = fields - .get("categoryId") - .map(_.convertTo[LongId[Category]]) - - val value = fields - .get("value") - .map(_.convertTo[FuzzyValue]) - - ExtractedDataLabel( - id = LongId(0), - dataId = LongId(0), - labelId = labelId, - categoryId = categoryId, - value = value - ) + def applyLabelsForExtractedData(json: JsValue, dataId: LongId[ExtractedData]): ExtractedDataLabel = json match { + case JsObject(fields) => + val labelId = fields + .get("id") + .map(_.convertTo[LongId[Label]]) + + val categoryId = fields + .get("categoryId") + .map(_.convertTo[LongId[Category]]) + + val value = fields + .get("value") + .map(_.convertTo[FuzzyValue]) + + ExtractedDataLabel( + id = LongId(0), + dataId = dataId, + labelId = labelId, + categoryId = categoryId, + value = value + ) - case _ => deserializationError(s"Expected Json Object as ExtractedDataLabel, but got $json") - } + case _ => deserializationError(s"Expected Json Object as ExtractedDataLabel, but got $json") } def applyUpdateToExtractedData(json: JsValue, orig: RichExtractedData): RichExtractedData = json match { case JsObject(fields) => - val keywordId = if (fields.contains("keywordId")) { - fields - .get("keywordId") - .map(_.convertTo[LongId[Keyword]]) - } else orig.extractedData.keywordId - - val evidence = if (fields.contains("evidence")) { - fields - .get("evidence") - .map(_.convertTo[String]) - } else orig.extractedData.evidenceText - - val meta = if (fields.contains("meta")) { - fields - .get("meta") - .map(_.convertTo[TextJson[Meta]]) - } else orig.extractedData.meta - - val labels = if (fields.contains("labels")) { - fields - .get("labels") - .map(_.convertTo[List[ExtractedDataLabel]]) - .getOrElse(List.empty[ExtractedDataLabel]) - } else orig.labels + val keywordId = fields + .get("keywordId") + .map(_.convertTo[Option[LongId[Keyword]]]) + .getOrElse(orig.extractedData.keywordId) + + val evidence = fields + .get("evidence") + .map(_.convertTo[Option[String]]) + .getOrElse(orig.extractedData.evidenceText) + + val meta = fields + .get("meta") + .map(_.convertTo[Option[TextJson[Meta]]]) + .getOrElse(orig.extractedData.meta) + + val labels = fields + .get("labels") + .map( + _.convertTo[Option[List[JsValue]]] + .getOrElse(List.empty[JsValue]) + .map(l => applyLabelsForExtractedData(l, orig.extractedData.id))) + .getOrElse(orig.labels) val extractedData = orig.extractedData.copy( keywordId = keywordId, @@ -105,7 +103,7 @@ object extracteddata { "keywordId" -> richData.extractedData.keywordId.toJson, "evidence" -> richData.extractedData.evidenceText.toJson, "meta" -> richData.extractedData.meta.toJson, - "labels" -> richData.labels.toJson + "labels" -> richData.labels.map(_.toJson).toJson ) override def read(json: JsValue): RichExtractedData = json match { @@ -130,8 +128,9 @@ object extracteddata { val labels = fields .get("labels") - .map(_.convertTo[List[ExtractedDataLabel]]) - .getOrElse(List.empty[ExtractedDataLabel]) + .map(_.convertTo[List[JsValue]]) + .getOrElse(List.empty[JsValue]) + .map(l => applyLabelsForExtractedData(l, LongId(0))) val extractedData = ExtractedData( documentId = documentId, diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientissue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientissue.scala index ffe31cf..7d35bd1 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientissue.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientissue.scala @@ -23,8 +23,8 @@ object patientissue { } def jsValueToPatientIssue(json: JsValue, patientId: UuidId[Patient], userId: StringId[User]): PatientIssue = { - json.asJsObject.getFields("text", "archiveRequired") match { - case Seq(text, archiveRequired) => + json.asJsObject.getFields("text") match { + case Seq(text) => PatientIssue( id = LongId(0), userId = userId, @@ -32,7 +32,7 @@ object patientissue { lastUpdate = LocalDateTime.MIN, isDraft = true, text = text.convertTo[String], - archiveRequired = archiveRequired.convertTo[Boolean] + archiveRequired = false ) case _ => deserializationError(s"Expected Json Object as PatientIssue, but got $json") diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientlabel.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientlabel.scala index e29b9fd..3b52833 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientlabel.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patientlabel.scala @@ -10,17 +10,15 @@ object patientlabel { def applyUpdateToPatientLabel(json: JsValue, orig: PatientLabel): PatientLabel = json match { case JsObject(fields) => - val primaryValue = if (fields.contains("primaryValue")) { - fields - .get("primaryValue") - .map(_.convertTo[FuzzyValue]) - } else orig.primaryValue + val primaryValue = fields + .get("primaryValue") + .map(_.convertTo[Option[FuzzyValue]]) + .getOrElse(orig.primaryValue) - val verifiedPrimaryValue = if (fields.contains("verifiedPrimaryValue")) { - fields - .get("verifiedPrimaryValue") - .map(_.convertTo[FuzzyValue]) - } else orig.verifiedPrimaryValue + val verifiedPrimaryValue = fields + .get("verifiedPrimaryValue") + .map(_.convertTo[Option[FuzzyValue]]) + .getOrElse(orig.verifiedPrimaryValue) orig.copy( primaryValue = primaryValue, @@ -56,7 +54,7 @@ object patientlabel { "evidenceText" -> evidence.evidenceText.toJson, "documentId" -> evidence.documentId.toJson, "evidenceId" -> evidence.evidenceId.toJson, - "reportId" -> evidence.isImplicitMatch.toJson, + "reportId" -> evidence.reportId.toJson, "documentType" -> evidence.documentType.toJson, "date" -> evidence.date.toJson, "providerType" -> evidence.providerType.toJson diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala index 352704a..e378dbd 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala @@ -6,6 +6,7 @@ import java.util.UUID import spray.json._ import xyz.driver.core.json.{EnumJsonFormat, GadtJsonFormat} import xyz.driver.pdsuicommon.domain.{LongId, TextJson, UuidId} +import xyz.driver.pdsuidomain.entities.MedicalRecord.Meta._ import xyz.driver.pdsuidomain.entities._ object record { @@ -43,12 +44,116 @@ object record { } } + implicit val duplicateMetaFormat: RootJsonFormat[Duplicate] = new RootJsonFormat[Duplicate] { + override def write(obj: Duplicate) = + JsObject( + "type" -> "duplicate".toJson, + "predicted" -> obj.predicted.toJson, + "startPage" -> obj.startPage.toJson, + "endPage" -> obj.endPage.toJson, + "startOriginalPage" -> obj.startOriginalPage.toJson, + "endOriginalPage" -> obj.endOriginalPage.toJson + ) + + override def read(json: JsValue): Duplicate = json match { + case JsObject(fields) => + val predicted = fields + .get("predicted") + .map(_.convertTo[Boolean]) + + val startPage = fields + .get("startPage") + .map(_.convertTo[Double]) + .getOrElse(deserializationError(s"Duplicate meta json object does not contain `startPage` field: $json")) + + val endPage = fields + .get("endPage") + .map(_.convertTo[Double]) + .getOrElse(deserializationError(s"Duplicate meta json object does not contain `endPage` field: $json")) + + val startOriginalPage = fields + .get("startOriginalPage") + .map(_.convertTo[Double]) + .getOrElse( + deserializationError(s"Duplicate meta json object does not contain `startOriginalPage` field: $json")) + + val endOriginalPage = fields + .get("endOriginalPage") + .map(_.convertTo[Double]) + + Duplicate( + predicted = predicted, + startPage = startPage, + endPage = endPage, + startOriginalPage = startOriginalPage, + endOriginalPage = endOriginalPage + ) + + case _ => deserializationError(s"Expected JsObject as Duplicate meta of medical record, but got $json") + } + } + + implicit val reorderMetaFormat: RootJsonFormat[Reorder] = new RootJsonFormat[Reorder] { + override def write(obj: Reorder) = + JsObject( + "type" -> "reorder".toJson, + "predicted" -> obj.predicted.toJson, + "items" -> obj.items.toJson + ) + + override def read(json: JsValue): Reorder = json match { + case JsObject(fields) => + val predicted = fields + .get("predicted") + .map(_.convertTo[Boolean]) + + val items = fields + .get("items") + .map(_.convertTo[Seq[Int]]) + .getOrElse(deserializationError(s"Reorder meta json object does not contain `items` field: $json")) + + Reorder( + predicted = predicted, + items = items + ) + + case _ => deserializationError(s"Expected JsObject as Reorder meta of medical record, but got $json") + } + } + + implicit val rotateMetaFormat: RootJsonFormat[Rotation] = new RootJsonFormat[Rotation] { + override def write(obj: Rotation) = + JsObject( + "type" -> "rotation".toJson, + "predicted" -> obj.predicted.toJson, + "items" -> obj.items.toJson + ) + + override def read(json: JsValue): Rotation = json match { + case JsObject(fields) => + val predicted = fields + .get("predicted") + .map(_.convertTo[Boolean]) + + val items = fields + .get("items") + .map(_.convertTo[Map[String, Int]]) + .getOrElse(deserializationError(s"Rotation meta json object does not contain `items` field: $json")) + + Rotation( + predicted = predicted, + items = items + ) + + case _ => deserializationError(s"Expected JsObject as Rotation meta of medical record, but got $json") + } + } + implicit val recordMetaTypeFormat: GadtJsonFormat[MedicalRecord.Meta] = { - import Meta._ - GadtJsonFormat.create[Meta]("meta")({ case m => m.metaType }) { - case "duplicate" => jsonFormat5(Duplicate.apply) - case "reorder" => jsonFormat2(Reorder.apply) - case "rotation" => jsonFormat2(Rotation.apply) + GadtJsonFormat.create[Meta]("type")({ case m => m.metaType }) { + case "duplicate" => duplicateMetaFormat + case "reorder" => reorderMetaFormat + case "rotation" => rotateMetaFormat } } @@ -120,11 +225,10 @@ object record { def applyUpdateToMedicalRecord(json: JsValue, orig: MedicalRecord): MedicalRecord = json match { case JsObject(fields) => - val meta = if (fields.contains("meta")) { - fields - .get("meta") - .map(_.convertTo[TextJson[List[Meta]]]) - } else orig.meta + val meta = fields + .get("meta") + .map(_.convertTo[Option[TextJson[List[Meta]]]]) + .getOrElse(orig.meta) orig.copy(meta = meta) case _ => deserializationError(s"Expected Json Object as partial MedicalRecord, but got $json") diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/recordissue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/recordissue.scala index 9e23b8a..4ae04d0 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/recordissue.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/recordissue.scala @@ -46,12 +46,6 @@ object recordissue { .map(_.convertTo[String]) .getOrElse(deserializationError(s"MedicalRecordIssue json object does not contain `text` field: $json")) - val archiveRequired = fields - .get("archiveRequired") - .map(_.convertTo[Boolean]) - .getOrElse( - deserializationError(s"MedicalRecordIssue json object does not contain `archiveRequired` field: $json")) - val startPage = fields.get("startPage").map(_.convertTo[Double]) val endPage = fields.get("endPage").map(_.convertTo[Double]) MedicalRecordIssue( @@ -61,7 +55,7 @@ object recordissue { lastUpdate = LocalDateTime.MIN, isDraft = true, text = text, - archiveRequired = archiveRequired, + archiveRequired = false, startPage = startPage, endPage = endPage ) -- cgit v1.2.3