diff --git a/src/main/java/com/hyperwallet/clientsdk/Hyperwallet.java b/src/main/java/com/hyperwallet/clientsdk/Hyperwallet.java index f9642d3c8..41bcae8eb 100644 --- a/src/main/java/com/hyperwallet/clientsdk/Hyperwallet.java +++ b/src/main/java/com/hyperwallet/clientsdk/Hyperwallet.java @@ -12,6 +12,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.TimeZone; /** @@ -160,6 +161,21 @@ public HyperwalletList listUsers(HyperwalletPaginationOptions o }); } + /** + * List Users + * + * @param options List filter option + * @return HyperwalletList of HyperwalletUser + */ + public HyperwalletList listUsers(HyperwalletUserListOptions options) { + String url = paginate(this.url + "/users", options); + if (options != null) { + url = addParameters(url, options.getParameters()); + } + return apiClient.get(url, new TypeReference>() { + }); + } + /** * Get Authentication Token * @@ -1001,6 +1017,26 @@ public HyperwalletPayPalAccount getPayPalAccount(String userToken, String payPal return apiClient.get(url + "/users/" + userToken + "/paypal-accounts/" + payPalAccountToken, HyperwalletPayPalAccount.class); } + /** + * Updates the PayPal Account's email address + * + * @param account PayPal Account to update. + * + * @return HyperwalletPayPalAccount updated PayPal Account + */ + public HyperwalletPayPalAccount updatePayPalAccount(HyperwalletPayPalAccount account) { + if (account == null) { + throw new HyperwalletException("PayPal Account is required"); + } + if (StringUtils.isEmpty(account.getUserToken())) { + throw new HyperwalletException("User token is required"); + } + if (StringUtils.isEmpty(account.getToken())) { + throw new HyperwalletException("PayPal Account token is required"); + } + return apiClient.put(url + "/users/" + account.getUserToken() + "/paypal-accounts/" + account.getToken(), account, HyperwalletPayPalAccount.class); + } + /** * List PayPal Accounts * @@ -1851,6 +1887,14 @@ private String addParameter(String url, String key, Object value) { return url + (url.indexOf("?") == -1 ? "?" : "&") + key + "=" + value; } + private String addParameters(String url, Map params) + { + for (Map.Entry param : params.entrySet()) { + url = addParameter(url, param.getKey(), param.getValue()); + } + return url; + } + private String convert(Date in) { if (in == null) { return null; @@ -1923,5 +1967,4 @@ private HyperwalletPayPalAccount copy(HyperwalletPayPalAccount payPalAccount) { payPalAccount = HyperwalletJsonUtil.fromJson(HyperwalletJsonUtil.toJson(payPalAccount), HyperwalletPayPalAccount.class); return payPalAccount; } - } diff --git a/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletPaginationOptions.java b/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletPaginationOptions.java index 224ad0d64..2ba513606 100644 --- a/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletPaginationOptions.java +++ b/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletPaginationOptions.java @@ -1,6 +1,7 @@ package com.hyperwallet.clientsdk.model; import java.util.Date; +import java.util.Map; public class HyperwalletPaginationOptions { diff --git a/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletUserListOptions.java b/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletUserListOptions.java new file mode 100644 index 000000000..f5e81715f --- /dev/null +++ b/src/main/java/com/hyperwallet/clientsdk/model/HyperwalletUserListOptions.java @@ -0,0 +1,88 @@ +package com.hyperwallet.clientsdk.model; + +import java.util.HashMap; +import java.util.Map; + +public class HyperwalletUserListOptions extends HyperwalletPaginationOptions +{ + /** + * Return the user with this particular client-defined identifier. + */ + private String clientUserId; + + /** + * Returns users with this email address. + */ + private String email; + + /** + * Returns users belonging to program specified by the provided token. + */ + private String programToken; + + /** + * Returns users with this account status. + */ + private HyperwalletUser.Status status; + + /** + * The user's verification status. A user may be required to verify their identity after a certain + * threshold of payments is reached. + */ + private HyperwalletUser.VerificationStatus verificationStatus; + + public String getClientUserId() { + return clientUserId; + } + + public HyperwalletUserListOptions setClientUserId(String clientUserId) { + this.clientUserId = clientUserId; + return this; + } + + public String getEmail() { + return email; + } + + public HyperwalletUserListOptions setEmail(String email) { + this.email = email; + return this; + } + + public String getProgramToken() { + return programToken; + } + + public HyperwalletUserListOptions setProgramToken(String programToken) { + this.programToken = programToken; + return this; + } + + public HyperwalletUser.Status getStatus() { + return status; + } + + public HyperwalletUserListOptions setStatus(HyperwalletUser.Status status) { + this.status = status; + return this; + } + + public HyperwalletUser.VerificationStatus getVerificationStatus() { + return verificationStatus; + } + + public HyperwalletUserListOptions setVerificationStatus(HyperwalletUser.VerificationStatus verificationStatus) { + this.verificationStatus = verificationStatus; + return this; + } + + public Map getParameters() { + return new HashMap() {{ + put("clientUserId", clientUserId); + put("email", email); + put("programToken", programToken); + put("status", status == null ? null : status.name()); + put("verificationStatus", verificationStatus == null ? null : verificationStatus.name()); + }}; + } +} diff --git a/src/test/java/com/hyperwallet/clientsdk/HyperwalletTest.java b/src/test/java/com/hyperwallet/clientsdk/HyperwalletTest.java index a0f839b22..bc4120aa7 100644 --- a/src/test/java/com/hyperwallet/clientsdk/HyperwalletTest.java +++ b/src/test/java/com/hyperwallet/clientsdk/HyperwalletTest.java @@ -17,6 +17,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.TimeZone; import static org.hamcrest.MatcherAssert.assertThat; @@ -345,6 +346,25 @@ public void testListUsers_withSomeParameters() throws Exception { Mockito.verify(mockApiClient).get(Mockito.eq("https://api.sandbox.hyperwallet.com/rest/v3/users?createdBefore=2016-06-29T17:58:26Z&sortBy=test-sort-by&offset=5"), Mockito.any(TypeReference.class)); } + @Test + public void testListUsers_withCustomParameters() throws Exception { + HyperwalletList response = new HyperwalletList(); + + Hyperwallet client = new Hyperwallet("test-username", "test-password"); + HyperwalletApiClient mockApiClient = createAndInjectHyperwalletApiClientMock(client); + + HyperwalletUserListOptions options = new HyperwalletUserListOptions(); + options.setLimit(100); + options.setEmail("test-email"); + + Mockito.when(mockApiClient.get(Mockito.anyString(), Mockito.any(TypeReference.class))).thenReturn(response); + + HyperwalletList resp = client.listUsers(options); + assertThat(resp, is(equalTo(response))); + + Mockito.verify(mockApiClient).get(Mockito.eq("https://api.sandbox.hyperwallet.com/rest/v3/users?limit=100&email=test-email"), Mockito.any(TypeReference.class)); + } + @Test public void testGetUserStatusTransition_noUserToken() { Hyperwallet client = new Hyperwallet("test-username", "test-password"); @@ -3130,6 +3150,84 @@ public void testGetPayPalAccount_successful() throws Exception { Mockito.verify(mockApiClient).get("https://api.sandbox.hyperwallet.com/rest/v3/users/test-user-token/paypal-accounts/test-paypal-account-token", payPalAccount.getClass()); } + @Test + public void testUpdatePayPalAccount_noPayPalAccount() { + Hyperwallet client = new Hyperwallet("test-username", "test-password"); + try { + client.updatePayPalAccount(null); + fail("Expect HyperwalletException"); + } catch (HyperwalletException e) { + assertThat(e.getErrorCode(), is(nullValue())); + assertThat(e.getResponse(), is(nullValue())); + assertThat(e.getErrorMessage(), is(equalTo("PayPal Account is required"))); + assertThat(e.getMessage(), is(equalTo("PayPal Account is required"))); + assertThat(e.getHyperwalletErrors(), is(nullValue())); + assertThat(e.getRelatedResources(), is(nullValue())); + } + } + + @Test + public void testUpdatePayPalAccount_noUserToken() { + HyperwalletPayPalAccount account = new HyperwalletPayPalAccount(); + + Hyperwallet client = new Hyperwallet("test-username", "test-password"); + try { + client.updatePayPalAccount(account); + fail("Expect HyperwalletException"); + } catch (HyperwalletException e) { + assertThat(e.getErrorCode(), is(nullValue())); + assertThat(e.getResponse(), is(nullValue())); + assertThat(e.getErrorMessage(), is(equalTo("User token is required"))); + assertThat(e.getMessage(), is(equalTo("User token is required"))); + assertThat(e.getHyperwalletErrors(), is(nullValue())); + assertThat(e.getRelatedResources(), is(nullValue())); + } + } + + @Test + public void testUpdatePayPalAccount_noPayPalAccountToken() { + HyperwalletPayPalAccount account = new HyperwalletPayPalAccount(); + account.setUserToken("test-user-token"); + + Hyperwallet client = new Hyperwallet("test-username", "test-password"); + try { + client.updatePayPalAccount(account); + fail("Expect HyperwalletException"); + } catch (HyperwalletException e) { + assertThat(e.getErrorCode(), is(nullValue())); + assertThat(e.getResponse(), is(nullValue())); + assertThat(e.getErrorMessage(), is(equalTo("PayPal Account token is required"))); + assertThat(e.getMessage(), is(equalTo("PayPal Account token is required"))); + assertThat(e.getHyperwalletErrors(), is(nullValue())); + assertThat(e.getRelatedResources(), is(nullValue())); + } + } + + @Test + public void testUpdatePayPalAccount_successful() throws Exception { + HyperwalletPayPalAccount account = new HyperwalletPayPalAccount(); + account.setToken("test-paypal-account-token"); + account.setUserToken("test-user-token"); + account.setEmail("test-email"); + + HyperwalletPayPalAccount accountResponse = new HyperwalletPayPalAccount(); + + Hyperwallet client = new Hyperwallet("test-username", "test-password"); + HyperwalletApiClient mockApiClient = createAndInjectHyperwalletApiClientMock(client); + + Mockito.when(mockApiClient.put(Mockito.anyString(), Mockito.anyObject(), Mockito.any(Class.class))).thenReturn(accountResponse); + + HyperwalletPayPalAccount resp = client.updatePayPalAccount(account); + assertThat(resp, is(equalTo(accountResponse))); + + ArgumentCaptor argument = ArgumentCaptor.forClass(HyperwalletPayPalAccount.class); + Mockito.verify(mockApiClient).put(Mockito.eq("https://api.sandbox.hyperwallet.com/rest/v3/users/test-user-token/paypal-accounts/test-paypal-account-token"), argument.capture(), Mockito.eq(account.getClass())); + + HyperwalletPayPalAccount apiClientBankAccount = argument.getValue(); + assertThat(apiClientBankAccount, is(notNullValue())); + assertThat(apiClientBankAccount.getEmail(), is(equalTo("test-email"))); + } + @Test public void testListPayPalAccount_noUserToken() { Hyperwallet client = new Hyperwallet("test-username", "test-password");