more test to core

This commit is contained in:
Milos Kozak 2021-02-18 18:12:39 +01:00
parent 6d32516aec
commit 8562a3e481
17 changed files with 339 additions and 310 deletions

View file

@ -65,7 +65,4 @@
<color name="splashBackground">#2E2E2E</color> <color name="splashBackground">#2E2E2E</color>
<color name="importListFileName">#FFFFFF</color>
<color name="importListAdditionalInfo">#BBBBBB</color>
</resources> </resources>

View file

@ -1,42 +0,0 @@
package info.nightscout.androidaps.utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class DecimalFormatterTest {
@Test
public void to0DecimalTest() {
Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33d).replace(",", "."));
Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33d, "U").replace(",", "."));
}
@Test
public void to1DecimalTest() {
Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33d).replace(",", "."));
Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33d, "U").replace(",", "."));
}
@Test
public void to2DecimalTest() {
Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333d).replace(",", "."));
Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333d, "U").replace(",", "."));
}
@Test
public void to3DecimalTest() {
Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333d).replace(",", "."));
Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333d, "U").replace(",", "."));
}
@Test
public void toPumpSupportedBolus() {
}
@Test
public void pumpSupportedBolusFormat() {
}
}

View file

@ -1,93 +0,0 @@
package info.nightscout.androidaps.utils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Created by mike on 12.03.2018.
*/
public class JsonHelperTest {
private String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}";
@Test
public void safeGetObjectTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
Object o = new Object();
assertEquals(o, JsonHelper.safeGetObject(null, "x", o));
assertEquals(o, JsonHelper.safeGetObject(object, "x", o));
assertNotEquals(o, JsonHelper.safeGetObject(object, "d", o));
}
@Test
public void safeGetJSONObjectTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
JSONObject o = new JSONObject();
assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o));
assertTrue(JsonHelper.safeGetJSONObject(object, "j", o).has("a"));
assertEquals(o, JsonHelper.safeGetJSONObject(object, "d", o));
}
@Test
public void safeGetStringTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
Object o = new Object();
assertNull(JsonHelper.safeGetString(null, "s"));
assertNull(JsonHelper.safeGetString(object, "notexisting"));
assertEquals("5", JsonHelper.safeGetString(object, "s"));
assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"));
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"));
assertEquals("default", JsonHelper.safeGetStringAllowNull(object, "notexisting", "default"));
assertNull(JsonHelper.safeGetStringAllowNull(object, "notexisting", null));
assertEquals("5", JsonHelper.safeGetStringAllowNull(object, "s", "default"));
}
@Test
public void safeGetDoubleTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
assertEquals(0.0d, JsonHelper.safeGetDouble(null, "notexisting"), 0.0d);
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
assertEquals(6d, JsonHelper.safeGetDouble(null, "notexisting", 6d), 0.0d);
assertEquals(6d, JsonHelper.safeGetDouble(object, "notexisting", 6d), 0.0d);
assertEquals(3d, JsonHelper.safeGetDouble(object, "d", 6d), 0.0d);
}
@Test
public void safeGetLntTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
}
@Test
public void safeGetLongTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
}
@Test
public void safeGetBooleanTest() throws JSONException {
JSONObject object = new JSONObject(jsonString);
assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"));
assertFalse(JsonHelper.safeGetBoolean(object, "notexisting"));
assertTrue(JsonHelper.safeGetBoolean(object, "b"));
}
}

View file

@ -1,22 +0,0 @@
package info.nightscout.androidaps.utils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringUtilsTest {
@Test
public void removeSurroundingQuotesTest() {
String compareString = "test";
assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString));
assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""));
assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString));
compareString = "te\"st";
assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString));
assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""));
assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString));
}
}

View file

@ -1,49 +0,0 @@
package info.nightscout.androidaps.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by mike on 26.03.2018.
*/
//@RunWith(PowerMockRunner.class)
public class TTest {
@Test
public void toUnits() {
Assert.assertEquals(1, T.msecs(1000).secs());
Assert.assertEquals(1, T.secs(60).mins());
Assert.assertEquals(1, T.mins(60).hours());
Assert.assertEquals(1, T.hours(24).days());
Assert.assertEquals(24, T.days(1).hours());
Assert.assertEquals(60000, T.mins(1).msecs());
}
@Test
public void now() {
Assert.assertTrue(Math.abs(T.now().msecs() - System.currentTimeMillis()) < 5000);
}
@Test
public void additions() {
long nowMsecs = System.currentTimeMillis();
T now = T.msecs(nowMsecs);
Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000);
Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000);
Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000);
Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000);
}
@Test
public void subtractions() {
long nowMsecs = System.currentTimeMillis();
T now = T.msecs(nowMsecs);
Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000);
Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000);
Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000);
Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000);
}
}

