diff options
Diffstat (limited to 'src/test')
20 files changed, 709 insertions, 346 deletions
diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala deleted file mode 100644 index d9d62dd..0000000 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala +++ /dev/null @@ -1,78 +0,0 @@ -package xyz.driver.pdsuidomain.formats.json - -/* -import java.time.LocalDateTime - -import org.scalatest.FreeSpecLike -import play.api.libs.json.Json - -import scala.collection.breakOut - -class ApiDocumentSuite extends FreeSpecLike { - - "ApiDocument" - { - "conforms to specification when serialized" in { - val document = ApiDocument( - id = 1L, - recordId = 123L, - physician = Some("physician"), - lastUpdate = LocalDateTime.now(), - `type` = "some-type", - startDate = LocalDateTime.now(), - endDate = Some(LocalDateTime.now()), - provider = "test-provider", - providerType = "test-provider-type", - status = "New", - assignee = Some(5L), - previousAssignee = None, - meta = "{}" - ) - - val actualJson = serialize(document) - val matcher = - """^\{ - |"id":"[^"]+", - |"recordId":"[^"]+", - |"physician":"[^"]+", - |"lastUpdate":"[^"]+", - |"type":"[^"]+", - |"startDate":"[^"]+", - |"endDate":"[^"]+", - |"provider":"[^"]+", - |"providerType":"[^"]+", - |"status":"[^"]+", - |"assignee":"[^"]+", - |"meta":\{[^\}]*\} - |\}""".stripMargin.lines.mkString.r - - assert( - matcher.pattern.matcher(actualJson).matches(), - s"""see https://driverinc.atlassian.net/wiki/display/RA/REST+API+Specification#RESTAPISpecification-DocumentObject - |pattern = ${matcher.pattern} - |json = $actualJson""".stripMargin - ) - } - - "has optional fields according to specification" in { - val expectedOptionalFields = Set("physician", "endDate", "assignee", "previousAssignee") - - val klass = classOf[ApiDocument] - val actualOptionalFields: Set[String] = klass.getDeclaredFields.collect({ - case x if x.getType == classOf[Option[_]] => x.getName - })(breakOut) - - assert( - actualOptionalFields === expectedOptionalFields, - """actual vs expected - |see https://driverinc.atlassian.net/wiki/display/RA/REST+API+Specification#RESTAPISpecification-DocumentObject - """.stripMargin.trim - ) - } - } - - private def serialize(document: ApiDocument): String = { - Json.stringify(Json.toJson(document)) - } - -} - */ diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala deleted file mode 100644 index 2ae52f9..0000000 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala +++ /dev/null @@ -1,263 +0,0 @@ -package xyz.driver.pdsuidomain.formats.json - -/* -import java.time.LocalDateTime - -import xyz.driver.pdsuicommon.domain._ -import org.davidbild.tristate.Tristate -import org.scalatest.FreeSpecLike -import play.api.libs.json.Json - -class ApiPartialDocumentSuite extends FreeSpecLike { - - "serialization" - { - "can be deserialized" in deserialize(defaultJson) - - "endDate" - { - def jsonWithEndDate(rawValue: String) = json("endDate" -> rawValue) - def jsonWithoutEndDate = json() - - "when specified" - { - "if non-empty object - should be parsed as date" in { - val rawEndDate = "2007-12-03T10:15:30.000" - val apiPartialEntity = deserialize(jsonWithEndDate(quoted(rawEndDate))) - assert(apiPartialEntity.endDate.exists(_ == LocalDateTime.parse(rawEndDate))) - } - - "if null - should be parsed as Absent" in { - val apiPartialEntity = deserialize(jsonWithEndDate("null")) - assert(apiPartialEntity.endDate.isAbsent) - } - } - - "when unspecified - should be parsed as Unspecified" in { - val apiPartialEntity = deserialize(jsonWithoutEndDate) - assert(apiPartialEntity.endDate.isUnspecified) - } - } - - "assignee" - { - def jsonWithAssignee(rawValue: String) = json("assignee" -> rawValue) - def jsonWithoutAssignee = json() - - "when specified" - { - "if non-empty object - should be parsed as UUID" in { - val rawAssignee = 123 - val apiPartialEntity = deserialize(jsonWithAssignee(quoted(rawAssignee.toString))) - assert(apiPartialEntity.assignee.exists(_ == rawAssignee)) - } - - "if null - should be parsed as Absent" in { - val apiPartialEntity = deserialize(jsonWithAssignee("null")) - assert(apiPartialEntity.assignee.isAbsent) - } - } - - "when unspecified - should be parsed as Unspecified" in { - val apiPartialEntity = deserialize(jsonWithoutAssignee) - assert(apiPartialEntity.assignee.isUnspecified) - } - } - - "meta" - { - def jsonWithMeta(rawValue: String) = json("meta" -> rawValue) - def jsonWithoutMeta = json() - - "when specified" - { - "if non-empty object - should be parsed as normalized JSON-string" in { - val apiPartialEntity = deserialize(jsonWithMeta(""" { "foo" : 1} """)) - assert(apiPartialEntity.meta.exists(_ == """{"foo":1}""")) - } - - "if null - should be parsed as Absent" in { - val apiPartialEntity = deserialize(jsonWithMeta("null")) - assert(apiPartialEntity.meta.isAbsent) - } - - "if empty object ({}) - should be parsed as Absent (same as null case)" in { - val apiPartialEntity = deserialize(jsonWithMeta(" { } ")) - assert(apiPartialEntity.meta.isAbsent) - } - } - - "when unspecified - should be parsed as Unspecified" in { - val apiPartialEntity = deserialize(jsonWithoutMeta) - assert(apiPartialEntity.meta.isUnspecified) - } - } - - def deserialize(json: String): ApiPartialDocument = { - Json.parse(json).as[ApiPartialDocument] - } - } - - "applyTo" - { - "endDate" - { - "should choose a value of the api partial document" - { - "if it present" in test(Tristate.Present(LocalDateTime.now().plusDays(1))) - "if it absent" in test(Tristate.Absent) - - def test(newEndDate: Tristate[LocalDateTime]): Unit = { - val origDocument = documentExample - val apiPartialDocument = apiPartialDocumentExample.copy(endDate = newEndDate) - - val newDocument = apiPartialDocument.applyTo(origDocument) - assert(newDocument.endDate === newEndDate.toOption) - } - } - - "should choose a value of the original document if it unspecified" in { - val newEndDate = Tristate.Unspecified - val origDocument = documentExample - val apiPartialDocument = apiPartialDocumentExample.copy(endDate = newEndDate) - - val newDocument = apiPartialDocument.applyTo(origDocument) - assert(newDocument.endDate == origDocument.endDate) - } - } - - "assignee" - { - "should choose a value of the api partial document" - { - "if it present" in test(Tristate.Present(123)) - "if it absent" in test(Tristate.Absent) - - def test(newAssignee: Tristate[Long]): Unit = { - val origDocument = documentExample - val apiPartialDocument = apiPartialDocumentExample.copy(assignee = newAssignee) - - val newDocument = apiPartialDocument.applyTo(origDocument) - assert(newDocument.assignee === newAssignee.toOption.map(StringId[User])) - } - } - - "should choose a value of the original document if it unspecified" in { - val newAssignee = Tristate.Unspecified - val origDocument = documentExample - val apiPartialDocument = apiPartialDocumentExample.copy(assignee = newAssignee) - - val newDocument = apiPartialDocument.applyTo(origDocument) - assert(newDocument.assignee == origDocument.assignee) - } - } - - /*"meta" - { - val documentId = Id[Document]() - def test(origMeta: Option[RawMeta], metaUpdate: Tristate[String], expectedNewMeta: Option[RawMeta]): Unit = { - val apiPartialDocument = apiPartialDocumentExample.copy(meta = metaUpdate) - - val actualNewMeta = apiPartialDocument.applyTo(documentId, origMeta) - assert(actualNewMeta == expectedNewMeta) - } - - "should choose a new meta's value" - { - "if it present" - { - "when original meta is present" in test( - origMeta = Some(RawMeta(documentId, """{"foo":42}""")), - metaUpdate = Tristate.Present("""{"bar":1}"""), - expectedNewMeta = Some(RawMeta(documentId, """{"bar":1}""")) - ) - - "when original meta is absent" in test( - origMeta = None, - metaUpdate = Tristate.Present("""{"bar":1}"""), - expectedNewMeta = Some(RawMeta(documentId, """{"bar":1}""")) - ) - } - - "if it absent" - { - "when original meta is present" in test( - origMeta = Some(RawMeta(documentId, """{"foo":42}""")), - metaUpdate = Tristate.Absent, - expectedNewMeta = None - ) - - "when original meta is absent" in test( - origMeta = None, - metaUpdate = Tristate.Absent, - expectedNewMeta = None - ) - } - } - - "should choose an original meta if a new one is unspecified" - { - "when original meta is present" in test( - origMeta = Some(RawMeta(documentId, """{"foo":42}""")), - metaUpdate = Tristate.Unspecified, - expectedNewMeta = Some(RawMeta(documentId, """{"foo":42}""")) - ) - - "when original meta is absent" in test( - origMeta = None, - metaUpdate = Tristate.Unspecified, - expectedNewMeta = None - ) - } - }*/ - } - - private def documentExample = Document( - id = LongId(1), - status = Document.Status.Organized, - assignee = Some(LongId(1)), - previousAssignee = None, - recordId = LongId(1), - physician = None, - documentType = "type", - providerName = "provider name", - providerType = "provider type", - meta = None, - startDate = LocalDateTime.now, - endDate = None, - lastUpdate = LocalDateTime.now - ) - - private def apiPartialDocumentExample = ApiPartialDocument( - recordId = Some(12), - physician = Some("new physician"), - `type` = Some("new type"), - startDate = Some(LocalDateTime.now), - endDate = Tristate.Present(LocalDateTime.now), - provider = Some("new provider name"), - providerType = Some("new provider type"), - status = Some("Extracted"), - assignee = Tristate.Present(13), - meta = Tristate.Present("""{"foo":42}""") - ) - - private def quoted(v: String): String = s""""$v"""" - - private def defaultJson = json( - "endDate" -> quoted("2007-12-03T10:15:30.000"), - "assignee" -> quoted("c8b1b818-e1eb-4831-b0c8-26753b109deb"), - "meta" -> """{"foo": 1}""" - ) - - /** - * Without fields: - * - endDate - * - assignee - * - meta - */ - private def json(addFields: (String, String)*): String = { - val addJson = addFields - .collect { - case (k, v) if v.nonEmpty => s""" "$k": $v """ - } - .mkString(",\n") - - s"""{ - |"recordId":"bc542d3d-a928-42b8-824d-ca1520b07500", - |"patientId":"foobar", - |"caseId":"bazquux", - |"physician":"zed", - |"lastUpdate":"2007-12-03T10:15:30.000", - |"type":"type1", - |"startDate":"2007-10-01T09:40:00.000", - |"provider":"some provider name", - |"providerType":"some provider type", - |"status":"New"${if (addJson.isEmpty) "" else ","} - |$addJson - |}""".stripMargin - } -} - */ diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatSuite.scala index 4a4b3c4..e7d37a0 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatTestSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ArmFormatSuite.scala @@ -5,7 +5,7 @@ import org.scalatest.{FlatSpec, Matchers} import xyz.driver.pdsuicommon.domain.{LongId, StringId} import xyz.driver.pdsuidomain.entities.Arm -class ArmFormatTestSuite extends FlatSpec with Matchers { +class ArmFormatSuite extends FlatSpec with Matchers { import arm._ "Json format for Arm" should "read and write correct JSON" in { diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatSuite.scala index 0167620..d24c4c1 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatTestSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/CriterionFormatSuite.scala @@ -6,7 +6,7 @@ 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 { +class CriterionFormatSuite extends FlatSpec with Matchers { import criterion._ "Json format for Criterion" should "read and write correct JSON" in { diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentFormatSuite.scala new file mode 100644 index 0000000..9394735 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentFormatSuite.scala @@ -0,0 +1,74 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.{LocalDate, LocalDateTime} + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, TextJson} +import xyz.driver.pdsuidomain.entities.Document + +class DocumentFormatSuite extends FlatSpec with Matchers { + import document._ + + "Json format for Document" should "read and write correct JSON" in { + val orig = Document( + id = LongId(1), + status = Document.Status.New, + assignee = None, + previousStatus = None, + previousAssignee = None, + lastActiveUserId = None, + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + recordId = LongId(101), + physician = Some("physician"), + typeId = Some(LongId(10)), + providerName = Some("provider 21"), + providerTypeId = Some(LongId(21)), + requiredType = Some(Document.RequiredType.OPN), + meta = None, + startDate = None, + endDate = None + ) + val writtenJson = documentFormat.write(orig) + + writtenJson should be ( + """{"id":1,"recordId":101,"physician":"physician","typeId":10,"provider":"provider 21","providerTypeId":21, + "requiredType":"OPN","startDate":null,"endDate":null,"status":"New","assignee":null,"previousStatus":null, + "previousAssignee":null,"lastActiveUser":null,"lastUpdate":"2017-08-10T18:00Z","meta":null}""".parseJson) + + val createDocumentJson = + """{"recordId":101,"physician":"physician","typeId":10,"provider":"provider 21","providerTypeId":21}""".parseJson + val expectedCreatedDocument = orig.copy( + id = LongId(0), + lastUpdate = LocalDateTime.MIN, + requiredType = None + ) + val parsedCreatedDocument = documentFormat.read(createDocumentJson) + parsedCreatedDocument should be(expectedCreatedDocument) + + val updateDocumentJson = + """{"startDate":"2017-08-10","endDate":"2018-08-10","meta":{"predicted":true,"startPage":1.0,"endPage":2.0}}""".parseJson + val expectedUpdatedDocument = orig.copy( + startDate = Some(LocalDate.parse("2017-08-10")), + endDate = Some(LocalDate.parse("2018-08-10")), + meta = Some(TextJson(Document.Meta(predicted = Some(true), startPage = 1.0, endPage = 2.0))) + ) + val parsedUpdatedDocument = applyUpdateToDocument(updateDocumentJson, orig) + parsedUpdatedDocument should be(expectedUpdatedDocument) + } + + "Json format for Document.Meta" should "read and write correct JSON" in { + val meta = Document.Meta(predicted = None, startPage = 1.0, endPage = 2.0) + val writtenJson = documentMetaFormat.write(meta) + writtenJson should be ("""{"startPage":1.0,"endPage":2.0}""".parseJson) + + val metaJsonWithoutPredicted = """{"startPage":1.0,"endPage":2.0}""".parseJson + val parsedMetaWithoutPredicted = documentMetaFormat.read(metaJsonWithoutPredicted) + parsedMetaWithoutPredicted should be(meta) + + val metaJsonWithPredicted = """{"predicted":true,"startPage":1.0,"endPage":2.0}""".parseJson + val parsedMetaWithPredicted = documentMetaFormat.read(metaJsonWithPredicted) + parsedMetaWithPredicted should be(meta.copy(predicted = Some(true))) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentHistoryFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentHistoryFormatSuite.scala new file mode 100644 index 0000000..ddbda1d --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentHistoryFormatSuite.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.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.DocumentHistory + +class DocumentHistoryFormatSuite extends FlatSpec with Matchers { + import documenthistory._ + + "Json format for DocumentHistory" should "read and write correct JSON" in { + val documentHistory = DocumentHistory( + id = LongId(10), + documentId = LongId(1), + executor = StringId("userId-001"), + state = DocumentHistory.State.Extract, + action = DocumentHistory.Action.Start, + created = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val writtenJson = documentHistoryFormat.write(documentHistory) + + writtenJson should be( + """{"id":10,"executor":"userId-001","documentId":1,"state":"Extract", + "action":"Start","created":"2017-08-10T18:00Z"}""".parseJson) + + val parsedDocumentHistory = documentHistoryFormat.read(writtenJson) + parsedDocumentHistory should be(documentHistory) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentIssueFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentIssueFormatSuite.scala new file mode 100644 index 0000000..1a8e3f0 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/DocumentIssueFormatSuite.scala @@ -0,0 +1,47 @@ +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} +import xyz.driver.pdsuidomain.entities.DocumentIssue + +class DocumentIssueFormatSuite extends FlatSpec with Matchers { + import documentissue._ + + "Json format for DocumentIssue" should "read and write correct JSON" in { + val documentIssue = DocumentIssue( + id = LongId(10), + documentId = LongId(1), + userId = StringId("userId-001"), + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + isDraft = false, + text = "message text", + archiveRequired = false, + startPage = Some(1.0), + endPage = Some(2.0) + ) + val writtenJson = documentIssueWriter.write(documentIssue) + + writtenJson should be( + """{"id":10,"userId":"userId-001","lastUpdate":"2017-08-10T18:00Z","isDraft":false, + "text":"message text","archiveRequired":false,"startPage":1.0,"endPage":2.0}""".parseJson) + + val createDocumentIssueJson = """{"text":"message text","startPage":1.0,"endPage":2.0}""".parseJson + val expectedCreatedDocumentIssue = documentIssue.copy(id = LongId(0), lastUpdate = LocalDateTime.MIN, isDraft = true) + val parsedCreateDocumentIssue = jsValueToDocumentIssue(createDocumentIssueJson, LongId(1), StringId("userId-001")) + parsedCreateDocumentIssue should be(expectedCreatedDocumentIssue) + + val updateDocumentIssueJson = + """{"text":"new issue text","evidence":"issue evidence","archiveRequired":true,"startPage":1.0,"endPage":4.0}""".parseJson + val expectedUpdatedDocumentIssue = documentIssue.copy( + text = "new issue text", + archiveRequired = true, + endPage = Some(4.0) + ) + val parsedUpdateDocumentIssue = applyUpdateToDocumentIssue(updateDocumentIssueJson, documentIssue) + parsedUpdateDocumentIssue should be(expectedUpdatedDocumentIssue) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExtractedDataFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExtractedDataFormatSuite.scala new file mode 100644 index 0000000..a4b8bab --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/ExtractedDataFormatSuite.scala @@ -0,0 +1,95 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, TextJson} +import xyz.driver.pdsuidomain.entities.ExtractedData.Meta +import xyz.driver.pdsuidomain.entities.{ExtractedData, ExtractedDataLabel} +import xyz.driver.pdsuidomain.services.ExtractedDataService.RichExtractedData + +class ExtractedDataFormatSuite extends FlatSpec with Matchers { + import extracteddata._ + + "Json format for ExtractedData" should "read and write correct JSON" in { + val extractedData = ExtractedData( + id = LongId(1), + documentId = LongId(101), + keywordId = Some(LongId(201)), + evidenceText = Some("evidence text"), + meta = None + ) + val extractedDataLabels = List( + ExtractedDataLabel( + id = LongId(1), + dataId = extractedData.id, + labelId = None, + categoryId = None, + value = Some(FuzzyValue.Yes) + ), + ExtractedDataLabel( + id = LongId(2), + dataId = extractedData.id, + labelId = Some(LongId(12)), + categoryId = Some(LongId(1)), + value = Some(FuzzyValue.No) + ) + ) + val origRichExtractedData = RichExtractedData( + extractedData = extractedData, + labels = extractedDataLabels + ) + val writtenJson = extractedDataFormat.write(origRichExtractedData) + + writtenJson should be ( + """{"id":1,"documentId":101,"keywordId":201,"evidence":"evidence text","meta":null, + "labels":[{"id":null,"categoryId":null,"value":"Yes"},{"id":12,"categoryId":1,"value":"No"}]}""".parseJson) + + val createExtractedDataJson = + """{"documentId":101,"keywordId":201,"evidence":"evidence text", + "labels":[{"value":"Yes"},{"id":12,"categoryId":1,"value":"No"}]}""".parseJson + val expectedCreatedExtractedData = origRichExtractedData.copy( + extractedData = extractedData.copy(id = LongId(0)), + labels = extractedDataLabels.map(_.copy(id = LongId(0), dataId = LongId(0))) + ) + val parsedCreatedExtractedData = extractedDataFormat.read(createExtractedDataJson) + parsedCreatedExtractedData should be(expectedCreatedExtractedData) + + val updateExtractedDataJson = + """{"evidence":"new evidence text","meta":{"keyword":{"page":1,"index":2,"sortIndex":"ASC"}, + "evidence":{"pageRatio":1.0,"start":{"page":1,"index":3,"offset":2},"end":{"page":2,"index":3,"offset":10}}}, + "labels":[{"id":20,"categoryId":1,"value":"Yes"},{"id":12,"categoryId":1,"value":"No"}]}""".parseJson + val updatedExtractedDataLabels = List( + ExtractedDataLabel( + id = LongId(0), + dataId = extractedData.id, + labelId = Some(LongId(20)), + categoryId = Some(LongId(1)), + value = Some(FuzzyValue.Yes) + ), + ExtractedDataLabel( + id = LongId(0), + dataId = extractedData.id, + labelId = Some(LongId(12)), + categoryId = Some(LongId(1)), + value = Some(FuzzyValue.No) + ) + ) + val expectedUpdatedExtractedData = origRichExtractedData.copy( + extractedData = extractedData.copy( + evidenceText = Some("new evidence text"), + meta = Some(TextJson(Meta( + keyword = Meta.Keyword(page = 1, pageRatio = None, index = 2, sortIndex = "ASC"), + evidence = Meta.Evidence( + pageRatio = 1.0, + start = Meta.TextLayerPosition(page = 1, index = 3, offset = 2), + end = Meta.TextLayerPosition(page = 2, index = 3, offset = 10) + ) + ))) + ), + labels = updatedExtractedDataLabels + ) + val parsedUpdatedExtractedData = applyUpdateToExtractedData(updateExtractedDataJson, origRichExtractedData) + parsedUpdatedExtractedData should be(expectedUpdatedExtractedData) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala index 280b2a7..784a655 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatTestSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala @@ -5,7 +5,7 @@ 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 { +class InterventionFormatSuite extends FlatSpec with Matchers { import intervention._ "Json format for Intervention" should "read and write correct JSON" in { diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordFormatSuite.scala new file mode 100644 index 0000000..899e5c9 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordFormatSuite.scala @@ -0,0 +1,77 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime +import java.util.UUID + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{LongId, TextJson, UuidId} +import xyz.driver.pdsuidomain.entities.{MedicalRecord, RecordRequestId} + +class MedicalRecordFormatSuite extends FlatSpec with Matchers { + import record._ + import MedicalRecord._ + + "Json format for MedicalRecord" should "read and write correct JSON" in { + val orig = MedicalRecord( + id = LongId(1), + status = Status.New, + assignee = None, + previousStatus = None, + previousAssignee = None, + lastActiveUserId = None, + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + physician = Some("physician"), + meta = None, + predictedMeta = None, + predictedDocuments = None, + disease = "Breast", + requestId = RecordRequestId(UUID.fromString("7b54a75d-4197-4b27-9045-b9b6cb131be9")), + caseId = None, + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343") + ) + val writtenJson = recordFormat.write(orig) + + writtenJson should be ( + """{"id":1,"status":"New","assignee":null,"previousStatus":null,"previousAssignee":null,"lastActiveUser":null, + "lastUpdate":"2017-08-10T18:00Z","meta":[],"patientId":"748b5884-3528-4cb9-904b-7a8151d6e343","caseId":null, + "requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9","disease":"Breast","physician":"physician"}""".parseJson) + + val createRecordJson = + """{"disease":"Breast","patientId":"748b5884-3528-4cb9-904b-7a8151d6e343","requestId":"7b54a75d-4197-4b27-9045-b9b6cb131be9"}""".parseJson + val expectedCreatedRecord = MedicalRecord( + id = LongId(0), + status = MedicalRecord.Status.New, + previousStatus = None, + assignee = None, + previousAssignee = None, + lastActiveUserId = None, + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + requestId = RecordRequestId(UUID.fromString("7b54a75d-4197-4b27-9045-b9b6cb131be9")), + disease = "Breast", + caseId = None, + physician = None, + meta = None, + predictedMeta = None, + predictedDocuments = None, + lastUpdate = LocalDateTime.now() + ) + val parsedCreatedRecord = recordFormat.read(createRecordJson).copy(lastUpdate = expectedCreatedRecord.lastUpdate) + parsedCreatedRecord should be(expectedCreatedRecord) + + val updateRecordJson = + """{"meta":[{"type":"duplicate","predicted":true,"startPage":1.0,"endPage":2.0,"startOriginalPage":1.0}, + {"type":"reorder","items":[1,2]}, + {"type":"rotation","items":{"item1":1,"item2":2}}]}""".parseJson + val expectedUpdatedRecord = orig.copy( + meta = Some(TextJson(List( + Meta.Duplicate(predicted = Some(true), startPage = 1.0, endPage = 2.0, startOriginalPage = 1.0, endOriginalPage = None), + Meta.Reorder(predicted = None, items = Seq(1, 2)), + Meta.Rotation(predicted = None, items = Map("item1" -> 1, "item2" -> 2)) + ))) + ) + val parsedUpdatedRecord = applyUpdateToMedicalRecord(updateRecordJson, orig) + parsedUpdatedRecord should be(expectedUpdatedRecord) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordHistoryFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordHistoryFormatSuite.scala new file mode 100644 index 0000000..5cd6c8d --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordHistoryFormatSuite.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.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.MedicalRecordHistory + +class MedicalRecordHistoryFormatSuite extends FlatSpec with Matchers { + import recordhistory._ + + "Json format for MedicalRecordHistory" should "read and write correct JSON" in { + val recordHistory = MedicalRecordHistory( + id = LongId(10), + recordId = LongId(1), + executor = StringId("userId-001"), + state = MedicalRecordHistory.State.Clean, + action = MedicalRecordHistory.Action.Start, + created = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val writtenJson = recordHistoryFormat.write(recordHistory) + + writtenJson should be( + """{"id":10,"executor":"userId-001","recordId":1,"state":"Clean", + "action":"Start","created":"2017-08-10T18:00Z"}""".parseJson) + + val parsedRecordHistory = recordHistoryFormat.read(writtenJson) + parsedRecordHistory should be(recordHistory) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordIssueFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordIssueFormatSuite.scala new file mode 100644 index 0000000..9b89c97 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/MedicalRecordIssueFormatSuite.scala @@ -0,0 +1,47 @@ +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} +import xyz.driver.pdsuidomain.entities.MedicalRecordIssue + +class MedicalRecordIssueFormatSuite extends FlatSpec with Matchers { + import recordissue._ + + "Json format for MedicalRecordIssue" should "read and write correct JSON" in { + val recordIssue = MedicalRecordIssue( + id = LongId(10), + recordId = LongId(1), + userId = StringId("userId-001"), + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + isDraft = false, + text = "message text", + archiveRequired = false, + startPage = Some(1.0), + endPage = Some(2.0) + ) + val writtenJson = recordIssueWriter.write(recordIssue) + + writtenJson should be( + """{"id":10,"userId":"userId-001","lastUpdate":"2017-08-10T18:00Z","isDraft":false, + "text":"message text","archiveRequired":false,"startPage":1.0,"endPage":2.0}""".parseJson) + + val createRecordIssueJson = """{"text":"message text","startPage":1.0,"endPage":2.0}""".parseJson + val expectedCreatedRecordIssue = recordIssue.copy(id = LongId(0), lastUpdate = LocalDateTime.MIN, isDraft = true) + val parsedCreateRecordIssue = jsValueToRecordIssue(createRecordIssueJson, LongId(1), StringId("userId-001")) + parsedCreateRecordIssue should be(expectedCreatedRecordIssue) + + val updateRecordIssueJson = + """{"text":"new issue text","evidence":"issue evidence","archiveRequired":true,"startPage":1.0,"endPage":4.0}""".parseJson + val expectedUpdatedRecordIssue = recordIssue.copy( + text = "new issue text", + archiveRequired = true, + endPage = Some(4.0) + ) + val parsedUpdateRecordIssue = applyUpdateToRecordIssue(updateRecordIssueJson, recordIssue) + parsedUpdateRecordIssue should be(expectedUpdatedRecordIssue) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientFormatSuite.scala new file mode 100644 index 0000000..2761d6a --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientFormatSuite.scala @@ -0,0 +1,36 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.{LocalDate, LocalDateTime} + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.UuidId +import xyz.driver.pdsuidomain.entities.{Patient, PatientOrderId} + +class PatientFormatSuite extends FlatSpec with Matchers { + import patient._ + + "Json format for Patient" should "read and write correct JSON" in { + val orig = Patient( + id = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + status = Patient.Status.New, + name = "John Doe", + dob = LocalDate.parse("1980-06-30"), + assignee = None, + previousStatus = None, + previousAssignee = None, + lastActiveUserId = None, + isUpdateRequired = false, + condition = "breast", + orderId = PatientOrderId("7b54a75d-4197-4b27-9045-b9b6cb131be9"), + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val writtenJson = patientWriter.write(orig) + + writtenJson should be ( + """{"id":"748b5884-3528-4cb9-904b-7a8151d6e343","dob":"1980-06-30","name":"John Doe","status":"New","assignee":null, + "previousStatus":null,"previousAssignee":null,"lastActiveUser":null,"lastUpdate":"2017-08-10T18:00Z", + "orderId":"7b54a75d-4197-4b27-9045-b9b6cb131be9","condition":"breast"}""".parseJson) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHistoryFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHistoryFormatSuite.scala new file mode 100644 index 0000000..c6a5a9f --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHistoryFormatSuite.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.domain.{LongId, StringId, UuidId} +import xyz.driver.pdsuidomain.entities.PatientHistory + +class PatientHistoryFormatSuite extends FlatSpec with Matchers { + import patienthistory._ + + "Json format for PatientHistory" should "read and write correct JSON" in { + val patientHistory = PatientHistory( + id = LongId(10), + patientId = UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), + executor = StringId("userId-001"), + state = PatientHistory.State.Verify, + action = PatientHistory.Action.Start, + created = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val writtenJson = patientHistoryFormat.write(patientHistory) + + writtenJson should be( + """{"id":10,"executor":"userId-001","patientId":"40892a07-c638-49d2-9795-1edfefbbcc7c","state":"Verify", + "action":"Start","created":"2017-08-10T18:00Z"}""".parseJson) + + val parsedPatientHistory = patientHistoryFormat.read(writtenJson) + parsedPatientHistory should be(patientHistory) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHypothesisFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHypothesisFormatSuite.scala new file mode 100644 index 0000000..2999cc1 --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientHypothesisFormatSuite.scala @@ -0,0 +1,31 @@ +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.PatientHypothesis + +class PatientHypothesisFormatSuite extends FlatSpec with Matchers { + import patienthypothesis._ + + "Json format for PatientHypothesis" should "read and write correct JSON" in { + val orig = PatientHypothesis( + id = UuidId("815d9715-1089-4775-b120-3afb983b9a97"), + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + hypothesisId = UuidId("e76e2fc4-a29c-44fb-a81b-8856d06bb1d4"), + rationale = None, + matchedTrials = 1 + ) + val writtenJson = patientHypothesisWriter.write((orig, true)) + + writtenJson should be ( + """{"id":"815d9715-1089-4775-b120-3afb983b9a97","patientId":"748b5884-3528-4cb9-904b-7a8151d6e343", + "hypothesisId":"e76e2fc4-a29c-44fb-a81b-8856d06bb1d4","rationale":null,"matchedTrials":1,"isRationaleRequired":true}""".parseJson) + + val updatePatientHypothesisJson = """{"rationale":"rationale"}""".parseJson + val expectedUpdatedPatientHypothesis = orig.copy(rationale = Some("rationale")) + val parsedUpdatePatientHypothesis = applyUpdateToPatientHypothesis(updatePatientHypothesisJson, orig) + parsedUpdatePatientHypothesis should be(expectedUpdatedPatientHypothesis) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientIssueFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientIssueFormatSuite.scala new file mode 100644 index 0000000..1e2a11e --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientIssueFormatSuite.scala @@ -0,0 +1,44 @@ +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.PatientIssue + +class PatientIssueFormatSuite extends FlatSpec with Matchers { + import patientissue._ + + "Json format for PatientIssue" should "read and write correct JSON" in { + val patientIssue = PatientIssue( + id = LongId(10), + patientId = UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), + userId = StringId("userId-001"), + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + isDraft = false, + text = "message text", + archiveRequired = false + ) + val writtenJson = patientIssueWriter.write(patientIssue) + + writtenJson should be( + """{"id":10,"userId":"userId-001","lastUpdate":"2017-08-10T18:00Z","isDraft":false, + "text":"message text","archiveRequired":false}""".parseJson) + + val createPatientIssueJson = """{"text":"message text"}""".parseJson + val expectedCreatedPatientIssue = patientIssue.copy(id = LongId(0), lastUpdate = LocalDateTime.MIN, isDraft = true) + val parsedCreatePatientIssue = jsValueToPatientIssue(createPatientIssueJson, UuidId("40892a07-c638-49d2-9795-1edfefbbcc7c"), StringId("userId-001")) + parsedCreatePatientIssue should be(expectedCreatedPatientIssue) + + val updatePatientIssueJson = + """{"text":"new issue text","evidence":"issue evidence","archiveRequired":true}""".parseJson + val expectedUpdatedPatientIssue = patientIssue.copy( + text = "new issue text", + archiveRequired = true + ) + val parsedUpdatePatientIssue = applyUpdateToPatientIssue(updatePatientIssueJson, patientIssue) + parsedUpdatePatientIssue should be(expectedUpdatedPatientIssue) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientLabelFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientLabelFormatSuite.scala new file mode 100644 index 0000000..e18239c --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/PatientLabelFormatSuite.scala @@ -0,0 +1,76 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDate + +import spray.json._ +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, UuidId} +import xyz.driver.pdsuidomain.entities.{PatientLabel, PatientLabelEvidenceView} + +class PatientLabelFormatSuite extends FlatSpec with Matchers { + + "Json format for PatientLabel" should "read and write correct JSON" in { + import patientlabel._ + val orig = PatientLabel( + id = LongId(1), + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + labelId = LongId(20), + primaryValue = Some(FuzzyValue.Yes), + verifiedPrimaryValue = None, + isVisible = true, + score = 1, + isImplicitMatch = false + ) + val writtenJson = patientLabelWriter.write((orig, true)) + + writtenJson should be ( + """{"id":1,"labelId":20,"primaryValue":"Yes","verifiedPrimaryValue":null,"isVisible":true,"isVerified":true, + "score":1,"isImplicitMatch":false}""".parseJson) + + val updatePatientLabelJson = """{"verifiedPrimaryValue":"No"}""".parseJson + val expectedUpdatedPatientLabel = orig.copy(verifiedPrimaryValue = Some(FuzzyValue.No)) + val parsedUpdatePatientLabel = applyUpdateToPatientLabel(updatePatientLabelJson, orig) + parsedUpdatePatientLabel should be(expectedUpdatedPatientLabel) + } + + "Json format for PatientLabelEvidence" should "read and write correct JSON" in { + import patientlabel._ + val orig = PatientLabelEvidenceView( + id = LongId(1), + value = FuzzyValue.Maybe, + evidenceText = "evidence text", + documentId = Some(LongId(21)), + evidenceId = Some(LongId(10)), + reportId = None, + documentType = "document type", + date = Some(LocalDate.parse("2017-08-10")), + providerType = "provider type", + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + labelId = LongId(20), + isImplicitMatch = false + ) + val writtenJson = patientLabelEvidenceWriter.write(orig) + + writtenJson should be ( + """{"id":1,"value":"Maybe","evidenceText":"evidence text","documentId":21,"evidenceId":10,"reportId":null, + "documentType":"document type","date":"2017-08-10","providerType":"provider type"}""".parseJson) + } + + "Json format for PatientLabelDefiningCriteria" should "read and write correct JSON" in { + import patientdefiningcriteria._ + val orig = PatientLabel( + id = LongId(1), + patientId = UuidId("748b5884-3528-4cb9-904b-7a8151d6e343"), + labelId = LongId(20), + primaryValue = Some(FuzzyValue.Yes), + verifiedPrimaryValue = Some(FuzzyValue.Yes), + isVisible = true, + score = 1, + isImplicitMatch = false + ) + val writtenJson = patientLabelDefiningCriteriaWriter.write(orig) + + writtenJson should be ("""{"id":1,"value":"Yes"}""".parseJson) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatTestSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatSuite.scala index 397d2aa..59cf779 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatTestSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialFormatSuite.scala @@ -7,7 +7,7 @@ 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 { +class TrialFormatSuite extends FlatSpec with Matchers { import trial._ "Json format for Trial" should "read and write correct JSON" in { @@ -45,6 +45,6 @@ class TrialFormatTestSuite extends FlatSpec with Matchers { val expectedUpdatedTrial = orig.copy(hypothesisId = None, overview = Some("new overview")) val parsedUpdateTrial = applyUpdateToTrial(updateTrialJson, orig) parsedUpdateTrial should be(expectedUpdatedTrial) - } + } diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialHistoryFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialHistoryFormatSuite.scala new file mode 100644 index 0000000..dbb143c --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialHistoryFormatSuite.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.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.TrialHistory + +class TrialHistoryFormatSuite extends FlatSpec with Matchers { + import trialhistory._ + + "Json format for TrialHistory" should "read and write correct JSON" in { + val trialHistory = TrialHistory( + id = LongId(10), + trialId = StringId("NCT000001"), + executor = StringId("userId-001"), + state = TrialHistory.State.Summarize, + action = TrialHistory.Action.Start, + created = LocalDateTime.parse("2017-08-10T18:00:00") + ) + val writtenJson = trialHistoryFormat.write(trialHistory) + + writtenJson should be( + """{"id":10,"executor":"userId-001","trialId":"NCT000001","state":"Summarize", + "action":"Start","created":"2017-08-10T18:00Z"}""".parseJson) + + val parsedTrialHistory = trialHistoryFormat.read(writtenJson) + parsedTrialHistory should be(trialHistory) + } + +} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialIssueFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialIssueFormatSuite.scala new file mode 100644 index 0000000..02e14ba --- /dev/null +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/TrialIssueFormatSuite.scala @@ -0,0 +1,49 @@ +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} +import xyz.driver.pdsuidomain.entities.TrialIssue + +class TrialIssueFormatSuite extends FlatSpec with Matchers { + import trialissue._ + + "Json format for TrialIssue" should "read and write correct JSON" in { + val trialIssue = TrialIssue( + id = LongId(10), + trialId = StringId("NCT000001"), + userId = StringId("userId-001"), + lastUpdate = LocalDateTime.parse("2017-08-10T18:00:00"), + isDraft = false, + text = "message text", + evidence = "evidence", + archiveRequired = false, + meta = "{}" + ) + val writtenJson = trialIssueWriter.write(trialIssue) + + writtenJson should be( + """{"id":10,"userId":"userId-001","lastUpdate":"2017-08-10T18:00Z","isDraft":false, + "text":"message text","evidence":"evidence","archiveRequired":false,"meta":"{}"}""".parseJson) + + val createTrialIssueJson = """{"text":"message text","evidence":"evidence","meta":"{}"}""".parseJson + val expectedCreatedTrialIssue = trialIssue.copy(id = LongId(0), lastUpdate = LocalDateTime.MIN, isDraft = true) + val parsedCreateTrialIssue = jsValueToTrialIssue(createTrialIssueJson, StringId("NCT000001"), StringId("userId-001")) + parsedCreateTrialIssue should be(expectedCreatedTrialIssue) + + val updateTrialIssueJson = + """{"text":"new issue text","evidence":"issue evidence","archiveRequired":true, + "meta":"{\"startPage\":1.0,\"endPage\":2.0}"}""".parseJson + val expectedUpdatedTrialIssue = trialIssue.copy( + text = "new issue text", + evidence = "issue evidence", + archiveRequired = true, + meta = """{"startPage":1.0,"endPage":2.0}""" + ) + val parsedUpdateTrialIssue = applyUpdateToTrialIssue(updateTrialIssueJson, trialIssue) + parsedUpdateTrialIssue should be(expectedUpdatedTrialIssue) + } + +} |