Skip to content

Commit

Permalink
improved coverage for JsonUtils (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
sravanthi-konduru authored and tovbinm committed Aug 22, 2018
1 parent 35a03b1 commit aac006a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions utils/src/test/resources/Person.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Foo Bar",
"address": {
"city": "Talkeetna",
"state": "AK"
}
}
5 changes: 5 additions & 0 deletions utils/src/test/resources/Person.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: Foo Bar
address:
city: Talkeetna
state: AK
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,50 @@ class JsonUtilsTest extends PropSpec with PropertyChecks with TestCommon {
}

property("handle random entries correctly") {
forAll(dataGen)(check)
forAll(dataGen)(checkJson)
forAll(dataGen)(checkYaml)
}

property("handle special entries correctly") {
forAll(specialDataGen)(check)
forAll(specialDataGen)(checkJson)
forAll(specialDataGen)(checkYaml)

}

property("handle empty collections correctly") {
forAll(doubles) { d => check(TestDouble(d, Array.empty, Seq.empty, Map.empty, None)) }
forAll(doubles) { d => checkJson(TestDouble(d, Array.empty, Seq.empty, Map.empty, None)) }
forAll(doubles) { d => checkYaml(TestDouble(d, Array.empty, Seq.empty, Map.empty, None)) }
}

property("read json file") {
val jsonFile = resourceFile(name = "Person.json")
val person = Person("Foo Bar", Address("Talkeetna", "AK"))
JsonUtils.fromFile[Person](jsonFile) match {
case Failure(e) => fail(e)
case Success(value) => value shouldBe person
}
}

property("read yml file") {
val ymlFile = resourceFile(name = "Person.yml")
val person = Person("Foo Bar", Address("Talkeetna", "AK"))
JsonUtils.fromFile[Person](ymlFile) match {
case Failure(e) => fail(e)
case Success(value) => value shouldBe person
}
}

def checkJson(data: TestDouble): Unit = {
val jsonValue: String = JsonUtils.toJsonString(data)
JsonUtils.fromString[TestDouble](jsonValue) match {
case Failure(e) => fail(e)
case Success(r) => assert(r, data)
}
}

def check(data: TestDouble): Unit = {
val json = JsonUtils.toJsonString(data)
JsonUtils.fromString[TestDouble](json) match {
def checkYaml(data: TestDouble): Unit = {
val yamlValue: String = JsonUtils.toYamlString(data)
JsonUtils.fromString[TestDouble](yamlValue) match {
case Failure(e) => fail(e)
case Success(r) => assert(r, data)
}
Expand Down Expand Up @@ -130,3 +160,7 @@ case class TestDouble
map: Map[Int, Seq[Long]],
nested: Option[TestDouble]
)

case class Person(name: String, address: Address)

case class Address(city: String, state: String)

0 comments on commit aac006a

Please sign in to comment.