Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Workspace Switching feature #512

Open
wants to merge 13 commits into
base: development
Choose a base branch
from
92 changes: 92 additions & 0 deletions android-core/src/androidTest/kotlin/com.mparticle/MParticleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.mparticle.internal.KitFrameworkWrapper
import com.mparticle.internal.MParticleJSInterface
import com.mparticle.internal.MessageManager
import com.mparticle.internal.PushRegistrationHelper.PushRegistration
import com.mparticle.internal.database.services.UploadService
import com.mparticle.networking.Matcher
import com.mparticle.networking.MockServer.JSONMatch
import com.mparticle.testutils.AndroidUtils
Expand Down Expand Up @@ -329,6 +330,97 @@ class MParticleTest : BaseCleanStartedEachTest() {
Assert.assertNull(MParticle.getInstance()!!.mMessageManager.location)
}

@Test
fun testSwitchWorkspacesCredentials() {
MParticle.setInstance(null)

val optionsBuilder = MParticleOptions.builder(mContext)
optionsBuilder.apiKey = "apiKey1"
optionsBuilder.apiSecret = "apiSecret1"
val options = optionsBuilder.build()

MParticle.start(options)
val instance1 = MParticle.getInstance()

Assert.assertEquals("apiKey1", instance1!!.mConfigManager.apiKey)
Assert.assertEquals("apiSecret1", instance1.mConfigManager.apiSecret)

val switchOptionsBuilder = MParticleOptions.builder(mContext)
switchOptionsBuilder.apiKey = "apiKey2"
switchOptionsBuilder.apiSecret = "apiSecret2"
val switchOptions = switchOptionsBuilder.build()

MParticle.switchWorkspace(switchOptions)
val instance2 = MParticle.getInstance()

Assert.assertEquals("apiKey2", instance2!!.mConfigManager.apiKey)
Assert.assertEquals("apiSecret2", instance2.mConfigManager.apiSecret)
}

@Test
fun testSwitchWorkspacesUploadSettings() {
MParticle.setInstance(null)

val optionsBuilder = MParticleOptions.builder(mContext)
optionsBuilder.apiKey = "apiKey1"
optionsBuilder.apiSecret = "apiSecret1"
val options = optionsBuilder.build()

MParticle.start(options)
val instance1 = MParticle.getInstance()

val eventsUrl1 = mServer.Endpoints().eventsUrl
Assert.assertEquals(true, eventsUrl1.path.contains(options.apiKey))

Assert.assertEquals("apiKey1", instance1!!.mConfigManager.apiKey)
Assert.assertEquals("apiSecret1", instance1.mConfigManager.apiSecret)
val event1 = MPEvent.Builder("event 1").build()
instance1.logEvent(event1)

val readyUploads1 = UploadService.getReadyUploads(instance1.mDatabaseManager.database)
Assert.assertEquals(0, readyUploads1.count())

val switchOptionsBuilder = MParticleOptions.builder(mContext)
switchOptionsBuilder.apiKey = "apiKey2"
switchOptionsBuilder.apiSecret = "apiSecret2"
val switchOptions = switchOptionsBuilder.build()

MParticle.switchWorkspace(switchOptions)
val instance2 = MParticle.getInstance()

val eventsUrl2 = mServer.Endpoints().eventsUrl
Assert.assertTrue(eventsUrl2.path.contains(switchOptions.apiKey))

val event2 = MPEvent.Builder("event 2").build()
instance2!!.logEvent(event2)

// TODO: https://go.mparticle.com/work/SQDSDKS-6840
val latch: CountDownLatch = MPLatch(1)
val received = AndroidUtils.Mutable(false)
mServer.waitForVerify(
Matcher(eventsUrl2).bodyMatch(
JSONMatch { jsonObject ->
val hasEvent2 = jsonObject.optJSONArray("msgs")
?.toList()
?.filterIsInstance<JSONObject>()
?.any { it.optString("n") == event2.eventName }
Assert.assertTrue(hasEvent2 ?: false)

return@JSONMatch hasEvent2 ?: false
}
)
) {
received.value = true
// TODO: https://go.mparticle.com/work/SQDSDKS-6842
// latch.countDown()
}

instance2.upload()

latch.await()
Assert.assertTrue(received.value)
}

