From e528447951c8f19cc690e5ded657ff5595e777a6 Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 10:27:53 +0200 Subject: [PATCH 1/6] partners api implemented --- .../com/messagebird/MessageBirdClient.java | 117 +++++++++++++----- .../com/messagebird/objects/AccessKey.java | 31 +++++ .../objects/ChildAccountCreateResponse.java | 51 ++++++++ .../objects/ChildAccountDetailedResponse.java | 13 ++ .../objects/ChildAccountResponse.java | 22 ++++ .../objects/PartnerAccountsResponse.java | 15 +++ .../messagebird/MessageBirdClientTest.java | 82 ++++++++++++ .../test/java/com/messagebird/TestUtil.java | 39 ++++++ .../main/java/ExampleCreateChildAccount.java | 26 ++++ .../main/java/ExampleDeleteChildAccount.java | 27 ++++ .../main/java/ExampleGetChildAccountById.java | 26 ++++ .../main/java/ExampleGetChildAccounts.java | 24 ++++ .../main/java/ExampleUpdateChildAccount.java | 25 ++++ 13 files changed, 468 insertions(+), 30 deletions(-) create mode 100644 api/src/main/java/com/messagebird/objects/AccessKey.java create mode 100644 api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java create mode 100644 api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java create mode 100644 api/src/main/java/com/messagebird/objects/ChildAccountResponse.java create mode 100644 api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java create mode 100644 examples/src/main/java/ExampleCreateChildAccount.java create mode 100644 examples/src/main/java/ExampleDeleteChildAccount.java create mode 100644 examples/src/main/java/ExampleGetChildAccountById.java create mode 100644 examples/src/main/java/ExampleGetChildAccounts.java create mode 100644 examples/src/main/java/ExampleUpdateChildAccount.java diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 0bff2580..378cf334 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -3,35 +3,7 @@ import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.NotFoundException; import com.messagebird.exceptions.UnauthorizedException; -import com.messagebird.objects.Balance; -import com.messagebird.objects.Contact; -import com.messagebird.objects.ContactList; -import com.messagebird.objects.ContactRequest; -import com.messagebird.objects.ErrorReport; -import com.messagebird.objects.FileUploadResponse; -import com.messagebird.objects.Group; -import com.messagebird.objects.GroupList; -import com.messagebird.objects.GroupRequest; -import com.messagebird.objects.Hlr; -import com.messagebird.objects.Lookup; -import com.messagebird.objects.LookupHlr; -import com.messagebird.objects.Message; -import com.messagebird.objects.MessageList; -import com.messagebird.objects.MessageResponse; -import com.messagebird.objects.MsgType; -import com.messagebird.objects.PagedPaging; -import com.messagebird.objects.PhoneNumbersLookup; -import com.messagebird.objects.PhoneNumbersResponse; -import com.messagebird.objects.PurchasedNumber; -import com.messagebird.objects.PurchasedNumberCreatedResponse; -import com.messagebird.objects.PurchasedNumbersResponse; -import com.messagebird.objects.PurchasedNumbersFilter; -import com.messagebird.objects.Verify; -import com.messagebird.objects.VerifyMessage; -import com.messagebird.objects.VerifyRequest; -import com.messagebird.objects.VoiceMessage; -import com.messagebird.objects.VoiceMessageList; -import com.messagebird.objects.VoiceMessageResponse; +import com.messagebird.objects.*; import com.messagebird.objects.conversations.Conversation; import com.messagebird.objects.conversations.ConversationList; import com.messagebird.objects.conversations.ConversationMessage; @@ -103,6 +75,7 @@ public class MessageBirdClient { static final String NUMBERS_CALLS_BASE_URL = "https://numbers.messagebird.com/v1"; static final String MESSAGING_BASE_URL = "https://messaging.messagebird.com/v1"; private static String[] supportedLanguages = {"de-DE", "en-AU", "en-UK", "en-US", "es-ES", "es-LA", "fr-FR", "it-IT", "nl-NL", "pt-BR"}; + static final String PARTNER_ACCOUNTS_BASE_URL = "https://partner-accounts.messagebird.com"; private static final String BALANCEPATH = "/balance"; private static final String CONTACTPATH = "/contacts"; @@ -496,7 +469,7 @@ Verify getVerifyObject(String id) throws NotFoundException, GeneralException, Un } /** - * @param id id is for the email message part of a verify object + * @param messageId is for the email message part of a verify object * @return Verify object * @throws NotFoundException if id is not found * @throws UnauthorizedException if client is unauthorized @@ -1848,4 +1821,88 @@ public String downloadFile(String id, String filename, String basePath) throws G final String url = String.format("%s%s/%s", MESSAGING_BASE_URL, FILES_PATH, id); return messageBirdService.getBinaryData(url, basePath, filename); } + + /** + * Function to create a child account + * + * @param name of child account to create + * @return ChildAccountResponse created + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + */ + public ChildAccountCreateResponse createChildAccount(String name) throws UnauthorizedException, GeneralException { + if (name == null) { + throw new IllegalArgumentException("Name must be specified."); + } + + String url = String.format("%s%s", PARTNER_ACCOUNTS_BASE_URL, "/child-accounts"); + return messageBirdService.sendPayLoad(url, name, ChildAccountCreateResponse.class); + } + + /** + * Function to update a child account + * + * @param id of child account to update + * @return ChildAccountResponse created + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + */ + public ChildAccountResponse updateChildAccount(String name, String id) throws UnauthorizedException, GeneralException { + if (name == null) { + throw new IllegalArgumentException("Name must be specified."); + } + + if (id == null) { + throw new IllegalArgumentException("Child account id must be specified."); + } + + final String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, id); + return messageBirdService.sendPayLoad("PATCH", url, name, ChildAccountResponse.class); + } + + /** + * Function to get a child account + * + * @param id of child account to update + * @return ChildAccountResponse created + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws NotFoundException if id is not found + */ + public ChildAccountDetailedResponse getChildAccountById(String id) throws UnauthorizedException, GeneralException, NotFoundException { + if (id == null) { + throw new IllegalArgumentException("Child account id must be specified."); + } + + return messageBirdService.requestByID(PARTNER_ACCOUNTS_BASE_URL, id, ChildAccountDetailedResponse.class); + } + + /** + * Function to get a child account + * + * @return ChildAccountResponse created + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + */ + public PartnerAccountsResponse getChildAccounts(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException { + verifyOffsetAndLimit(offset, limit); + return messageBirdService.requestList(PARTNER_ACCOUNTS_BASE_URL, offset, limit, PartnerAccountsResponse.class); + } + + /** + * Function to delete a child account + * + * @param id of child account to delete + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception + * @throws NotFoundException if id is not found + */ + public void deleteChildAccount(String id) throws UnauthorizedException, GeneralException, NotFoundException { + if (id == null) { + throw new IllegalArgumentException("Child account id must be specified."); + } + + String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, id); + messageBirdService.deleteByID(url, id); + } } diff --git a/api/src/main/java/com/messagebird/objects/AccessKey.java b/api/src/main/java/com/messagebird/objects/AccessKey.java new file mode 100644 index 00000000..14505dc7 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/AccessKey.java @@ -0,0 +1,31 @@ +package com.messagebird.objects; + +public class AccessKey { + private String id; + private String key; + private String mod; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getMod() { + return mod; + } + + public void setMod(String mod) { + this.mod = mod; + } +} diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java new file mode 100644 index 00000000..768fd71f --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java @@ -0,0 +1,51 @@ +package com.messagebird.objects; + +import java.util.List; + +public class ChildAccountCreateResponse { + private String id; + private String name; + private List accessKeys; + private String signingKey; + private String invoiceAggregation; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getAccessKeys() { + return accessKeys; + } + + public void setAccessKeys(List accessKeys) { + this.accessKeys = accessKeys; + } + + public String getSigningKey() { + return signingKey; + } + + public void setSigningKey(String signingKey) { + this.signingKey = signingKey; + } + + public String getInvoiceAggregation() { + return invoiceAggregation; + } + + public void setInvoiceAggregation(String invoiceAggregation) { + this.invoiceAggregation = invoiceAggregation; + } +} diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java new file mode 100644 index 00000000..d3aac825 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java @@ -0,0 +1,13 @@ +package com.messagebird.objects; + +public class ChildAccountDetailedResponse extends ChildAccountResponse{ + private String invoiceAggregation; + + public String getInvoiceAggregation() { + return invoiceAggregation; + } + + public void setInvoiceAggregation(String invoiceAggregation) { + this.invoiceAggregation = invoiceAggregation; + } +} diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java new file mode 100644 index 00000000..2e4853aa --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java @@ -0,0 +1,22 @@ +package com.messagebird.objects; + +public class ChildAccountResponse { + private String id; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java b/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java new file mode 100644 index 00000000..410cd360 --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java @@ -0,0 +1,15 @@ +package com.messagebird.objects; + +import java.util.List; + +public class PartnerAccountsResponse { + private List childAccountResponses; + + public List getChildAccountResponses() { + return childAccountResponses; + } + + public void setChildAccountResponses(List childAccountResponses) { + this.childAccountResponses = childAccountResponses; + } +} diff --git a/api/src/test/java/com/messagebird/MessageBirdClientTest.java b/api/src/test/java/com/messagebird/MessageBirdClientTest.java index 956b177b..457cd94e 100644 --- a/api/src/test/java/com/messagebird/MessageBirdClientTest.java +++ b/api/src/test/java/com/messagebird/MessageBirdClientTest.java @@ -19,6 +19,7 @@ import java.util.Map; import static com.messagebird.MessageBirdClient.*; +import static com.messagebird.TestUtil.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals; @@ -1042,4 +1043,85 @@ public void testDownloadFileWithNullBasePath() throws GeneralException, Unauthor String url = MESSAGING_BASE_URL + FILES_PATH + "/" + id; verify(messageBirdServiceMock, times(1)).getBinaryData(url, null, filename); } + + @Test + public void testCreateChildAccounts() throws GeneralException, UnauthorizedException { + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + ChildAccountCreateResponse childAccountCreateResponse = createChildAccountCreateResponse(); + when(messageBirdServiceMock.sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , "name", ChildAccountCreateResponse.class)) + .thenReturn(childAccountCreateResponse); + final ChildAccountCreateResponse response = messageBirdClientInjectMock.createChildAccount("name"); + + verify(messageBirdServiceMock, times(1)) + .sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , "name", ChildAccountCreateResponse.class); + assertNotNull(response); + assertEquals(response.getId(), childAccountCreateResponse.getId()); + assertEquals(response.getName(), childAccountCreateResponse.getName()); + assertEquals(response.getInvoiceAggregation(), childAccountCreateResponse.getInvoiceAggregation()); + assertEquals(response.getSigningKey(), childAccountCreateResponse.getSigningKey()); + assertEquals(response.getAccessKeys().get(0).getId(), childAccountCreateResponse.getAccessKeys().get(0).getId()); + assertEquals(response.getAccessKeys().get(0).getKey(), childAccountCreateResponse.getAccessKeys().get(0).getKey()); + assertEquals(response.getAccessKeys().get(0).getMod(), childAccountCreateResponse.getAccessKeys().get(0).getMod()); + } + + @Test + public void testGetChildAccount() throws GeneralException, UnauthorizedException, NotFoundException { + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + ChildAccountDetailedResponse childAccountDetailedResponse = createChildAccountDetailedResponse(); + when(messageBirdServiceMock.requestByID(PARTNER_ACCOUNTS_BASE_URL, "ANY_ID", ChildAccountDetailedResponse.class)) + .thenReturn(childAccountDetailedResponse); + ChildAccountDetailedResponse response = messageBirdClientInjectMock.getChildAccountById("ANY_ID"); + + verify(messageBirdServiceMock, times(1)) + .requestByID(PARTNER_ACCOUNTS_BASE_URL, "ANY_ID", ChildAccountDetailedResponse.class); + assertNotNull(response); + assertEquals(response.getId(), childAccountDetailedResponse.getId()); + assertEquals(response.getName(), childAccountDetailedResponse.getName()); + assertEquals(response.getInvoiceAggregation(), childAccountDetailedResponse.getInvoiceAggregation()); + } + + @Test + public void testGetChildAccounts() throws GeneralException, UnauthorizedException { + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + PartnerAccountsResponse partnerAccountsResponse = createPartnerAccountsResponse(); + when(messageBirdServiceMock.requestList(PARTNER_ACCOUNTS_BASE_URL, 1, 10, PartnerAccountsResponse.class)) + .thenReturn(partnerAccountsResponse); + + PartnerAccountsResponse response = messageBirdClientInjectMock.getChildAccounts(1, 10); + + verify(messageBirdServiceMock, times(1)) + .requestList(PARTNER_ACCOUNTS_BASE_URL, 1, 10, PartnerAccountsResponse.class); + assertNotNull(response); + assertEquals(response.getChildAccountResponses().get(0).getId(), partnerAccountsResponse.getChildAccountResponses().get(0).getId()); + assertEquals(response.getChildAccountResponses().get(0).getName(), partnerAccountsResponse.getChildAccountResponses().get(0).getName()); + } + + @Test + public void testUpdateChildAccount() throws GeneralException, UnauthorizedException { + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + ChildAccountResponse childAccountResponse = createChildAccountResponse(); + final String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, "ANY_ID"); + when(messageBirdServiceMock.sendPayLoad("PATCH", url, "ANY_NAME", ChildAccountResponse.class)) + .thenReturn(childAccountResponse); + ChildAccountResponse response = messageBirdClientInjectMock.updateChildAccount("ANY_NAME", "ANY_ID"); + + verify(messageBirdServiceMock, times(1)) + .sendPayLoad("PATCH", url, "ANY_NAME", ChildAccountResponse.class); + assertNotNull(response); + assertEquals(response.getId(), childAccountResponse.getId()); + assertEquals(response.getName(), childAccountResponse.getName()); + } + + @Test + public void testDeleteChildAccount() throws GeneralException, UnauthorizedException, NotFoundException { + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); + MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); + String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, "ANY_ID"); + doNothing().when(messageBirdServiceMock).deleteByID(url, "ANY_ID"); + messageBirdClientInjectMock.deleteChildAccount("id"); + } } diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index 5a854b45..fef25193 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -240,4 +240,43 @@ static ConversationWebhookCreateRequest createConversationWebhookRequest() { ) ); } + + public static ChildAccountCreateResponse createChildAccountCreateResponse() { + final AccessKey accessKey = new AccessKey(); + accessKey.setId("ANY_ID"); + accessKey.setKey("ANY_KEY"); + accessKey.setMod("ANY_MOD"); + + final ChildAccountCreateResponse childAccountCreateResponse = new ChildAccountCreateResponse(); + childAccountCreateResponse.setId("ANY_ID"); + childAccountCreateResponse.setName("ANY_NAME"); + childAccountCreateResponse.setAccessKeys(Collections.singletonList(accessKey)); + childAccountCreateResponse.setInvoiceAggregation("ANY_INVOICE_AGGREGATION"); + childAccountCreateResponse.setSigningKey("ANY_SIGNING_KEY"); + + return childAccountCreateResponse; + } + + public static ChildAccountDetailedResponse createChildAccountDetailedResponse(){ + final ChildAccountDetailedResponse childAccountDetailedResponse = new ChildAccountDetailedResponse(); + childAccountDetailedResponse.setId("ANY_ID"); + childAccountDetailedResponse.setName("ANY_NAME"); + childAccountDetailedResponse.setInvoiceAggregation("ANY_INVOICE_AGGREGATION"); + return childAccountDetailedResponse; + } + + public static ChildAccountResponse createChildAccountResponse(){ + final ChildAccountResponse childAccountResponse = new ChildAccountResponse(); + childAccountResponse.setId("ANY_ID"); + childAccountResponse.setName("ANY_NAME"); + return childAccountResponse; + } + + public static PartnerAccountsResponse createPartnerAccountsResponse(){ + final ChildAccountResponse childAccountResponse = createChildAccountResponse(); + final PartnerAccountsResponse partnerAccountsResponse = new PartnerAccountsResponse(); + partnerAccountsResponse.setChildAccountResponses(Collections.singletonList(childAccountResponse)); + return partnerAccountsResponse; + } + } diff --git a/examples/src/main/java/ExampleCreateChildAccount.java b/examples/src/main/java/ExampleCreateChildAccount.java new file mode 100644 index 00000000..e4ec5df7 --- /dev/null +++ b/examples/src/main/java/ExampleCreateChildAccount.java @@ -0,0 +1,26 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; + +public class ExampleCreateChildAccount { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key and name of child account arguments"); + return; + } + + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + System.out.println("Creating a child account of partner accounts"); + messageBirdClient.createChildAccount(args[1]); + System.out.println("Child account is created"); + + } catch (GeneralException | UnauthorizedException exception) { + exception.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleDeleteChildAccount.java b/examples/src/main/java/ExampleDeleteChildAccount.java new file mode 100644 index 00000000..2724a59a --- /dev/null +++ b/examples/src/main/java/ExampleDeleteChildAccount.java @@ -0,0 +1,27 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; + +public class ExampleDeleteChildAccount { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key and id of child account arguments"); + return; + } + + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + System.out.println("Deleting a child account of partner accounts"); + messageBirdClient.deleteChildAccount(args[1]); + System.out.println("Child account is deleted"); + + } catch (GeneralException | UnauthorizedException | NotFoundException exception) { + exception.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleGetChildAccountById.java b/examples/src/main/java/ExampleGetChildAccountById.java new file mode 100644 index 00000000..ae1acbc9 --- /dev/null +++ b/examples/src/main/java/ExampleGetChildAccountById.java @@ -0,0 +1,26 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.NotFoundException; +import com.messagebird.exceptions.UnauthorizedException; + +public class ExampleGetChildAccountById { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key and id of child account arguments"); + return; + } + + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + System.out.println("Get a child account by id"); + messageBirdClient.getChildAccountById(args[1]); + + } catch (GeneralException | UnauthorizedException | NotFoundException exception) { + exception.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleGetChildAccounts.java b/examples/src/main/java/ExampleGetChildAccounts.java new file mode 100644 index 00000000..5d7be592 --- /dev/null +++ b/examples/src/main/java/ExampleGetChildAccounts.java @@ -0,0 +1,24 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; + +public class ExampleGetChildAccounts { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key, offset, limit arguments"); + return; + } + + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + System.out.println("Get a child accounts of partner account"); + messageBirdClient.getChildAccounts(Integer.valueOf(args[1]), Integer.valueOf(args[2])); + } catch (GeneralException | UnauthorizedException exception) { + exception.printStackTrace(); + } + } +} diff --git a/examples/src/main/java/ExampleUpdateChildAccount.java b/examples/src/main/java/ExampleUpdateChildAccount.java new file mode 100644 index 00000000..e90b9b51 --- /dev/null +++ b/examples/src/main/java/ExampleUpdateChildAccount.java @@ -0,0 +1,25 @@ +import com.messagebird.MessageBirdClient; +import com.messagebird.MessageBirdService; +import com.messagebird.MessageBirdServiceImpl; +import com.messagebird.exceptions.GeneralException; +import com.messagebird.exceptions.UnauthorizedException; + +public class ExampleUpdateChildAccount { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Please specify your access key, name of child account, id of child account parameters"); + return; + } + + final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]); + final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr); + + try { + System.out.println("Updating a child account"); + messageBirdClient.updateChildAccount(args[1], args[2]); + System.out.println("Child account is updated"); + } catch (GeneralException | UnauthorizedException exception) { + exception.printStackTrace(); + } + } +} From 7dfcb052dd3366dddbc170b3425347dbb41d4a65 Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 11:05:20 +0200 Subject: [PATCH 2/6] updated after review --- .../objects/ChildAccountCreateResponse.java | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java index 768fd71f..57aff09e 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java @@ -2,29 +2,11 @@ import java.util.List; -public class ChildAccountCreateResponse { - private String id; - private String name; +public class ChildAccountCreateResponse extends ChildAccountResponse{ private List accessKeys; private String signingKey; private String invoiceAggregation; - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - public List getAccessKeys() { return accessKeys; } From 3895bc1cb85d281f02d67ed6458eaf235ecb072f Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 14:11:57 +0200 Subject: [PATCH 3/6] minor things are updated --- .../java/com/messagebird/MessageBirdClient.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 7a817c3a..7cdfbc68 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -678,8 +678,8 @@ public VoiceCallFlowResponse sendVoiceCallFlow(final VoiceCallFlowRequest voiceC * @param id String * @param voiceCallFlowRequest VoiceCallFlowRequest * @return VoiceCallFlowResponse - * @throws UnauthorizedException - * @throws GeneralException + * @throws UnauthorizedException if client is unauthorized + * @throws GeneralException general exception */ public VoiceCallFlowResponse updateVoiceCallFlow(String id, VoiceCallFlowRequest voiceCallFlowRequest) throws UnauthorizedException, GeneralException { @@ -1893,7 +1893,7 @@ public TemplateList listWhatsAppTemplates() throws UnauthorizedException, Genera * Retrieves the template of an existing template name. * * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable - * @return {@code List} template list + * @return {@code List} template list * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception * @throws NotFoundException if template name is not found @@ -1920,7 +1920,7 @@ public List getWhatsAppTemplatesBy(final String templateName) * @param templateName A name as returned by getWhatsAppTemplateBy in the name variable * @param language A language code as returned by getWhatsAppTemplateBy in the language variable * - * @return {@code WhatsAppTemplateResponse} template list + * @return {@code TemplateResponse} template list * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception * @throws NotFoundException if template name and language are not found @@ -2000,7 +2000,7 @@ public void deleteTemplatesBy(final String templateName, final String language) * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception */ - public ChildAccountCreateResponse createChildAccount(String name) throws UnauthorizedException, GeneralException { + public ChildAccountCreateResponse createChildAccount(final String name) throws UnauthorizedException, GeneralException { if (name == null) { throw new IllegalArgumentException("Name must be specified."); } @@ -2017,7 +2017,7 @@ public ChildAccountCreateResponse createChildAccount(String name) throws Unautho * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception */ - public ChildAccountResponse updateChildAccount(String name, String id) throws UnauthorizedException, GeneralException { + public ChildAccountResponse updateChildAccount(final String name, final String id) throws UnauthorizedException, GeneralException { if (name == null) { throw new IllegalArgumentException("Name must be specified."); } @@ -2039,7 +2039,7 @@ public ChildAccountResponse updateChildAccount(String name, String id) throws Un * @throws GeneralException general exception * @throws NotFoundException if id is not found */ - public ChildAccountDetailedResponse getChildAccountById(String id) throws UnauthorizedException, GeneralException, NotFoundException { + public ChildAccountDetailedResponse getChildAccountById(final String id) throws UnauthorizedException, GeneralException, NotFoundException { if (id == null) { throw new IllegalArgumentException("Child account id must be specified."); } @@ -2067,7 +2067,7 @@ public PartnerAccountsResponse getChildAccounts(final Integer offset, final Inte * @throws GeneralException general exception * @throws NotFoundException if id is not found */ - public void deleteChildAccount(String id) throws UnauthorizedException, GeneralException, NotFoundException { + public void deleteChildAccount(final String id) throws UnauthorizedException, GeneralException, NotFoundException { if (id == null) { throw new IllegalArgumentException("Child account id must be specified."); } From 45952f878658869158fd0302b8ff8cb5c45f2815 Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 14:38:59 +0200 Subject: [PATCH 4/6] added toString methods --- .../main/java/com/messagebird/objects/AccessKey.java | 9 +++++++++ .../objects/ChildAccountCreateResponse.java | 11 +++++++++++ .../objects/ChildAccountDetailedResponse.java | 9 +++++++++ .../com/messagebird/objects/ChildAccountResponse.java | 8 ++++++++ examples/src/main/java/ExampleCreateChildAccount.java | 5 +++-- 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/messagebird/objects/AccessKey.java b/api/src/main/java/com/messagebird/objects/AccessKey.java index 14505dc7..65ee9fd7 100644 --- a/api/src/main/java/com/messagebird/objects/AccessKey.java +++ b/api/src/main/java/com/messagebird/objects/AccessKey.java @@ -28,4 +28,13 @@ public String getMod() { public void setMod(String mod) { this.mod = mod; } + + @Override + public String toString() { + return "AccessKey{" + + "id='" + id + '\'' + + ", key='" + key + '\'' + + ", mod='" + mod + '\'' + + '}'; + } } diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java index 57aff09e..dcf201b6 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountCreateResponse.java @@ -30,4 +30,15 @@ public String getInvoiceAggregation() { public void setInvoiceAggregation(String invoiceAggregation) { this.invoiceAggregation = invoiceAggregation; } + + @Override + public String toString() { + return "ChildAccountCreateResponse{" + + "id='" + getId() + '\'' + + ", name='" + getName() + '\'' + + ", accessKeys=" + accessKeys + '\'' + + ", signingKey='" + signingKey + '\'' + + ", invoiceAggregation='" + invoiceAggregation + '\'' + + '}'; + } } diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java index d3aac825..cdf23d4a 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java @@ -10,4 +10,13 @@ public String getInvoiceAggregation() { public void setInvoiceAggregation(String invoiceAggregation) { this.invoiceAggregation = invoiceAggregation; } + + @Override + public String toString() { + return "ChildAccountDetailedResponse{" + + "id='" + getId() + '\'' + + ", name='" + getName() + '\'' + + "invoiceAggregation='" + invoiceAggregation + '\'' + + '}'; + } } diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java index 2e4853aa..637a258f 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java @@ -19,4 +19,12 @@ public String getName() { public void setName(String name) { this.name = name; } + + @Override + public String toString() { + return "ChildAccountResponse{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + '}'; + } } diff --git a/examples/src/main/java/ExampleCreateChildAccount.java b/examples/src/main/java/ExampleCreateChildAccount.java index e4ec5df7..037f90dd 100644 --- a/examples/src/main/java/ExampleCreateChildAccount.java +++ b/examples/src/main/java/ExampleCreateChildAccount.java @@ -3,6 +3,7 @@ import com.messagebird.MessageBirdServiceImpl; import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.ChildAccountCreateResponse; public class ExampleCreateChildAccount { public static void main(String[] args) { @@ -16,8 +17,8 @@ public static void main(String[] args) { try { System.out.println("Creating a child account of partner accounts"); - messageBirdClient.createChildAccount(args[1]); - System.out.println("Child account is created"); + ChildAccountCreateResponse childAccount = messageBirdClient.createChildAccount(args[1]); + System.out.println("Child account is created: " + childAccount.toString()); } catch (GeneralException | UnauthorizedException exception) { exception.printStackTrace(); From 6cc657f88cb81e62a63967710057e24e61242ea2 Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 16:56:59 +0200 Subject: [PATCH 5/6] updated examples --- .../com/messagebird/MessageBirdClient.java | 22 +++---- .../objects/ChildAccountDetailedResponse.java | 2 +- .../objects/ChildAccountRequest.java | 13 +++++ .../objects/ChildAccountResponse.java | 6 +- .../objects/PartnerAccountsResponse.java | 12 +--- .../messagebird/MessageBirdClientTest.java | 57 ++++++++++--------- .../test/java/com/messagebird/TestUtil.java | 2 +- .../main/java/ExampleCreateChildAccount.java | 5 +- .../main/java/ExampleGetChildAccountById.java | 5 +- .../main/java/ExampleGetChildAccounts.java | 4 +- .../main/java/ExampleUpdateChildAccount.java | 5 +- 11 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 api/src/main/java/com/messagebird/objects/ChildAccountRequest.java diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 7cdfbc68..5f8d489f 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -47,7 +47,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.HashSet; /** * Message bird general client @@ -1995,18 +1994,18 @@ public void deleteTemplatesBy(final String templateName, final String language) /** * Function to create a child account * - * @param name of child account to create + * @param childAccountRequest of child account to create * @return ChildAccountResponse created * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception */ - public ChildAccountCreateResponse createChildAccount(final String name) throws UnauthorizedException, GeneralException { - if (name == null) { + public ChildAccountCreateResponse createChildAccount(final ChildAccountRequest childAccountRequest) throws UnauthorizedException, GeneralException { + if (childAccountRequest.getName() == null || childAccountRequest.getName().isEmpty()) { throw new IllegalArgumentException("Name must be specified."); } String url = String.format("%s%s", PARTNER_ACCOUNTS_BASE_URL, "/child-accounts"); - return messageBirdService.sendPayLoad(url, name, ChildAccountCreateResponse.class); + return messageBirdService.sendPayLoad(url, childAccountRequest, ChildAccountCreateResponse.class); } /** @@ -2025,9 +2024,10 @@ public ChildAccountResponse updateChildAccount(final String name, final String i if (id == null) { throw new IllegalArgumentException("Child account id must be specified."); } - + final ChildAccountRequest childAccountRequest = new ChildAccountRequest(); + childAccountRequest.setName(name); final String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, id); - return messageBirdService.sendPayLoad("PATCH", url, name, ChildAccountResponse.class); + return messageBirdService.sendPayLoad("PATCH", url, childAccountRequest, ChildAccountResponse.class); } /** @@ -2043,8 +2043,7 @@ public ChildAccountDetailedResponse getChildAccountById(final String id) throws if (id == null) { throw new IllegalArgumentException("Child account id must be specified."); } - - return messageBirdService.requestByID(PARTNER_ACCOUNTS_BASE_URL, id, ChildAccountDetailedResponse.class); + return messageBirdService.requestByID(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", id, ChildAccountDetailedResponse.class); } /** @@ -2056,7 +2055,7 @@ public ChildAccountDetailedResponse getChildAccountById(final String id) throws */ public PartnerAccountsResponse getChildAccounts(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException { verifyOffsetAndLimit(offset, limit); - return messageBirdService.requestList(PARTNER_ACCOUNTS_BASE_URL, offset, limit, PartnerAccountsResponse.class); + return messageBirdService.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", offset, limit, PartnerAccountsResponse.class); } /** @@ -2072,7 +2071,8 @@ public void deleteChildAccount(final String id) throws UnauthorizedException, Ge throw new IllegalArgumentException("Child account id must be specified."); } - String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, id); + String url = String.format("%s/child-accounts", PARTNER_ACCOUNTS_BASE_URL); + System.out.println("url: " + url); messageBirdService.deleteByID(url, id); } } diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java index cdf23d4a..e39d5969 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountDetailedResponse.java @@ -16,7 +16,7 @@ public String toString() { return "ChildAccountDetailedResponse{" + "id='" + getId() + '\'' + ", name='" + getName() + '\'' + - "invoiceAggregation='" + invoiceAggregation + '\'' + + ", invoiceAggregation='" + invoiceAggregation + '\'' + '}'; } } diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountRequest.java b/api/src/main/java/com/messagebird/objects/ChildAccountRequest.java new file mode 100644 index 00000000..aa0e9e5f --- /dev/null +++ b/api/src/main/java/com/messagebird/objects/ChildAccountRequest.java @@ -0,0 +1,13 @@ +package com.messagebird.objects; + +public class ChildAccountRequest { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java index 637a258f..de232f05 100644 --- a/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java +++ b/api/src/main/java/com/messagebird/objects/ChildAccountResponse.java @@ -1,6 +1,10 @@ package com.messagebird.objects; -public class ChildAccountResponse { +import java.io.Serializable; + +public class ChildAccountResponse implements Serializable { + private static final long serialVersionUID = -8605510461438669942L; + private String id; private String name; diff --git a/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java b/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java index 410cd360..deb82067 100644 --- a/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java +++ b/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java @@ -1,15 +1,5 @@ package com.messagebird.objects; -import java.util.List; +public class PartnerAccountsResponse extends ListBase{ -public class PartnerAccountsResponse { - private List childAccountResponses; - - public List getChildAccountResponses() { - return childAccountResponses; - } - - public void setChildAccountResponses(List childAccountResponses) { - this.childAccountResponses = childAccountResponses; - } } diff --git a/api/src/test/java/com/messagebird/MessageBirdClientTest.java b/api/src/test/java/com/messagebird/MessageBirdClientTest.java index b523a2a4..c948154e 100644 --- a/api/src/test/java/com/messagebird/MessageBirdClientTest.java +++ b/api/src/test/java/com/messagebird/MessageBirdClientTest.java @@ -134,7 +134,7 @@ public void testListScheduledMessages() throws Exception { public void testListScheduledMessagesWrongFilter() throws Exception { Map filters = new LinkedHashMap<>(); filters.put("does not exist", null); - + messageBirdClient.listMessagesFiltered(null, null, filters); } @@ -816,7 +816,7 @@ public void testListNumbersForPurchase() throws IllegalArgumentException, Genera final PhoneNumbersResponse response = messageBirdClientMock.listNumbersForPurchase("NL"); verify(messageBirdServiceMock, times(1)).requestByID(url, "NL", PhoneNumbersResponse.class); - + assertNotNull(response); assertEquals(response, mockedResponse); } @@ -835,7 +835,7 @@ public void testListNumbersForPurchaseWithParams() throws IllegalArgumentExcepti options.setLimit(1); options.setNumber(562); options.setSearchPattern(PhoneNumberSearchPattern.START); - + when(messageBirdServiceMock.requestByID(url, "US", options.toHashMap(), PhoneNumbersResponse.class)) .thenReturn(mockedResponse); @@ -853,12 +853,12 @@ public void testPurchaseNumber() throws UnauthorizedException, GeneralException MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock); - + final Map payload = new LinkedHashMap(); payload.put("number", "15625267429"); payload.put("countryCode", "US"); payload.put("billingIntervalMonths", 1); - + when(messageBirdServiceMock.sendPayLoad(url, payload, PurchasedNumberCreatedResponse.class)) .thenReturn(purchasedNumberMockData); final PurchasedNumberCreatedResponse response = messageBirdClientMock.purchaseNumber("15625267429", "US", 1); @@ -866,13 +866,13 @@ public void testPurchaseNumber() throws UnauthorizedException, GeneralException assertNotNull(response); assertEquals(response, purchasedNumberMockData); } - + @Test public void testListPurchasedNumbers() throws UnauthorizedException, GeneralException, NotFoundException { final String url = String.format("%s/phone-numbers", NUMBERS_CALLS_BASE_URL); - + PurchasedNumbersResponse purchasedNumbersMockData = new PurchasedNumbersResponse(); - + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock); @@ -895,9 +895,9 @@ public void testListPurchasedNumbers() throws UnauthorizedException, GeneralExce @Test public void testViewPurchasedNumber() throws UnauthorizedException, GeneralException, NotFoundException { final String url = String.format("%s/phone-numbers", NUMBERS_CALLS_BASE_URL); - + PurchasedNumber purchasedNumberMockData = new PurchasedNumber(); - + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock); when(messageBirdServiceMock.requestByID(url, "15625267429", PurchasedNumber.class)) @@ -913,15 +913,15 @@ public void testViewPurchasedNumber() throws UnauthorizedException, GeneralExce public void updatePurchasedNumber() throws UnauthorizedException, GeneralException { final String phoneNumber = "15625267429"; final String url = String.format("%s/phone-numbers/%s", NUMBERS_CALLS_BASE_URL, phoneNumber); - + PurchasedNumber updatedNumberMock = new PurchasedNumber(); - + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock); - + final Map> payload = new HashMap>(); payload.put("tags", Collections.singletonList("tag")); - + when(messageBirdServiceMock.sendPayLoad("PATCH", url, payload, PurchasedNumber.class)) .thenReturn(updatedNumberMock); final PurchasedNumber response = messageBirdClientMock.updateNumber(phoneNumber, "tag"); @@ -934,10 +934,10 @@ public void updatePurchasedNumber() throws UnauthorizedException, GeneralExcept public void deletePurchasedNumber() throws UnauthorizedException, GeneralException, NotFoundException { final String phoneNumber = "15625267429"; final String url = String.format("%s/phone-numbers", NUMBERS_CALLS_BASE_URL); - + MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientMock = new MessageBirdClient(messageBirdServiceMock); - + messageBirdClientMock.cancelNumber(phoneNumber); verify(messageBirdServiceMock, times(1)).deleteByID(url, phoneNumber); } @@ -1225,12 +1225,14 @@ public void testCreateChildAccounts() throws GeneralException, UnauthorizedExcep MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); ChildAccountCreateResponse childAccountCreateResponse = createChildAccountCreateResponse(); - when(messageBirdServiceMock.sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , "name", ChildAccountCreateResponse.class)) + ChildAccountRequest childAccountRequest = new ChildAccountRequest(); + childAccountRequest.setName("name"); + when(messageBirdServiceMock.sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , childAccountRequest, ChildAccountCreateResponse.class)) .thenReturn(childAccountCreateResponse); - final ChildAccountCreateResponse response = messageBirdClientInjectMock.createChildAccount("name"); + final ChildAccountCreateResponse response = messageBirdClientInjectMock.createChildAccount(childAccountRequest); verify(messageBirdServiceMock, times(1)) - .sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , "name", ChildAccountCreateResponse.class); + .sendPayLoad(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts" , childAccountRequest, ChildAccountCreateResponse.class); assertNotNull(response); assertEquals(response.getId(), childAccountCreateResponse.getId()); assertEquals(response.getName(), childAccountCreateResponse.getName()); @@ -1246,12 +1248,12 @@ public void testGetChildAccount() throws GeneralException, UnauthorizedException MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); ChildAccountDetailedResponse childAccountDetailedResponse = createChildAccountDetailedResponse(); - when(messageBirdServiceMock.requestByID(PARTNER_ACCOUNTS_BASE_URL, "ANY_ID", ChildAccountDetailedResponse.class)) + when(messageBirdServiceMock.requestByID(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", "ANY_ID", ChildAccountDetailedResponse.class)) .thenReturn(childAccountDetailedResponse); ChildAccountDetailedResponse response = messageBirdClientInjectMock.getChildAccountById("ANY_ID"); verify(messageBirdServiceMock, times(1)) - .requestByID(PARTNER_ACCOUNTS_BASE_URL, "ANY_ID", ChildAccountDetailedResponse.class); + .requestByID(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", "ANY_ID", ChildAccountDetailedResponse.class); assertNotNull(response); assertEquals(response.getId(), childAccountDetailedResponse.getId()); assertEquals(response.getName(), childAccountDetailedResponse.getName()); @@ -1263,16 +1265,16 @@ public void testGetChildAccounts() throws GeneralException, UnauthorizedExceptio MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); PartnerAccountsResponse partnerAccountsResponse = createPartnerAccountsResponse(); - when(messageBirdServiceMock.requestList(PARTNER_ACCOUNTS_BASE_URL, 1, 10, PartnerAccountsResponse.class)) + when(messageBirdServiceMock.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, PartnerAccountsResponse.class)) .thenReturn(partnerAccountsResponse); PartnerAccountsResponse response = messageBirdClientInjectMock.getChildAccounts(1, 10); verify(messageBirdServiceMock, times(1)) - .requestList(PARTNER_ACCOUNTS_BASE_URL, 1, 10, PartnerAccountsResponse.class); + .requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, PartnerAccountsResponse.class); assertNotNull(response); - assertEquals(response.getChildAccountResponses().get(0).getId(), partnerAccountsResponse.getChildAccountResponses().get(0).getId()); - assertEquals(response.getChildAccountResponses().get(0).getName(), partnerAccountsResponse.getChildAccountResponses().get(0).getName()); + assertEquals(response.getItems().get(0).getId(), partnerAccountsResponse.getItems().get(0).getId()); + assertEquals(response.getItems().get(0).getName(), partnerAccountsResponse.getItems().get(0).getName()); } @Test @@ -1280,13 +1282,12 @@ public void testUpdateChildAccount() throws GeneralException, UnauthorizedExcept MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); ChildAccountResponse childAccountResponse = createChildAccountResponse(); - final String url = String.format("%s/child-accounts/%s", PARTNER_ACCOUNTS_BASE_URL, "ANY_ID"); - when(messageBirdServiceMock.sendPayLoad("PATCH", url, "ANY_NAME", ChildAccountResponse.class)) + when(messageBirdServiceMock.sendPayLoad(any(), any(), any(), any())) .thenReturn(childAccountResponse); ChildAccountResponse response = messageBirdClientInjectMock.updateChildAccount("ANY_NAME", "ANY_ID"); verify(messageBirdServiceMock, times(1)) - .sendPayLoad("PATCH", url, "ANY_NAME", ChildAccountResponse.class); + .sendPayLoad(any(), any(), any(), any()); assertNotNull(response); assertEquals(response.getId(), childAccountResponse.getId()); assertEquals(response.getName(), childAccountResponse.getName()); diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index 0e293855..a66c0aad 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -384,7 +384,7 @@ public static ChildAccountResponse createChildAccountResponse(){ public static PartnerAccountsResponse createPartnerAccountsResponse(){ final ChildAccountResponse childAccountResponse = createChildAccountResponse(); final PartnerAccountsResponse partnerAccountsResponse = new PartnerAccountsResponse(); - partnerAccountsResponse.setChildAccountResponses(Collections.singletonList(childAccountResponse)); + partnerAccountsResponse.setItems(Collections.singletonList(childAccountResponse)); return partnerAccountsResponse; } } diff --git a/examples/src/main/java/ExampleCreateChildAccount.java b/examples/src/main/java/ExampleCreateChildAccount.java index 037f90dd..2c71ae51 100644 --- a/examples/src/main/java/ExampleCreateChildAccount.java +++ b/examples/src/main/java/ExampleCreateChildAccount.java @@ -3,6 +3,7 @@ import com.messagebird.MessageBirdServiceImpl; import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.ChildAccountRequest; import com.messagebird.objects.ChildAccountCreateResponse; public class ExampleCreateChildAccount { @@ -17,7 +18,9 @@ public static void main(String[] args) { try { System.out.println("Creating a child account of partner accounts"); - ChildAccountCreateResponse childAccount = messageBirdClient.createChildAccount(args[1]); + final ChildAccountRequest childAccountRequest = new ChildAccountRequest(); + childAccountRequest.setName(args[1]); + ChildAccountCreateResponse childAccount = messageBirdClient.createChildAccount(childAccountRequest); System.out.println("Child account is created: " + childAccount.toString()); } catch (GeneralException | UnauthorizedException exception) { diff --git a/examples/src/main/java/ExampleGetChildAccountById.java b/examples/src/main/java/ExampleGetChildAccountById.java index ae1acbc9..2e42fd6e 100644 --- a/examples/src/main/java/ExampleGetChildAccountById.java +++ b/examples/src/main/java/ExampleGetChildAccountById.java @@ -4,6 +4,7 @@ import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.NotFoundException; import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.ChildAccountDetailedResponse; public class ExampleGetChildAccountById { public static void main(String[] args) { @@ -17,8 +18,8 @@ public static void main(String[] args) { try { System.out.println("Get a child account by id"); - messageBirdClient.getChildAccountById(args[1]); - + ChildAccountDetailedResponse response = messageBirdClient.getChildAccountById(args[1]); + System.out.println("Response: " + response.toString()); } catch (GeneralException | UnauthorizedException | NotFoundException exception) { exception.printStackTrace(); } diff --git a/examples/src/main/java/ExampleGetChildAccounts.java b/examples/src/main/java/ExampleGetChildAccounts.java index 5d7be592..01a29dbb 100644 --- a/examples/src/main/java/ExampleGetChildAccounts.java +++ b/examples/src/main/java/ExampleGetChildAccounts.java @@ -3,6 +3,7 @@ import com.messagebird.MessageBirdServiceImpl; import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.PartnerAccountsResponse; public class ExampleGetChildAccounts { public static void main(String[] args) { @@ -16,7 +17,8 @@ public static void main(String[] args) { try { System.out.println("Get a child accounts of partner account"); - messageBirdClient.getChildAccounts(Integer.valueOf(args[1]), Integer.valueOf(args[2])); + PartnerAccountsResponse response = messageBirdClient.getChildAccounts(Integer.valueOf(args[1]), Integer.valueOf(args[2])); + System.out.println("response: " + response); } catch (GeneralException | UnauthorizedException exception) { exception.printStackTrace(); } diff --git a/examples/src/main/java/ExampleUpdateChildAccount.java b/examples/src/main/java/ExampleUpdateChildAccount.java index e90b9b51..1976f6ab 100644 --- a/examples/src/main/java/ExampleUpdateChildAccount.java +++ b/examples/src/main/java/ExampleUpdateChildAccount.java @@ -3,6 +3,7 @@ import com.messagebird.MessageBirdServiceImpl; import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.UnauthorizedException; +import com.messagebird.objects.ChildAccountResponse; public class ExampleUpdateChildAccount { public static void main(String[] args) { @@ -16,8 +17,8 @@ public static void main(String[] args) { try { System.out.println("Updating a child account"); - messageBirdClient.updateChildAccount(args[1], args[2]); - System.out.println("Child account is updated"); + ChildAccountResponse response = messageBirdClient.updateChildAccount(args[1], args[2]); + System.out.println("Child account is updated: " + response.toString()); } catch (GeneralException | UnauthorizedException exception) { exception.printStackTrace(); } From 89715d8b384b10ba864cfb29a8dacd2d3c2e628a Mon Sep 17 00:00:00 2001 From: "deniz@messagebird.com" Date: Fri, 8 Oct 2021 17:33:47 +0200 Subject: [PATCH 6/6] getAccounts is fixed --- .../java/com/messagebird/MessageBirdClient.java | 4 ++-- .../objects/PartnerAccountsResponse.java | 5 ----- .../com/messagebird/MessageBirdClientTest.java | 15 ++++++++------- api/src/test/java/com/messagebird/TestUtil.java | 7 ------- .../src/main/java/ExampleGetChildAccounts.java | 6 ++++-- 5 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java diff --git a/api/src/main/java/com/messagebird/MessageBirdClient.java b/api/src/main/java/com/messagebird/MessageBirdClient.java index 5f8d489f..cd8ca60a 100644 --- a/api/src/main/java/com/messagebird/MessageBirdClient.java +++ b/api/src/main/java/com/messagebird/MessageBirdClient.java @@ -2053,9 +2053,9 @@ public ChildAccountDetailedResponse getChildAccountById(final String id) throws * @throws UnauthorizedException if client is unauthorized * @throws GeneralException general exception */ - public PartnerAccountsResponse getChildAccounts(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException { + public List getChildAccounts(final Integer offset, final Integer limit) throws UnauthorizedException, GeneralException { verifyOffsetAndLimit(offset, limit); - return messageBirdService.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", offset, limit, PartnerAccountsResponse.class); + return messageBirdService.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", offset, limit, List.class); } /** diff --git a/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java b/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java deleted file mode 100644 index deb82067..00000000 --- a/api/src/main/java/com/messagebird/objects/PartnerAccountsResponse.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.messagebird.objects; - -public class PartnerAccountsResponse extends ListBase{ - -} diff --git a/api/src/test/java/com/messagebird/MessageBirdClientTest.java b/api/src/test/java/com/messagebird/MessageBirdClientTest.java index c948154e..139ede8a 100644 --- a/api/src/test/java/com/messagebird/MessageBirdClientTest.java +++ b/api/src/test/java/com/messagebird/MessageBirdClientTest.java @@ -24,6 +24,7 @@ import static com.messagebird.MessageBirdClient.*; import static com.messagebird.TestUtil.*; +import static com.messagebird.TestUtil.createChildAccountDetailedResponse; import static org.junit.Assert.*; import static org.mockito.Mockito.*; import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals; @@ -1264,17 +1265,17 @@ public void testGetChildAccount() throws GeneralException, UnauthorizedException public void testGetChildAccounts() throws GeneralException, UnauthorizedException { MessageBirdService messageBirdServiceMock = mock(MessageBirdService.class); MessageBirdClient messageBirdClientInjectMock = new MessageBirdClient(messageBirdServiceMock); - PartnerAccountsResponse partnerAccountsResponse = createPartnerAccountsResponse(); - when(messageBirdServiceMock.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, PartnerAccountsResponse.class)) - .thenReturn(partnerAccountsResponse); + List childAccountResponses = Collections.singletonList(createChildAccountResponse()); + when(messageBirdServiceMock.requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, List.class)) + .thenReturn(childAccountResponses); - PartnerAccountsResponse response = messageBirdClientInjectMock.getChildAccounts(1, 10); + List response = messageBirdClientInjectMock.getChildAccounts(1, 10); verify(messageBirdServiceMock, times(1)) - .requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, PartnerAccountsResponse.class); + .requestList(PARTNER_ACCOUNTS_BASE_URL + "/child-accounts", 1, 10, List.class); assertNotNull(response); - assertEquals(response.getItems().get(0).getId(), partnerAccountsResponse.getItems().get(0).getId()); - assertEquals(response.getItems().get(0).getName(), partnerAccountsResponse.getItems().get(0).getName()); + assertEquals(response.get(0).getId(), childAccountResponses.get(0).getId()); + assertEquals(response.get(0).getName(), childAccountResponses.get(0).getName()); } @Test diff --git a/api/src/test/java/com/messagebird/TestUtil.java b/api/src/test/java/com/messagebird/TestUtil.java index a66c0aad..7518776b 100644 --- a/api/src/test/java/com/messagebird/TestUtil.java +++ b/api/src/test/java/com/messagebird/TestUtil.java @@ -380,11 +380,4 @@ public static ChildAccountResponse createChildAccountResponse(){ childAccountResponse.setName("ANY_NAME"); return childAccountResponse; } - - public static PartnerAccountsResponse createPartnerAccountsResponse(){ - final ChildAccountResponse childAccountResponse = createChildAccountResponse(); - final PartnerAccountsResponse partnerAccountsResponse = new PartnerAccountsResponse(); - partnerAccountsResponse.setItems(Collections.singletonList(childAccountResponse)); - return partnerAccountsResponse; - } } diff --git a/examples/src/main/java/ExampleGetChildAccounts.java b/examples/src/main/java/ExampleGetChildAccounts.java index 01a29dbb..79dd17cb 100644 --- a/examples/src/main/java/ExampleGetChildAccounts.java +++ b/examples/src/main/java/ExampleGetChildAccounts.java @@ -3,7 +3,9 @@ import com.messagebird.MessageBirdServiceImpl; import com.messagebird.exceptions.GeneralException; import com.messagebird.exceptions.UnauthorizedException; -import com.messagebird.objects.PartnerAccountsResponse; +import com.messagebird.objects.ChildAccountResponse; + +import java.util.List; public class ExampleGetChildAccounts { public static void main(String[] args) { @@ -17,7 +19,7 @@ public static void main(String[] args) { try { System.out.println("Get a child accounts of partner account"); - PartnerAccountsResponse response = messageBirdClient.getChildAccounts(Integer.valueOf(args[1]), Integer.valueOf(args[2])); + List response = messageBirdClient.getChildAccounts(Integer.valueOf(args[1]), Integer.valueOf(args[2])); System.out.println("response: " + response); } catch (GeneralException | UnauthorizedException exception) { exception.printStackTrace();