Skip to content

Commit 4f29b94

Browse files
committed
test: 100% branch coverage
1 parent da3b751 commit 4f29b94

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/main/kotlin/com/vonage/client/kt/Messages.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class Messages internal constructor(private val client: MessagesClient) {
5454
* - **500**: Internal server error.
5555
*/
5656
fun send(message: MessageRequest, sandbox: Boolean = false): UUID =
57-
(if (sandbox) client.useSandboxEndpoint()
58-
else client.useRegularEndpoint()).sendMessage(message).messageUuid
57+
(if (sandbox) client.useSandboxEndpoint() else client.useRegularEndpoint())
58+
.sendMessage(message).messageUuid
5959

6060
/**
6161
* Call this method to update an existing message.

src/main/kotlin/com/vonage/client/kt/Vonage.kt

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ package com.vonage.client.kt
1818
import com.vonage.client.HttpConfig
1919
import com.vonage.client.VonageClient
2020

21+
/**
22+
* Denotes the version of the Vonage Kotlin SDK being used, in SemVer format.
23+
*/
2124
const val VONAGE_KOTLIN_SDK_VERSION = "1.1.2"
25+
26+
/**
27+
* The non-overridable user agent string used by the SDK.
28+
*/
2229
private const val SDK_USER_AGENT = "vonage-kotlin-sdk/$VONAGE_KOTLIN_SDK_VERSION"
2330

2431
/**
@@ -173,27 +180,34 @@ class Vonage(config: VonageClient.Builder.() -> Unit) {
173180
* - **VONAGE_PRIVATE_KEY_PATH**: Path to the private key file of the application.
174181
*/
175182
fun VonageClient.Builder.authFromEnv(): VonageClient.Builder {
176-
val apiKey = System.getenv("VONAGE_API_KEY")
177-
val apiSecret = System.getenv("VONAGE_API_SECRET")
178-
val signatureSecret = System.getenv("VONAGE_SIGNATURE_SECRET")
179-
val applicationId = System.getenv("VONAGE_APPLICATION_ID")
180-
val privateKeyPath = System.getenv("VONAGE_PRIVATE_KEY_PATH")
181-
if (apiKey != null) apiKey(apiKey)
182-
if (apiSecret != null) apiSecret(apiSecret)
183-
if (signatureSecret != null) signatureSecret(signatureSecret)
184-
if (applicationId != null) applicationId(applicationId)
185-
if (privateKeyPath != null) privateKeyPath(privateKeyPath)
183+
setFromEnvIfNotNull("VONAGE_API_KEY") { apiKey(it) }
184+
setFromEnvIfNotNull("VONAGE_API_SECRET") { apiSecret(it) }
185+
setFromEnvIfNotNull("VONAGE_SIGNATURE_SECRET") { signatureSecret(it) }
186+
setFromEnvIfNotNull("VONAGE_APPLICATION_ID") { applicationId(it) }
187+
setFromEnvIfNotNull("VONAGE_PRIVATE_KEY_PATH") { privateKeyPath(it) }
188+
setFromEnvIfNotNull("VONAGE_PRIVATE_KEY_CONTENTS") { privateKeyContents(it) }
186189
return this
187190
}
188191

192+
/**
193+
* Calls the setter function with the value of the environment variable if it is not null.
194+
*
195+
* @param envVar The name of the environment variable.
196+
* @param setter The setter function to call with the value of the environment variable.
197+
*/
198+
private fun setFromEnvIfNotNull(envVar: String, setter: (String) -> Unit) {
199+
val value = System.getenv(envVar)
200+
if (value != null) setter(value)
201+
}
202+
189203
/**
190204
* Optional HTTP configuration options for the client.
191205
*
192206
* @param init The config options lambda.
193207
* @return The builder.
194208
*/
195209
fun VonageClient.Builder.httpConfig(init: HttpConfig.Builder.() -> Unit): VonageClient.Builder {
196-
// Workaround to prevent the default prefix being overriden
210+
// Workaround to prevent the default prefix being overridden
197211
val applied = HttpConfig.builder().apply(init)
198212
val customUa = applied.build().customUserAgent
199213
val adjustedUa = if (customUa.isNullOrBlank()) SDK_USER_AGENT else "$SDK_USER_AGENT $customUa"

src/test/kotlin/com/vonage/client/kt/MessagesTest.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class MessagesTest : AbstractTest() {
4242
private val caption = "Additional text to accompany the media"
4343
private val captionMap = mapOf("caption" to caption)
4444

45-
4645
private fun testSend(expectedBodyParams: Map<String, Any>, req: MessageRequest) {
4746
mockPost(expectedUrl = sendUrl, status = 202, authType = authType,
4847
expectedRequestParams = expectedBodyParams,
@@ -661,4 +660,20 @@ class MessagesTest : AbstractTest() {
661660
assertEquals(networkCode, parsed.destinationNetworkCode)
662661
assertEquals(smsCount, parsed.smsTotalCount)
663662
}
663+
664+
@Test
665+
fun `send sandbox real request fails with 401`() {
666+
try {
667+
client.send(
668+
smsText { from(altNumber); to(toNumber); text(text) },
669+
sandbox = true
670+
)
671+
}
672+
catch (ex: MessageResponseException) {
673+
assertEquals(401, ex.statusCode)
674+
assertNotNull(ex.title)
675+
assertNotNull(ex.detail)
676+
assertNotNull(ex.instance)
677+
}
678+
}
664679
}

src/test/kotlin/com/vonage/client/kt/SmsTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,22 @@ class SmsTest : AbstractTest() {
222222
assertEquals("Number De-activated.", response[offset].errorText)
223223
assertEquals(OK, response[++offset].status)
224224
}
225+
226+
@Test
227+
fun `wasSuccessfullySent with empty response returns false`() {
228+
mockPostQueryParams(sendUrl,
229+
expectedRequestParams = mapOf(
230+
"from" to from, "to" to toNumber,
231+
"text" to text, "type" to "text",
232+
),
233+
authType = AuthType.API_KEY_SECRET_HEADER,
234+
expectedResponseParams = mapOf(
235+
"message-count" to "-1",
236+
"messages" to emptyList<SmsSubmissionResponseMessage>()
237+
)
238+
)
239+
val response = client.sendText(from, toNumber, text)
240+
assertNotNull(response)
241+
assertFalse(response.wasSuccessfullySent())
242+
}
225243
}

0 commit comments

Comments
 (0)