-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(multi-id): add MerchantIdHeaderMapper to add x-merchant-id heade…
…r to setContact and refreshContactToken requests SUITEDEV-35599 Co-authored-by: LasOri <[email protected]> Co-authored-by: megamegax <[email protected]>
- Loading branch information
1 parent
54337b7
commit 1766486
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
emarsys/src/androidTest/java/com/emarsys/mapper/MerchantIdHeaderMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
emarsys/src/main/java/com/emarsys/mapper/MerchantIdHeaderMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |