Rewrites JsonHelperTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-20 23:03:09 -07:00
parent 107f6d05dd
commit e793620b6f

View file

@ -1,7 +1,7 @@
package info.nightscout.core.utils package info.nightscout.core.utils
import com.google.common.truth.Truth.assertThat
import org.json.JSONObject import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
@ -13,67 +13,67 @@ class JsonHelperTest {
fun safeGetObjectTest() { fun safeGetObjectTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
val o = Any() val o = Any()
Assertions.assertEquals(o, JsonHelper.safeGetObject(null, "x", o)) assertThat(JsonHelper.safeGetObject(null, "x", o)).isEqualTo(o)
Assertions.assertEquals(o, JsonHelper.safeGetObject(json, "x", o)) assertThat(JsonHelper.safeGetObject(json, "x", o)).isEqualTo(o)
Assertions.assertNotEquals(o, JsonHelper.safeGetObject(json, "d", o)) assertThat(JsonHelper.safeGetObject(json, "d", o)).isNotEqualTo(o)
} }
@Test @Test
fun safeGetJSONObjectTest() { fun safeGetJSONObjectTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
val o = JSONObject() val o = JSONObject()
Assertions.assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o)) assertThat(JsonHelper.safeGetJSONObject(null, "x", o)).isEqualTo(o)
Assertions.assertTrue(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a")) assertThat(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a")).isTrue()
Assertions.assertEquals(o, JsonHelper.safeGetJSONObject(json, "d", o)) assertThat(JsonHelper.safeGetJSONObject(json, "d", o)).isEqualTo(o)
} }
@Test @Test
fun safeGetStringTest() { fun safeGetStringTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
Assertions.assertNull(JsonHelper.safeGetString(null, "s")) assertThat(JsonHelper.safeGetString(null, "s")).isNull()
Assertions.assertNull(JsonHelper.safeGetString(json, "notexisting")) assertThat(JsonHelper.safeGetString(json, "notexisting")).isNull()
Assertions.assertEquals("5", JsonHelper.safeGetString(json, "s")) assertThat(JsonHelper.safeGetString(json, "s")).isEqualTo("5")
Assertions.assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default")) assertThat(JsonHelper.safeGetString(null, "notexisting", "default")).isEqualTo("default")
Assertions.assertEquals("default", JsonHelper.safeGetString(json, "notexisting", "default")) assertThat(JsonHelper.safeGetString(json, "notexisting", "default")).isEqualTo("default")
Assertions.assertEquals("5", JsonHelper.safeGetString(json, "s", "default")) assertThat(JsonHelper.safeGetString(json, "s", "default")).isEqualTo("5")
Assertions.assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default")) assertThat(JsonHelper.safeGetStringAllowNull(null, "notexisting", "default")).isEqualTo("default")
Assertions.assertEquals("default", JsonHelper.safeGetStringAllowNull(json, "notexisting", "default")) assertThat(JsonHelper.safeGetStringAllowNull(json, "notexisting", "default")).isEqualTo("default")
Assertions.assertNull(JsonHelper.safeGetStringAllowNull(json, "notexisting", null)) assertThat(JsonHelper.safeGetStringAllowNull(json, "notexisting", null)).isNull()
Assertions.assertEquals("5", JsonHelper.safeGetStringAllowNull(json, "s", "default")) assertThat(JsonHelper.safeGetStringAllowNull(json, "s", "default")).isEqualTo("5")
} }
@Test @Test
fun safeGetDoubleTest() { fun safeGetDoubleTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
Assertions.assertEquals(0.0, JsonHelper.safeGetDouble(json, "notexisting"), 0.0) assertThat(JsonHelper.safeGetDouble(json, "notexisting")).isWithin(0.0).of(0.0)
Assertions.assertEquals(0.0, JsonHelper.safeGetDouble(null, "notexisting"), 0.0) assertThat(JsonHelper.safeGetDouble(null, "notexisting")).isWithin(0.0).of(0.0)
Assertions.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d"), 0.000001) assertThat(JsonHelper.safeGetDouble(json, "d")).isWithin(0.000001).of(3.0)
Assertions.assertEquals(6.0, JsonHelper.safeGetDouble(null, "notexisting", 6.0), 0.0) assertThat(JsonHelper.safeGetDouble(null, "notexisting", 6.0)).isWithin(0.0).of(6.0)
Assertions.assertEquals(6.0, JsonHelper.safeGetDouble(json, "notexisting", 6.0), 0.0) assertThat(JsonHelper.safeGetDouble(json, "notexisting", 6.0)).isWithin(0.0).of(6.0)
Assertions.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d", 6.0), 0.0) assertThat(JsonHelper.safeGetDouble(json, "d", 6.0)).isWithin(0.0).of(3.0)
} }
@Test @Test
fun safeGetLntTest() { fun safeGetLntTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
Assertions.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong()) assertThat(JsonHelper.safeGetInt(null, "notexisting").toLong()).isEqualTo(0)
Assertions.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong()) assertThat(JsonHelper.safeGetInt(json, "notexisting").toLong()).isEqualTo(0)
Assertions.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong()) assertThat(JsonHelper.safeGetInt(json, "i").toLong()).isEqualTo(4)
} }
@Test @Test
fun safeGetLongTest() { fun safeGetLongTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
Assertions.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong()) assertThat(JsonHelper.safeGetInt(null, "notexisting").toLong()).isEqualTo(0)
Assertions.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong()) assertThat(JsonHelper.safeGetInt(json, "notexisting").toLong()).isEqualTo(0)
Assertions.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong()) assertThat(JsonHelper.safeGetInt(json, "i").toLong()).isEqualTo(4)
} }
@Test @Test
fun safeGetBooleanTest() { fun safeGetBooleanTest() {
val json = JSONObject(jsonString) val json = JSONObject(jsonString)
Assertions.assertFalse(JsonHelper.safeGetBoolean(null, "notexisting")) assertThat(JsonHelper.safeGetBoolean(null, "notexisting")).isFalse()
Assertions.assertFalse(JsonHelper.safeGetBoolean(json, "notexisting")) assertThat(JsonHelper.safeGetBoolean(json, "notexisting")).isFalse()
Assertions.assertTrue(JsonHelper.safeGetBoolean(json, "b")) assertThat(JsonHelper.safeGetBoolean(json, "b")).isTrue()
} }
} }