From 6afd2678f1c7900c0ff160af426047205a53ca11 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 13 Jun 2022 15:35:09 +0200 Subject: [PATCH] move test code --- .../versionChecker/AllowedVersionsTest.kt | 53 ++++++++++++++++--- .../versionChecker/AllowedVersions.kt | 34 ------------ 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt index 1b14ba64d0..37fe08b20a 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt @@ -1,16 +1,52 @@ package info.nightscout.androidaps.plugins.constraints.versionChecker -import org.joda.time.DateTime import org.joda.time.LocalDate -import org.junit.Assert -import org.junit.Assert.* +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test class AllowedVersionsTest { + fun generateSupportedVersions(): String = + JSONArray() + // Android API versions + .put(JSONObject().apply { + put("minAndroid", 1) // 1.0 + put("maxAndroid", 23) // 6.0.1 + }) + .put(JSONObject().apply { + put("minAndroid", 24) // 7.0 + put("maxAndroid", 25) // 7.1.2 + put("supported", "2.6.2") + }) + .put(JSONObject().apply { + put("minAndroid", 26) // 8.0 + put("maxAndroid", 27) // 8.1 + put("supported", "2.8.2") + }) + .put(JSONObject().apply { + put("minAndroid", 28) // 9.0 + put("maxAndroid", 99) + put("supported", "2.8.2") + }) + // Version time limitation + .put(JSONObject().apply { + put("endDate", "2021-11-07") + put("version", "2.9.0-beta1") + }) + .put(JSONObject().apply { + put("endDate", "2021-11-07") + put("version", "3.0-beta1") + }) + .toString() + @Test fun generateSupportedVersionsTest() { - val definition = AllowedVersions().generateSupportedVersions() + val definition = generateSupportedVersions() assertNull(AllowedVersions().findByApi(definition, 0)) assertFalse(AllowedVersions().findByApi(definition, 1)?.has("supported") ?: true) assertFalse(AllowedVersions().findByApi(definition, 23)?.has("supported") ?: true) @@ -24,9 +60,10 @@ class AllowedVersionsTest { @Test fun findByVersionTest() { //val definition = AllowedVersions().generateSupportedVersions() - val definition = "[{\"minAndroid\":1,\"maxAndroid\":23},{\"minAndroid\":24,\"maxAndroid\":25,\"supported\":\"2.6.2\"},{\"minAndroid\":26,\"maxAndroid\":27,\"supported\":\"2.8.2\"},{\"minAndroid\":28,\"maxAndroid\":99,\"supported\":\"2.8.2\"},{\"endDate\":\"2021-11-07\",\"version\":\"2.9.0-beta1\"},{\"endDate\":\"2021-11-02\",\"version\":\"3.0-beta1\"},{\"endDate\":\"2021-11-04\",\"version\":\"3.0-beta2\"},{\"endDate\":\"2021-11-10\",\"version\":\"3.0-beta3\"},{\"endDate\":\"2021-11-14\",\"version\":\"3.0-beta4\"}\n" + - " ,{\"endDate\":\"2021-11-16\",\"version\":\"3.0-beta5\"}\n" + - "]" + val definition = + "[{\"minAndroid\":1,\"maxAndroid\":23},{\"minAndroid\":24,\"maxAndroid\":25,\"supported\":\"2.6.2\"},{\"minAndroid\":26,\"maxAndroid\":27,\"supported\":\"2.8.2\"},{\"minAndroid\":28,\"maxAndroid\":99,\"supported\":\"2.8.2\"},{\"endDate\":\"2021-11-07\",\"version\":\"2.9.0-beta1\"},{\"endDate\":\"2021-11-02\",\"version\":\"3.0-beta1\"},{\"endDate\":\"2021-11-04\",\"version\":\"3.0-beta2\"},{\"endDate\":\"2021-11-10\",\"version\":\"3.0-beta3\"},{\"endDate\":\"2021-11-14\",\"version\":\"3.0-beta4\"}\n" + + " ,{\"endDate\":\"2021-11-16\",\"version\":\"3.0-beta5\"}\n" + + "]" assertNull(AllowedVersions().findByVersion(definition, "2.6.0")) assertTrue(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.has("endDate") ?: false) assertEquals("2021-11-07", AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate")) @@ -34,7 +71,7 @@ class AllowedVersionsTest { @Test fun endDateToMilliseconds() { - val definition = AllowedVersions().generateSupportedVersions() + val definition = generateSupportedVersions() val endDate = AllowedVersions().endDateToMilliseconds(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate") ?: "1000/01/01") val dateTime = LocalDate(endDate) assertEquals(2021, dateTime.year) diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt b/core/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt index 6d6c1e945a..9cf779cfe2 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt @@ -4,43 +4,9 @@ import org.joda.time.LocalDate import org.json.JSONArray import org.json.JSONException import org.json.JSONObject -import java.lang.Exception class AllowedVersions { - fun generateSupportedVersions(): String = - JSONArray() - // Android API versions - .put(JSONObject().apply { - put("minAndroid", 1) // 1.0 - put("maxAndroid", 23) // 6.0.1 - }) - .put(JSONObject().apply { - put("minAndroid", 24) // 7.0 - put("maxAndroid", 25) // 7.1.2 - put("supported", "2.6.2") - }) - .put(JSONObject().apply { - put("minAndroid", 26) // 8.0 - put("maxAndroid", 27) // 8.1 - put("supported", "2.8.2") - }) - .put(JSONObject().apply { - put("minAndroid", 28) // 9.0 - put("maxAndroid", 99) - put("supported", "2.8.2") - }) - // Version time limitation - .put(JSONObject().apply { - put("endDate", "2021-11-07") - put("version", "2.9.0-beta1") - }) - .put(JSONObject().apply { - put("endDate", "2021-11-07") - put("version", "3.0-beta1") - }) - .toString() - fun findByApi(definition: String?, api: Int): JSONObject? { if (definition == null) return null try {