diff --git a/src/main/kotlin/com/vonage/client/kt/Conversion.kt b/src/main/kotlin/com/vonage/client/kt/Conversion.kt index 3659fd4..4be1be8 100644 --- a/src/main/kotlin/com/vonage/client/kt/Conversion.kt +++ b/src/main/kotlin/com/vonage/client/kt/Conversion.kt @@ -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) diff --git a/src/main/kotlin/com/vonage/client/kt/Messages.kt b/src/main/kotlin/com/vonage/client/kt/Messages.kt index da37e5f..1448a7c 100644 --- a/src/main/kotlin/com/vonage/client/kt/Messages.kt +++ b/src/main/kotlin/com/vonage/client/kt/Messages.kt @@ -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() diff --git a/src/main/kotlin/com/vonage/client/kt/Redact.kt b/src/main/kotlin/com/vonage/client/kt/Redact.kt new file mode 100644 index 0000000..d66d347 --- /dev/null +++ b/src/main/kotlin/com/vonage/client/kt/Redact.kt @@ -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) + } +} diff --git a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt index 0ba3744..d378718 100644 --- a/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/AbstractTest.kt @@ -95,20 +95,22 @@ abstract class AbstractTest { else -> throw IllegalArgumentException("Unhandled HTTP method: $this") } - protected fun Map.toFormEncodedString(): String { + private fun Map.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.toJson(): String = ObjectMapper().writeValueAsString(this) + protected fun mockPostQueryParams(expectedUrl: String, expectedRequestParams: Map, status: Int = 200, expectedResponseParams: Map? = 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) @@ -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()} @@ -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 } } diff --git a/src/test/kotlin/com/vonage/client/kt/ConversionTest.kt b/src/test/kotlin/com/vonage/client/kt/ConversionTest.kt index 1fa7df0..006bf43 100644 --- a/src/test/kotlin/com/vonage/client/kt/ConversionTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/ConversionTest.kt @@ -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) + } } \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/RedactTest.kt b/src/test/kotlin/com/vonage/client/kt/RedactTest.kt new file mode 100644 index 0000000..ffd8326 --- /dev/null +++ b/src/test/kotlin/com/vonage/client/kt/RedactTest.kt @@ -0,0 +1,9 @@ +package com.vonage.client.kt + +import kotlin.test.* + +class RedactTest : AbstractTest() { + private val redactClient = vonage.redact + + +} \ No newline at end of file diff --git a/src/test/kotlin/com/vonage/client/kt/SmsTest.kt b/src/test/kotlin/com/vonage/client/kt/SmsTest.kt index 89101af..a7d9c4a 100644 --- a/src/test/kotlin/com/vonage/client/kt/SmsTest.kt +++ b/src/test/kotlin/com/vonage/client/kt/SmsTest.kt @@ -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( @@ -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, @@ -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) } @@ -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, @@ -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,