From 442579b27ccbac82cb001a5b02402a593d005977 Mon Sep 17 00:00:00 2001 From: Kseniya Tomskikh Date: Mon, 14 Aug 2017 14:15:14 +0600 Subject: PDSUI-2188 Created tests for export and dictionary API --- .../json/sprayformats/bridgeuploadqueue.scala | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala') diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala new file mode 100644 index 0000000..77fb4d2 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/bridgeuploadqueue.scala @@ -0,0 +1,68 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue +import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue.Item + +object bridgeuploadqueue { + import DefaultJsonProtocol._ + import common._ + + implicit val queueUploadItemFormat: RootJsonFormat[BridgeUploadQueue.Item] = new RootJsonFormat[Item] { + override def write(obj: Item) = + JsObject( + "kind" -> obj.kind.toJson, + "tag" -> obj.tag.toJson, + "created" -> obj.created.toJson, + "attempts" -> obj.attempts.toJson, + "nextAttempt" -> obj.nextAttempt.toJson, + "completed" -> obj.completed.toJson + ) + + override def read(json: JsValue): Item = json match { + case JsObject(fields) => + val kind = fields + .get("kind") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `kind` field: $json")) + + val tag = fields + .get("tag") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `tag` field: $json")) + + val created = fields + .get("created") + .map(_.convertTo[LocalDateTime]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `created` field: $json")) + + val attempts = fields + .get("attempts") + .map(_.convertTo[Int]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `attempts` field: $json")) + + val nextAttempt = fields + .get("nextAttempt") + .map(_.convertTo[LocalDateTime]) + .getOrElse( + deserializationError(s"BridgeUploadQueue.Item json object does not contain `nextAttempt` field: $json")) + + BridgeUploadQueue.Item( + kind = kind, + tag = tag, + created = created, + attempts = attempts, + nextAttempt = nextAttempt, + completed = true, + dependencyKind = None, + dependencyTag = None + ) + + case _ => deserializationError(s"Expected Json Object as BridgeUploadQueue.Item, but got $json") + } + } +} -- cgit v1.2.3