Skip to content

Commit

Permalink
test: Fix SmsTest & finish ConversionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jul 8, 2024
1 parent 816fd11 commit 761dbcd
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
7 changes: 2 additions & 5 deletions src/main/kotlin/com/vonage/client/kt/Conversion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import java.util.*

class Conversion(private val conversionClient: ConversionClient) {

private fun convert(type: ConversionRequest.Type, messageId: String, delivered: Boolean,
timestamp: Instant? = null) =
conversionClient.submitConversion(type, messageId, delivered,
if (timestamp == null) null else Date.from(timestamp)
)
private fun convert(type: ConversionRequest.Type, messageId: String, delivered: Boolean, timestamp: Instant) =
conversionClient.submitConversion(type, messageId, delivered, Date.from(timestamp))

fun convertSms(messageId: String, delivered: Boolean, timestamp: Instant = Instant.now()) =
convert(ConversionRequest.Type.SMS, messageId, delivered, timestamp)
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/com/vonage/client/kt/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Messages(private val client: MessagesClient) {
client.useSandboxEndpoint().sendMessage(message).messageUuid
}

fun parseInboundMessage(json: String) : InboundMessage = InboundMessage.fromJson(json)

fun smsText(init: SmsTextRequest.Builder.() -> Unit): SmsTextRequest =
SmsTextRequest.builder().apply(init).build()

Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/com/vonage/client/kt/Redact.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.vonage.client.kt

import com.vonage.client.redact.*

class Redact(private val redactClient: RedactClient) {

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) {
redactClient.redactTransaction(messageId, RedactRequest.Product.MESSAGES, direction)
}

fun redactCall(callId: String, direction: RedactRequest.Type = RedactRequest.Type.OUTBOUND) {
redactClient.redactTransaction(callId, RedactRequest.Product.VOICE, direction)
}

fun redactInsight(requestId: String) {
redactClient.redactTransaction(requestId, RedactRequest.Product.NUMBER_INSIGHTS)
}

fun redactVerification(requestId: String) {
redactClient.redactTransaction(requestId, RedactRequest.Product.VERIFY)
}
}
10 changes: 6 additions & 4 deletions src/test/kotlin/com/vonage/client/kt/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,22 @@ abstract class AbstractTest {
else -> throw IllegalArgumentException("Unhandled HTTP method: $this")
}

