forked from scala-jsonapi/scala-jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleSpec.scala
95 lines (85 loc) · 2.85 KB
/
ExampleSpec.scala
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.zalando.jsonapi.json
import org.scalatest.{ MustMatchers, WordSpec }
import org.zalando.jsonapi.json.sprayjson.SprayJsonJsonapiProtocol
import org.zalando.jsonapi.model.JsonApiObject.StringValue
import org.zalando.jsonapi.model.RootObject.ResourceObject
import org.zalando.jsonapi.model.{ Attribute, Links, RootObject }
import org.zalando.jsonapi.{ JsonapiRootObjectWriter, _ }
import spray.json._
class ExampleSpec extends WordSpec with MustMatchers with SprayJsonJsonapiProtocol {
"JsonapiRootObject" when {
"using root object serializer" must {
"serialize accordingly" in {
val json =
"""
{
"data": {
"id": "42",
"type": "person",
"attributes": {
"name": "foobar"
}
}
}
""".stripMargin.parseJson
Person(42, "foobar").rootObject.toJson mustEqual json
}
}
"serialize accordingly with links object in data object" in {
implicit val personJsonapiRootObjectWriter: JsonapiRootObjectWriter[Person] = new JsonapiRootObjectWriter[Person] {
override def toJsonapi(person: Person) = {
RootObject(data = Some(ResourceObject(
`type` = "person",
id = Some(person.id.toString),
attributes = Some(List(
Attribute("name", StringValue(person.name))
)), links = Some(List(Links.Self("http://test.link/person/42", None))))))
}
}
val json =
"""
{
"data": {
"id": "42",
"type": "person",
"attributes": {
"name": "foobar"
},
"links": {
"self": "http://test.link/person/42"
}
}
}
""".stripMargin.parseJson
Person(42, "foobar").rootObject.toJson mustEqual json
}
"serialize accordingly with links object in root object" in {
implicit val personJsonapiRootObjectWriter: JsonapiRootObjectWriter[Person] = new JsonapiRootObjectWriter[Person] {
override def toJsonapi(person: Person) = {
RootObject(data = Some(ResourceObject(
`type` = "person",
id = Some(person.id.toString),
attributes = Some(List(
Attribute("name", StringValue(person.name))
)))), links = Some(List(Links.Next("http://test.link/person/43", None))))
}
}
val json =
"""
{
"data": {
"id": "42",
"type": "person",
"attributes": {
"name": "foobar"
}
},
"links": {
"next": "http://test.link/person/43"
}
}
""".stripMargin.parseJson
Person(42, "foobar").rootObject.toJson mustEqual json
}
}
}