Skip to content

Commit 9821050

Browse files
committed
test: Numbers API errors
1 parent 3fbc8cc commit 9821050

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

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

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ package com.vonage.client.kt
1717

1818
import com.vonage.client.numbers.*
1919
import com.vonage.client.numbers.UpdateNumberRequest.CallbackType
20+
import org.junit.jupiter.api.assertThrows
2021
import java.util.*
2122
import kotlin.test.*
2223

2324
class NumbersTest : AbstractTest() {
24-
private val numbersClient = vonage.numbers
25+
private val client = vonage.numbers
26+
private val authType = AuthType.API_KEY_SECRET_HEADER
2527
private val country = "GB"
2628
private val targetApiKey = "1a2345b7"
2729
private val moSmppSysType = "inbound"
30+
private val buyEndpoint = "buy"
31+
private val cancelEndpoint = "cancel"
32+
private val updateEndpoint = "update"
2833
private val featureNames = Feature.entries.map(Feature::name)
2934
private val pattern = "1337*"
3035
private val count = 1247
3136
private val size = 25
3237
private val index = 6
33-
private val existingNumber = numbersClient.number(country, toNumber)
38+
private val errorCode = 401
39+
private val existingNumber = client.number(country, toNumber)
3440
private val baseRequestParams = mapOf(
3541
"country" to existingNumber.countryCode,
3642
"msisdn" to existingNumber.msisdn
@@ -40,25 +46,44 @@ class NumbersTest : AbstractTest() {
4046
"error-code" to "200",
4147
"error-code-label" to "success"
4248
)
49+
private val errorResponse = mapOf(
50+
"error-code" to errorCode.toString(),
51+
"error-code-label" to "authentication failed"
52+
)
53+
54+
private fun assertThrowsGet(url: String, invocation: Numbers.() -> Any) {
55+
mockGet(expectedUrl = url,
56+
status = errorCode, authType = authType,
57+
expectedResponseParams = errorResponse
58+
)
59+
assertThrows<NumbersResponseException> { invocation.invoke(client) }
60+
}
61+
62+
private fun assertThrowsPost(endpoint: String, invocation: Numbers.ExistingNumber.() -> Any) {
63+
mockPost(expectedUrl = "/number/$endpoint",
64+
status = errorCode, authType = authType,
65+
expectedResponseParams = errorResponse
66+
)
67+
assertThrows<NumbersResponseException> { invocation.invoke(existingNumber) }
68+
}
4369

4470
private fun mockAction(endpoint: String, additionalParams: Map<String, String> = mapOf()) {
45-
mockPostQueryParams(
46-
expectedUrl = "/number/$endpoint",
71+
mockPostQueryParams(expectedUrl = "/number/$endpoint",
4772
expectedRequestParams = baseRequestParams + additionalParams,
48-
authType = AuthType.API_KEY_SECRET_HEADER,
49-
expectedResponseParams = successResponseMap
73+
authType = authType, expectedResponseParams = successResponseMap
5074
)
5175
}
5276

5377
private fun assertOwnedNumbers(params: Map<String, Any>, invocation: Numbers.() -> ListNumbersResponse) {
5478
val type = Type.MOBILE_LVN
5579
val voiceCallbackType = CallbackType.SIP
5680
val messagesCallbackValue = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab"
57-
81+
val url = "/account/numbers"
82+
5883
mockGet(
59-
expectedUrl = "/account/numbers",
84+
expectedUrl = url,
6085
expectedQueryParams = params,
61-
authType = AuthType.API_KEY_SECRET_HEADER,
86+
authType = authType,
6287
expectedResponseParams = mapOf(
6388
"count" to count,
6489
"numbers" to listOf(
@@ -77,7 +102,7 @@ class NumbersTest : AbstractTest() {
77102
)
78103
)
79104

80-
val response = invocation.invoke(numbersClient)
105+
val response = invocation.invoke(client)
81106
assertNotNull(response)
82107
assertEquals(count, response.count)
83108
val numbers = response.numbers
@@ -105,14 +130,17 @@ class NumbersTest : AbstractTest() {
105130
assertEquals(UUID.fromString(messagesCallbackValue), main.messagesCallbackValue)
106131
assertEquals(voiceCallbackType, CallbackType.fromString(main.voiceCallbackType))
107132
assertEquals(sipUri, main.voiceCallbackValue)
133+
134+
assertThrowsGet(url, invocation)
108135
}
109136

110137
private fun assertAvailableNumbers(params: Map<String, Any>, invocation: Numbers.() -> SearchNumbersResponse) {
111138
val landline = "44800123456"
139+
val url = "/number/search"
112140
mockGet(
113-
expectedUrl = "/number/search",
141+
expectedUrl = url,
114142
expectedQueryParams = params,
115-
authType = AuthType.API_KEY_SECRET_HEADER,
143+
authType = authType,
116144
expectedResponseParams = mapOf(
117145
"count" to count,
118146
"numbers" to listOf(
@@ -132,7 +160,7 @@ class NumbersTest : AbstractTest() {
132160
)
133161
)
134162

135-
val response = invocation.invoke(numbersClient)
163+
val response = invocation.invoke(client)
136164
assertNotNull(response)
137165
assertEquals(count, response.count)
138166
val numbers = response.numbers
@@ -168,41 +196,48 @@ class NumbersTest : AbstractTest() {
168196
assertEquals(Feature.SMS, Feature.fromString(mobileFeatures[0]))
169197
assertEquals(Feature.MMS, Feature.fromString(mobileFeatures[1]))
170198
assertNull(mobile.cost)
199+
200+
assertThrowsGet(url, invocation)
171201
}
172202

173203
@Test
174204
fun `buy number`() {
175-
mockAction("buy")
205+
mockAction(buyEndpoint)
176206
existingNumber.buy()
207+
assertThrowsPost(buyEndpoint) { buy() }
177208
}
178209

179210
@Test
180211
fun `buy number with target api key`() {
181-
mockAction("buy", targetApiKeyMap)
212+
mockAction(buyEndpoint, targetApiKeyMap)
182213
existingNumber.buy(targetApiKey)
214+
assertThrowsPost(buyEndpoint) { buy(targetApiKey) }
183215
}
184216

185217
@Test
186218
fun `cancel number`() {
187-
mockAction("cancel")
219+
mockAction(cancelEndpoint)
188220
existingNumber.cancel()
221+
assertThrowsPost(cancelEndpoint) { cancel() }
189222
}
190223

191224
@Test
192225
fun `cancel number with target api key`() {
193-
mockAction("cancel", targetApiKeyMap)
226+
mockAction(cancelEndpoint, targetApiKeyMap)
194227
existingNumber.cancel(targetApiKey)
228+
assertThrowsPost(cancelEndpoint) { cancel(targetApiKey) }
195229
}
196230

197231
@Test
198232
fun `update no parameters`() {
199-
mockAction("update")
233+
mockAction(updateEndpoint)
200234
existingNumber.update {}
235+
assertThrowsPost(updateEndpoint) { update {} }
201236
}
202237

203238
@Test
204239
fun `update all parameters`() {
205-
mockAction("update", mapOf(
240+
mockAction(updateEndpoint, mapOf(
206241
"app_id" to applicationId,
207242
"moHttpUrl" to moCallbackUrl,
208243
"moSmppSysType" to moSmppSysType,

0 commit comments

Comments
 (0)