@Throws(JSONException::class, InterruptedException::class)
private fun testReset(resetRunnable: Runnable) {
for (i in 0..9) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mparticle.internal.Constants
import com.mparticle.internal.MPUtility
import com.mparticle.internal.MParticleApiClientImpl.MPRampException
import com.mparticle.internal.MParticleApiClientImpl.MPThrottleException
import com.mparticle.internal.database.UploadSettings
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
import com.mparticle.testutils.TestingUtils
Expand Down Expand Up @@ -36,7 +37,7 @@ class UploadMessageTest : BaseCleanStartedEachTest() {
val matchingJSONEvents: MutableMap<Long, MutableMap<String, JSONObject>> = HashMap()
AccessUtils.setMParticleApiClient(object : EmptyMParticleApiClient() {
@Throws(IOException::class, MPThrottleException::class, MPRampException::class)
override fun sendMessageBatch(message: String): Int {
override fun sendMessageBatch(message: String, uploadSettings: UploadSettings): Int {
handler.post {
try {
val jsonObject = JSONObject(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
}
}
checked[0] = true
latch.countDown()
} catch (e: JSONException) {
e.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.mparticle.internal.database.tables.ReportingTableTest
import com.mparticle.internal.database.tables.SessionTableTest
import com.mparticle.internal.database.tables.UserAttributeTableTest
import com.mparticle.internal.messages.BaseMPMessage
import com.mparticle.networking.NetworkOptions
import com.mparticle.testutils.TestingUtils
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -90,7 +91,7 @@ class UpgradeVersionTest : BaseTableTest() {
1L
)
SessionService.insertSession(db, message, "key", "", "", 1L)
UploadService.insertAliasRequest(db, "key", JSONObject().put("key", "value"))
UploadService.insertAliasRequest(db, JSONObject().put("key", "value"), UploadSettings("apiKey", "secret", NetworkOptions.builder().build(), "", ""))
UserAttributesService.insertAttribute(db, "key", "value", 1L, false, 1L)

// test to make sure there are values in the database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.internal.AccessUtils
import com.mparticle.internal.ConfigManager
import com.mparticle.internal.database.UploadSettings
import com.mparticle.testutils.BaseCleanInstallEachTest
import org.junit.Assert
import org.junit.Assert.assertEquals
Expand All @@ -23,7 +24,7 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret"))
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name, false)
defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
}
MParticle.setInstance(null)
}
Expand Down Expand Up @@ -147,9 +148,9 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
.build()
MParticle.start(options)
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
map.forEach { key, value ->
mConfigManager?.setCredentials(key, value)
val prefix = mConfigManager?.podPrefix
map.forEach { (key, value) ->
mConfigManager.setCredentials(key, value)
val prefix = mConfigManager.getPodPrefix(key)
assertEquals(value, prefix)
assertEquals(
"${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com",
Expand Down Expand Up @@ -215,13 +216,14 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
)
.build()
MParticle.start(options)
val uploadSettings = UploadSettings(apiKey, "secret", options.networkOptions, "", "")
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, true)
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, uploadSettings)
assertEquals(defaultUrls[endpoint].toString(), generatedUrl.defaultUrl.toString())
}
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, false)
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, uploadSettings)
Assert.assertNotEquals(defaultUrls[endpoint].toString(), generatedUrl.toString())
Assert.assertFalse(generatedUrl === generatedUrl.defaultUrl)
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mparticle.networking
import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.internal.AccessUtils
import com.mparticle.internal.database.UploadSettings
import com.mparticle.testutils.BaseCleanInstallEachTest
import org.junit.After
import org.junit.Assert
Expand All @@ -22,7 +23,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret"))
setClients()
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
defaultUrls[endpoint] = mpClient.getUrl(endpoint, endpoint.name)
defaultUrls[endpoint] = mpClient.getUrl(endpoint, endpoint.name, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
}
MParticle.setInstance(null)
}
Expand Down Expand Up @@ -61,7 +62,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
var randIdentityPath = mRandomUtils.getAlphaString(10)
Assert.assertEquals(
"/v1/$randIdentityPath",
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath, null).path
)
Assert.assertEquals(
NetworkOptionsManager.MP_URL,
Expand All @@ -84,7 +85,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
"/v1/$randIdentityPath",
identityClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
randIdentityPath
randIdentityPath,
null
).path
)
}
Expand Down Expand Up @@ -139,7 +141,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
var randIdentityPath = mRandomUtils.getAlphaString(10)
Assert.assertEquals(
"/v1/$randIdentityPath",
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath, null).path
)
Assert.assertEquals(
audienceUrl,
Expand All @@ -162,7 +164,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
"/v1/$randIdentityPath",
identityClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
randIdentityPath
randIdentityPath,
null
).path
)

