-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTestHalConstruction.scala
184 lines (163 loc) · 6.25 KB
/
TestHalConstruction.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package play.api.hal
import org.scalatest.{ FunSuite, Matchers }
import play.api.hal.Hal._
import play.api.libs.json.Json
class TestHalConstruction extends FunSuite with Matchers {
case class TestData(total: Int, currency: String, status: String)
implicit val testWrites = Json.writes[TestData]
test("A mininmal HAL resource is a JSON object") {
val data = TestData(20, "EUR", "shipped")
data.asResource.json should equal(Json.toJson(data))
}
test("A HAL resource may contain only links") {
(Hal.links(
HalLink("self", "/orders"),
HalLink("next", "/orders?page=2"),
HalLink("find", "/orders{?id}", templated = true))).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/orders" },
"next": { "href": "/orders?page=2" },
"find": { "href": "/orders{?id}", "templated": true }
}
}""".stripMargin))
}
test("A HAL resouce may contain links and state") {
val data = TestData(20, "EUR", "shipped")
(data.asResource ++
HalLink("self", "/orders") ++
HalLink("next", "/orders?page=2") ++
HalLink("find", "/orders{?id}", templated = true)).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/orders" },
"next": { "href": "/orders?page=2" },
"find": { "href": "/orders{?id}", "templated": true }
},
"total" : 20,
"currency" : "EUR",
"status": "shipped"
}""".stripMargin))
}
test("a HAL resource may embed links") {
val json = TestData(20, "EUR", "shipped")
Json.toJson(Hal.state(json))
val selfLink = HalLink("self", "/blog-post").asResource
val authorLink = HalLink("author", "/people/alan-watts")
val embeddedAuthorState = Json.obj(
"name" -> "Alan Watts",
"born" -> "January 6, 1915",
"died" -> "November 16, 1973").asResource
(selfLink ++ Hal.embeddedLink(authorLink, embeddedAuthorState)).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/blog-post" },
"author": { "href": "/people/alan-watts" }
},
"_embedded": {
"author": {
"_links": { "self": { "href": "/people/alan-watts" } },
"name": "Alan Watts",
"born": "January 6, 1915",
"died": "November 16, 1973"
}
}
}""".stripMargin))
}
test("a HAL resource may embed multible resources") {
val baseResource =
Json.obj("currentlyProcessing" -> 14, "shippedToday" -> 20).asResource ++
HalLink("self", "/orders") ++
HalLink("next", "/orders?page=2") ++
HalLink("find", "/orders{?id}", templated = true)
val resource1 =
TestData(30, "USD", "shipped").asResource ++
HalLink("self", "/orders/123") ++
HalLink("basket", "/baskets/98712") ++
HalLink("customer", "/customers/7809")
val resource2 =
TestData(20, "USD", "processing").asResource ++
HalLink("self", "/orders/124") ++
HalLink("basket", "/baskets/97213") ++
HalLink("customer", "/customers/12369")
val res = baseResource ++ Hal.embedded("orders", resource1, resource2)
res.json should equal(Json.parse("""{
"_links": {
"self": { "href": "/orders" },
"next": { "href": "/orders?page=2" },
"find": { "href": "/orders{?id}", "templated": true }
},
"_embedded": {
"orders": [{
"_links": {
"self": { "href": "/orders/123" },
"basket": { "href": "/baskets/98712" },
"customer": { "href": "/customers/7809" }
},
"total": 30,
"currency": "USD",
"status": "shipped"
},{
"_links": {
"self": { "href": "/orders/124" },
"basket": { "href": "/baskets/97213" },
"customer": { "href": "/customers/12369" }
},
"total": 20,
"currency": "USD",
"status": "processing"
}]
},
"currentlyProcessing": 14,
"shippedToday": 20
}""".stripMargin))
}
test("provide alternative names for composition") {
val data = TestData(20, "EUR", "shipped")
data.asResource ++
HalLink("self", "/orders") ++
HalLink("next", "/orders?page=2") ++
HalLink("find", "/orders{?id}", templated = true) should equal(
data.asResource include
HalLink("self", "/orders") include
HalLink("next", "/orders?page=2") include
HalLink("find", "/orders{?id}", templated = true)
)
}
test("provide support for optional link attributes") {
Hal.links(
HalLink("self", "/orders").withDeprecation("http://www.thisisdeprecated.com"),
HalLink("next", "/orders?page=2").withType("application/json"),
HalLink("find", "/orders{?id}", templated = true).withHreflang("de")).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/orders", "deprecation": "http://www.thisisdeprecated.com" },
"next": { "href": "/orders?page=2", "type": "application/json" },
"find": { "href": "/orders{?id}", "templated": true, "hreflang": "de" }
}
}""".stripMargin)
)
}
test("provide support for arbitrary link attributes") {
Hal.links(
HalLink("self", "/orders").withLinkAttributes(Json.obj("isRequired" -> true))
).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/orders", "isRequired": true }
}
}""".stripMargin)
)
}
test("provide support for arbitrary link attributes (from seq)") {
Hal.linksSeq(
HalLink("self", "/orders").withLinkAttributes(Json.obj("isRequired" -> true)) :: Nil
).json should equal(
Json.parse("""{
"_links": {
"self": { "href": "/orders", "isRequired": true }
}
}""".stripMargin)
)
}
}