-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathConfigMigrationTest.kt
More file actions
153 lines (129 loc) · 5.33 KB
/
ConfigMigrationTest.kt
File metadata and controls
153 lines (129 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.mparticle.internal
import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.testutils.BaseCleanInstallEachTest
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
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 {
assertTrue(it.config?.isEmpty()!!)
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).kitConfigPreferences?.edit()
?.remove(ConfigManager.KIT_CONFIG_KEY)
ConfigManager.getPreferences(mContext).edit()
.putString(oldConfigSharedprefsKey, config.toString()).apply()
}
private fun assertOldConfigState(config: JSONObject) {
assertEquals(
config.toString(),
ConfigManager.getPreferences(mContext)
.getString(oldConfigSharedprefsKey, JSONArray().toString())
)
}
private fun assertNewConfigState(config: JSONObject) {
val configString = ConfigManager.getPreferences(mContext)
.getString(ConfigManager.CONFIG_JSON, JSONArray().toString())
assertNull(JSONObject(configString).optJSONArray(ConfigManager.KEY_EMBEDDED_KITS))
assertEquals(
config.optString(ConfigManager.KEY_EMBEDDED_KITS, JSONArray().toString()),
ConfigManager.getInstance(mContext).kitConfigPreferences?.getString(
ConfigManager.KIT_CONFIG_KEY,
JSONArray().toString()
)
)
}
}