This repository has been archived by the owner on Mar 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageCompanionsSpec.scala
287 lines (269 loc) · 9.33 KB
/
MessageCompanionsSpec.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.dhpcs.jsonrpc
import com.dhpcs.jsonrpc.JsonRpcMessage._
import com.dhpcs.jsonrpc.Message.MessageFormats
import com.dhpcs.jsonrpc.MessageCompanionsSpec._
import org.scalatest._
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads.min
import play.api.libs.json._
class MessageCompanionsSpec extends FreeSpec {
"A Command" - {
"with an invalid method" - {
val jsonRpcRequestMessage = JsonRpcRequestMessage(
method = "invalidMethod",
JsObject.empty,
NumericCorrelationId(1)
)
val jsError = JsError("unknown method invalidMethod")
s"fails to decode with error $jsError" in assert(
Command.read(jsonRpcRequestMessage) === jsError
)
}
"of type AddTransactionCommand" - {
"with params of the wrong type" - {
val jsonRpcRequestMessage = JsonRpcRequestMessage(
method = "addTransaction",
JsArray.empty,
NumericCorrelationId(1)
)
val jsError = JsError(__, "command parameters must be named")
s"fails to decode with error $jsError" in assert(
Command.read(jsonRpcRequestMessage) === jsError
)
}
"with empty params" - {
val jsonRpcRequestMessage = JsonRpcRequestMessage(
method = "addTransaction",
JsObject.empty,
NumericCorrelationId(1)
)
val jsError = JsError(
Seq(
(__ \ "to", Seq(JsonValidationError("error.path.missing"))),
(__ \ "from", Seq(JsonValidationError("error.path.missing"))),
(__ \ "value", Seq(JsonValidationError("error.path.missing")))
)
)
s"fails to decode with error $jsError" in assert(
Command.read(jsonRpcRequestMessage) === jsError
)
}
val addTransactionCommand = AddTransactionCommand(
from = 0,
to = 1,
value = BigDecimal(1000000),
description = Some("Property purchase"),
metadata = Some(
Json.obj(
"property" -> "The TARDIS"
)
)
)
val id = NumericCorrelationId(1)
val jsonRpcRequestMessage = JsonRpcRequestMessage(
method = "addTransaction",
Json.obj(
"from" -> 0,
"to" -> 1,
"value" -> BigDecimal(1000000),
"description" -> "Property purchase",
"metadata" -> Json.obj(
"property" -> "The TARDIS"
)
),
NumericCorrelationId(1)
)
s"decodes to $addTransactionCommand" in assert(
Command.read(jsonRpcRequestMessage) ===
JsSuccess(addTransactionCommand)
)
s"encodes to $jsonRpcRequestMessage" in assert(
Command.write(addTransactionCommand, id) === jsonRpcRequestMessage
)
}
}
"A Response of type AddTransactionResponse" - {
val addTransactionResponse = AddTransactionResponse(id = 0)
val id = NumericCorrelationId(1)
val jsonRpcResponseMessage = JsonRpcResponseSuccessMessage(
JsNumber(0),
NumericCorrelationId(1)
)
val method = "addTransaction"
s"decodes to $addTransactionResponse" in assert(
Response.read(jsonRpcResponseMessage, method) === JsSuccess(
addTransactionResponse)
)
s"encodes to $jsonRpcResponseMessage" in assert(
Response.write(addTransactionResponse, id) === jsonRpcResponseMessage
)
}
"A Notification" - {
"with an invalid method" - {
val jsonRpcNotificationMessage = JsonRpcNotificationMessage(
method = "invalidMethod",
JsObject.empty
)
val jsError = JsError("unknown method invalidMethod")
s"fails to decode with error $jsError" in assert(
Notification.read(jsonRpcNotificationMessage) === jsError
)
}
"of type TransactionAddedNotification" - {
"with params of the wrong type" - {
val jsonRpcNotificationMessage = JsonRpcNotificationMessage(
method = "transactionAdded",
JsArray.empty
)
val jsError = JsError(__, "notification parameters must be named")
s"fails to decode with error $jsError" in assert(
Notification.read(jsonRpcNotificationMessage) === jsError
)
}
"with empty params" - {
val jsonRpcNotificationMessage = JsonRpcNotificationMessage(
method = "transactionAdded",
JsObject.empty
)
val jsError = JsError(__ \ "transaction", "error.path.missing")
s"fails to decode with error $jsError" in assert(
Notification.read(jsonRpcNotificationMessage) === jsError
)
}
val clientJoinedZoneNotification = TransactionAddedNotification(
Transaction(
id = 0,
from = 0,
to = 1,
value = BigDecimal(1000000)
)
)
val jsonRpcNotificationMessage = JsonRpcNotificationMessage(
method = "transactionAdded",
params = Json.obj(
"transaction" -> Json.parse(
"""{ "id" : 0, "from" : 0, "to" : 1, "value" : 1000000 }""")
)
)
s"decodes to $clientJoinedZoneNotification" in assert(
Notification.read(jsonRpcNotificationMessage) === JsSuccess(
clientJoinedZoneNotification)
)
s"encodes to $jsonRpcNotificationMessage" in assert(
Notification
.write(clientJoinedZoneNotification) === jsonRpcNotificationMessage
)
}
}
}
object MessageCompanionsSpec {
private sealed abstract class Message
private sealed abstract class Command extends Message
private final case class UpdateAccountCommand(account: Account)
extends Command
private final case class AddTransactionCommand(
from: Int,
to: Int,
value: BigDecimal,
description: Option[String] = None,
metadata: Option[JsObject] = None)
extends Command {
require(value >= 0)
}
private object AddTransactionCommand {
implicit final val AddTransactionCommandFormat
: Format[AddTransactionCommand] = (
(JsPath \ "from").format[Int] and
(JsPath \ "to").format[Int] and
(JsPath \ "value").format(min[BigDecimal](0)) and
(JsPath \ "description").formatNullable[String] and
(JsPath \ "metadata").formatNullable[JsObject]
)(
(from, to, value, description, metadata) =>
AddTransactionCommand(
from,
to,
value,
description,
metadata
),
addTransactionCommand =>
(addTransactionCommand.from,
addTransactionCommand.to,
addTransactionCommand.value,
addTransactionCommand.description,
addTransactionCommand.metadata)
)
}
private object Command extends CommandCompanion[Command] {
override final val CommandFormats = MessageFormats(
"updateAccount" -> Json.format[UpdateAccountCommand],
"addTransaction" -> Json.format[AddTransactionCommand]
)
}
private sealed abstract class Response extends Message
private case object UpdateAccountResponse extends Response
private final case class AddTransactionResponse(id: Int) extends Response
private object Response extends ResponseCompanion[Response] {
override final val ResponseFormats = MessageFormats(
"updateAccount" -> Message.objectFormat(UpdateAccountResponse),
"addTransaction" -> Format[AddTransactionResponse](
Reads.of[Int].map(AddTransactionResponse),
Writes.of[Int].contramap(_.id)
)
)
}
private sealed abstract class Notification extends Message
private final case class AccountUpdatedNotification(account: Account)
extends Notification
private final case class TransactionAddedNotification(
transaction: Transaction)
extends Notification
private object Notification extends NotificationCompanion[Notification] {
override final val NotificationFormats = MessageFormats(
"accountUpdated" -> Json.format[AccountUpdatedNotification],
"transactionAdded" -> Json.format[TransactionAddedNotification]
)
}
private final case class Account(id: Int,
name: Option[String] = None,
metadata: Option[JsObject] = None)
private object Account {
implicit final val AccountFormat: Format[Account] = Json.format[Account]
}
private final case class Transaction(id: Int,
from: Int,
to: Int,
value: BigDecimal,
description: Option[String] = None,
metadata: Option[JsObject] = None) {
require(value >= 0)
}
private object Transaction {
implicit final val TransactionFormat: Format[Transaction] = (
(JsPath \ "id").format[Int] and
(JsPath \ "from").format[Int] and
(JsPath \ "to").format[Int] and
(JsPath \ "value").format(min[BigDecimal](0)) and
(JsPath \ "description").formatNullable[String] and
(JsPath \ "metadata").formatNullable[JsObject]
)(
(id, from, to, value, description, metadata) =>
Transaction(
id,
from,
to,
value,
description,
metadata
),
transaction =>
(transaction.id,
transaction.from,
transaction.to,
transaction.value,
transaction.description,
transaction.metadata)
)
}
}