From 97b37e18e4579677aacef17e327f50f2673523f0 Mon Sep 17 00:00:00 2001 From: Adrian Domin Date: Tue, 18 Jul 2023 12:27:29 +0200 Subject: [PATCH] Add tests to check serialization and deserialization --- .../client4/circe/BackendStubCirceTests.scala | 15 +++++++ .../sttp/client4/BackendStubJson4sTests.scala | 37 +++++++++++++++++ .../jsoniter/BackendStubJsoniterTests.scala | 27 ++++++++++++ .../client4/BackendStubPlayJsonTests.scala | 39 ++++++++++++++++++ .../client4/BackendStubSprayJsonTests.scala | 41 +++++++++++++++++++ .../ziojson/BackendStubZioJsonTests.scala | 40 ++++++++++++++++++ .../ziojson/BackendStubZioJsonTests.scala | 40 ++++++++++++++++++ 7 files changed, 239 insertions(+) create mode 100644 json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala create mode 100644 json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala create mode 100644 json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala create mode 100644 json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala create mode 100644 json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala create mode 100644 json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala diff --git a/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala b/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala index 0b5f9505b1..aff630add2 100644 --- a/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala +++ b/json/circe/src/test/scala/sttp/client4/circe/BackendStubCirceTests.scala @@ -6,8 +6,12 @@ import sttp.client4.testing.SyncBackendStub import io.circe.generic.auto._ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers +import io.circe.syntax.EncoderOps +import io.circe.JsonObject +import sttp.model.Uri class BackendStubCirceTests extends AnyFlatSpec with Matchers with ScalaFutures { + it should "deserialize to json using a string stub" in { val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") val r = basicRequest.get(uri"http://example.org").response(asJson[Person]).send(backend) @@ -15,5 +19,16 @@ class BackendStubCirceTests extends AnyFlatSpec with Matchers with ScalaFutures r.body should be(Right(Person("John"))) } + it should "serialize from JsonObject using implicit upickleBodySerializer" in { + + val jObject: JsonObject = JsonObject(("location", "hometown".asJson), ("bio", "Scala programmer".asJson)) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) + val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) + + r.is200 should be(true) + r.body should be(jObject) + } + case class Person(name: String) } diff --git a/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala b/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala new file mode 100644 index 0000000000..16b930fce9 --- /dev/null +++ b/json/json4s/src/test/scala/sttp/client4/BackendStubJson4sTests.scala @@ -0,0 +1,37 @@ +package sttp.client4 + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import org.json4s.{native, DefaultFormats, JField, JObject} +import org.json4s.JsonAST.JString + +case class Person(name: String) + +class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { + + implicit val serialization = native.Serialization + implicit val formats = DefaultFormats + + import json4s._ + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } + + it should "serialize from JsObject using implicit upickleBodySerializer" in { + val jObject: JObject = JObject(JField("location", JString("hometown")), JField("bio", JString("Scala programmer"))) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) + val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) + + r.is200 should be(true) + r.body should be(jObject) + } +} diff --git a/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala new file mode 100644 index 0000000000..d59c9df35c --- /dev/null +++ b/json/jsoniter/src/test/scala/sttp/client4/jsoniter/BackendStubJsoniterTests.scala @@ -0,0 +1,27 @@ +package sttp.client4.jsoniter + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec +import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker +import sttp.client4.basicRequest + +case class Person(name: String) + +object Person { + implicit val personJsonValueCodec: JsonValueCodec[Person] = JsonCodecMaker.make +} + +class BackendStubJsoniterTests extends AnyFlatSpec with Matchers with ScalaFutures { + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } +} diff --git a/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala b/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala new file mode 100644 index 0000000000..60f128cdfc --- /dev/null +++ b/json/play-json/src/test/scala/sttp/client4/BackendStubPlayJsonTests.scala @@ -0,0 +1,39 @@ +package sttp.client4 + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import play.api.libs.json.{JsObject, JsValue, Json} +import play.api.libs.json.{JsString, OFormat} +import playJson._ + +case class Person(name: String) + +object Person { + implicit val personFormat: OFormat[Person] = Json.format[Person] +} + +class BackendStubSprayJsonTests extends AnyFlatSpec with Matchers with ScalaFutures { + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } + + it should "serialize from JsObject using implicit upickleBodySerializer" in { + val fields: Seq[(String, JsValue)] = + Seq[(String, JsValue)](("location", JsString("hometown")), ("bio", JsString("Scala programmer"))) + val json: JsObject = JsObject(fields) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(json) + val r = basicRequest.get(Uri("http://example.org")).body(json).send(backend) + + r.is200 should be(true) + r.body should be(json) + } +} diff --git a/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala b/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala new file mode 100644 index 0000000000..300d5a6b2d --- /dev/null +++ b/json/spray-json/src/test/scala/sttp/client4/BackendStubSprayJsonTests.scala @@ -0,0 +1,41 @@ +package sttp.client4 + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import spray.json.{enrichAny, JsObject} +import spray.json.DefaultJsonProtocol.{jsonFormat1, RootJsObjectFormat, StringJsonFormat} +import spray.json.RootJsonFormat +import sprayJson._ + +case class Person(name: String) + +object Person { + implicit val personRootJsonFormat: RootJsonFormat[Person] = jsonFormat1(Person.apply) +} + +class BackendStubSprayJsonTests extends AnyFlatSpec with Matchers with ScalaFutures { + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } + + it should "serialize from JsObject using implicit upickleBodySerializer" in { + val json: JsObject = JsObject( + "location" -> "hometown".toJson, + "bio" -> "Scala programmer".toJson + ) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(json) + val r = basicRequest.get(Uri("http://example.org")).body(json).send(backend) + + r.is200 should be(true) + r.body should be(json) + } +} diff --git a/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala b/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala new file mode 100644 index 0000000000..15b7ff9b37 --- /dev/null +++ b/json/zio-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala @@ -0,0 +1,40 @@ +package sttp.client4.ziojson + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.basicRequest +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import zio.json.ast.Json +import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} +import zio.Chunk + +case class Person(name: String) + +object Person { + implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person] + implicit val codec: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person] +} + +class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } + + it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { + val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) + val jObject: Json.Obj = Json.Obj(fields) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) + val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) + + r.is200 should be(true) + r.body should be(jObject) + } +} diff --git a/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala b/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala new file mode 100644 index 0000000000..2ec8371e5c --- /dev/null +++ b/json/zio1-json/src/test/scala/sttp/client4/ziojson/BackendStubZioJsonTests.scala @@ -0,0 +1,40 @@ +package sttp.client4.ziojson + +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import sttp.client4.testing.SyncBackendStub +import sttp.model.Uri +import sttp.client4.basicRequest +import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder} +import zio.json.ast.Json +import zio.Chunk + +case class Person(name: String) + +object Person { + implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person] + implicit val codec: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person] +} + +class BackendStubJson4sTests extends AnyFlatSpec with Matchers with ScalaFutures { + + it should "deserialize to json using a string stub" in { + val backend = SyncBackendStub.whenAnyRequest.thenRespond("""{"name": "John"}""") + val r = basicRequest.get(Uri("http://example.org")).response(asJson[Person]).send(backend) + + r.is200 should be(true) + r.body should be(Right(Person("John"))) + } + + it should "serialize from Json.Obj using implicit zioJsonBodySerializer" in { + val fields: Chunk[(String, Json)] = Chunk(("location", Json.Str("hometown")), ("bio", Json.Str("Scala programmer"))) + val jObject: Json.Obj = Json.Obj(fields) + + val backend = SyncBackendStub.whenAnyRequest.thenRespond(jObject) + val r = basicRequest.get(Uri("http://example.org")).body(jObject).send(backend) + + r.is200 should be(true) + r.body should be(jObject) + } +}