From 715514f9ec64115f94f4cee01432349ecc0bff73 Mon Sep 17 00:00:00 2001 From: Pavel Kachalouski Date: Fri, 11 May 2018 20:14:21 +0200 Subject: [PATCH] Marshalling/unmarshalling test. --- .idea/modules/root-build.iml | 2 +- .idea/modules/root.iml | 82 +++++++++---------- .../eu/xeppaka/bot1/TelegramEntities.scala | 29 +++++-- src/test/scala/example/MarshalSpec.scala | 33 ++++++++ 4 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 src/test/scala/example/MarshalSpec.scala diff --git a/.idea/modules/root-build.iml b/.idea/modules/root-build.iml index 414f9c7..ee53ba2 100644 --- a/.idea/modules/root-build.iml +++ b/.idea/modules/root-build.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/root.iml b/.idea/modules/root.iml index 7739fce..b3e3660 100644 --- a/.idea/modules/root.iml +++ b/.idea/modules/root.iml @@ -15,45 +15,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -63,7 +26,44 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala b/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala index 2034199..aaae2bc 100644 --- a/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala +++ b/src/main/scala/eu/xeppaka/bot1/TelegramEntities.scala @@ -10,8 +10,18 @@ object TelegramEntities extends SprayJsonSupport with DefaultJsonProtocol { error_code: Option[Int] = None, result: T) + object Response { + implicit val responseGetMeFormat: RootJsonFormat[Response[GetMe]] = jsonFormat4(Response[GetMe]) + implicit val responseWebhookInfoFormat: RootJsonFormat[Response[WebhookInfo]] = jsonFormat4(Response[WebhookInfo]) + implicit val responseStringFormat: RootJsonFormat[Response[String]] = jsonFormat4(Response[String]) + } + case class GetMe(id: Int, is_bot: Boolean, first_name: String, username: String) + object GetMe { + implicit val getMeFormat: RootJsonFormat[GetMe] = jsonFormat4(GetMe.apply) + } + case class InlineQuery(id: String, from: User, location: Location, @@ -98,11 +108,19 @@ object TelegramEntities extends SprayJsonSupport with DefaultJsonProtocol { case class InputFile() + object InputFile { + implicit val inputFileFormat: RootJsonFormat[InputFile] = jsonFormat0(InputFile.apply) + } + case class Webhook(url: String, certificate: Option[InputFile] = None, max_connections: Option[Int] = None, allowed_updates: Option[Seq[String]] = None) + object Webhook { + implicit val webHookFormat: RootJsonFormat[Webhook] = jsonFormat4(Webhook.apply) + } + case class WebhookInfo(url: String, has_custom_certificate: Boolean, pending_update_count: Int, @@ -111,12 +129,7 @@ object TelegramEntities extends SprayJsonSupport with DefaultJsonProtocol { max_connections: Option[Int] = None, allowed_updates: Option[Seq[String]] = None) - implicit val inputFileFormat: RootJsonFormat[InputFile] = jsonFormat0(InputFile) - implicit val webHookFormat: RootJsonFormat[Webhook] = jsonFormat4(Webhook) - implicit val webHookInfoFormat: RootJsonFormat[WebhookInfo] = jsonFormat7(WebhookInfo) - implicit val getMeFormat: RootJsonFormat[GetMe] = jsonFormat4(GetMe) - // responses - implicit val responseGetMeFormat: RootJsonFormat[Response[GetMe]] = jsonFormat4(Response[GetMe]) - implicit val responseWebhookInfoFormat: RootJsonFormat[Response[WebhookInfo]] = jsonFormat4(Response[WebhookInfo]) - implicit val responseStringFormat: RootJsonFormat[Response[String]] = jsonFormat4(Response[String]) + object WebhookInfo { + implicit val webHookInfoFormat: RootJsonFormat[WebhookInfo] = jsonFormat7(WebhookInfo.apply) + } } diff --git a/src/test/scala/example/MarshalSpec.scala b/src/test/scala/example/MarshalSpec.scala new file mode 100644 index 0000000..d7bfe69 --- /dev/null +++ b/src/test/scala/example/MarshalSpec.scala @@ -0,0 +1,33 @@ +package example + +import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport +import akka.http.scaladsl.unmarshalling.Unmarshal +import akka.stream.Materializer +import akka.util.ByteString +import org.scalatest.FlatSpec + +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration._ +import spray.json._ + +case class Text(t: Option[String], t1: Option[String]) + +trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol { + implicit val textFormat = jsonFormat2(Text) +} + +class MarshalSpec extends FlatSpec with JsonSupport { + + "Marshal to MessageEntity" should "work" in { + implicit val mat: Materializer = null + val body = + """ + |{"t1": "some text value"} + """.stripMargin +// val bytes = ByteString(body) + val f = Unmarshal(body).to[Text] + val entity = Await.result(f, 1 second) + println(entity) + } +}