Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved coverage for JsonUtils #73

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve code coverage for JsonUtils
  • Loading branch information
sravanthi-konduru committed Aug 22, 2018
commit 5f6007782050f18d433d31f6a7752990b0ce2dbf
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": "Alvin Alexander",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a random name? Lets just put name “Foo Bar”

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is random. Updated to 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: Alvin Alexander
address:
city: Talkeetna
state: AK
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package com.salesforce.op.utils.json

import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.salesforce.op.test.TestCommon
import com.salesforce.op.utils.json.Format.Format
import org.junit.runner.RunWith
import org.scalacheck.Gen
import org.scalatest.junit.JUnitRunner
Expand Down Expand Up @@ -79,20 +80,46 @@ class JsonUtilsTest extends PropSpec with PropertyChecks with TestCommon {
}

property("handle random entries correctly") {
forAll(dataGen)(check)
forAll(dataGen) { d => (check(d, Format.Json)) }
forAll(dataGen) { d => (check(d, Format.Yaml)) }
}

property("handle special entries correctly") {
forAll(specialDataGen)(check)
forAll(specialDataGen) { d => (check(d, Format.Json)) }
forAll(specialDataGen) { d => (check(d, Format.Yaml)) }

}

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

property(testName = "handle json file reading properly") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply do: property(“read json file”)

val jsonFile = resourceFile(name = "Person.json")
val person = Person("Alvin Alexander", Address("Talkeetna", "AK"))
JsonUtils.fromFile[Person](jsonFile) match {
case Failure(e) => fail(e)
case Success(value) => assert(value, person)
}
}

def check(data: TestDouble): Unit = {
val json = JsonUtils.toJsonString(data)
JsonUtils.fromString[TestDouble](json) match {
property(testName = "handle yml file reading properly") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

property(“read yaml file”)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

val ymlFile = resourceFile(name = "Person.yml")
val person = Person("Alvin Alexander", Address("Talkeetna", "AK"))
JsonUtils.fromFile[Person](ymlFile) match {
case Failure(e) => fail(e)
case Success(value) => assert(value, person)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's a case class you dont need a special assertion method but you can use the equality directly, i.e value shouldBe person - https://www.alessandrolacava.com/blog/scala-case-classes-in-depth/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know that.Thanks. Updated!

}
}

def check(data: TestDouble, format: Format): Unit = {
val strValue: String =
format match {
case Format.Json => JsonUtils.toJsonString(data)
case Format.Yaml => JsonUtils.toYamlString(data)
}
JsonUtils.fromString[TestDouble](strValue) match {
case Failure(e) => fail(e)
case Success(r) => assert(r, data)
}
Expand All @@ -118,6 +145,11 @@ class JsonUtilsTest extends PropSpec with PropertyChecks with TestCommon {
v.seq.size shouldBe expected.size
}

def assert(v: Person, expected: Person): Assertion = {
assert(v.name === expected.name)
assert(v.address === expected.address)
}

}

case class TestDouble
Expand All @@ -130,3 +162,12 @@ 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)

private object Format extends Enumeration {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to reate enum. Simple create two check function: checkYaml and checkJson

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

type Format = Value
val Json, Yaml = Value
}