Skip to content

Commit

Permalink
feat(SDK-4183): changes normalisation to follow LP logic (#700)
Browse files Browse the repository at this point in the history
* feat(SDK-4183): changes normalisation to follow LP logic

- makes changes to event names and properties and also to charged event logic
- tries exact name match first
- tries match with normalised event name
- tries match after normalising both event name/property and triggers we get from BE

* tests(SDK-4183): adds test cases for nomalisation logic

- adds for both charged and normal events.
  • Loading branch information
CTLalit committed Nov 29, 2024
1 parent 496f82f commit 586dcdc
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EventAdapter(
val profileAttrName: String? = null // for profile events only
) {

private val systemPropToKey = mapOf(
internal val systemPropToKey = mapOf(
"CT App Version" to CLTAP_APP_VERSION,
"ct_app_version" to CLTAP_APP_VERSION,
"CT Latitude" to CLTAP_LATITUDE,
Expand Down Expand Up @@ -81,11 +81,20 @@ class EventAdapter(
return items
.filterNotNull()
.map { productMap: Map<String, Any> ->
val normalisedMap = productMap.map {
Utils.getNormalizedName(it.key) to it.value
}.toMap()

TriggerValue(normalisedMap[Utils.getNormalizedName(propertyName)])
var op = productMap[propertyName]

if (op == null) {
op = productMap[Utils.getNormalizedName(propertyName)]
}

if (op == null) {
val normalisedMap = productMap.map {
Utils.getNormalizedName(it.key) to it.value
}.toMap()
op = normalisedMap[Utils.getNormalizedName(propertyName)]
}
TriggerValue(op)
}.filter { it.value != null }
}

Expand All @@ -110,22 +119,35 @@ class EventAdapter(

@VisibleForTesting
internal fun getActualPropertyValue(propertyName: String): Any? {
val normalisedMap = eventProperties.map { item ->
Utils.getNormalizedName(item.key) to item.value
}.toMap()

var value = normalisedMap[Utils.getNormalizedName(propertyName)]
var value = evaluateActualPropertyValue(propertyName)

if (value == null) {
value = when (propertyName) {
CLTAP_PROP_CAMPAIGN_ID -> eventProperties[NOTIFICATION_ID_TAG]
NOTIFICATION_ID_TAG -> eventProperties[CLTAP_PROP_CAMPAIGN_ID]
CLTAP_PROP_VARIANT -> eventProperties[INAPP_WZRK_PIVOT]
INAPP_WZRK_PIVOT -> eventProperties[CLTAP_PROP_VARIANT]
else -> systemPropToKey[propertyName]?.let { eventProperties[it] }
CLTAP_PROP_CAMPAIGN_ID -> evaluateActualPropertyValue(NOTIFICATION_ID_TAG)
NOTIFICATION_ID_TAG -> evaluateActualPropertyValue(CLTAP_PROP_CAMPAIGN_ID)
CLTAP_PROP_VARIANT -> evaluateActualPropertyValue(INAPP_WZRK_PIVOT)
INAPP_WZRK_PIVOT -> evaluateActualPropertyValue(CLTAP_PROP_VARIANT)
else -> systemPropToKey[propertyName]?.let { evaluateActualPropertyValue(it) }
}
}

return value
}

private fun evaluateActualPropertyValue(propertyName: String): Any? {
var value = eventProperties[propertyName]

if (value == null) {
value = eventProperties[Utils.getNormalizedName(propertyName)]
}

if (value == null) {
val normalisedMap = eventProperties.map { item ->
Utils.getNormalizedName(item.key) to item.value
}.toMap()
value = normalisedMap[Utils.getNormalizedName(propertyName)]
}
return value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,49 @@ class EventAdapterTest : BaseTestCase() {
assertFalse { expected.isList() }
}

@Test
fun testGetPropertyPresentSpecialCases() {
// Arrange
val eventProperties = mapOf(
"name" to "John",
Constants.CLTAP_PROP_CAMPAIGN_ID to "cpid",
Constants.NOTIFICATION_ID_TAG to "some_wzrk_id",
Constants.CLTAP_PROP_VARIANT to "some variant",
Constants.INAPP_WZRK_PIVOT to "some_wzrk_pivot",
)
val eventAdapter = EventAdapter("eventName", eventProperties)

val triggerProps = listOf(
Constants.CLTAP_PROP_CAMPAIGN_ID,
Constants.NOTIFICATION_ID_TAG,
Constants.CLTAP_PROP_VARIANT,
Constants.INAPP_WZRK_PIVOT
)

val expectedResults = listOf(
TriggerValue("cpid"),
TriggerValue("some_wzrk_id"),
TriggerValue("some variant"),
TriggerValue("some_wzrk_pivot")
)

val results = mutableListOf<TriggerValue>().apply {
triggerProps.forEach {
add(eventAdapter.getPropertyValue(it))
}
}

results.forEachIndexed { index, result ->
val expected = expectedResults[index]
// Assert
assertNotNull(result)
assertEquals(expected.stringValue(), result.stringValue())
assertNull(result.numberValue())
assertNull(result.listValue())
assertFalse { expected.isList() }
}
}

@Test
fun testGetPropertyPresentNormalisations() {
// Arrange
Expand Down Expand Up @@ -92,6 +135,94 @@ class EventAdapterTest : BaseTestCase() {
assertTrue { expected1.isList() }
}

@Test
fun testGetPropertyPresentNormalisationsExactMatch() {
// Arrange
val eventProperties = mapOf(
"fullname" to "John",
"full name" to "Lenon",
"FullName" to "Augustus",
"age" to 30
)
val eventAdapter = EventAdapter("eventName", eventProperties)

// Act
val result = eventAdapter.getPropertyValue("fullname")
val expected = TriggerValue("John")

// Assert
assertNotNull(result)
assertEquals(expected.stringValue(), result.stringValue())
assertNull(result.numberValue())
assertNull(result.listValue())
assertFalse { expected.isList() }

// Act
val result1 = eventAdapter.getPropertyValue("full name")
val expected1 = TriggerValue("Lenon")

// Assert
assertNotNull(result1)
assertEquals(expected1.stringValue(), result1.stringValue())
assertNull(result1.numberValue())
assertNull(result1.listValue())
assertFalse { expected1.isList() }
}

@Test
fun testGetPropertyPresentNormalisationsNormalisedMatch() {
// Arrange
val eventProperties = mapOf(
"fullname" to "John",
"full-name" to "Lennon",
"age" to 30
)
val eventAdapter = EventAdapter("eventName", eventProperties)

// Act
val result = eventAdapter.getPropertyValue("full name")
val expected = TriggerValue("John")

// Assert
assertNotNull(result)
assertEquals(expected.stringValue(), result.stringValue())
assertNull(result.numberValue())
assertNull(result.listValue())
assertFalse { expected.isList() }

// Act
val result1 = eventAdapter.getPropertyValue("FullName")
val expected1 = TriggerValue("John")

// Assert
assertNotNull(result1)
assertEquals(expected1.stringValue(), result1.stringValue())
assertNull(result1.numberValue())
assertNull(result1.listValue())
assertFalse { expected1.isList() }
}

@Test
fun testGetPropertyPresentNormalisationsNormalisedMatchBothSides() {
// Arrange
val eventProperties = mapOf(
"FullName" to "John",
"age" to 30
)
val eventAdapter = EventAdapter("eventName", eventProperties)

// Act
val result = eventAdapter.getPropertyValue("full name")
val expected = TriggerValue("John")

// Assert
assertNotNull(result)
assertEquals(expected.stringValue(), result.stringValue())
assertNull(result.numberValue())
assertNull(result.listValue())
assertFalse { expected.isList() }
}

@Test
fun testGetPropertyMissing() {
// Arrange
Expand Down Expand Up @@ -188,6 +319,73 @@ class EventAdapterTest : BaseTestCase() {
assertEquals(listOf(330), result4.map { it.numberValue() })
}

@Test
fun testGetItemValuePresentExactMatch() {
// Arrange
val items = listOf(
mapOf("itemName" to "item1", "itemPrice" to 10),
mapOf("item mame" to "item2", "itemPrice" to 20)
)
val eventAdapter = EventAdapter("itemName", emptyMap(), items)
val expected = TriggerValue("item1")

// Act
val result = eventAdapter.getItemValue("itemName")

// Assert
assertNotNull(result)
result.onEach {
assertNull(it.numberValue())
assertNull(it.listValue())
}
assertEquals(listOf("item1"), result.map { it.stringValue() })
assertFalse(expected.isList())
}

@Test
fun testGetItemValuePresentNormalisedMatch() {
// Arrange
val items = listOf(
mapOf("itemName" to "item1", "itemPrice" to 10),
)
val eventAdapter = EventAdapter("item name", emptyMap(), items)
val expected = TriggerValue("item1")

// Act
val result = eventAdapter.getItemValue("itemName")

// Assert
assertNotNull(result)
result.onEach {
assertNull(it.numberValue())
assertNull(it.listValue())
}
assertEquals(listOf("item1"), result.map { it.stringValue() })
assertFalse(expected.isList())
}

@Test
fun testGetItemValuePresentNormalisedMatchBothWays() {
// Arrange
val items = listOf(
mapOf("item name" to "item1", "itemPrice" to 10),
)
val eventAdapter = EventAdapter("ItemName", emptyMap(), items)
val expected = TriggerValue("item1")

// Act
val result = eventAdapter.getItemValue("itemName")

// Assert
assertNotNull(result)
result.onEach {
assertNull(it.numberValue())
assertNull(it.listValue())
}
assertEquals(listOf("item1"), result.map { it.stringValue() })
assertFalse(expected.isList())
}

@Test
fun testGetItemValueMissing() {
// Arrange
Expand Down

0 comments on commit 586dcdc

Please sign in to comment.