-
Notifications
You must be signed in to change notification settings - Fork 391
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
Changes from 1 commit
9e57bb8
5f60077
42176ed
6d8d5d0
b1accb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "Alvin Alexander", | ||
"address": { | ||
"city": "Talkeetna", | ||
"state": "AK" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -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") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. property(“read yaml file”) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
@@ -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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
type Format = Value | ||
val Json, Yaml = Value | ||
} |
There was a problem hiding this comment.
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”
There was a problem hiding this comment.
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!