From 5f524a7adc5dc5d04267916893de08f9c52d1d8c Mon Sep 17 00:00:00 2001 From: Sai Date: Tue, 12 Dec 2023 14:11:27 -0600 Subject: [PATCH] Moving the validation logic --- .../api/ShopperInsightsClient.kt | 12 ++++++++++++ .../api/ShopperInsightsRequest.kt | 12 +++--------- .../api/ShopperInsightsClientUnitTest.kt | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsClient.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsClient.kt index cdce293af3..e56b0df214 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsClient.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsClient.kt @@ -23,6 +23,18 @@ class ShopperInsightsClient @VisibleForTesting internal constructor( request: ShopperInsightsRequest, callback: ShopperInsightsCallback ) { + if (request.email == null && request.phone == null) { + callback.onResult( + ShopperInsightsResult.Failure( + IllegalArgumentException( + "One of ShopperInsightsRequest.email or " + + "ShopperInsightsRequest.phone must be non-null." + ) + ) + ) + return + } + // TODO: - Add isAppInstalled checks for PP & Venmo. DTBTSDK-3176 paymentReadyAPI.processRequest(request) // Hardcoded result diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt index 7f5da8a34a..010a496d2e 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt @@ -6,12 +6,6 @@ package com.braintreepayments.api * Note: **This feature is in beta. It's public API may change in future releases.** */ data class ShopperInsightsRequest( - val email: String?, - val phone: BuyerPhone? -) { - init { - require(email != null || phone != null) { - "Both email and phone cannot be null." - } - } -} + var email: String?, + var phone: BuyerPhone? +) diff --git a/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt b/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt index 7643b709c3..c1b317ae99 100644 --- a/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt +++ b/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt @@ -1,6 +1,7 @@ package com.braintreepayments.api import io.mockk.mockk +import kotlin.test.assertEquals import kotlin.test.assertIs import org.junit.Assert.assertNotNull import org.junit.Before @@ -38,4 +39,19 @@ class ShopperInsightsClientUnitTest { assertNotNull(successResult.response.isVenmoRecommended) } } + + @Test + fun `testGetRecommendedPaymentMethods - request object has null properties`() { + val request = ShopperInsightsRequest(null, null) + sut.getRecommendedPaymentMethods(request) { result -> + assertNotNull(result) + val error = assertIs(result) + val iae = assertIs(error.error) + assertEquals( + "One of ShopperInsightsRequest.email or " + + "ShopperInsightsRequest.phone must be non-null.", + iae.message + ) + } + } }