protected fun Map<String, Any>.toFormEncodedString(): String {
private fun Map<String, Any>.toFormEncodedString(): String {
val utf8 = StandardCharsets.UTF_8.toString()
return entries.joinToString("&") { (key, value) ->
"${URLEncoder.encode(key, utf8)}=${URLEncoder.encode(value.toString(), utf8)}"
}
}

private fun Map<String, Any>.toJson(): String = ObjectMapper().writeValueAsString(this)

protected fun mockPostQueryParams(expectedUrl: String, expectedRequestParams: Map<String, Any>,
status: Int = 200, expectedResponseParams: Map<String, Any>? = null) {
val stub = post(urlPathEqualTo(expectedUrl))
expectedRequestParams.forEach {(k, v) -> stub.withFormParam(k, equalTo(v.toString()))}
val response = aResponse().withStatus(status)
if (expectedResponseParams != null) {
response.withBody(expectedResponseParams.toFormEncodedString())
response.withBody(expectedResponseParams.toJson())
}
stub.willReturn(response)
wiremock.stubFor(stub)
Expand Down Expand Up @@ -153,7 +155,7 @@ abstract class AbstractTest {
}
if (expectedParams != null) when (contentType) {
ContentType.APPLICATION_JSON -> {
body equalTo ObjectMapper().writeValueAsString(expectedParams)
body equalTo expectedParams.toJson()
}
else -> {
expectedParams.forEach {(k, v) -> queryParams contains k equalTo v.toString()}
Expand Down Expand Up @@ -205,7 +207,7 @@ abstract class AbstractTest {
else status ?: 200

if (expectedBody != null) {
body = ObjectMapper().writeValueAsString(expectedBody)
body = expectedBody.toJson()
header = "Content-Type" to ContentType.APPLICATION_JSON.mime
}
}
Expand Down
31 changes: 23 additions & 8 deletions src/test/kotlin/com/vonage/client/kt/ConversionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,41 @@ import kotlin.test.*

class ConversionTest : AbstractTest() {
private val conversionClient = vonage.conversion
private val smsEndpoint = "sms"
private val voiceEndpoint = "voice"

private fun mockSuccess(id: String, endpoint: String, delivered: Boolean) {
private fun mockSuccess(id: String, endpoint: String, delivered: Boolean, includeTimestamp: Boolean) {
mockPostQueryParams("/conversions/$endpoint", mapOf(
"message-id" to id,
"delivered" to delivered,
"timestamp" to timestampDateStr
))
"delivered" to delivered
) + if (includeTimestamp) mapOf("timestamp" to timestampDateStr) else mapOf())
}

@Test
fun `submit sms conversion`() {
fun `submit sms conversion with timestamp`() {
val delivered = true
mockSuccess(smsMessageId, "sms", delivered)
mockSuccess(smsMessageId, smsEndpoint, delivered, true)
conversionClient.convertSms(smsMessageId, delivered, timestamp)
}

@Test
fun `submit voice conversion`() {
fun `submit sms conversion without timestamp`() {
val delivered = false
mockSuccess(callIdStr, "voice", delivered)
mockSuccess(smsMessageId, smsEndpoint, delivered, false)
conversionClient.convertSms(smsMessageId, delivered)
}

@Test
fun `submit voice conversion with timestamp`() {
val delivered = false
mockSuccess(callIdStr, voiceEndpoint, delivered, true)
conversionClient.convertVoice(callIdStr, delivered, timestamp)
}

@Test
fun `submit voice conversion without timestamp`() {
val delivered = true
mockSuccess(callIdStr, voiceEndpoint, delivered, false)
conversionClient.convertVoice(callIdStr, delivered)
}
}
9 changes: 9 additions & 0 deletions src/test/kotlin/com/vonage/client/kt/RedactTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vonage.client.kt

import kotlin.test.*

class RedactTest : AbstractTest() {
private val redactClient = vonage.redact


}
14 changes: 6 additions & 8 deletions src/test/kotlin/com/vonage/client/kt/SmsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class SmsTest : AbstractTest() {
val messagePrice = "0.03330000"
val network = "23410"

mockPost(sendUrl, requestParams, authType = AuthType.API_KEY_SECRET_QUERY_PARAMS,
contentType = ContentType.FORM_URLENCODED, expectedResponseParams = mapOf(
mockPostQueryParams(sendUrl, requestParams, expectedResponseParams = mapOf(
"message-count" to "1",
"messages" to listOf(
mapOf(
Expand Down Expand Up @@ -87,7 +86,7 @@ class SmsTest : AbstractTest() {
"text" to text,
"type" to "text",
"callback" to callback,
"status-report-req" to statusReport,
"status-report-req" to if (statusReport) 1 else 0,
"message-class" to 1,
"ttl" to ttl,
"client-ref" to clientRef,
Expand All @@ -107,7 +106,7 @@ class SmsTest : AbstractTest() {
fun `send binary message success required parameters`() {
testSuccessSingleMessage(mapOf(
"from" to from, "to" to toNumber, "type" to "binary",
"body" to textHexEncoded, "udh" to udhHex
"body" to textHexEncoded, "udh" to udhHex.lowercase()
)) {
smsClient.sendBinary(from, toNumber, text.encodeToByteArray(), udhBinary)
}
Expand All @@ -120,10 +119,10 @@ class SmsTest : AbstractTest() {
"to" to toNumber,
"body" to textHexEncoded,
"type" to "binary",
"udh" to udhHex,
"udh" to udhHex.lowercase(),
"protocol-id" to protocolId,
"callback" to callback,
"status-report-req" to statusReport,
"status-report-req" to if (statusReport) 1 else 0,
"message-class" to 2,
"ttl" to ttl,
"client-ref" to clientRef,
Expand All @@ -146,8 +145,7 @@ class SmsTest : AbstractTest() {
)
val successMap = mapOf("status" to "0")

mockPost(sendUrl, expectedRequestParams, authType = AuthType.API_KEY_SECRET_QUERY_PARAMS,
contentType = ContentType.FORM_URLENCODED, expectedResponseParams = mapOf(
mockPostQueryParams(sendUrl, expectedRequestParams, expectedResponseParams = mapOf(
"message-count" to "2147483647",
"messages" to listOf(
successMap, successMap, successMap, successMap,
Expand Down

0 comments on commit 761dbcd

Please sign in to comment.