From 180e06212affd06e8fe3384a130b8181990b7a1b Mon Sep 17 00:00:00 2001 From: carmelo-iriti Date: Tue, 10 Oct 2023 16:09:47 +0200 Subject: [PATCH] check feature --- .../cmplibrary/creation/ComponentFactory.kt | 24 +++++++++++++++++++ .../cmplibrary/creation/ComponentFactory.kt | 7 ++++++ .../sourcepoint/cmplibrary/SpDiagnostic.kt | 3 +-- .../cmplibrary/consent/ConsentManager.kt | 8 +++++-- .../cmplibrary/creation/Factory.kt | 4 +++- .../creation/SpConfigDataBuilder.kt | 2 +- .../cmplibrary/model/exposed/SpConfig.kt | 1 - .../cmplibrary/creation/ComponentFactory.kt | 7 ++++++ .../cmplibrary/creation/ComponentFactory.kt | 7 ++++++ .../cmplibrary/exception}/LoggerImpl.kt | 0 .../consent/ConsentManagerImplTest.kt | 16 ++++++------- .../app/v6/MainActivityKotlinTest.kt | 21 +++++++++++----- 12 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 cmplibrary/src/debug/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt create mode 100644 cmplibrary/src/localProd/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt create mode 100644 cmplibrary/src/preprod/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt create mode 100644 cmplibrary/src/release/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt rename cmplibrary/src/release/java/{com.sourcepoint.cmplibrary.exception => com/sourcepoint/cmplibrary/exception}/LoggerImpl.kt (100%) diff --git a/cmplibrary/src/debug/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt b/cmplibrary/src/debug/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt new file mode 100644 index 000000000..402822c60 --- /dev/null +++ b/cmplibrary/src/debug/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt @@ -0,0 +1,24 @@ +package com.sourcepoint.cmplibrary.creation + +import android.content.Context +import com.sourcepoint.cmplibrary.SpDiagnosticImpl +import com.sourcepoint.cmplibrary.data.network.connection.ConnectionManager +import com.sourcepoint.cmplibrary.data.network.connection.create + +internal fun getConnectionManager(appCtx: Context): ConnectionManager { + + val mockObject: ConnectionManager? = SpDiagnosticImpl.fetch("connectionTest") + ?.let { + object : ConnectionManager { + override val isConnected: Boolean + get() { + Thread.sleep(1000) + return it + } + } + } + + return mockObject ?: ConnectionManager.create(appCtx) + + +} \ No newline at end of file diff --git a/cmplibrary/src/localProd/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt b/cmplibrary/src/localProd/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt new file mode 100644 index 000000000..271290c12 --- /dev/null +++ b/cmplibrary/src/localProd/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt @@ -0,0 +1,7 @@ +package com.sourcepoint.cmplibrary.creation + +import android.content.Context +import com.sourcepoint.cmplibrary.data.network.connection.ConnectionManager +import com.sourcepoint.cmplibrary.data.network.connection.create + +internal fun getConnectionManager(appCtx: Context): ConnectionManager = ConnectionManager.create(appCtx) \ No newline at end of file diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpDiagnostic.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpDiagnostic.kt index 2c98126df..6905f5b37 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpDiagnostic.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpDiagnostic.kt @@ -1,6 +1,5 @@ package com.sourcepoint.cmplibrary - internal interface SpDiagnostic { fun fetchOrStore(key: String, block: () -> Any): Any fun fetch(key: String): T? @@ -18,4 +17,4 @@ internal object SpDiagnosticImpl : SpDiagnostic { } } -data class Diagnostic(val key:String, val block: () -> Any) +data class Diagnostic(val key: String, val block: () -> Any) diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/consent/ConsentManager.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/consent/ConsentManager.kt index f6752c9e3..ee6a601f1 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/consent/ConsentManager.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/consent/ConsentManager.kt @@ -84,8 +84,12 @@ private class ConsentManagerImpl( override var sPConsentsError: ((Throwable) -> Unit)? = null override val storedConsent: Boolean - get() = dataStorage.getCcpaConsentResp() != null || - dataStorage.consentStatus != null + get() { + return dataStorage.ccpaConsentStatus != null || + dataStorage.gdprConsentStatus != null || + dataStorage.getCcpaConsentResp() != null || + dataStorage.consentStatus != null + } override fun enqueueConsent(consentActionImpl: ConsentActionImpl) { sendConsent(consentActionImpl) diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/Factory.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/Factory.kt index 70539c619..04408a0ed 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/Factory.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/Factory.kt @@ -60,7 +60,7 @@ fun makeConsentLib( val errorManager = errorMessageManager(campaignManager, client) val logger = spConfig.logger ?: createLogger(errorManager) val jsonConverter = JsonConverter.create() - val connManager = spConfig.connectionManager ?: ConnectionManager.create(appCtx) + val connManager = getConnectionManager(appCtx) val responseManager = ResponseManager.create(jsonConverter, logger) val networkClient = networkClient(okHttpClient, responseManager, logger) val viewManager = ViewsManager.create(WeakReference(activity), connManager, spConfig.messageTimeout) @@ -97,3 +97,5 @@ fun makeConsentLib( connectionManager = connManager, ) } + + diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/SpConfigDataBuilder.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/SpConfigDataBuilder.kt index 9c9e1b767..dfe836a27 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/SpConfigDataBuilder.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/creation/SpConfigDataBuilder.kt @@ -42,7 +42,7 @@ class SpConfigDataBuilder { } operator fun Diagnostic.unaryPlus() { - if(BuildConfig.DEBUG) SpDiagnosticImpl.fetchOrStore(key, block) + if (BuildConfig.DEBUG) SpDiagnosticImpl.fetchOrStore(key, block) } fun addAccountId(accountId: Int): SpConfigDataBuilder = apply { diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/model/exposed/SpConfig.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/model/exposed/SpConfig.kt index 620e5e1b6..a6b6c4df5 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/model/exposed/SpConfig.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/model/exposed/SpConfig.kt @@ -18,7 +18,6 @@ data class SpConfig( @JvmField val campaignsEnv: CampaignsEnv = CampaignsEnv.PUBLIC, @JvmField val logger: Logger? = null, @JvmField val spGppConfig: SpGppConfig? = null, - @JvmField val connectionManager: ConnectionManager? = null, ) data class SpCampaign( diff --git a/cmplibrary/src/preprod/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt b/cmplibrary/src/preprod/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt new file mode 100644 index 000000000..271290c12 --- /dev/null +++ b/cmplibrary/src/preprod/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt @@ -0,0 +1,7 @@ +package com.sourcepoint.cmplibrary.creation + +import android.content.Context +import com.sourcepoint.cmplibrary.data.network.connection.ConnectionManager +import com.sourcepoint.cmplibrary.data.network.connection.create + +internal fun getConnectionManager(appCtx: Context): ConnectionManager = ConnectionManager.create(appCtx) \ No newline at end of file diff --git a/cmplibrary/src/release/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt b/cmplibrary/src/release/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt new file mode 100644 index 000000000..271290c12 --- /dev/null +++ b/cmplibrary/src/release/java/com/sourcepoint/cmplibrary/creation/ComponentFactory.kt @@ -0,0 +1,7 @@ +package com.sourcepoint.cmplibrary.creation + +import android.content.Context +import com.sourcepoint.cmplibrary.data.network.connection.ConnectionManager +import com.sourcepoint.cmplibrary.data.network.connection.create + +internal fun getConnectionManager(appCtx: Context): ConnectionManager = ConnectionManager.create(appCtx) \ No newline at end of file diff --git a/cmplibrary/src/release/java/com.sourcepoint.cmplibrary.exception/LoggerImpl.kt b/cmplibrary/src/release/java/com/sourcepoint/cmplibrary/exception/LoggerImpl.kt similarity index 100% rename from cmplibrary/src/release/java/com.sourcepoint.cmplibrary.exception/LoggerImpl.kt rename to cmplibrary/src/release/java/com/sourcepoint/cmplibrary/exception/LoggerImpl.kt diff --git a/cmplibrary/src/test/java/com/sourcepoint/cmplibrary/consent/ConsentManagerImplTest.kt b/cmplibrary/src/test/java/com/sourcepoint/cmplibrary/consent/ConsentManagerImplTest.kt index b5cc924e5..2884532f5 100644 --- a/cmplibrary/src/test/java/com/sourcepoint/cmplibrary/consent/ConsentManagerImplTest.kt +++ b/cmplibrary/src/test/java/com/sourcepoint/cmplibrary/consent/ConsentManagerImplTest.kt @@ -78,29 +78,29 @@ class ConsentManagerImplTest { @Test fun `GIVEN a GDPR stored consent in SP VERIFY that savedConsentByUser RETURN true`() { - every { dataStorage.getGdprConsentResp() }.returns("gdpr") - every { dataStorage.getCcpaConsentResp() }.returns(null) + every { dataStorage.gdprConsentStatus }.returns("gdpr") + every { dataStorage.ccpaConsentStatus }.returns(null) consentManager.storedConsent.assertTrue() } @Test fun `GIVEN a GDPR and CCPA stored consent in SP VERIFY that savedConsentByUser RETURN true`() { - every { dataStorage.getGdprConsentResp() }.returns("gdpr") - every { dataStorage.getCcpaConsentResp() }.returns("ccpa") + every { dataStorage.gdprConsentStatus }.returns("gdpr") + every { dataStorage.ccpaConsentStatus }.returns("ccpa") consentManager.storedConsent.assertTrue() } @Test fun `GIVEN a CCPA stored consent in SP VERIFY that savedConsentByUser RETURN true`() { - every { dataStorage.getCcpaConsentResp() }.returns("ccpa") - every { dataStorage.getGdprConsentResp() }.returns(null) + every { dataStorage.ccpaConsentStatus }.returns("ccpa") + every { dataStorage.gdprConsentStatus }.returns(null) consentManager.storedConsent.assertTrue() } @Test fun `GIVEN a MISSING stored consent in SP VERIFY that savedConsentByUser RETURN false`() { - every { dataStorage.getGdprConsentResp() }.returns(null) - every { dataStorage.getCcpaConsentResp() }.returns(null) + every { dataStorage.gdprConsentStatus }.returns(null) + every { dataStorage.ccpaConsentStatus }.returns(null) consentManager.storedConsent.assertFalse() } } diff --git a/samples/app/src/androidTest/java/com/sourcepoint/app/v6/MainActivityKotlinTest.kt b/samples/app/src/androidTest/java/com/sourcepoint/app/v6/MainActivityKotlinTest.kt index 9656649ce..1c1d19c5c 100644 --- a/samples/app/src/androidTest/java/com/sourcepoint/app/v6/MainActivityKotlinTest.kt +++ b/samples/app/src/androidTest/java/com/sourcepoint/app/v6/MainActivityKotlinTest.kt @@ -89,7 +89,6 @@ class MainActivityKotlinTest { propertyName = "mobile.multicampaign.demo" messLanguage = MessageLanguage.ENGLISH +(CampaignType.GDPR) - +(Diagnostic("connectionTest") { true }) } private val toggoConfig = config { @@ -129,6 +128,17 @@ class MainActivityKotlinTest { +(CampaignType.CCPA) } + private val spConfWithoutInternetConnection = config { + accountId = 22 + propertyId = 16893 + propertyName = "mobile.multicampaign.demo" + messLanguage = MessageLanguage.ENGLISH + messageTimeout = 5000 + +(CampaignType.GDPR) + +(CampaignType.CCPA) + +(Diagnostic("connectionTest") { false }) + } + private val spConfNative = config { accountId = 22 propertyId = 18958 @@ -429,7 +439,7 @@ class MainActivityKotlinTest { loadKoinModules( mockModule( - spConfig = spConf, + spConfig = spConfWithoutInternetConnection, gdprPmId = "488393", ccpaPmId = "509688", spClientObserver = listOf(spClient), @@ -442,7 +452,8 @@ class MainActivityKotlinTest { wr { verify(exactly = 0) { spClient.onConsentReady(any()) } } wr { verify(exactly = 0) { spClient.onUIReady(any()) } } wr { verify(exactly = 0) { spClient.onUIFinished(any()) } } - wr { verify(exactly = 1) { spClient.onSpFinished(any()) } } + // TODO We have to change the behaviour of the graceful degradation, onSpFinished must be always called + wr { verify(exactly = 0) { spClient.onSpFinished(any()) } } } @Test @@ -454,7 +465,7 @@ class MainActivityKotlinTest { loadKoinModules( mockModule( - spConfig = spConf, + spConfig = spConfWithoutInternetConnection, gdprPmId = "488393", ccpaPmId = "509688", spClientObserver = listOf(spClient), @@ -474,10 +485,8 @@ class MainActivityKotlinTest { check { v7Consent.getInt(it) }?.let { v -> spEditor?.putInt(it, v) } } spEditor?.apply() - } - wr { sp!!.contains("sp.gdpr.key.consent.status").assertTrue() } wr { verify(exactly = 1) { spClient.onConsentReady(any()) } } wr { verify(exactly = 0) { spClient.onError(any()) } } wr { verify(exactly = 0) { spClient.onUIReady(any()) } }