Skip to content

Commit 75be102

Browse files
authored
fix issue related to not setting categories for the ConsentCategoryProvider. (#15)
1 parent 2fd2d75 commit 75be102

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ build
66

77
.idea
88
lib/build
9-
local.properties
9+
local.properties
10+
.DS_Store

lib/src/main/java/com/segment/analytics/kotlin/destinations/consent/ConsentManager.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import com.segment.analytics.kotlin.destinations.consent.Constants.CONSENT_SETTI
1616
import com.segment.analytics.kotlin.destinations.consent.Constants.EVENT_SEGMENT_CONSENT_PREFERENCE
1717
import com.segment.analytics.kotlin.destinations.consent.Constants.HAS_UNMAPPED_DESTINATIONS_KEY
1818
import kotlinx.serialization.json.JsonArray
19+
import kotlinx.serialization.json.JsonElement
1920
import kotlinx.serialization.json.JsonPrimitive
2021
import kotlinx.serialization.json.buildJsonObject
22+
import kotlinx.serialization.json.jsonArray
2123
import kotlinx.serialization.json.jsonObject
2224
import sovran.kotlin.SynchronousStore
2325
import java.util.*
@@ -83,6 +85,9 @@ class ConsentManager(
8385
}
8486
}
8587

88+
// Add the list of categories to the Consent Category Provider so it knows which
89+
// categories we care about.
90+
this.consentProvider.setCategoryList(state.allCategories)
8691
}
8792

8893
private fun consentStateFrom(settings: Settings): ConsentState {
@@ -106,9 +111,9 @@ class ConsentManager(
106111
}
107112
}
108113

109-
// Set hasUnmappedDestinations
114+
// Set hasUnmappedDestinations and allCategories
110115
try {
111-
settings.toJsonElement().jsonObject.get(CONSENT_SETTINGS_KEY)?.let {
116+
settings.consentSettings.let {
112117
it.jsonObject.getBoolean(HAS_UNMAPPED_DESTINATIONS_KEY)
113118
?.let { serverHasUnmappedDestinations ->
114119
println("hasUnmappedDestinations jsonElement: $serverHasUnmappedDestinations")
@@ -117,7 +122,13 @@ class ConsentManager(
117122

118123
val allCategoriesJson = it.jsonObject.get(ALL_CATEGORIES_KEY)
119124
allCategoriesJson?.let {
120-
it.jsonObject.values.forEach { jsonElement -> allCategories.add(jsonElement.toString())}
125+
126+
127+
it.jsonArray.forEach { jsonElement ->
128+
129+
if (jsonElement is JsonElement && jsonElement is JsonPrimitive) {
130+
allCategories.add(jsonElement.content)}
131+
}
121132
}
122133
}
123134
} catch (t: Throwable) {

lib/src/test/kotlin/com/segment/analytics/kotlin/destinations/consent/ConsentManagerTests.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import com.segment.analytics.kotlin.core.platform.Plugin
1111
import com.segment.analytics.kotlin.core.utilities.LenientJson
1212
import com.segment.analytics.kotlin.core.utilities.toJsonElement
1313
import io.mockk.every
14+
import io.mockk.mockk
1415
import io.mockk.mockkStatic
1516
import io.mockk.spyk
17+
import io.mockk.verify
1618
import kotlinx.coroutines.test.TestScope
1719
import kotlinx.coroutines.test.UnconfinedTestDispatcher
1820
import kotlinx.serialization.json.JsonElement
@@ -246,4 +248,42 @@ class ConsentManagerTests {
246248
var resultingEvent = segmentBlocker.execute(event)
247249
assertNotNull(resultingEvent)
248250
}
249-
}
251+
252+
@Test
253+
fun `ConsentManager calls ConsentCategoryProvider_setCategories() when update() is called`() {
254+
val store = SynchronousStore()
255+
store.provide(ConsentState.defaultState)
256+
store.provide(
257+
ConsentState(
258+
mutableMapOf(),
259+
true,
260+
mutableListOf("foo"),
261+
true)
262+
)
263+
264+
val cp = mockk<ConsentCategoryProvider>()
265+
every { cp.getCategories() } returns mapOf()
266+
every { cp.setCategoryList(any())} returns Unit
267+
268+
val consentManager = ConsentManager(store, cp)
269+
270+
val settings = Settings(
271+
integrations = emptyJsonObject,
272+
plan = buildJsonObject { put("foo", JsonPrimitive("bar")) },
273+
middlewareSettings = buildJsonObject { put("foo", JsonPrimitive("bar")) },
274+
edgeFunction = buildJsonObject { put("foo", JsonPrimitive("bar")) },
275+
consentSettings = buildJsonObject {
276+
put(Constants.ALL_CATEGORIES_KEY, buildJsonArray {
277+
add(JsonPrimitive("foo"))
278+
})
279+
put(Constants.HAS_UNMAPPED_DESTINATIONS_KEY, JsonPrimitive(false))
280+
}
281+
)
282+
283+
consentManager.setup(analytics)
284+
consentManager.update(settings, Plugin.UpdateType.Initial)
285+
286+
// Make sure we called setCategories was called.
287+
verify { cp.setCategoryList(listOf("foo")) }
288+
}
289+
}

testapp/src/main/java/com/segment/analytics/destinations/mydestination/testapp/MainApplication.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class MainApplication: Application() {
5454
val consentPlugin = ConsentManager(store, consentCategoryProvider)
5555

5656
analytics.add(consentPlugin)
57+
consentPlugin.start()
5758
}
5859

5960

0 commit comments

Comments
 (0)