Skip to content

Commit

Permalink
Add tests to check serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Domin committed Jul 18, 2023
1 parent dea0475 commit 97b37e1
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ 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)
r.is200 should be(true)
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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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")))
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 97b37e1

Please sign in to comment.