forked from mParticle/mparticle-android-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split configuration into core and kits (mParticle#103)
- Loading branch information
1 parent
a5a934a
commit 766e0d3
Showing
12 changed files
with
471 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
android-core/src/androidTest/kotlin/com.mparticle/internal/ConfigMigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.mparticle.internal | ||
|
||
import com.mparticle.MParticle | ||
import com.mparticle.MParticleOptions | ||
import com.mparticle.testutils.BaseCleanInstallEachTest | ||
import org.json.JSONObject | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
|
||
class ConfigMigrationTest : BaseCleanInstallEachTest() { | ||
private val oldConfigSharedprefsKey = "json" | ||
|
||
@Test | ||
fun testConfigContentsMigratedProperly() { | ||
// store config in the old place | ||
val oldConfig = JSONObject().put("foo", "bar") | ||
setOldConfigState(oldConfig) | ||
|
||
// double check that there is nothing stored in the new place | ||
assertOldConfigState(oldConfig) | ||
|
||
// initialize ConfigManager | ||
val configManager = ConfigManager(mContext) | ||
|
||
// make sure the config is in the new place and gone from the old | ||
assertNewConfigState(oldConfig) | ||
|
||
// make sure config is also available via ConfigManager api | ||
assertEquals(oldConfig.toString(), configManager.config) | ||
} | ||
|
||
@Test | ||
fun testConfigMetadataRemainsDuringMigration() { | ||
val oldConfig = JSONObject().put("foo", "bar") | ||
val testTimestamp = System.currentTimeMillis() - 1000L | ||
val testEtag = "some etag" | ||
val testIfModified = "foo bar" | ||
|
||
// set metadata + config in the old place | ||
ConfigManager(mContext).apply { | ||
saveConfigJson(JSONObject(), null, testEtag, testIfModified, testTimestamp) | ||
setOldConfigState(oldConfig) | ||
} | ||
|
||
assertOldConfigState(oldConfig) | ||
|
||
// trigger migration, check that config metadata remained the same | ||
ConfigManager(mContext).apply { | ||
assertNewConfigState(oldConfig) | ||
assertEquals(oldConfig.toString(), config) | ||
assertEquals(testTimestamp, configTimestamp) | ||
assertEquals(testEtag, etag) | ||
assertEquals(testIfModified, ifModified) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMigratingStaleConfig() { | ||
val oldConfig = JSONObject().put("foo", "bar") | ||
val testTimestamp = System.currentTimeMillis() - 1000L | ||
val testEtag = "some etag" | ||
val testIfModified = "foo bar" | ||
|
||
// set metadata + config in the old place | ||
ConfigManager(mContext).apply { | ||
saveConfigJson(JSONObject(), null, testEtag, testIfModified, testTimestamp) | ||
setOldConfigState(oldConfig) | ||
} | ||
|
||
assertOldConfigState(oldConfig) | ||
|
||
MParticleOptions.builder(mContext) | ||
.credentials("key", "secret") | ||
.configMaxAgeSeconds(0) | ||
.build() | ||
.let { | ||
// start it the simple way, we don't want to block or anything so we can test config state | ||
MParticle.start(it) | ||
} | ||
|
||
// make sure config is deleted | ||
MParticle.getInstance()!!.Internal().configManager.let { | ||
assertNull(it.config) | ||
assertNull(it.configTimestamp) | ||
assertNull(it.etag) | ||
assertNull(it.ifModified) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMigratingNotStaleConfig() { | ||
val oldConfig = JSONObject().put("foo", "bar") | ||
val testTimestamp = System.currentTimeMillis() - 1000L | ||
val testEtag = "some etag" | ||
val testIfModified = "foo bar" | ||
|
||
// set metadata + config in the old place | ||
ConfigManager(mContext).apply { | ||
saveConfigJson(JSONObject(), null, testEtag, testIfModified, testTimestamp) | ||
setOldConfigState(oldConfig) | ||
} | ||
|
||
assertOldConfigState(oldConfig) | ||
|
||
MParticleOptions.builder(mContext) | ||
.credentials("key", "secret") | ||
.configMaxAgeSeconds(Integer.MAX_VALUE) | ||
.build() | ||
.let { | ||
// start it the simple way, we don't want to block or anything so we can test config state | ||
MParticle.start(it) | ||
} | ||
|
||
// make sure config is deleted | ||
MParticle.getInstance()!!.Internal().configManager.let { | ||
assertEquals(oldConfig.toString(), it.config) | ||
assertEquals(testTimestamp, it.configTimestamp) | ||
assertEquals(testEtag, it.etag) | ||
assertEquals(testIfModified, it.ifModified) | ||
} | ||
} | ||
|
||
private fun setOldConfigState(config: JSONObject) { | ||
ConfigManager.getInstance(mContext).getKitConfigPreferences().edit().remove(ConfigManager.KIT_CONFIG_KEY) | ||
ConfigManager.getPreferences(mContext).edit().putString(oldConfigSharedprefsKey, config.toString()).apply() | ||
} | ||
|
||
private fun assertOldConfigState(config: JSONObject) { | ||
assertNull(ConfigManager.getInstance(mContext).getKitConfigPreferences().getString(ConfigManager.KIT_CONFIG_KEY, null)) | ||
assertEquals(config.toString(), ConfigManager.getPreferences(mContext).getString(oldConfigSharedprefsKey, null)) | ||
} | ||
|
||
private fun assertNewConfigState(config: JSONObject) { | ||
val configString = ConfigManager.getPreferences(mContext).getString(ConfigManager.CONFIG_JSON, null) | ||
assertNull(JSONObject(configString).optJSONArray(ConfigManager.KEY_EMBEDDED_KITS)) | ||
assertEquals(config.optString(ConfigManager.KEY_EMBEDDED_KITS, null), ConfigManager.getInstance(mContext).kitConfigPreferences.getString(ConfigManager.KIT_CONFIG_KEY, null)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.