Skip to content

Commit

Permalink
feat(remote-config): remoteConfig refresh is part of the setup flow
Browse files Browse the repository at this point in the history
SUITEDEV-35377

Co-authored-by: LasOri <[email protected]>
Co-authored-by: megamegax <[email protected]>
Co-authored-by: Andras Sarro <[email protected]>
  • Loading branch information
4 people committed Mar 13, 2024
1 parent 673ab83 commit 5efa86c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 90 deletions.
33 changes: 29 additions & 4 deletions emarsys-sdk/src/androidTest/java/com/emarsys/EmarsysTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import com.emarsys.Emarsys.trackDeepLink
import com.emarsys.clientservice.ClientServiceApi
import com.emarsys.common.feature.InnerFeature
import com.emarsys.config.ConfigApi
import com.emarsys.config.ConfigInternal
import com.emarsys.config.EmarsysConfig
import com.emarsys.config.FetchRemoteConfigAction
import com.emarsys.core.activity.ActivityLifecycleWatchdog
import com.emarsys.core.activity.CurrentActivityWatchdog
import com.emarsys.core.api.experimental.FlipperFeature
Expand Down Expand Up @@ -139,6 +139,7 @@ class EmarsysTest : AnnotationSpec() {
private lateinit var mockLoggingMobileEngageApi: MobileEngageApi
private lateinit var mockLoggingPredict: PredictApi
private lateinit var mockConfig: ConfigApi
private lateinit var mockConfigInternal: ConfigInternal
private lateinit var mockMessageInbox: MessageInboxApi
private lateinit var mockHardwareIdProvider: HardwareIdProvider
private lateinit var mockLanguageProvider: LanguageProvider
Expand Down Expand Up @@ -207,6 +208,7 @@ class EmarsysTest : AnnotationSpec() {
mockLoggingPredict = mock()
mockPredictRestricted = mock()
mockConfig = mock()
mockConfigInternal = mock()
mockMessageInbox = mock()
mockLogic = mock()
mockRecommendationFilter = mock()
Expand Down Expand Up @@ -269,6 +271,7 @@ class EmarsysTest : AnnotationSpec() {
predictRestricted = mockPredictRestricted,
loggingPredictRestricted = mockPredictRestricted,
config = mockConfig,
configInternal = mockConfigInternal,
eventService = mockEventServiceApi,
loggingEventService = mockLoggingEventServiceApi,
deepLink = mockDeepLinkApi,
Expand All @@ -284,7 +287,7 @@ class EmarsysTest : AnnotationSpec() {
}

@Test
fun testSetup_whenMobileEngageApplicationCodeAndMerchantIdAreNull_mobileEngageAndPredict_shouldBeDisabled() {
fun testSetup_whenMobileEngageApplicationCodeAndMerchantIdAreNull_mobileEngageAndPredict_shouldBeDisabled_andShouldNotFetchRemoteConfig() {
val config = createConfig()
.applicationCode(null)
.merchantId(null)
Expand All @@ -295,13 +298,24 @@ class EmarsysTest : AnnotationSpec() {

FeatureRegistry.isFeatureEnabled(InnerFeature.MOBILE_ENGAGE) shouldBe false
FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) shouldBe false
verify(mockConfigInternal, times(0)).refreshRemoteConfig(any())
}

@Test
fun testSetup_whenMobileEngageApplicationCodeIsNotNull_mobileEngageFeature_shouldBeEnabled() {
fun testSetup_shouldNotFetchRemoteConfig_when_ApplicationCode_is_invalid() {
listOf("", "null", "nil", "0", null).forEach {
verifyRemoteConfigFetchWithInvalidAppcode(it)
}
}

@Test
fun testSetup_whenMobileEngageApplicationCodeIsNotNull_mobileEngageFeature_shouldBeEnabled_andFetchRemoteConfig() {
setup(mobileEngageConfig)

runBlockingOnCoreSdkThread()

FeatureRegistry.isFeatureEnabled(InnerFeature.MOBILE_ENGAGE) shouldBe true
verify(mockConfigInternal).refreshRemoteConfig(any())
}

@Test
Expand Down Expand Up @@ -508,7 +522,6 @@ class EmarsysTest : AnnotationSpec() {
numberOfElementsIn(actions, DeepLinkAction::class.java).toLong() shouldBe 1
numberOfElementsIn(actions, DeviceInfoStartAction::class.java).toLong() shouldBe 1
numberOfElementsIn(actions, FetchGeofencesAction::class.java).toLong() shouldBe 1
numberOfElementsIn(actions, FetchRemoteConfigAction::class.java).toLong() shouldBe 1
}
}

Expand Down Expand Up @@ -908,6 +921,18 @@ class EmarsysTest : AnnotationSpec() {
.enableExperimentalFeatures(*experimentalFeatures)
}

private fun verifyRemoteConfigFetchWithInvalidAppcode(appCode: String?) {
val config = createConfig()
.applicationCode(appCode)
.merchantId(null)
.build()
setup(config)

runBlockingOnCoreSdkThread()

verify(mockConfigInternal, times(0)).refreshRemoteConfig(any())
}

private fun runBlockingOnCoreSdkThread(callback: (() -> Unit)? = null) {
val latch = CountDownLatch(1)
var exception: Exception? = null
Expand Down
20 changes: 20 additions & 0 deletions emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.emarsys

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.ProcessLifecycleOwner
import com.emarsys.common.feature.InnerFeature.EVENT_SERVICE_V4
import com.emarsys.common.feature.InnerFeature.MOBILE_ENGAGE
Expand All @@ -17,6 +18,7 @@ import com.emarsys.core.database.trigger.TriggerType
import com.emarsys.core.feature.FeatureRegistry
import com.emarsys.core.util.log.Logger
import com.emarsys.core.util.log.entry.CrashLog
import com.emarsys.core.util.log.entry.StatusLog
import com.emarsys.di.DefaultEmarsysDependencies
import com.emarsys.di.EmarsysDependencyInjection
import com.emarsys.di.emarsys
Expand Down Expand Up @@ -95,6 +97,8 @@ object Emarsys {
initializeMobileEngageContact()
}
}

refreshRemoteConfig(emarsysConfig.applicationCode)
}

private fun registerLifecycleObservers() {
Expand Down Expand Up @@ -228,4 +232,20 @@ object Emarsys {
.setContact()
}
}

private fun refreshRemoteConfig(applicationCode: String?) {
if (!listOf("", "null", "nil", "0").contains(applicationCode?.lowercase()) && applicationCode != null) {
emarsys().configInternal.proxyApi(mobileEngage().concurrentHandlerHolder)
.refreshRemoteConfig {
it?.let {
val logEntry = StatusLog(Emarsys::class.java, "refreshRemoteConfig", mapOf("applicationCode" to applicationCode, "exception" to it.message))
Logger.log(logEntry)
}
}
} else {
Log.w("EmarsysSdk", "Invalid applicationCode: $applicationCode")
val logEntry = StatusLog(Emarsys::class.java, "refreshRemoteConfig", mapOf("applicationCode" to applicationCode))
Logger.log(logEntry)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.emarsys.config.ConfigApi
import com.emarsys.config.ConfigInternal
import com.emarsys.config.DefaultConfigInternal
import com.emarsys.config.EmarsysConfig
import com.emarsys.config.FetchRemoteConfigAction
import com.emarsys.config.RemoteConfigResponseMapper
import com.emarsys.core.DefaultCoreCompletionHandler
import com.emarsys.core.Mapper
Expand Down Expand Up @@ -333,7 +332,6 @@ open class DefaultEmarsysComponent(config: EmarsysConfig) : EmarsysComponent {
DeviceInfoStartAction(clientServiceInternal, deviceInfoPayloadStorage, deviceInfo),
DeepLinkAction(deepLinkInternal),
FetchGeofencesAction(geofenceInternal),
FetchRemoteConfigAction(configInternal) { logInitialSetup(config) },
AppStartAction(eventServiceInternal, contactTokenStorage)
)
ActivityLifecycleActionRegistry(concurrentHandlerHolder, currentActivityProvider, actions)
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 5efa86c

Please sign in to comment.