Skip to content

Commit

Permalink
feat(predict): changeMerchantId should reset contact
Browse files Browse the repository at this point in the history
SUITEDEV-35599

Co-authored-by: kovacszsoltizsolt <[email protected]>
Co-authored-by: LordAndras <[email protected]>
  • Loading branch information
3 people committed May 2, 2024
1 parent ab0268d commit 8475b5b
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 33 deletions.
4 changes: 2 additions & 2 deletions emarsys-sdk/src/androidTest/java/com/emarsys/EmarsysTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ class EmarsysTest : AnnotationSpec() {

runBlockingOnCoreSdkThread {
verify(mockMobileEngageApi).clearContact(completionListener)
verify(mockPredictRestricted).clearContact()
verify(mockPredictRestricted).clearVisitorId()
}
}

Expand All @@ -832,7 +832,7 @@ class EmarsysTest : AnnotationSpec() {
verifyNoInteractions(mockPredictRestricted)
}
verify(mockLoggingMobileEngageApi).clearContact(completionListener)
verify(mockLoggingPredictRestricted).clearContact()
verify(mockLoggingPredictRestricted).clearVisitorId()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ConfigTest : AnnotationSpec() {
fun testChangeMerchantId_delegatesTo_internal() {
config.changeMerchantId("testMerchantId")

verify(mockConfigInternal).changeMerchantId("testMerchantId")
verify(mockConfigInternal).changeMerchantId("testMerchantId", null)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class PredictRestrictedTest : AnnotationSpec() {

@Test
fun testPredict_clearContact_delegatesTo_Predict_Internal() {
predictRestricted.clearContact()
Mockito.verify(mockPredictInternal).clearContact()
predictRestricted.clearVisitorId()
Mockito.verify(mockPredictInternal).clearVisitorId()
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ object Emarsys {
.clearContact(completionListener)
EmarsysDependencyInjection.predictRestrictedApi()
.proxyApi(mobileEngage().concurrentHandlerHolder)
.clearContact()
.clearVisitorId()
}

private fun clearPredictOnlyContact(completionListener: CompletionListener?) {
Expand Down
2 changes: 1 addition & 1 deletion emarsys-sdk/src/main/java/com/emarsys/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ class Config : ConfigApi {
}

override fun changeMerchantId(merchantId: String?) {
emarsys().configInternal.changeMerchantId(merchantId)
emarsys().configInternal.changeMerchantId(merchantId, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ import com.emarsys.mobileengage.push.NotificationInformationListenerProvider
import com.emarsys.mobileengage.push.PushInternal
import com.emarsys.mobileengage.push.PushTokenProvider
import com.emarsys.mobileengage.push.SilentNotificationInformationListenerProvider
import com.emarsys.request.CoreCompletionHandlerRefreshTokenProxyProvider
import com.emarsys.mobileengage.request.MobileEngageRequestModelFactory
import com.emarsys.mobileengage.request.mapper.ContactTokenHeaderMapper
import com.emarsys.mobileengage.request.mapper.DefaultRequestHeaderMapper
Expand Down Expand Up @@ -180,14 +179,15 @@ import com.emarsys.predict.PredictRestrictedApi
import com.emarsys.predict.endpoint.Endpoint.PREDICT_BASE_URL
import com.emarsys.predict.provider.PredictRequestModelBuilderProvider
import com.emarsys.predict.request.PredictHeaderFactory
import com.emarsys.predict.request.PredictMultiIdRequestModelFactory
import com.emarsys.predict.request.PredictRequestContext
import com.emarsys.predict.response.VisitorIdResponseHandler
import com.emarsys.predict.response.XPResponseHandler
import com.emarsys.predict.shard.PredictShardListMerger
import com.emarsys.predict.storage.PredictStorageKey
import com.emarsys.push.Push
import com.emarsys.push.PushApi
import com.emarsys.predict.request.PredictMultiIdRequestModelFactory
import com.emarsys.request.CoreCompletionHandlerRefreshTokenProxyProvider
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailabilityLight
import com.google.android.gms.location.FusedLocationProviderClient
Expand Down Expand Up @@ -935,7 +935,8 @@ open class DefaultEmarsysComponent(config: EmarsysConfig) : EmarsysComponent {
logLevelStorage,
crypto,
clientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
predictInternal
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PredictRestricted(private val loggingInstance: Boolean = false) : PredictR
.clearPredictOnlyContact(completionListener)
}

override fun clearContact() {
override fun clearVisitorId() {
(if (loggingInstance) predict().loggingPredictInternal else predict().predictInternal)
.clearContact()
.clearVisitorId()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface PredictRestrictedApi {

fun clearPredictOnlyContact(completionListener: CompletionListener?)

fun clearContact()
fun clearVisitorId()
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)
)
}
Expand Down Expand Up @@ -275,7 +276,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)
val latch = CountDownLatch(1)
val completionListener = CompletionListener {
Expand Down Expand Up @@ -327,7 +329,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

configInternal.changeApplicationCode(OTHER_APPLICATION_CODE) {
Expand Down Expand Up @@ -379,7 +382,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

configInternal.changeApplicationCode(OTHER_APPLICATION_CODE) {
Expand Down Expand Up @@ -577,24 +581,47 @@ class DefaultConfigInternalTest : AnnotationSpec() {

@Test
fun testChangeMerchantId_shouldEnableFeature() {
configInternal.changeMerchantId(MERCHANT_ID)
configInternal.changeMerchantId(MERCHANT_ID, null)

FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) shouldBe true
}

@Test
fun testChangeMerchantId_whenMobileEngageDisabled_shouldClearContact() {
configInternal.changeMerchantId(MERCHANT_ID, null)

FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) shouldBe true
verify(mockPredictInternal).clearPredictOnlyContact(null)
verifyNoInteractions(mockMobileEngageInternal)
}

@Test
fun testChangeMerchantId_whenMobileEngageEnabled_shouldClearContact() {
val latch = CountDownLatch(1)

FeatureRegistry.enableFeature(InnerFeature.MOBILE_ENGAGE)
configInternal.changeMerchantId(MERCHANT_ID) { latch.countDown() }

latch.await()

FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) shouldBe true
verify(mockMobileEngageInternal).clearContact(null)
verify(mockPredictInternal).clearVisitorId()
}

@Test
fun testChangeMerchantId_shouldDisableFeature() {
FeatureRegistry.enableFeature(InnerFeature.PREDICT)

configInternal.changeMerchantId(null)
configInternal.changeMerchantId(null, null)

verify(mockPredictRequestContext).merchantId = null
FeatureRegistry.isFeatureEnabled(InnerFeature.PREDICT) shouldBe false
}

@Test
fun testChangeMerchantId_shouldSaveMerchantId() {
configInternal.changeMerchantId(MERCHANT_ID)
configInternal.changeMerchantId(MERCHANT_ID, null)

verify(mockPredictRequestContext).merchantId = MERCHANT_ID
}
Expand Down Expand Up @@ -700,7 +727,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

configInternal.fetchRemoteConfig(resultListener)
Expand Down Expand Up @@ -735,7 +763,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

val resultListener =
Expand Down Expand Up @@ -768,7 +797,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

val resultListener =
Expand Down Expand Up @@ -809,7 +839,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

configInternal.fetchRemoteConfigSignature(resultListener)
Expand Down Expand Up @@ -844,7 +875,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

val resultListener = FakeResultListener<String>(latch, FakeResultListener.Mode.MAIN_THREAD)
Expand Down Expand Up @@ -876,7 +908,8 @@ class DefaultConfigInternalTest : AnnotationSpec() {
mockLogLevelStorage,
mockCrypto,
mockClientServiceInternal,
concurrentHandlerHolder
concurrentHandlerHolder,
mockPredictInternal
)

val resultListener = FakeResultListener<String>(latch, FakeResultListener.Mode.MAIN_THREAD)
Expand Down
2 changes: 1 addition & 1 deletion emarsys/src/main/java/com/emarsys/config/ConfigInternal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface ConfigInternal {

fun changeApplicationCode(applicationCode: String?, completionListener: CompletionListener?)

fun changeMerchantId(merchantId: String?)
fun changeMerchantId(merchantId: String?, completionListener: CompletionListener?)

fun refreshRemoteConfig(completionListener: CompletionListener?)

Expand Down
16 changes: 14 additions & 2 deletions emarsys/src/main/java/com/emarsys/config/DefaultConfigInternal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.emarsys.mobileengage.MobileEngageInternal
import com.emarsys.mobileengage.MobileEngageRequestContext
import com.emarsys.mobileengage.client.ClientServiceInternal
import com.emarsys.mobileengage.push.PushInternal
import com.emarsys.predict.PredictInternal
import com.emarsys.predict.request.PredictRequestContext
import java.util.concurrent.CountDownLatch

Expand All @@ -42,7 +43,8 @@ class DefaultConfigInternal(
private val logLevelStorage: Storage<String?>,
private val crypto: Crypto,
private val clientServiceInternal: ClientServiceInternal,
private val concurrentHandlerHolder: ConcurrentHandlerHolder
private val concurrentHandlerHolder: ConcurrentHandlerHolder,
private val predictInternal: PredictInternal
) : ConfigInternal {

override val applicationCode: String?
Expand Down Expand Up @@ -154,13 +156,23 @@ class DefaultConfigInternal(
}
}

override fun changeMerchantId(merchantId: String?) {
override fun changeMerchantId(merchantId: String?, completionListener: CompletionListener?) {
predictRequestContext.merchantId = merchantId
if (FeatureRegistry.isFeatureEnabled(InnerFeature.MOBILE_ENGAGE)) {
mobileEngageInternal.clearContact(null)
predictInternal.clearVisitorId()
} else {
predictInternal.clearPredictOnlyContact(null)
}
if (merchantId == null) {
FeatureRegistry.disableFeature(InnerFeature.PREDICT)
} else {
FeatureRegistry.enableFeature(InnerFeature.PREDICT)
}

concurrentHandlerHolder.postOnMain {
completionListener?.onCompleted(null)
}
}

override fun refreshRemoteConfig(completionListener: CompletionListener?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class DefaultPredictInternalTest : AnnotationSpec() {

@Test
fun testClearContact_shouldRemove_visitorIdFromKeyValueStore() {
predictInternal.clearContact()
predictInternal.clearVisitorId()

verify(mockKeyValueStore).remove("predict_visitor_id")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DefaultPredictInternal(
}
}

override fun clearContact() {
override fun clearVisitorId() {
keyValueStore.remove(VISITOR_ID_KEY)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LoggingPredictInternal(private val klass: Class<*>) : PredictInternal {
debug(MethodNotAllowed(klass, callerMethodName, parameters))
}

override fun clearContact() {
override fun clearVisitorId() {
val callerMethodName = SystemUtils.getCallerMethodName()
debug(MethodNotAllowed(klass, callerMethodName, null))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.emarsys.predict.api.model.RecommendationFilter
interface PredictInternal {
fun setContact(contactFieldId: Int, contactFieldValue: String, completionListener: CompletionListener?)
fun clearPredictOnlyContact(completionListener: CompletionListener?)
fun clearContact()
fun clearVisitorId()
fun trackCart(items: List<CartItem>): String
fun trackPurchase(orderId: String, items: List<CartItem>): String
fun trackItemView(itemId: String): String
Expand Down

0 comments on commit 8475b5b

Please sign in to comment.