Expand All @@ -183,7 +186,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
defaultUrls[MParticleBaseClientImpl.Endpoint.IDENTITY]?.path,
mpClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
MParticleBaseClientImpl.Endpoint.IDENTITY.name
MParticleBaseClientImpl.Endpoint.IDENTITY.name,
null
).path
)
}
Expand Down Expand Up @@ -237,7 +241,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
var randIdentityPath = mRandomUtils.getAlphaString(10)
Assert.assertEquals(
"/v1/$randIdentityPath",
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath, null).path
)
Assert.assertEquals(
audienceUrl,
Expand All @@ -260,7 +264,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
"/v1/$randIdentityPath",
identityClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
randIdentityPath
randIdentityPath,
null
).path
)

Expand All @@ -281,7 +286,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
defaultUrls[MParticleBaseClientImpl.Endpoint.IDENTITY]?.path,
mpClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
MParticleBaseClientImpl.Endpoint.IDENTITY.name
MParticleBaseClientImpl.Endpoint.IDENTITY.name,
null
).path
)
}
Expand Down Expand Up @@ -360,7 +366,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
val randIdentityPath = mRandomUtils.getAlphaString(10)
Assert.assertEquals(
"/$randIdentityPath",
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath, null).path
)

// test the that the Path is still the default one (make sure the overrideSubdirectory is not kicking in when it shouldn't)
Expand Down Expand Up @@ -391,7 +397,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
identityPath,
mpClient.getUrl(
MParticleBaseClientImpl.Endpoint.IDENTITY,
MParticleBaseClientImpl.Endpoint.IDENTITY.name
MParticleBaseClientImpl.Endpoint.IDENTITY.name,
null
).path
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.mparticle.MParticle
import com.mparticle.identity.IdentityApiRequest
import com.mparticle.internal.AccessUtils
import com.mparticle.internal.database.UploadSettings
import com.mparticle.testutils.AndroidUtils
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
Expand Down Expand Up @@ -97,14 +98,15 @@ open class PinningTest : BaseCleanStartedEachTest() {
latch.countDown()
}
try {
AccessUtils.getApiClient().sendMessageBatch(JSONObject().toString())
AccessUtils.getApiClient().sendMessageBatch(JSONObject().toString(), UploadSettings("apiKey", "secret", NetworkOptions.builder().build(), "", ""))
} catch (_: Exception) {
}
latch.await()
Assert.assertTrue(called.value)
}

companion object {
@JvmStatic
@BeforeClass
fun beforeClass() {
MParticle.reset(InstrumentationRegistry.getInstrumentation().context)
Expand Down
Loading
Loading