Skip to content

Commit

Permalink
Merge pull request #19 from XYOracleNetwork/feature/sdk-settings-acco…
Browse files Browse the repository at this point in the history
…unt-storage

sdk settings for account storage
  • Loading branch information
jonesmac authored Nov 13, 2024
2 parents ae700e5 + f535aba commit f5e95ce
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package network.xyo.client

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import network.xyo.client.datastore.XyoAccountPrefsRepository
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.witness.system.info.XyoSystemInfoWitness
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals

class XyoAccountPrefsRepositoryTest {

private lateinit var appContext: Context

private val apiDomainBeta = "${TestConstants.nodeUrlBeta}/Archivist"

@Before
fun useAppContext() {
// Context of the app under test.
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext
}

@Test
fun testAccountPersistence() {
runBlocking {
val prefsRepository =
XyoAccountPrefsRepository.getInstance(appContext)
prefsRepository.clearSavedAccountKey()

val panel = XyoPanel(
appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf(
XyoSystemInfoWitness()
)
)
panel.resolveNodes()
val generatedAddress = panel.defaultAccount?.address?.hex
assertNotEquals(generatedAddress, null)

val panel2 = XyoPanel(
appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf(
XyoSystemInfoWitness()
)
)
panel2.resolveNodes()
val secondGeneratedAddress = panel2.defaultAccount?.address?.hex
assertEquals(generatedAddress, secondGeneratedAddress)
}
}

@Test
fun testClearingExistingAccount() {
runBlocking {
val instance = XyoAccountPrefsRepository.getInstance(appContext)
val originalAddress = instance.getAccount().private.hex

instance.clearSavedAccountKey()

val refreshedAddress = instance.getAccount().private.hex

assert(originalAddress !== refreshedAddress)
}
}

@Test
fun testUpdatingAccountPreferences() {
runBlocking {
val instance = XyoAccountPrefsRepository.getInstance(appContext)
val originalAddress = instance.getAccount().private.hex

open class UpdatedAccountPreferences : AccountPreferences {
override val fileName = "network-xyo-sdk-prefs-1"
override val storagePath = "__xyo-client-sdk-1__"
}

val updatedAccountPrefs = UpdatedAccountPreferences()

val refreshedInstance =
XyoAccountPrefsRepository.refresh(appContext, updatedAccountPrefs)

// Test that accountPreferences are updated
assertEquals(
refreshedInstance.accountPreferences.fileName,
updatedAccountPrefs.fileName
)
assertEquals(
refreshedInstance.accountPreferences.storagePath,
updatedAccountPrefs.storagePath
)

val refreshedAddress = refreshedInstance.getAccount().private.hex

assert(originalAddress !== refreshedAddress)
}
}
}
24 changes: 4 additions & 20 deletions sdk/src/androidTest/java/network/xyo/client/XyoPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import network.xyo.client.address.XyoAccount
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.datastore.PrefsRepository
import network.xyo.client.datastore.XyoAccountPrefsRepository
import network.xyo.client.datastore.defaults
import network.xyo.client.payload.XyoPayload
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationWitness
import network.xyo.client.witness.system.info.XyoSystemInfoPayload
Expand Down Expand Up @@ -94,7 +96,7 @@ class XyoPanelTest {
@Test
fun testSimplePanelReport() {
runBlocking {
val panel = XyoPanel(appContext, fun(_context:Context, _: String?): XyoEventPayload {
val panel = XyoPanel(appContext, fun(_:Context, _: String?): XyoEventPayload {
return XyoEventPayload("test_event")
})
val result = panel.reportAsyncQuery()
Expand Down Expand Up @@ -124,22 +126,4 @@ class XyoPanelTest {
assertInstanceOf<XyoSystemInfoPayload>(results.payloads?.first())
}
}

@Test
fun testAccountPersistence() {
runBlocking {
val prefsRepository = PrefsRepository(appContext)
prefsRepository.clearSavedAccountKey()

val panel = XyoPanel(appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf(XyoSystemInfoWitness()))
panel.resolveNodes()
val generatedAddress = panel.defaultAccount?.address?.hex
assertNotEquals(generatedAddress, null)

val panel2 = XyoPanel(appContext, arrayListOf(Pair(apiDomainBeta, null)), listOf(XyoSystemInfoWitness()))
panel2.resolveNodes()
val secondGeneratedAddress = panel2.defaultAccount?.address?.hex
assertEquals(generatedAddress, secondGeneratedAddress)
}
}
}
53 changes: 53 additions & 0 deletions sdk/src/androidTest/java/network/xyo/client/XyoSdkTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package network.xyo.client

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.settings.DefaultXyoSdkSettings
import network.xyo.client.settings.XyoSdk
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals

class XyoSdkTest {

private lateinit var appContext: Context

@Before
fun resetSingleton() {
XyoSdk.resetInstance()
}

@Before
fun useAppContext() {
// Context of the app under test.
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext
}

@Test
fun testDefaultSettings() {
runBlocking {
val instance = XyoSdk.getInstance(appContext)
assertEquals(instance.settings.accountPreferences.fileName, DefaultXyoSdkSettings().accountPreferences.fileName)
assertEquals(instance.settings.accountPreferences.storagePath, DefaultXyoSdkSettings().accountPreferences.storagePath)
}
}

@Test
fun testCustomSettings() {
runBlocking {
class UpdatedAccountPreferences : AccountPreferences {
override val fileName = "network-xyo-sdk-prefs-1"
override val storagePath = "__xyo-client-sdk-1__"
}
class UpdatedSettings: DefaultXyoSdkSettings() {
override val accountPreferences = UpdatedAccountPreferences()
}
val updatedSettings = UpdatedSettings()
val instance = XyoSdk.getInstance(appContext, updatedSettings)
assertEquals(instance.settings.accountPreferences.fileName, updatedSettings.accountPreferences.fileName)
assertEquals(instance.settings.accountPreferences.storagePath, updatedSettings.accountPreferences.storagePath)
}
}
}
4 changes: 2 additions & 2 deletions sdk/src/main/java/network/xyo/client/XyoPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import network.xyo.client.archivist.api.XyoArchivistApiConfig
import network.xyo.client.archivist.wrapper.ArchivistWrapper
import network.xyo.client.boundwitness.XyoBoundWitnessBuilder
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.datastore.PrefsRepository
import network.xyo.client.datastore.XyoAccountPrefsRepository
import network.xyo.client.node.client.NodeClient
import network.xyo.client.node.client.PostQueryResult
import network.xyo.client.payload.XyoPayload
Expand Down Expand Up @@ -77,7 +77,7 @@ class XyoPanel(val context: Context, private val archivists: List<XyoArchivistAp

suspend fun resolveNodes(resetNodes: Boolean = false) {
if (resetNodes) nodes = null
this.defaultAccount = PrefsRepository(context).getAccount()
this.defaultAccount = XyoAccountPrefsRepository.getInstance(context).getAccount()
if (nodeUrlsAndAccounts?.isNotEmpty() == true) {
nodes = mutableListOf<NodeClient>().let {
this@XyoPanel.nodeUrlsAndAccounts?.forEach { pair ->
Expand Down
72 changes: 0 additions & 72 deletions sdk/src/main/java/network/xyo/client/datastore/PrefsRepository.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package network.xyo.client.datastore

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.MultiProcessDataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import network.xyo.client.settings.DefaultXyoSdkSettings
import network.xyo.data.PrefsDataStoreProtos.PrefsDataStore
import java.io.File

val defaults = DefaultXyoSdkSettings()

fun Context.xyoAccountDataStore(name: String?, path: String?): DataStore<PrefsDataStore> {
val resolvedName = name ?: defaults.accountPreferences.fileName
val resolvedPath = path ?: defaults.accountPreferences.storagePath

val dataStoreFile = File(filesDir, "$resolvedPath/$resolvedName")

return MultiProcessDataStoreFactory.create(
serializer = PrefsDataStoreSerializer,
produceFile = { dataStoreFile },
corruptionHandler = ReplaceFileCorruptionHandler(
produceNewData = { PrefsDataStore.getDefaultInstance() }
),
scope = CoroutineScope(Dispatchers.IO)
)
}
Loading

0 comments on commit f5e95ce

Please sign in to comment.