Skip to content

Commit

Permalink
fix(push): map default action payload as json instead of string
Browse files Browse the repository at this point in the history
SUITEDEV-35495

Co-authored-by: megamegax <[email protected]>
  • Loading branch information
LordAndras and megamegax committed Apr 3, 2024
1 parent 77639b1 commit aaf5cb5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@ package com.emarsys


import android.app.Application
import android.location.Location
import android.location.LocationManager
import android.os.Build
import android.os.SystemClock
import com.emarsys.config.EmarsysConfig
import com.emarsys.core.util.RetryUtil
import com.emarsys.di.emarsys
import com.emarsys.mobileengage.api.geofence.Trigger
import com.emarsys.mobileengage.api.geofence.TriggerType
import com.emarsys.mobileengage.api.inbox.Message
import com.emarsys.testUtil.AnnotationSpec
import com.emarsys.testUtil.E2ETestUtils
import com.emarsys.testUtil.InstrumentationRegistry
import com.emarsys.testUtil.ReflectionTestUtils
import com.emarsys.testUtil.RetryUtils
import com.emarsys.testUtil.rules.DuplicatedThreadRule
import com.emarsys.testUtil.rules.RetryRule
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import org.json.JSONObject
import org.junit.Rule
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import com.emarsys.mobileengage.api.geofence.Geofence as MEGeofence

class EmarsysE2ETests : AnnotationSpec() {

Expand Down Expand Up @@ -160,68 +150,6 @@ class EmarsysE2ETests : AnnotationSpec() {
}
}

@Test
@Ignore
fun testGeofence() {
setup(APPLICATION_CODE)
RetryUtil.retry {
val fusedLocationProviderClient = emarsys().fusedLocationProviderClient
fusedLocationProviderClient.setMockMode(true)
val mockLocation = Location(LocationManager.GPS_PROVIDER).apply {
longitude = 30.0
latitude = 30.0
accuracy = 30.0f
altitude = 100.0
time = System.currentTimeMillis()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
}
}
fusedLocationProviderClient.setMockLocation(mockLocation)
Emarsys.geofence.enable()
Emarsys.geofence.setInitialEnterTriggerEnabled(true)


val latch = CountDownLatch(1)
emarsys().concurrentHandlerHolder.coreHandler.post {
fusedLocationProviderClient.lastLocation.addOnSuccessListener { currentLocation ->
val testAction = JSONObject(
mapOf<String, Any?>(
"id" to "geofenceActionId",
"type" to "MEAppEvent",
"name" to "geofence",
"payload" to mapOf(
"name" to "Home",
"trigger_type" to "enter"
)
)
)
val geofence = MEGeofence(
"testGeofence", currentLocation.latitude, currentLocation.longitude,
400.0, 0.0, listOf(Trigger("testAction", TriggerType.ENTER, 0, testAction))
)

val geofenceInternal = emarsys().geofenceInternal
ReflectionTestUtils.setInstanceField(
geofenceInternal,
"nearestGeofences",
listOf(geofence)
)
geofenceInternal.registerGeofences(listOf(geofence))
}

}
Emarsys.geofence.setEventHandler { _, name, payload ->
name shouldBe "geofence"
(payload?.get("name") ?: "") shouldBe "Home"
(payload?.get("trigger_type") ?: "") shouldBe "enter"

latch.countDown()
}
latch.await(3, TimeUnit.SECONDS)
}
}

private fun changeApplicationCode(appCode: String?) {
val changeApplicationCodeLatch = CountDownLatch(1)
Emarsys.config.changeApplicationCode(appCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class RemoteMessageMapperV2Test : AnnotationSpec() {
val input: MutableMap<String, String> = createRemoteMessage()
input["ems.tap_actions.default_action.name"] = "test name"
input["ems.tap_actions.default_action.url"] = "test url"
input["ems.tap_actions.default_action.payload"] = " test payload"
input["ems.tap_actions.default_action.payload"] = """{"key":"test payload"}"""

val notificationData = remoteMessageMapperV2.map(input)

Expand All @@ -232,10 +232,10 @@ class RemoteMessageMapperV2Test : AnnotationSpec() {
input["ems.tap_actions.default_action.name"] = "test name"
input["ems.tap_actions.default_action.type"] = "MECustomEvent"
input["ems.tap_actions.default_action.url"] = "test url"
input["ems.tap_actions.default_action.payload"] = "test payload"
input["ems.tap_actions.default_action.payload"] = """{"key":"test payload"}"""

val expectedDefaultAction =
"""{"name":"test name","type":"MECustomEvent","url":"test url","payload":"test payload"}"""
"""{"name":"test name","type":"MECustomEvent","url":"test url","payload":{"key":"test payload"}}"""

val notificationData = remoteMessageMapperV2.map(input)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ class RemoteMessageMapperV2(
val url = remoteMessageData.getOrDefault("ems.tap_actions.default_action.url", null)
val payload =
remoteMessageData.getOrDefault("ems.tap_actions.default_action.payload", null)
val payloadObject = payload?.let { JSONObject(payload) }

val defaultAction = if (type != null) {
JSONObject()
.put("name", name)
.put("type", type)
.put("url", url)
.put("payload", payload)
.put("payload", payloadObject)
} else null

return defaultAction?.toString()
Expand Down

0 comments on commit aaf5cb5

Please sign in to comment.