Marshalling/unmarshalling test.

This commit is contained in:
Pavel Kachalouski
2018-05-11 20:14:21 +02:00
parent 2aa352ae4e
commit 715514f9ec
4 changed files with 96 additions and 50 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}
}