Skip to content

Commit

Permalink
refactor MessageResponse adding derivedConsents
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilveirah committed Sep 10, 2024
1 parent 5c854fc commit 8f423d9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ 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,
val dateCreated: String? = null,
val expirationDate: String? = null,
val signedLspa: Boolean? = null,
var uspstring: String? = null,
val rejectedVendors: List<String>? = emptyList(),
val rejectedCategories: List<String>? = emptyList(),
val rejectedVendors: List<String> = emptyList(),
val rejectedCategories: List<String> = emptyList(),
val status: CCPAConsentStatus = CCPAConsentStatus.Unknown,
val webConsentPayload: String? = null,
@SerialName("GPPData") val gppData: JsonObject = JsonObject(emptyMap()),
) {
@Serializable(with = CCPAConsentStatus.Serializer::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Campaign>,
val campaigns: List<Campaign<@Contextual Any>>,
val localState: String,
val nonKeyedLocalState: String
) {
@Serializable
sealed class Campaign {
sealed class Campaign<ConsentClass> {
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
Expand Down Expand Up @@ -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<GDPRConsent>()

@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<USNatConsent>()

@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<String>? = emptyList(),
val rejectedCategories: List<String>? = 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<CCPAConsent>()

@Serializable
data class MessageMetaData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 8f423d9

Please sign in to comment.