Skip to content

Commit c81ccc9

Browse files
authored
Merge pull request #75 from mailtrap/MT-23420-java-delete-sub-account
add sub account deletion
2 parents 551dd6d + 2bff76e commit c81ccc9

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎examples/java/io/mailtrap/examples/organizations/SubAccountsExample.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ public static void main(String[] args) {
2626
final var createdSubAccount = client.organizationsApi().subAccounts()
2727
.createSubAccount(ORGANIZATION_ID, createRequest);
2828
System.out.println(createdSubAccount);
29+
30+
// Permanent – removes all sub account data. Deleting the last sub account deletes the organization.
31+
// Requires organization admin permissions for the organization.
32+
client.organizationsApi().subAccounts().deleteSubAccount(ORGANIZATION_ID, createdSubAccount.getId());
33+
System.out.println("Sub account " + createdSubAccount.getId() + " deleted");
2934
}
3035
}

‎src/main/java/io/mailtrap/api/subaccounts/SubAccounts.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ public interface SubAccounts {
2626
*/
2727
SubAccount createSubAccount(long organizationId, CreateSubAccountRequest request);
2828

29+
/**
30+
* Delete a sub account from the specified organization.
31+
* Requires organization admin permissions.
32+
* The organization must have the Organization & Sub-accounts feature enabled; otherwise the
33+
* call fails with 403. A concurrent deletion of the same sub account fails with 409.
34+
* <p>
35+
* Deletion is permanent and removes all sub account data. Deleting the last sub account
36+
* of an organization deletes the organization as well. A repeated call for the same
37+
* sub account fails with 404. Rate limit – 10 requests per minute per organization.
38+
*
39+
* @param organizationId unique organization ID
40+
* @param subAccountId unique sub account ID
41+
*/
42+
void deleteSubAccount(long organizationId, long subAccountId);
43+
2944
}

‎src/main/java/io/mailtrap/api/subaccounts/SubAccountsImpl.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public SubAccount createSubAccount(final long organizationId, final CreateSubAcc
3434
SubAccount.class
3535
);
3636
}
37+
38+
@Override
39+
public void deleteSubAccount(final long organizationId, final long subAccountId) {
40+
httpClient.delete(
41+
String.format(apiHost + "/api/organizations/%d/sub_accounts/%d", organizationId, subAccountId),
42+
new RequestData(),
43+
Void.class
44+
);
45+
}
3746
}

‎src/test/java/io/mailtrap/api/subaccounts/SubAccountsImplTest.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
import java.util.List;
1616

17+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1718
import static org.junit.jupiter.api.Assertions.assertEquals;
1819
import static org.junit.jupiter.api.Assertions.assertNotNull;
1920

2021
class SubAccountsImplTest extends BaseTest {
2122

2223
private final long organizationId = 1001L;
24+
private final long subAccountId = 12345L;
2325

2426
private SubAccounts api;
2527

@@ -30,7 +32,10 @@ public void init() {
3032
"GET", null, "api/subaccounts/getSubAccountsResponse.json"),
3133

3234
DataMock.build(Constants.GENERAL_HOST + "/api/organizations/" + organizationId + "/sub_accounts",
33-
"POST", "api/subaccounts/createSubAccountRequest.json", "api/subaccounts/createSubAccountResponse.json")
35+
"POST", "api/subaccounts/createSubAccountRequest.json", "api/subaccounts/createSubAccountResponse.json"),
36+
37+
DataMock.build(Constants.GENERAL_HOST + "/api/organizations/" + organizationId + "/sub_accounts/" + subAccountId,
38+
"DELETE", null, null)
3439
));
3540

3641
final MailtrapConfig testConfig = new MailtrapConfig.Builder()
@@ -61,4 +66,9 @@ void test_createSubAccount() {
6166
assertEquals(12347L, response.getId());
6267
assertEquals("New Team Account", response.getName());
6368
}
69+
70+
@Test
71+
void test_deleteSubAccount() {
72+
assertDoesNotThrow(() -> api.deleteSubAccount(organizationId, subAccountId));
73+
}
6474
}

0 commit comments

Comments
 (0)