Skip to content

Commit

Permalink
feat(multi-id): add MerchantIdHeaderMapper to add x-merchant-id heade…
Browse files Browse the repository at this point in the history
…r to setContact and refreshContactToken requests

SUITEDEV-35599

Co-authored-by: LasOri <[email protected]>
Co-authored-by: megamegax <[email protected]>
  • Loading branch information
3 people committed Apr 16, 2024
1 parent 54337b7 commit 1766486
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import com.emarsys.inapp.InApp
import com.emarsys.inapp.InAppApi
import com.emarsys.inbox.MessageInbox
import com.emarsys.inbox.MessageInboxApi
import com.emarsys.mapper.MerchantIdHeaderMapper
import com.emarsys.mobileengage.DefaultMobileEngageInternal
import com.emarsys.mobileengage.LoggingMobileEngageInternal
import com.emarsys.mobileengage.MobileEngage
Expand Down Expand Up @@ -1045,6 +1046,7 @@ open class DefaultEmarsysComponent(config: EmarsysConfig) : EmarsysComponent {
MobileEngageHeaderMapper(requestContext, requestModelHelper),
OpenIdTokenRequestMapper(requestContext, requestModelHelper),
ContactTokenHeaderMapper(requestContext, requestModelHelper),
MerchantIdHeaderMapper(requestContext, requestModelHelper, predictRequestContext),
DefaultRequestHeaderMapper(requestContext),
DeviceEventStateRequestMapper(
requestContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.emarsys.mapper

import com.emarsys.core.request.model.RequestMethod
import com.emarsys.core.request.model.RequestModel
import com.emarsys.mobileengage.MobileEngageRequestContext
import com.emarsys.mobileengage.util.RequestModelHelper
import com.emarsys.predict.request.PredictRequestContext
import com.emarsys.testUtil.AnnotationSpec
import io.kotest.matchers.shouldBe
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.stub

class MerchantIdHeaderMapperTest : AnnotationSpec() {

private companion object {
const val TIMESTAMP = 123456789L
const val REQUEST_ID = "request_id"
const val MERCHANT_ID = "testMerchantId"
const val APPLICATION_CODE = "applicationCode"
}

private lateinit var mockRequestContext: MobileEngageRequestContext
private lateinit var mockRequestModelHelper: RequestModelHelper
private lateinit var mockPredictRequestContext: PredictRequestContext

private lateinit var merchantIdHeaderMapper: MerchantIdHeaderMapper

@Before
fun setUp() {
mockRequestContext = mock {
on { applicationCode } doReturn APPLICATION_CODE
}
mockRequestModelHelper = mock {
on { isMobileEngageSetContactRequest(any()) } doReturn false
on { isRefreshContactTokenRequest(any()) } doReturn false
}

mockPredictRequestContext = mock()

merchantIdHeaderMapper =
MerchantIdHeaderMapper(
mockRequestContext,
mockRequestModelHelper,
mockPredictRequestContext
)
}

@Test
fun testMap_shouldAddMerchantIdHeader_whenMobileEngageSetContactRequest_andMerchantIdIsPresentInPredictRequestContext() {
val originalRequestModel = createSetContactRequest()
mockPredictRequestContext.stub {
on { merchantId } doReturn MERCHANT_ID
}
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn true
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn false
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe createSetContactRequest(
extraHeaders = mapOf(
MerchantIdHeaderMapper.MERCHANT_ID_HEADER to MERCHANT_ID
)
)
}

@Test
fun testMap_shouldAddMerchantIdHeader_whenMobileEngageRefreshContactTokenRequest_andMerchantIdIsPresentInPredictRequestContext() {
val originalRequestModel = createRefreshContactTokenRequest()
mockPredictRequestContext.stub {
on { merchantId } doReturn MERCHANT_ID
}
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn false
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn true
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe createRefreshContactTokenRequest(
extraHeaders = mapOf(
MerchantIdHeaderMapper.MERCHANT_ID_HEADER to MERCHANT_ID
)
)
}

@Test
fun testMap_whenNotSetContactOrRefreshContactTokenRequest_shouldIgnoreRequest() {
val originalRequestModel = RequestModel(
"https://me-client.eservice.emarsys.net/v3/notSetContactOrRefreshContactTokenRequest",
RequestMethod.POST,
null,
mapOf(),
TIMESTAMP,
Long.MAX_VALUE,
REQUEST_ID
)
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn false
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn false
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe originalRequestModel
}


@Test
fun testMap_shouldNotAddMerchantIdHeader_whenMobileEngageSetContactRequest_andMerchantIdIsMissingFromPredictRequestContext() {
val originalRequestModel = createSetContactRequest()
mockPredictRequestContext.stub {
on { merchantId } doReturn null
}
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn true
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn false
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe originalRequestModel
}

@Test
fun testMap_shouldNotAddMerchantIdHeader_whenMobileEngageRefreshContactTokenRequest_andMerchantIdIsMissingFromPredictRequestContext() {
val originalRequestModel = createRefreshContactTokenRequest()
mockPredictRequestContext.stub {
on { merchantId } doReturn null
}
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn false
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn true
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe originalRequestModel
}

@Test
fun testMap_shouldNotAddMerchantIdHeader_whenMobileEngageSetContactRequest_andMerchantIdIsEmptyInPredictRequestContext() {
val originalRequestModel = createSetContactRequest()
mockPredictRequestContext.stub {
on { merchantId } doReturn ""
}
mockRequestModelHelper.stub {
on { isMobileEngageSetContactRequest(originalRequestModel) } doReturn true
on { isRefreshContactTokenRequest(originalRequestModel) } doReturn false
}

val updatedRequestModel = merchantIdHeaderMapper.map(originalRequestModel)

updatedRequestModel shouldBe originalRequestModel
}


private fun createSetContactRequest(extraHeaders: Map<String, String> = mapOf()) = RequestModel(
"https://me-client.eservice.emarsys.net/v3/apps/$APPLICATION_CODE/client/contact",
RequestMethod.POST,
null,
extraHeaders,
TIMESTAMP,
Long.MAX_VALUE,
REQUEST_ID
)

private fun createRefreshContactTokenRequest(extraHeaders: Map<String, String> = mapOf()) =
RequestModel(
"https://me-client.eservice.emarsys.net/v3/apps/${APPLICATION_CODE}/client/contact-token",
RequestMethod.POST,
null,
extraHeaders,
TIMESTAMP,
Long.MAX_VALUE,
REQUEST_ID
)

}
32 changes: 32 additions & 0 deletions emarsys/src/main/java/com/emarsys/mapper/MerchantIdHeaderMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.emarsys.mapper

import com.emarsys.core.request.model.RequestModel
import com.emarsys.mobileengage.MobileEngageRequestContext
import com.emarsys.mobileengage.request.mapper.AbstractRequestMapper
import com.emarsys.mobileengage.util.RequestModelHelper
import com.emarsys.predict.request.PredictRequestContext

class MerchantIdHeaderMapper(
override val requestContext: MobileEngageRequestContext,
override val requestModelHelper: RequestModelHelper,
private val predictRequestContext: PredictRequestContext
) : AbstractRequestMapper(requestContext, requestModelHelper) {

companion object {
const val MERCHANT_ID_HEADER = "X-Merchant-Id"
}

override fun createHeaders(requestModel: RequestModel): Map<String, String> {
val headers: MutableMap<String, String> = requestModel.headers.toMutableMap()
if(!predictRequestContext.merchantId.isNullOrBlank()) {
headers[MERCHANT_ID_HEADER] = predictRequestContext.merchantId!!
}

return headers
}

override fun shouldMapRequestModel(requestModel: RequestModel): Boolean {
return requestModelHelper.isMobileEngageSetContactRequest(requestModel) ||
requestModelHelper.isRefreshContactTokenRequest(requestModel)
}
}

0 comments on commit 1766486

Please sign in to comment.