Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a11ec944 authored by Nishant D's avatar Nishant D
Browse files

Testcase to evaluate json string caching and deserialization

parent 36159576
Loading
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ ext {
    android_compile_sdk_version = 33
    android_target_sdk_version = 33
    core_version = '1.10.1'
    gson_version = '2.9.0'
    kotlin_reflection = '1.8.10'
}

android {
@@ -39,10 +41,10 @@ android {
dependencies {

    implementation "androidx.core:core-ktx:$core_version"
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation "com.google.code.gson:gson:$gson_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_reflection"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.8.10"
}
 No newline at end of file
+31 −4
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import androidx.test.platform.app.InstrumentationRegistry
import app.lounge.storage.cache.PersistedConfiguration
import app.lounge.storage.cache.PersistenceKey
import app.lounge.storage.cache.configurations
import com.google.gson.Gson
import org.junit.Assert
import org.junit.Before
import org.junit.Test
@@ -58,12 +59,26 @@ class PersistentStorageTest {
            testConfiguration.evaluateValue(classifier = returnType, key = persistentKey)
        }
    }

    @Test
    fun testSetStringJsonReturnSerializedObject() {
        testConfiguration.authData = sampleTokensJson
        val result: String = testConfiguration.callMethod("authData") as String

        val expectedTokens = Tokens(
            aasToken = "ya29.a0AWY7Cknq6ueSCNVN6F7jB",
            ac2dmToken = "ABFEt1X6tnDsra6QUsjVsjIWz0T5F",
            authToken = "gXKyx_qC5EO64ECheZFonpJOtxbY")

        Assert.assertEquals(
            "Tokens should match with $expectedTokens",
            expectedTokens,
            result.toTokens()
        )
    }
}

// Utils function for `Persistence` Testcase only
private val testIntValue : Int = (1..10).random()
private const val testStringValue: String = "quick brown fox jump over the lazy dog"
private const val testBooleanValue: Boolean = true

private inline fun <reified T> T.callMethod(name: String, vararg args: Any?): Any? =
    T::class
@@ -87,3 +102,15 @@ private fun PersistedConfiguration.evaluateValue(classifier: KClassifier?, key:
            "Expected to be `$testBooleanValue`", this.callMethod(key.name) as Boolean)
    }
}

// region test sample data for shared preference verification
private val testIntValue : Int = (1..10).random()
private const val testStringValue: String = "quick brown fox jump over the lazy dog"
private const val testBooleanValue: Boolean = true

private const val sampleTokensJson = "{\"aasToken\": \"ya29.a0AWY7Cknq6ueSCNVN6F7jB\"," +
        "\"ac2dmToken\": \"ABFEt1X6tnDsra6QUsjVsjIWz0T5F\"," +
        "\"authToken\": \"gXKyx_qC5EO64ECheZFonpJOtxbY\"}"
data class Tokens(val aasToken: String, val ac2dmToken: String, val authToken: String)
fun String.toTokens() = Gson().fromJson(this, Tokens::class.java)
// endregion
 No newline at end of file