View file

@ -46,6 +46,17 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
testOptions {
unitTests {
returnDefaultValues = true
includeAndroidResources = true
all {
maxParallelForks = 10
forkEvery = 20
}
}
}
} }

View file

@ -109,6 +109,12 @@ dependencies {
api 'com.scottyab:rootbeer-lib:0.0.8' api 'com.scottyab:rootbeer-lib:0.0.8'
testImplementation "junit:junit:$junit_version" testImplementation "junit:junit:$junit_version"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}"
testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}"
testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}"
testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}"
androidTestImplementation "androidx.test.ext:junit:$androidx_junit" androidTestImplementation "androidx.test.ext:junit:$androidx_junit"
androidTestImplementation "androidx.test:rules:$androidx_rules" androidTestImplementation "androidx.test:rules:$androidx_rules"
testImplementation 'org.json:json:20201115' // Needed for JsonHelperTest
} }

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/careportal_cardview" android:id="@+id/careportal_cardview"
@ -29,7 +30,7 @@
android:layout_marginEnd="6dp" android:layout_marginEnd="6dp"
android:layout_marginBottom="1dp" android:layout_marginBottom="1dp"
card_view:srcCompat="@drawable/ic_meta_format" card_view:srcCompat="@drawable/ic_meta_format"
android:tint="@color/importListFileName" /> app:tint="@color/importListFileName" />
<TextView <TextView
android:id="@+id/filelist_name" android:id="@+id/filelist_name"
@ -90,7 +91,7 @@
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="1dp" android:layout_marginBottom="1dp"
card_view:srcCompat="@drawable/ic_meta_name" card_view:srcCompat="@drawable/ic_meta_name"
android:tint="@color/importListAdditionalInfo" /> app:tint="@color/importListAdditionalInfo" />
<TextView <TextView
android:id="@+id/meta_device_name" android:id="@+id/meta_device_name"
@ -122,7 +123,7 @@
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="1dp" android:layout_marginBottom="1dp"
card_view:srcCompat="@drawable/ic_meta_date" card_view:srcCompat="@drawable/ic_meta_date"
android:tint="@color/importListAdditionalInfo" /> app:tint="@color/importListAdditionalInfo" />
<TextView <TextView
android:id="@+id/meta_date_time" android:id="@+id/meta_date_time"

View file

@ -85,5 +85,9 @@
<color name="metadataTextWarning">#FF8C00</color> <color name="metadataTextWarning">#FF8C00</color>
<color name="metadataTextError">#FF5555</color> <color name="metadataTextError">#FF5555</color>
<!-- Import/Export -->
<color name="importListAdditionalInfo">#BBBBBB</color>
<color name="importListFileName">#FFFFFF</color>
</resources> </resources>

View file

@ -0,0 +1,40 @@
package info.nightscout.androidaps
import info.nightscout.androidaps.logging.AAPSLoggerTest
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
import org.junit.Before
import org.junit.Rule
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import java.util.*
@Suppress("SpellCheckingInspection")
open class TestBase {
val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
// Add a JUnit rule that will setup the @Mock annotated vars and log.
// Another possibility would be to add `MockitoAnnotations.initMocks(this) to the setup method.
@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@Before
fun setupLocale() {
Locale.setDefault(Locale.ENGLISH)
System.setProperty("disableFirebase", "true")
}
// Workaround for Kotlin nullability.
// https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791
// https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin
fun <T> anyObject(): T {
Mockito.any<T>()
return uninitialized()
}
@Suppress("Unchecked_Cast")
fun <T> uninitialized(): T = null as T
}

View file

