Skip to content

Commit

Permalink
VersionCheckerUtilsImpl improvement & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Jan 3, 2025
1 parent 3510a4e commit 4344a18
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TriggerTimeRangeTest : TriggerTestBase() {
}

@Test fun friendlyDescriptionTest() {
assertThat(TriggerTimeRange(injector).friendlyDescription()).isEqualTo("Time is between 12:34PM and 12:34PM")
assertThat(TriggerTimeRange(injector).friendlyDescription()).isEqualTo("Time is between 12:34 PM and 12:34 PM")
}

@Test fun iconTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.LocalTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class AllowedVersions {
object AllowedVersions {

fun findByApi(definition: String?, api: Int): JSONObject? {
if (definition == null) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class VersionCheckerUtilsImpl @Inject constructor(
Thread {
try {
val definition: String = URL("https://raw.githubusercontent.com/nightscout/AndroidAPS/versions/definition.json").readText()
val version: String? = AllowedVersions().findByApi(definition, Build.VERSION.SDK_INT)?.optString("supported")
val newVersionAvailable = compareWithCurrentVersion(version, config.get().VERSION_NAME)
val version: String? = AllowedVersions.findByApi(definition, Build.VERSION.SDK_INT)?.optString("supported")
val newVersionByApi = compareWithCurrentVersion(version, config.get().VERSION_NAME)

// App expiration
if (newVersionAvailable) {
if (newVersionByApi || config.get().isDev()) {
var endDate = sp.getLong(rh.gs(app.aaps.core.utils.R.string.key_app_expiration) + "_" + config.get().VERSION_NAME, 0)
AllowedVersions().findByVersion(definition, config.get().VERSION_NAME)?.let { expirationJson ->
AllowedVersions().endDateToMilliseconds(expirationJson.getString("endDate"))?.let { ed ->
AllowedVersions.findByVersion(definition, config.get().VERSION_NAME)?.let { expirationJson ->
AllowedVersions.endDateToMilliseconds(expirationJson.getString("endDate"))?.let { ed ->
endDate = ed + T.days(1).msecs()
sp.putLong(rh.gs(app.aaps.core.utils.R.string.key_app_expiration) + "_" + config.get().VERSION_NAME, endDate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,40 @@ class AllowedVersionsTest {
@Test
fun generateSupportedVersionsTest() {
val definition = generateSupportedVersions()
assertThat(AllowedVersions().findByApi(definition, 0)).isNull()
assertThat(AllowedVersions().findByApi(definition, 1)!!.has("supported")).isFalse()
assertThat(AllowedVersions().findByApi(definition, 23)!!.has("supported")).isFalse()
assertThat(AllowedVersions().findByApi(definition, 24)!!.getString("supported")).isEqualTo("2.6.2")
assertThat(AllowedVersions().findByApi(definition, 25)!!.getString("supported")).isEqualTo("2.6.2")
assertThat(AllowedVersions().findByApi(definition, 26)!!.getString("supported")).isEqualTo("2.8.2")
assertThat(AllowedVersions().findByApi(definition, 27)!!.getString("supported")).isEqualTo("2.8.2")
assertThat(AllowedVersions().findByApi(definition, 28)!!.getString("supported")).isEqualTo("3.2.0.4")
assertThat(AllowedVersions().findByApi(definition, 29)!!.getString("supported")).isEqualTo("3.2.0.4")
assertThat(AllowedVersions().findByApi(definition, 30)!!.getString("supported")).isEqualTo("3.3.0")
assertThat(AllowedVersions.findByApi(definition, 0)).isNull()
assertThat(AllowedVersions.findByApi(definition, 1)!!.has("supported")).isFalse()
assertThat(AllowedVersions.findByApi(definition, 23)!!.has("supported")).isFalse()
assertThat(AllowedVersions.findByApi(definition, 24)!!.getString("supported")).isEqualTo("2.6.2")
assertThat(AllowedVersions.findByApi(definition, 25)!!.getString("supported")).isEqualTo("2.6.2")
assertThat(AllowedVersions.findByApi(definition, 26)!!.getString("supported")).isEqualTo("2.8.2")
assertThat(AllowedVersions.findByApi(definition, 27)!!.getString("supported")).isEqualTo("2.8.2")
assertThat(AllowedVersions.findByApi(definition, 28)!!.getString("supported")).isEqualTo("3.2.0.4")
assertThat(AllowedVersions.findByApi(definition, 29)!!.getString("supported")).isEqualTo("3.2.0.4")
assertThat(AllowedVersions.findByApi(definition, 30)!!.getString("supported")).isEqualTo("3.3.0")
}

@Test
fun findByVersionTest() {
//val definition = AllowedVersions().generateSupportedVersions()
//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\":29,\"supported\":\"3.2.0.4\"},{\"minAndroid\":30,\"maxAndroid\":99,\"supported\":\"3.3.0\"},{\"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" +
"]"
assertThat(AllowedVersions().findByVersion(definition, "2.6.0")).isNull()
assertThat(AllowedVersions().findByVersion(definition, "2.9.0-beta1")!!.has("endDate")).isTrue()
assertThat(AllowedVersions().findByVersion(definition, "2.9.0-beta1")!!.getString("endDate")).isEqualTo("2021-11-07")
assertThat(AllowedVersions.findByVersion(definition, "2.6.0")).isNull()
assertThat(AllowedVersions.findByVersion(definition, "2.9.0-beta1")!!.has("endDate")).isTrue()
assertThat(AllowedVersions.findByVersion(definition, "2.9.0-beta1")!!.getString("endDate")).isEqualTo("2021-11-07")
}

@Suppress("SpellCheckingInspection")
@Test
fun endDateToMilliseconds() {
val definition = generateSupportedVersions()
val endDate = AllowedVersions().endDateToMilliseconds(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate") ?: "1000/01/01") ?: 0L
val endDate = AllowedVersions.endDateToMilliseconds(AllowedVersions.findByVersion(definition, "2.9.0-beta1")?.getString("endDate") ?: "1000/01/01") ?: 0L
val dateTime = LocalDate.ofInstant(Instant.ofEpochMilli(endDate), ZoneId.systemDefault())
assertThat(dateTime.year).isEqualTo(2021)
assertThat(dateTime.monthValue).isEqualTo(11)
assertThat(dateTime.dayOfMonth).isEqualTo(7)

assertThat(AllowedVersions().endDateToMilliseconds("abdef")).isNull()
assertThat(AllowedVersions.endDateToMilliseconds("abdef")).isNull()
}
}

0 comments on commit 4344a18

Please sign in to comment.