Skip to content

Commit

Permalink
Add async endpoint test
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jul 25, 2024
1 parent fe5e7c3 commit 9f7161a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.vonage</groupId>
<artifactId>server-sdk</artifactId>
<version>8.9.3</version>
<version>8.9.4</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/NumberInsight.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ class NumberInsight(private val niClient: InsightClient) {
niClient.getAdvancedNumberInsight(AdvancedInsightRequest.builder().async(false)
.number(number).country(countryCode).cnam(cnam).realTimeData(realTimeData).build()
)

fun advancedAsync(number: String, callbackUrl: String, countryCode: String? = null, cnam: Boolean = false) {
niClient.getAdvancedNumberInsight(
AdvancedInsightRequest.builder().async(true)
.number(number).country(countryCode).cnam(cnam).callback(callbackUrl).build()
)
}
}
2 changes: 2 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ abstract class AbstractTest {
protected val timestamp2Str = "2020-01-29T14:08:30.201Z"
protected val timestamp2: Instant = Instant.parse(timestamp2Str)
protected val currency = "EUR"
protected val exampleUrlBase = "https://example.com"
protected val callbackUrl = "$exampleUrlBase/callback"

private val port = 8081
private val wiremock: WireMockServer = WireMockServer(
Expand Down
103 changes: 68 additions & 35 deletions src/test/kotlin/com/vonage/client/kt/NumberInsightTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,64 +44,85 @@ class NumberInsightTest : AbstractTest() {


private enum class InsightType {
BASIC, STANDARD, ADVANCED
BASIC, STANDARD, ADVANCED, ADVANCED_ASYNC
}

private fun mockInsight(type: InsightType, optionalParams: Boolean = false) {
val expectedRequestParams = mutableMapOf<String, Any>("number" to toNumber)
if (optionalParams) {
expectedRequestParams["country"] = countryCode
if (type != InsightType.BASIC) {
expectedRequestParams["cnam"] = true
expectedRequestParams["cnam"] = cnam
}
if (type == InsightType.ADVANCED) {
expectedRequestParams["real_time_data"] = true
expectedRequestParams["real_time_data"] = realTimeData
}
}

val expectedResponseParams = mutableMapOf<String, Any>(
"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
"status_message" to statusMessage
)
if (type != InsightType.BASIC) {
val callerIdentity = mapOf(
"caller_name" to callerName,
"last_name" to lastName,
"first_name" to firstName,
"caller_type" to callerType.name.lowercase()
if (type != InsightType.ADVANCED_ASYNC) {
expectedResponseParams.putAll(
mapOf(
"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,
"current_carrier" to mapOf(
"network_code" to currentNetworkCode,
"name" to currentName,
"country" to currentCountry,
"network_type" to currentNetworkType
),
"ported" to ported.name.lowercase(),
"original_carrier" to mapOf(
"network_code" to originalNetworkCode,
"name" to originalName,
"country" to originalCountry,
"network_type" to originalNetworkType
),
"caller_identity" to callerIdentity
"remaining_balance" to remainingBalance
))

if (type == InsightType.STANDARD) {
expectedResponseParams.putAll(callerIdentity)
if (type == InsightType.ADVANCED_ASYNC) {
expectedResponseParams.putAll(mapOf(
"number" to toNumber,
"error_text" to statusMessage
))
}
else {
val callerIdentity = mapOf(
"caller_name" to callerName,
"last_name" to lastName,
"first_name" to firstName,
"caller_type" to callerType.name.lowercase()
)

expectedResponseParams.putAll(
mapOf(
"refund_price" to refundPrice,
"current_carrier" to mapOf(
"network_code" to currentNetworkCode,
"name" to currentName,
"country" to currentCountry,
"network_type" to currentNetworkType
),
"ported" to ported.name.lowercase(),
"original_carrier" to mapOf(
"network_code" to originalNetworkCode,
"name" to originalName,
"country" to originalCountry,
"network_type" to originalNetworkType
),
"caller_identity" to callerIdentity
)
)

if (type == InsightType.STANDARD) {
expectedResponseParams.putAll(callerIdentity)
}
}
}

if (type == InsightType.ADVANCED) {
expectedResponseParams.putAll(mapOf(
"roaming" to mapOf(
Expand All @@ -122,7 +143,7 @@ class NumberInsightTest : AbstractTest() {
}

mockPostQueryParams(
expectedUrl = "/ni/${type.name.lowercase()}/json",
expectedUrl = "/ni/${type.name.lowercase().replace('_', '/')}/json",
expectedRequestParams = expectedRequestParams,
expectedResponseParams = expectedResponseParams
)
Expand Down Expand Up @@ -226,4 +247,16 @@ class NumberInsightTest : AbstractTest() {
mockInsight(InsightType.ADVANCED, true)
assertAdvancedResponse(niClient.advanced(toNumber, countryCode, cnam, realTimeData))
}

@Test
fun `advanced async insight required params`() {
mockInsight(InsightType.ADVANCED_ASYNC, false)
niClient.advancedAsync(toNumber, callbackUrl)
}

@Test
fun `advanced async insight all params`() {
mockInsight(InsightType.ADVANCED_ASYNC, true)
niClient.advancedAsync(toNumber, callbackUrl, countryCode, cnam)
}
}

0 comments on commit 9f7161a

Please sign in to comment.