Skip to content

Commit

Permalink
Add JSON serialization test for User model (DanielaSfregola#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-ursachi committed Dec 5, 2017
1 parent 0a81ed1 commit f0cebfa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.sbt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libraryDependencies ++= {
val Json4s = "3.5.0"
val Spec2 = "3.8.6"
val ScalaLogging = "3.5.0"
val RandomDataGenerator = "2.3"

Seq(
"com.typesafe" % "config" % Typesafe,
Expand All @@ -29,7 +30,8 @@ libraryDependencies ++= {
"com.typesafe.scala-logging" %% "scala-logging" % ScalaLogging,
"org.specs2" %% "specs2-core" % Spec2 % "test",
"org.specs2" %% "specs2-mock" % Spec2 % "test",
"com.typesafe.akka" %% "akka-testkit" % Akka % "test"
"com.typesafe.akka" %% "akka-testkit" % Akka % "test",
"com.danielasfregola" %% "random-data-generator" % RandomDataGenerator % "test"
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.danielasfregola.twitter4s.entities

import java.util.Date

import com.danielasfregola.randomdatagenerator.RandomDataGenerator
import com.danielasfregola.twitter4s.http.serializers.JsonSupport
import org.json4s.native.Serialization
import org.scalacheck.Gen.alphaChar
import org.scalacheck.{Arbitrary, Gen}
import org.specs2.mutable.Specification
import org.specs2.specification.core.Fragment

import scala.reflect._

class SerializationSpec extends Specification with RandomDataGenerator with JsonSupport {

// We serialize dates to second precision
implicit val arbitraryDate: Arbitrary[Date] = Arbitrary {
for {
timeInMicroseconds: Long <- Gen.chooseNum(1142899200L, 1512442349L)
} yield {
new Date(timeInMicroseconds * 1000)
}
}

implicit val arbitraryProfileImage: Arbitrary[ProfileImage] = Arbitrary {
for {
prefix: String <- Gen.nonEmptyListOf(alphaChar).map(_.mkString)
suffix: String <- Gen.oneOf("_mini", "_normal", "_bigger", "")
} yield {
ProfileImage(s"${prefix}_$suffix.jpg")
}
}

"JSON serialization" should {

def roundtripTest[T <: AnyRef : Manifest : Arbitrary]: Fragment = {

val className = classTag[T].runtimeClass.getSimpleName

s"round-trip successfully for $className" in {
val randomEntity = random[T]

val serializedJson = Serialization.write[T](randomEntity)

val deserializedEntity = Serialization.read[T](serializedJson)

deserializedEntity === randomEntity
}
}

roundtripTest[User]
}
}

0 comments on commit f0cebfa

Please sign in to comment.