@ -1,97 +1,98 @@
package info.nightscout.androidaps.utils package info.nightscout.androidaps.utils
import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.TestBase
import org.hamcrest.CoreMatchers.containsString import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.CoreMatchers.not import org.hamcrest.CoreMatchers.not
import org.junit.Assert import org.junit.Assert
import org.junit.Assume.assumeThat import org.junit.Assume.assumeThat
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.powermock.core.classloader.annotations.PowerMockIgnore import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.modules.junit4.PowerMockRunner import org.powermock.modules.junit4.PowerMockRunner
// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size // https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size
// https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26 // https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26
// Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it // Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it
// when test is run in Vanilla JVM without policy - Invalid key size exception is thrown // when test is run in Vanilla JVM without policy - Invalid key size exception is thrown
fun assumeAES256isSupported(cryptoUtil: CryptoUtil) { fun assumeAES256isSupported(cryptoUtil: CryptoUtil) {
cryptoUtil.lastException?.message?.let { exceptionMessage -> cryptoUtil.lastException?.message?.let { exceptionMessage ->
assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size"))) assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size")))
} }
} }
@PowerMockIgnore("javax.crypto.*") @Suppress("SpellCheckingInspection")
@RunWith(PowerMockRunner::class) @PowerMockIgnore("javax.crypto.*")
class CryptoUtilTest: TestBase() { @RunWith(PowerMockRunner::class)
class CryptoUtilTest: TestBase() {
var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger)
private var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger)
@Test
fun testFixedSaltCrypto() { @Test
val salt = byteArrayOf( fun testFixedSaltCrypto() {
-33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28, val salt = byteArrayOf(
111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15) -33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28,
111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15)
val password = "thisIsFixedPassword"
val payload = "FIXED-PAYLOAD" val password = "thisIsFixedPassword"
val payload = "FIXED-PAYLOAD"
val encrypted = cryptoUtil.encrypt(password, salt, payload)
assumeAES256isSupported(cryptoUtil) val encrypted = cryptoUtil.encrypt(password, salt, payload)
Assert.assertNotNull(encrypted) assumeAES256isSupported(cryptoUtil)
Assert.assertNotNull(encrypted)
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
assumeAES256isSupported(cryptoUtil) val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
Assert.assertEquals(decrypted, payload) assumeAES256isSupported(cryptoUtil)
} Assert.assertEquals(decrypted, payload)
}
@Test
fun testStandardCrypto() { @Test
val salt = cryptoUtil.mineSalt() fun testStandardCrypto() {
val salt = cryptoUtil.mineSalt()
val password = "topSikret"
val payload = "{what:payloadYouWantToProtect}" val password = "topSikret"
val payload = "{what:payloadYouWantToProtect}"
val encrypted = cryptoUtil.encrypt(password, salt, payload)
assumeAES256isSupported(cryptoUtil) val encrypted = cryptoUtil.encrypt(password, salt, payload)
Assert.assertNotNull(encrypted) assumeAES256isSupported(cryptoUtil)
Assert.assertNotNull(encrypted)
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
assumeAES256isSupported(cryptoUtil) val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
Assert.assertEquals(decrypted, payload) assumeAES256isSupported(cryptoUtil)
} Assert.assertEquals(decrypted, payload)
}
@Test
fun testHashVector() { @Test
val payload = "{what:payloadYouWantToProtect}" fun testHashVector() {
val hash = cryptoUtil.sha256(payload) val payload = "{what:payloadYouWantToProtect}"
Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13") val hash = cryptoUtil.sha256(payload)
} Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13")
}
@Test
fun testHmac() { @Test
val payload = "{what:payloadYouWantToProtect}" fun testHmac() {
val password = "topSikret" val payload = "{what:payloadYouWantToProtect}"
val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool val password = "topSikret"
val hash = cryptoUtil.hmac256(payload, password) val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool
Assert.assertEquals(hash, expectedHmac) val hash = cryptoUtil.hmac256(payload, password)
} Assert.assertEquals(hash, expectedHmac)
}
@Test
fun testPlainPasswordCheck() { @Test
Assert.assertTrue(cryptoUtil.checkPassword("same", "same")) fun testPlainPasswordCheck() {
Assert.assertFalse(cryptoUtil.checkPassword("same", "other")) Assert.assertTrue(cryptoUtil.checkPassword("same", "same"))
} Assert.assertFalse(cryptoUtil.checkPassword("same", "other"))
}
@Test
fun testHashedPasswordCheck() { @Test
Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret"))) fun testHashedPasswordCheck() {
Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret"))) Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret")))
Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret")))
Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1")) Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
} Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
}
}
}

View file

@ -2,7 +2,7 @@ package info.nightscout.androidaps.utils
import android.content.Context import android.content.Context
import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.R import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test

View file

@ -0,0 +1,30 @@
package info.nightscout.androidaps.utils
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
class DecimalFormatterTest {
@Test fun to0DecimalTest() {
Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33).replace(",", "."))
Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33, "U").replace(",", "."))
}
@Test fun to1DecimalTest() {
Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33).replace(",", "."))
Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33, "U").replace(",", "."))
}
@Test fun to2DecimalTest() {
Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333).replace(",", "."))
Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333, "U").replace(",", "."))
}
@Test fun to3DecimalTest() {
Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333).replace(",", "."))
Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333, "U").replace(",", "."))
}
}

View file

