2019-02-26 20:38:27 +01:00
|
|
|
package info.nightscout.androidaps.utils;
|
2018-03-12 11:12:41 +01:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 12.03.2018.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class JsonHelperTest {
|
|
|
|
|
|
|
|
String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\"}";
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void runTest() throws JSONException {
|
|
|
|
JSONObject object = new JSONObject(jsonString);
|
|
|
|
assertEquals(null, JsonHelper.safeGetString(object, "notexisting"));
|
|
|
|
assertEquals("5", JsonHelper.safeGetString(object, "s"));
|
2018-03-12 11:22:19 +01:00
|
|
|
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
|
|
|
|
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
|
2018-03-12 11:12:41 +01:00
|
|
|
|
|
|
|
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
|
|
|
|
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
|
|
|
|
|
|
|
|
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
|
|
|
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
|
|
|
}
|
|
|
|
}
|