Skip to content

Commit 55c399b

Browse files
committed
refactor: Add ExistingSubaccount class
1 parent cdf3c5a commit 55c399b

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ class Subaccounts internal constructor(private val client: SubaccountsClient) {
3131
return client.createSubaccount(builder.build())
3232
}
3333

34-
fun getSubaccount(subaccountKey: String): Account = client.getSubaccount(subaccountKey)
34+
fun subaccount(subaccountKey: String): ExistingSubaccount = ExistingSubaccount(subaccountKey)
3535

36-
fun updateSubaccount(subaccountKey: String, name: String? = null,
37-
usePrimaryAccountBalance: Boolean? = null, suspend: Boolean? = null): Account {
38-
val builder = UpdateSubaccountRequest.builder(subaccountKey)
39-
if (name != null) {
40-
builder.name(name)
41-
}
42-
if (usePrimaryAccountBalance != null) {
43-
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
44-
}
45-
if (suspend != null) {
46-
builder.suspended(suspend)
36+
inner class ExistingSubaccount internal constructor(val subaccountKey: String) {
37+
38+
fun get(): Account = client.getSubaccount(subaccountKey)
39+
40+
fun suspended(suspend: Boolean): Account =
41+
client.updateSubaccount(UpdateSubaccountRequest.builder(subaccountKey).suspended(suspend).build())
42+
43+
fun update(name: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
44+
val builder = UpdateSubaccountRequest.builder(subaccountKey).name(name)
45+
if (usePrimaryAccountBalance != null) {
46+
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
47+
}
48+
return client.updateSubaccount(builder.build())
4749
}
48-
return client.updateSubaccount(builder.build())
4950
}
5051

5152
fun listCreditTransfers(startDate: Instant? = null, endDate: Instant? = null,

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import java.time.temporal.ChronoUnit
2424

2525
class SubaccountsTest : AbstractTest() {
2626
private val client = vonage.subaccounts
27+
private val authType = AuthType.API_KEY_SECRET_HEADER
28+
private val existingSubaccount = client.subaccount(apiKey2)
2729
private val baseUrl = "/accounts/$apiKey"
2830
private val subaccountsUrl = "$baseUrl/subaccounts"
2931
private val existingSubUrl = "$subaccountsUrl/$apiKey2"
30-
private val authType = AuthType.API_KEY_SECRET_HEADER
3132
private val name = "Subaccount department A"
3233
private val primaryName = "Primary Account"
3334
private val balance = 93.26
@@ -54,7 +55,7 @@ class SubaccountsTest : AbstractTest() {
5455
private fun assertEqualsSampleSubaccount(parsed: Account) {
5556
assertNotNull(parsed)
5657
assertEquals(secret, parsed.secret)
57-
assertEquals(apiKey2, parsed.apiKey)
58+
assertEquals(existingSubaccount.subaccountKey, parsed.apiKey)
5859
assertEquals(name, parsed.name)
5960
assertEquals(apiKey, parsed.primaryAccountApiKey)
6061
assertEquals(usePrimary, parsed.usePrimaryAccountBalance)
@@ -246,37 +247,39 @@ class SubaccountsTest : AbstractTest() {
246247
expectedUrl = existingSubUrl, authType = authType,
247248
expectedResponseParams = sampleSubaccountMap
248249
)
249-
assertEqualsSampleSubaccount(client.getSubaccount(apiKey2))
250-
assert401ApiResponseException<SubaccountsResponseException>("$subaccountsUrl/$apiKey2", HttpMethod.GET) {
251-
client.getSubaccount(apiKey2)
250+
assertEqualsSampleSubaccount(existingSubaccount.get())
251+
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.GET) {
252+
existingSubaccount.get()
252253
}
253254
}
254255

255256
@Test
256-
fun `update subaccount all parameters`() {
257+
fun `update subaccount`() {
257258
mockPatch(
258259
expectedUrl = existingSubUrl, authType = authType,
259260
expectedRequestParams = mapOf(
260261
"name" to name,
261-
"use_primary_account_balance" to usePrimary,
262-
"suspended" to suspended
262+
"use_primary_account_balance" to usePrimary
263263
),
264264
expectedResponseParams = sampleSubaccountMap
265265
)
266-
assertEqualsSampleSubaccount(client.updateSubaccount(apiKey2, name, usePrimary, suspended))
266+
assertEqualsSampleSubaccount(existingSubaccount.update(name, usePrimary))
267267
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.PATCH) {
268-
client.updateSubaccount(apiKey2, suspend = suspended)
268+
existingSubaccount.update(usePrimaryAccountBalance = usePrimary)
269269
}
270270
}
271271

272272
@Test
273-
fun `update subaccount name only`() {
273+
fun `suspend subaccount`() {
274274
mockPatch(
275275
expectedUrl = existingSubUrl, authType = authType,
276-
expectedRequestParams = mapOf("name" to name),
276+
expectedRequestParams = mapOf("suspended" to suspended),
277277
expectedResponseParams = sampleSubaccountMap
278278
)
279-
assertEqualsSampleSubaccount(client.updateSubaccount(subaccountKey = apiKey2, name = name))
279+
assertEqualsSampleSubaccount(existingSubaccount.suspended(suspended))
280+
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.PATCH) {
281+
existingSubaccount.suspended(!suspended)
282+
}
280283
}
281284

282285
@Test

0 commit comments

Comments
 (0)