1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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 CriterionFormatSuite 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 = "{}",
inclusion = None
)
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","inclusion":null,"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","inclusion":null,
"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,"inclusion":true}""".parseJson
val expectedUpdatedCriterion = richCriterion.copy(
criterion = criterion.copy(
text = Some("new text"),
isCompound = true,
meta = "{}",
inclusion = Some(true)
))
val parsedUpdateCriterion = applyUpdateToCriterion(updateCriterionJson, richCriterion)
parsedUpdateCriterion should be(expectedUpdatedCriterion)
}
}
|