@@ -17,20 +17,26 @@ package com.vonage.client.kt
17
17
18
18
import com.vonage.client.numbers.*
19
19
import com.vonage.client.numbers.UpdateNumberRequest.CallbackType
20
+ import org.junit.jupiter.api.assertThrows
20
21
import java.util.*
21
22
import kotlin.test.*
22
23
23
24
class NumbersTest : AbstractTest () {
24
- private val numbersClient = vonage.numbers
25
+ private val client = vonage.numbers
26
+ private val authType = AuthType .API_KEY_SECRET_HEADER
25
27
private val country = " GB"
26
28
private val targetApiKey = " 1a2345b7"
27
29
private val moSmppSysType = " inbound"
30
+ private val buyEndpoint = " buy"
31
+ private val cancelEndpoint = " cancel"
32
+ private val updateEndpoint = " update"
28
33
private val featureNames = Feature .entries.map(Feature ::name)
29
34
private val pattern = " 1337*"
30
35
private val count = 1247
31
36
private val size = 25
32
37
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)
34
40
private val baseRequestParams = mapOf (
35
41
" country" to existingNumber.countryCode,
36
42
" msisdn" to existingNumber.msisdn
@@ -40,25 +46,44 @@ class NumbersTest : AbstractTest() {
40
46
" error-code" to " 200" ,
41
47
" error-code-label" to " success"
42
48
)
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
+ }
43
69
44
70
private fun mockAction (endpoint : String , additionalParams : Map <String , String > = mapOf()) {
45
- mockPostQueryParams(
46
- expectedUrl = " /number/$endpoint " ,
71
+ mockPostQueryParams(expectedUrl = " /number/$endpoint " ,
47
72
expectedRequestParams = baseRequestParams + additionalParams,
48
- authType = AuthType .API_KEY_SECRET_HEADER ,
49
- expectedResponseParams = successResponseMap
73
+ authType = authType, expectedResponseParams = successResponseMap
50
74
)
51
75
}
52
76
53
77
private fun assertOwnedNumbers (params : Map <String , Any >, invocation : Numbers .() -> ListNumbersResponse ) {
54
78
val type = Type .MOBILE_LVN
55
79
val voiceCallbackType = CallbackType .SIP
56
80
val messagesCallbackValue = " aaaaaaaa-bbbb-cccc-dddd-0123456789ab"
57
-
81
+ val url = " /account/numbers"
82
+
58
83
mockGet(
59
- expectedUrl = " /account/numbers " ,
84
+ expectedUrl = url ,
60
85
expectedQueryParams = params,
61
- authType = AuthType . API_KEY_SECRET_HEADER ,
86
+ authType = authType ,
62
87
expectedResponseParams = mapOf (
63
88
" count" to count,
64
89
" numbers" to listOf (
@@ -77,7 +102,7 @@ class NumbersTest : AbstractTest() {
77
102
)
78
103
)
79
104
80
- val response = invocation.invoke(numbersClient )
105
+ val response = invocation.invoke(client )
81
106
assertNotNull(response)
82
107
assertEquals(count, response.count)
83
108
val numbers = response.numbers
@@ -105,14 +130,17 @@ class NumbersTest : AbstractTest() {
105
130
assertEquals(UUID .fromString(messagesCallbackValue), main.messagesCallbackValue)
106
131
assertEquals(voiceCallbackType, CallbackType .fromString(main.voiceCallbackType))
107
132
assertEquals(sipUri, main.voiceCallbackValue)
133
+
134
+ assertThrowsGet(url, invocation)
108
135
}
109
136
110
137
private fun assertAvailableNumbers (params : Map <String , Any >, invocation : Numbers .() -> SearchNumbersResponse ) {
111
138
val landline = " 44800123456"
139
+ val url = " /number/search"
112
140
mockGet(
113
- expectedUrl = " /number/search " ,
141
+ expectedUrl = url ,
114
142
expectedQueryParams = params,
115
- authType = AuthType . API_KEY_SECRET_HEADER ,
143
+ authType = authType ,
116
144
expectedResponseParams = mapOf (
117
145
" count" to count,
118
146
" numbers" to listOf (
@@ -132,7 +160,7 @@ class NumbersTest : AbstractTest() {
132
160
)
133
161
)
134
162
135
- val response = invocation.invoke(numbersClient )
163
+ val response = invocation.invoke(client )
136
164
assertNotNull(response)
137
165
assertEquals(count, response.count)
138
166
val numbers = response.numbers
@@ -168,41 +196,48 @@ class NumbersTest : AbstractTest() {
168
196
assertEquals(Feature .SMS , Feature .fromString(mobileFeatures[0 ]))
169
197
assertEquals(Feature .MMS , Feature .fromString(mobileFeatures[1 ]))
170
198
assertNull(mobile.cost)
199
+
200
+ assertThrowsGet(url, invocation)
171
201
}
172
202
173
203
@Test
174
204
fun `buy number` () {
175
- mockAction(" buy " )
205
+ mockAction(buyEndpoint )
176
206
existingNumber.buy()
207
+ assertThrowsPost(buyEndpoint) { buy() }
177
208
}
178
209
179
210
@Test
180
211
fun `buy number with target api key` () {
181
- mockAction(" buy " , targetApiKeyMap)
212
+ mockAction(buyEndpoint , targetApiKeyMap)
182
213
existingNumber.buy(targetApiKey)
214
+ assertThrowsPost(buyEndpoint) { buy(targetApiKey) }
183
215
}
184
216
185
217
@Test
186
218
fun `cancel number` () {
187
- mockAction(" cancel " )
219
+ mockAction(cancelEndpoint )
188
220
existingNumber.cancel()
221
+ assertThrowsPost(cancelEndpoint) { cancel() }
189
222
}
190
223
191
224
@Test
192
225
fun `cancel number with target api key` () {
193
- mockAction(" cancel " , targetApiKeyMap)
226
+ mockAction(cancelEndpoint , targetApiKeyMap)
194
227
existingNumber.cancel(targetApiKey)
228
+ assertThrowsPost(cancelEndpoint) { cancel(targetApiKey) }
195
229
}
196
230
197
231
@Test
198
232
fun `update no parameters` () {
199
- mockAction(" update " )
233
+ mockAction(updateEndpoint )
200
234
existingNumber.update {}
235
+ assertThrowsPost(updateEndpoint) { update {} }
201
236
}
202
237
203
238
@Test
204
239
fun `update all parameters` () {
205
- mockAction(" update " , mapOf (
240
+ mockAction(updateEndpoint , mapOf (
206
241
" app_id" to applicationId,
207
242
" moHttpUrl" to moCallbackUrl,
208
243
" moSmppSysType" to moSmppSysType,
0 commit comments