@ -0,0 +1,82 @@
package info.nightscout.androidaps.utils
import org.json.JSONObject
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
@Suppress("SpellCheckingInspection")
@RunWith(PowerMockRunner::class)
class JsonHelperTest {
private val jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}"
@Test
fun safeGetObjectTest() {
val json = JSONObject(jsonString)
val o = Any()
Assert.assertEquals(o, JsonHelper.safeGetObject(null, "x", o))
Assert.assertEquals(o, JsonHelper.safeGetObject(json, "x", o))
Assert.assertNotEquals(o, JsonHelper.safeGetObject(json, "d", o))
}
@Test
fun safeGetJSONObjectTest() {
val json = JSONObject(jsonString)
val o = JSONObject()
Assert.assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o))
Assert.assertTrue(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a"))
Assert.assertEquals(o, JsonHelper.safeGetJSONObject(json, "d", o))
}
@Test
fun safeGetStringTest() {
val json = JSONObject(jsonString)
Assert.assertNull(JsonHelper.safeGetString(null, "s"))
Assert.assertNull(JsonHelper.safeGetString(json, "notexisting"))
Assert.assertEquals("5", JsonHelper.safeGetString(json, "s"))
Assert.assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"))
Assert.assertEquals("default", JsonHelper.safeGetString(json, "notexisting", "default"))
Assert.assertEquals("5", JsonHelper.safeGetString(json, "s", "default"))
Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"))
Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(json, "notexisting", "default"))
Assert.assertNull(JsonHelper.safeGetStringAllowNull(json, "notexisting", null))
Assert.assertEquals("5", JsonHelper.safeGetStringAllowNull(json, "s", "default"))
}
@Test
fun safeGetDoubleTest() {
val json = JSONObject(jsonString)
Assert.assertEquals(0.0, JsonHelper.safeGetDouble(json, "notexisting"), 0.0)
Assert.assertEquals(0.0, JsonHelper.safeGetDouble(null, "notexisting"), 0.0)
Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d"), 0.000001)
Assert.assertEquals(6.0, JsonHelper.safeGetDouble(null, "notexisting", 6.0), 0.0)
Assert.assertEquals(6.0, JsonHelper.safeGetDouble(json, "notexisting", 6.0), 0.0)
Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d", 6.0), 0.0)
}
@Test
fun safeGetLntTest() {
val json = JSONObject(jsonString)
Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
}
@Test
fun safeGetLongTest() {
val json = JSONObject(jsonString)
Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
}
@Test
fun safeGetBooleanTest() {
val json = JSONObject(jsonString)
Assert.assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"))
Assert.assertFalse(JsonHelper.safeGetBoolean(json, "notexisting"))
Assert.assertTrue(JsonHelper.safeGetBoolean(json, "b"))
}
}

View file

@ -32,4 +32,9 @@ class RoundTest {
Assert.assertEquals(2.0, Round.ceilTo(1.49999, 1.0), 0.00000001) Assert.assertEquals(2.0, Round.ceilTo(1.49999, 1.0), 0.00000001)
Assert.assertEquals(0.0, Round.ceilTo(0.0, 1.0), 0.00000001) Assert.assertEquals(0.0, Round.ceilTo(0.0, 1.0), 0.00000001)
} }
@Test
fun isSameTest() {
Assert.assertTrue(Round.isSame(0.54, 0.54))
}
} }

View file

@ -0,0 +1,18 @@
package info.nightscout.androidaps.utils
import org.junit.Assert
import org.junit.Test
class StringUtilsTest {
@Test fun removeSurroundingQuotesTest() {
var compareString = "test"
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString))
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""))
Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString))
compareString = "te\"st"
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString))
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""))
Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString))
}
}

View file

@ -0,0 +1,40 @@
package info.nightscout.androidaps.utils
import org.junit.Assert
import org.junit.Test
import kotlin.math.abs
@Suppress("SpellCheckingInspection")
class TTest {
@Test fun toUnits() {
Assert.assertEquals(1, T.msecs(1000).secs())
Assert.assertEquals(1, T.secs(60).mins())
Assert.assertEquals(1, T.mins(60).hours())
Assert.assertEquals(1, T.hours(24).days())
Assert.assertEquals(24, T.days(1).hours())
Assert.assertEquals(60000, T.mins(1).msecs())
}
@Test fun now() {
Assert.assertTrue(abs(T.now().msecs() - System.currentTimeMillis()) < 5000)
}
@Test fun additions() {
val nowMsecs = System.currentTimeMillis()
val now = T.msecs(nowMsecs)
Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000)
Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000)
Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000)
Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000)
}
@Test fun subtractions() {
val nowMsecs = System.currentTimeMillis()
val now = T.msecs(nowMsecs)
Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000)
Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000)
Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000)
Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000)
}
}