Skip to content

Commit

Permalink
refactor: Add ExistingSubaccount class
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 9, 2024
1 parent cdf3c5a commit 55c399b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
27 changes: 14 additions & 13 deletions src/main/kotlin/com/vonage/client/kt/Subaccounts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ class Subaccounts internal constructor(private val client: SubaccountsClient) {
return client.createSubaccount(builder.build())
}

fun getSubaccount(subaccountKey: String): Account = client.getSubaccount(subaccountKey)
fun subaccount(subaccountKey: String): ExistingSubaccount = ExistingSubaccount(subaccountKey)

fun updateSubaccount(subaccountKey: String, name: String? = null,
usePrimaryAccountBalance: Boolean? = null, suspend: Boolean? = null): Account {
val builder = UpdateSubaccountRequest.builder(subaccountKey)
if (name != null) {
builder.name(name)
}
if (usePrimaryAccountBalance != null) {
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
}
if (suspend != null) {
builder.suspended(suspend)
inner class ExistingSubaccount internal constructor(val subaccountKey: String) {

fun get(): Account = client.getSubaccount(subaccountKey)

fun suspended(suspend: Boolean): Account =
client.updateSubaccount(UpdateSubaccountRequest.builder(subaccountKey).suspended(suspend).build())

fun update(name: String? = null, usePrimaryAccountBalance: Boolean? = null): Account {
val builder = UpdateSubaccountRequest.builder(subaccountKey).name(name)
if (usePrimaryAccountBalance != null) {
builder.usePrimaryAccountBalance(usePrimaryAccountBalance)
}
return client.updateSubaccount(builder.build())
}
return client.updateSubaccount(builder.build())
}

fun listCreditTransfers(startDate: Instant? = null, endDate: Instant? = null,
Expand Down
29 changes: 16 additions & 13 deletions src/test/kotlin/com/vonage/client/kt/SubaccountsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import java.time.temporal.ChronoUnit

class SubaccountsTest : AbstractTest() {
private val client = vonage.subaccounts
private val authType = AuthType.API_KEY_SECRET_HEADER
private val existingSubaccount = client.subaccount(apiKey2)
private val baseUrl = "/accounts/$apiKey"
private val subaccountsUrl = "$baseUrl/subaccounts"
private val existingSubUrl = "$subaccountsUrl/$apiKey2"
private val authType = AuthType.API_KEY_SECRET_HEADER
private val name = "Subaccount department A"
private val primaryName = "Primary Account"
private val balance = 93.26
Expand All @@ -54,7 +55,7 @@ class SubaccountsTest : AbstractTest() {
private fun assertEqualsSampleSubaccount(parsed: Account) {
assertNotNull(parsed)
assertEquals(secret, parsed.secret)
assertEquals(apiKey2, parsed.apiKey)
assertEquals(existingSubaccount.subaccountKey, parsed.apiKey)
assertEquals(name, parsed.name)
assertEquals(apiKey, parsed.primaryAccountApiKey)
assertEquals(usePrimary, parsed.usePrimaryAccountBalance)
Expand Down Expand Up @@ -246,37 +247,39 @@ class SubaccountsTest : AbstractTest() {
expectedUrl = existingSubUrl, authType = authType,
expectedResponseParams = sampleSubaccountMap
)
assertEqualsSampleSubaccount(client.getSubaccount(apiKey2))
assert401ApiResponseException<SubaccountsResponseException>("$subaccountsUrl/$apiKey2", HttpMethod.GET) {
client.getSubaccount(apiKey2)
assertEqualsSampleSubaccount(existingSubaccount.get())
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.GET) {
existingSubaccount.get()
}
}

@Test
fun `update subaccount all parameters`() {
fun `update subaccount`() {
mockPatch(
expectedUrl = existingSubUrl, authType = authType,
expectedRequestParams = mapOf(
"name" to name,
"use_primary_account_balance" to usePrimary,
"suspended" to suspended
"use_primary_account_balance" to usePrimary
),
expectedResponseParams = sampleSubaccountMap
)
assertEqualsSampleSubaccount(client.updateSubaccount(apiKey2, name, usePrimary, suspended))
assertEqualsSampleSubaccount(existingSubaccount.update(name, usePrimary))
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.PATCH) {
client.updateSubaccount(apiKey2, suspend = suspended)
existingSubaccount.update(usePrimaryAccountBalance = usePrimary)
}
}

@Test
fun `update subaccount name only`() {
fun `suspend subaccount`() {
mockPatch(
expectedUrl = existingSubUrl, authType = authType,
expectedRequestParams = mapOf("name" to name),
expectedRequestParams = mapOf("suspended" to suspended),
expectedResponseParams = sampleSubaccountMap
)
assertEqualsSampleSubaccount(client.updateSubaccount(subaccountKey = apiKey2, name = name))
assertEqualsSampleSubaccount(existingSubaccount.suspended(suspended))
assert401ApiResponseException<SubaccountsResponseException>(existingSubUrl, HttpMethod.PATCH) {
existingSubaccount.suspended(!suspended)
}
}

@Test
Expand Down

0 comments on commit 55c399b

Please sign in to comment.