diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/models/consents/CCPAConsent.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/models/consents/CCPAConsent.kt index 95f67f3..9a4e1b7 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/models/consents/CCPAConsent.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/models/consents/CCPAConsent.kt @@ -5,6 +5,8 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject + +// TODO: implement USPString logic @Serializable data class CCPAConsent( val uuid: String? = null, @@ -12,9 +14,10 @@ data class CCPAConsent( val expirationDate: String? = null, val signedLspa: Boolean? = null, var uspstring: String? = null, - val rejectedVendors: List? = emptyList(), - val rejectedCategories: List? = emptyList(), + val rejectedVendors: List = emptyList(), + val rejectedCategories: List = emptyList(), val status: CCPAConsentStatus = CCPAConsentStatus.Unknown, + val webConsentPayload: String? = null, @SerialName("GPPData") val gppData: JsonObject = JsonObject(emptyMap()), ) { @Serializable(with = CCPAConsentStatus.Serializer::class) diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/responses/MessagesResponse.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/responses/MessagesResponse.kt index 0d7104e..334a6ba 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/responses/MessagesResponse.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/network/responses/MessagesResponse.kt @@ -4,30 +4,30 @@ import com.sourcepoint.mobile_core.models.consents.ConsentStatus import com.sourcepoint.mobile_core.models.SPMessageLanguage import com.sourcepoint.mobile_core.models.consents.CCPAConsent import com.sourcepoint.mobile_core.models.consents.ConsentStrings +import com.sourcepoint.mobile_core.models.consents.GDPRConsent import com.sourcepoint.mobile_core.models.consents.SPGDPRVendorGrants import com.sourcepoint.mobile_core.models.consents.USNatConsent import com.sourcepoint.mobile_core.utils.IntEnum import com.sourcepoint.mobile_core.utils.IntEnumSerializer import com.sourcepoint.mobile_core.utils.StringEnumWithDefaultSerializer +import kotlinx.serialization.Contextual import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject @Serializable data class MessagesResponse( - val campaigns: List, + val campaigns: List>, val localState: String, val nonKeyedLocalState: String ) { @Serializable - sealed class Campaign { + sealed class Campaign { abstract val type: String + abstract val derivedConsents: ConsentClass? val url: String? = null -// abstract val userConsent: Consent TODO: decide whether to implement an abstract userConsent attr val message: Message? = null val messageMetaData: MessageMetaData? = null - val dateCreated: String? = null - val webConsentPayload: String? = null } @Serializable @@ -88,33 +88,81 @@ data class MessagesResponse( @SerialName("GDPR") data class GDPR( override val type: String = "GDPR", - val euconsent: String, - val grants: SPGDPRVendorGrants, - val childPmId: String?, - val expirationDate: String, - val consentStatus: ConsentStatus, - @SerialName("TCData") val tcData: JsonObject, - ): Campaign() + private val euconsent: String?, + private val grants: SPGDPRVendorGrants?, + private val childPmId: String?, + private val consentStatus: ConsentStatus?, + private val dateCreated: String?, + private val expirationDate: String?, + private val webConsentPayload: String?, + @SerialName("TCData") val tcData: JsonObject?, + override val derivedConsents: GDPRConsent? = if ( + euconsent != null && + grants != null && + consentStatus != null + ) GDPRConsent( + euconsent = euconsent, + grants = grants, + consentStatus = consentStatus, + webConsentPayload = webConsentPayload, + expirationDate = expirationDate, + dateCreated = dateCreated, + tcData = tcData ?: JsonObject(emptyMap()) + ) else null + ): Campaign() @Serializable @SerialName("usnat") data class USNat( override val type: String = "usnat", - val expirationDate: String, - val consentStatus: ConsentStatus, - val consentStrings: ConsentStrings, - val userConsents: USNatConsent.USNatUserConsents, - @SerialName("GPPData") val gppData: JsonObject - ): Campaign() + private val consentStatus: ConsentStatus?, + private val consentStrings: ConsentStrings?, + private val userConsents: USNatConsent.USNatUserConsents?, + private val dateCreated: String?, + private val expirationDate: String?, + private val webConsentPayload: String?, + @SerialName("GPPData") val gppData: JsonObject?, + override val derivedConsents: USNatConsent? = if ( + consentStrings != null && + userConsents != null && + consentStatus != null + ) USNatConsent( + dateCreated = dateCreated, + expirationDate = expirationDate, + consentStatus = consentStatus, + consentStrings = consentStrings, + userConsents = userConsents, + webConsentPayload = webConsentPayload, + gppData = gppData ?: JsonObject(emptyMap()) + ) else null + ): Campaign() @Serializable @SerialName("CCPA") data class CCPA( override val type: String = "CCPA", - val expirationDate: String, val status: CCPAConsent.CCPAConsentStatus, - @SerialName("GPPData") val gppData: JsonObject - ): Campaign() + val signedLspa: Boolean?, + val rejectedVendors: List? = emptyList(), + val rejectedCategories: List? = emptyList(), + val dateCreated: String?, + val expirationDate: String?, + val webConsentPayload: String?, + @SerialName("GPPData") val gppData: JsonObject?, + override val derivedConsents: CCPAConsent? = if ( + rejectedVendors != null && + rejectedCategories != null && + signedLspa != null + ) CCPAConsent( + dateCreated = dateCreated, + expirationDate = expirationDate, + signedLspa = signedLspa, + rejectedCategories = rejectedCategories, + rejectedVendors = rejectedVendors, + webConsentPayload = webConsentPayload, + gppData = gppData ?: JsonObject(emptyMap()) + ) else null + ): Campaign() @Serializable data class MessageMetaData( diff --git a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt index 554e54b..e4f4f5a 100644 --- a/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt +++ b/core/src/commonTest/kotlin/com/sourcepoint/mobile_core/network/SourcepointClientTest.kt @@ -6,6 +6,8 @@ import com.sourcepoint.mobile_core.models.SPCampaignEnv import com.sourcepoint.mobile_core.models.SPMessageLanguage import com.sourcepoint.mobile_core.models.consents.CCPAConsent import com.sourcepoint.mobile_core.models.consents.ConsentStatus +import com.sourcepoint.mobile_core.models.consents.GDPRConsent +import com.sourcepoint.mobile_core.models.consents.USNatConsent import com.sourcepoint.mobile_core.network.responses.MessagesResponse import com.sourcepoint.mobile_core.network.requests.MessagesRequest import kotlinx.coroutines.test.runTest @@ -71,30 +73,45 @@ class SourcepointClientTest { ) } - private fun assertCampaignConsents(campaign: MessagesResponse.Campaign) { + private fun assertCampaignConsents(campaign: MessagesResponse.Campaign<*>) { when(campaign) { - is MessagesResponse.GDPR -> assertCampaignConsents(campaign) - is MessagesResponse.USNat -> assertCampaignConsents(campaign) - is MessagesResponse.CCPA -> assertCampaignConsents(campaign) + is MessagesResponse.GDPR -> assertCampaignConsents(campaign.derivedConsents) + is MessagesResponse.USNat -> assertCampaignConsents(campaign.derivedConsents) + is MessagesResponse.CCPA -> assertCampaignConsents(campaign.derivedConsents) } } - private fun assertCampaignConsents(gdpr: MessagesResponse.GDPR) { - assertNotEquals("", gdpr.euconsent) - assertTrue(gdpr.tcData.isNotEmpty()) - assertTrue(gdpr.grants.isNotEmpty()) + private fun assertCampaignConsents(consents: GDPRConsent?) { + assertNotNull(consents) + assertTrue(consents.euconsent!!.isNotEmpty()) + assertTrue(consents.tcData.isNotEmpty()) + assertTrue(consents.grants.isNotEmpty()) + assertTrue(consents.dateCreated!!.isNotEmpty()) + assertTrue(consents.expirationDate!!.isNotEmpty()) + assertTrue(consents.webConsentPayload!!.isNotEmpty()) } - private fun assertCampaignConsents(usnat: MessagesResponse.USNat) { - assertTrue(usnat.gppData.isNotEmpty()) - assertTrue(usnat.consentStrings.isNotEmpty()) - assertTrue(usnat.userConsents.vendors.isEmpty()) - assertTrue(usnat.userConsents.categories.isNotEmpty()) + private fun assertCampaignConsents(consents: USNatConsent?) { + assertNotNull(consents) + assertTrue(consents.gppData.isNotEmpty()) + assertTrue(consents.consentStrings.isNotEmpty()) + assertTrue(consents.userConsents.vendors.isEmpty()) + assertTrue(consents.userConsents.categories.isNotEmpty()) + assertTrue(consents.dateCreated!!.isNotEmpty()) + assertTrue(consents.expirationDate!!.isNotEmpty()) + assertTrue(consents.webConsentPayload!!.isNotEmpty()) } - private fun assertCampaignConsents(ccpa: MessagesResponse.CCPA) { - assertTrue(ccpa.gppData.isNotEmpty()) - assertNotEquals(CCPAConsent.CCPAConsentStatus.Unknown, ccpa.status) + private fun assertCampaignConsents(consents: CCPAConsent?) { + assertNotNull(consents) + assertTrue(consents.gppData.isNotEmpty()) + assertNotNull(consents.signedLspa) + assertNotEquals(CCPAConsent.CCPAConsentStatus.Unknown, consents.status) + assertTrue(consents.rejectedCategories.isNotEmpty()) + assertTrue(consents.rejectedVendors.isNotEmpty()) + assertTrue(consents.dateCreated!!.isNotEmpty()) + assertTrue(consents.expirationDate!!.isNotEmpty()) + assertTrue(consents.webConsentPayload!!.isNotEmpty()) } @Test @@ -133,9 +150,7 @@ class SourcepointClientTest { response.campaigns.forEach { campaign -> assertNotNull(campaign.url) assertNotNull(campaign.message) - assertNotNull(campaign.dateCreated) assertNotNull(campaign.messageMetaData) - assertNotNull(campaign.webConsentPayload) assertCampaignConsents(campaign) }