From 6ce4122c45a5fd58a2f518c9e99a4f2ccd03b2d7 Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Wed, 24 Jul 2024 15:50:30 +0100 Subject: [PATCH] Add NIv1 implementation --- .../com/vonage/client/kt/NumberInsight.kt | 21 +++- .../kotlin/com/vonage/client/kt/Redact.kt | 15 +-- .../com/vonage/client/kt/NumberInsightTest.kt | 119 +++++++++++++++++- 3 files changed, 143 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/vonage/client/kt/NumberInsight.kt b/src/main/kotlin/com/vonage/client/kt/NumberInsight.kt index 409a0ce..b4bb539 100644 --- a/src/main/kotlin/com/vonage/client/kt/NumberInsight.kt +++ b/src/main/kotlin/com/vonage/client/kt/NumberInsight.kt @@ -1,7 +1,26 @@ package com.vonage.client.kt -import com.vonage.client.insight.InsightClient +import com.vonage.client.insight.* class NumberInsight(private val niClient: InsightClient) { + fun basic(number: String, countryCode: String? = null): BasicInsightResponse = + niClient.getBasicNumberInsight(number, countryCode) + + fun standard(number: String, countryCode: String? = null, cnam: Boolean? = null): StandardInsightResponse = + niClient.getStandardNumberInsight(StandardInsightRequest.builder() + .number(number).country(countryCode).cnam(cnam).build() + ) + + fun advanced(number: String, countryCode: String? = null, cnam: Boolean = false, + realTimeData: Boolean = false): AdvancedInsightResponse = + niClient.getAdvancedNumberInsight(AdvancedInsightRequest.builder().async(false) + .number(number).country(countryCode).cnam(cnam).realTimeData(realTimeData).build() + ) + + /*fun advancedAsync(number: String, countryCode: String? = null, cnam: Boolean = false, + callbackUrl: String? = null) = + niClient.getAdvancedNumberInsight(AdvancedInsightRequest.builder().async(true) + .number(number).country(countryCode).cnam(cnam).callback(callbackUrl).build() + )*/ } diff --git a/src/main/kotlin/com/vonage/client/kt/Redact.kt b/src/main/kotlin/com/vonage/client/kt/Redact.kt index d66d347..d7e5a4f 100644 --- a/src/main/kotlin/com/vonage/client/kt/Redact.kt +++ b/src/main/kotlin/com/vonage/client/kt/Redact.kt @@ -4,23 +4,18 @@ import com.vonage.client.redact.* class Redact(private val redactClient: RedactClient) { - fun redactSms(messageId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) { + fun redactSms(messageId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) = redactClient.redactTransaction(messageId, RedactRequest.Product.SMS, direction) - } - fun redactMessage(messageId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) { + fun redactMessage(messageId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) = redactClient.redactTransaction(messageId, RedactRequest.Product.MESSAGES, direction) - } - fun redactCall(callId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) { + fun redactCall(callId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) = redactClient.redactTransaction(callId, RedactRequest.Product.VOICE, direction) - } - fun redactInsight(requestId: String) { + fun redactInsight(requestId: String) = redactClient.redactTransaction(requestId, RedactRequest.Product.NUMBER_INSIGHTS) - } - fun redactVerification(requestId: String) { + fun redactVerification(requestId: String) = redactClient.redactTransaction(requestId, RedactRequest.Product.VERIFY) - } } diff --git a/src/test/kotlin/com/vonage/client/kt/NumberInsightTest.kt b/src/test/kotlin/com/vonage/client/kt/NumberInsightTest.kt index a0cd0ad..f355178 100644 --- a/src/test/kotlin/com/vonage/client/kt/NumberInsightTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/NumberInsightTest.kt @@ -1,12 +1,129 @@ package com.vonage.client.kt +import com.vonage.client.insight.* import kotlin.test.* class NumberInsightTest : AbstractTest() { private val niClient = vonage.numberInsight + private val cnam = true + private val realTimeData = true + private val statusMessage = "Success" + private val nationalNumber = "07712 345689" + private val countryCode = "GB" + private val countryCodeIso3 = "GBR" + private val countryName = "United Kingdom" + private val countryPrefix = "44" + private val requestPrice = "0.035900000" + private val refundPrice = "0.01500000" + private val remainingBalance = "1.23456789" + private val reachable = "reachable" + + + private enum class InsightType { + BASIC, STANDARD, ADVANCED + } + + private fun mockInsight(type: InsightType, optionalParams: Boolean = false) { + val expectedRequestParams = mutableMapOf("number" to toNumber) + if (optionalParams) { + expectedRequestParams["country"] = countryCode + if (type != InsightType.BASIC) { + expectedRequestParams["cnam"] = true + } + if (type == InsightType.ADVANCED) { + expectedRequestParams["real_time_data"] = true + } + } + + val expectedResponseParams = mutableMapOf( + "status" to 0, + "status_message" to statusMessage, + "request_id" to testUuidStr, + "international_format_number" to toNumber, + "national_format_number" to nationalNumber, + "country_code" to countryCode, + "country_code_iso3" to countryCodeIso3, + "country_name" to countryName, + "country_prefix" to countryPrefix + ) + if (type != InsightType.BASIC) { + expectedResponseParams.putAll(mapOf( + "request_price" to requestPrice, + "refund_price" to refundPrice, + "remaining_balance" to remainingBalance, + // TODO: the rest + )) + } + if (type == InsightType.ADVANCED) { + expectedResponseParams.putAll(mapOf( + "reachable" to reachable + // TODO: the rest + )) + } + + mockPostQueryParams( + expectedUrl = "/ni/${type.name.lowercase()}/json", + expectedRequestParams = expectedRequestParams, + expectedResponseParams = expectedResponseParams + ) + } + + private fun assertBasicResponse(response: BasicInsightResponse) { + assertNotNull(response) + assertEquals(InsightStatus.SUCCESS, response.status) + assertEquals(statusMessage, response.statusMessage) + assertEquals(testUuidStr, response.requestId) + assertEquals(toNumber, response.internationalFormatNumber) + assertEquals(nationalNumber, response.nationalFormatNumber) + assertEquals(countryCode, response.countryCode) + assertEquals(countryCodeIso3, response.countryCodeIso3) + assertEquals(countryName, response.countryName) + assertEquals(countryPrefix, response.countryPrefix) + } + + private fun assertStandardResponse(response: StandardInsightResponse) { + assertBasicResponse(response) + // TODO + } + + private fun assertAdvancedResponse(response: AdvancedInsightResponse) { + assertStandardResponse(response) + // TODO + } @Test - fun `basic insight`() { + fun `basic insight required params`() { + mockInsight(InsightType.BASIC, false) + assertBasicResponse(niClient.basic(toNumber)) + } + @Test + fun `basic insight all params`() { + mockInsight(InsightType.BASIC, true) + assertBasicResponse(niClient.basic(toNumber, countryCode)) + } + + @Test + fun `standard insight required params`() { + mockInsight(InsightType.STANDARD, false) + assertStandardResponse(niClient.standard(toNumber)) + } + + @Test + fun `standard insight all params`() { + mockInsight(InsightType.STANDARD, true) + assertStandardResponse(niClient.standard(toNumber, countryCode, cnam)) + } + + @Test + fun `advanced insight required params`() { + mockInsight(InsightType.ADVANCED, false) + assertStandardResponse(niClient.advanced(toNumber)) + } + + @Test + fun `advanced insight all params`() { + mockInsight(InsightType.ADVANCED, true) + assertStandardResponse(niClient.advanced(toNumber, countryCode, cnam, realTimeData)) } } \ No newline at end of file