Skip to content

Commit

Permalink
check feature
Browse files Browse the repository at this point in the history
  • Loading branch information
carmelo-iriti committed Oct 10, 2023
1 parent 0508545 commit 180e062
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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<Boolean>("connectionTest")
?.let {
object : ConnectionManager {
override val isConnected: Boolean
get() {
Thread.sleep(1000)
return it
}
}
}

return mockObject ?: ConnectionManager.create(appCtx)


}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sourcepoint.cmplibrary


internal interface SpDiagnostic {
fun fetchOrStore(key: String, block: () -> Any): Any
fun <T : Any> fetch(key: String): T?
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>(activity), connManager, spConfig.messageTimeout)
Expand Down Expand Up @@ -97,3 +97,5 @@ fun makeConsentLib(
connectionManager = connManager,
)
}


Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class MainActivityKotlinTest {
propertyName = "mobile.multicampaign.demo"
messLanguage = MessageLanguage.ENGLISH
+(CampaignType.GDPR)
+(Diagnostic("connectionTest") { true })
}

private val toggoConfig = config {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -429,7 +439,7 @@ class MainActivityKotlinTest {

loadKoinModules(
mockModule(
spConfig = spConf,
spConfig = spConfWithoutInternetConnection,
gdprPmId = "488393",
ccpaPmId = "509688",
spClientObserver = listOf(spClient),
Expand All @@ -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
Expand All @@ -454,7 +465,7 @@ class MainActivityKotlinTest {

loadKoinModules(
mockModule(
spConfig = spConf,
spConfig = spConfWithoutInternetConnection,
gdprPmId = "488393",
ccpaPmId = "509688",
spClientObserver = listOf(spClient),
Expand All @@ -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()) } }
Expand Down

0 comments on commit 180e062

Please sign in to comment.