Skip to content

Commit

Permalink
added option to add empty transfer method and testing for 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimk87 committed Nov 25, 2016
1 parent 579e5c3 commit 0a4dfeb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
29 changes: 27 additions & 2 deletions src/main/java/com/hyperwallet/clientsdk/Hyperwallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ public HyperwalletList<HyperwalletWebhookNotification> listWebhookEvents(Hyperwa
/**
* Create a Transfer Method
*
* @param jsonCacheToken JSON cache token
* @param transferMethod Transfer Method object to create
* @param jsonCacheToken String JSON cache token
* @param transferMethod TransferMethod object to create
* @return HyperwalletTransferMethod Transfer Method object created
*/
public HyperwalletTransferMethod createTransferMethod(String jsonCacheToken, HyperwalletTransferMethod transferMethod) {
Expand All @@ -959,6 +959,31 @@ public HyperwalletTransferMethod createTransferMethod(String jsonCacheToken, Hyp
return apiClient.post(url + "/users/"+transferMethod.getUserToken()+"/transfer-methods", transferMethod, HyperwalletTransferMethod.class, headers);
}


/**
* Create a Transfer Method
*
* @param jsonCacheToken String JSON cache token
* @param userToken String user token
* @return HyperwalletTransferMethod Transfer Method object created
*/
public HyperwalletTransferMethod createTransferMethod(String jsonCacheToken, String userToken) {

if (StringUtils.isEmpty(userToken)) {
throw new HyperwalletException("User token is required");
}
if (StringUtils.isEmpty(jsonCacheToken)) {
throw new HyperwalletException("JSON token is required");
}

HyperwalletTransferMethod transferMethod = new HyperwalletTransferMethod();
transferMethod.setUserToken(userToken);

HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Json-Cache-Token", jsonCacheToken);

return apiClient.post(url + "/users/"+transferMethod.getUserToken()+"/transfer-methods", transferMethod, HyperwalletTransferMethod.class, headers);
}
//--------------------------------------
// Internal utils
//--------------------------------------
Expand Down
107 changes: 103 additions & 4 deletions src/test/java/com/hyperwallet/clientsdk/HyperwalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import java.util.Locale;
import java.util.TimeZone;

import static org.testng.Assert.fail;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.testng.Assert.fail;

/**
* @author fkrauthan
Expand Down Expand Up @@ -2793,7 +2792,7 @@ public void testTransferMethod_noTransferMethod() {
Hyperwallet client = new Hyperwallet("test-username", "test-password");
String jsonCacheToken = "token123-123-123";
try {
client.createTransferMethod(jsonCacheToken, null);
client.createTransferMethod(jsonCacheToken, (HyperwalletTransferMethod)null);
fail("Expect HyperwalletException");
} catch (HyperwalletException e) {
assertThat(e.getErrorCode(), is(nullValue()));
Expand Down Expand Up @@ -2825,7 +2824,7 @@ public void testCreateTransferMethod_noJsonCacheToken() {
}

@Test
public void testCreateTransferMethod_noUseroken() {
public void testCreateTransferMethod_noUserokenInTransferMethod() {
HyperwalletTransferMethod transferMethod = new HyperwalletTransferMethod();

Hyperwallet client = new Hyperwallet("test-username", "test-password");
Expand All @@ -2842,6 +2841,106 @@ public void testCreateTransferMethod_noUseroken() {
}
}

@Test
public void testCreateTransferMethod_noUseroken() {

Hyperwallet client = new Hyperwallet("test-username", "test-password");
String jsonCacheToken = "token123-123-123";

try {
client.createTransferMethod(jsonCacheToken, "");
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()));
}
}

@Test
public void testCreateTransferMethod_CompletedUserToken() throws Exception{

HyperwalletTransferMethod transferMethodResponse = new HyperwalletTransferMethod();

Hyperwallet client = new Hyperwallet("test-username", "test-password");
HyperwalletApiClient mockApiClient = createAndInjectHyperwalletApiClientMock(client);

Mockito.when(mockApiClient.post(Mockito.anyString(), Mockito.anyObject(), Mockito.any(Class.class), Mockito.any(HashMap.class))).thenReturn(transferMethodResponse);

String jsonCacheToken = "token123-123-123";
String userToken = "test-user-token";
HyperwalletTransferMethod resp = client.createTransferMethod(jsonCacheToken, userToken);
assertThat(resp, is(equalTo(transferMethodResponse)));

ArgumentCaptor<HyperwalletTransferMethod> argument = ArgumentCaptor.forClass(HyperwalletTransferMethod.class);
Mockito.verify(mockApiClient).post(Mockito.eq("https://api.sandbox.hyperwallet.com/rest/v3/users/test-user-token/transfer-methods"), argument.capture(), Mockito.eq(resp.getClass()), (HashMap<String, String>) Mockito.anyMapOf(String.class, String.class));

HyperwalletTransferMethod apiClientTransferMethod = argument.getValue();

assertThat(apiClientTransferMethod, is(notNullValue()));
assertThat(apiClientTransferMethod.getUserToken(), is(notNullValue()));

assertThat(apiClientTransferMethod.getToken(), is(nullValue()));
assertThat(apiClientTransferMethod.getStatus(), is(nullValue()));
assertThat(apiClientTransferMethod.getCreatedOn(), is(nullValue()));
assertThat(apiClientTransferMethod.getCountry(), is(nullValue()));
assertThat(apiClientTransferMethod.getType(), is(nullValue()));
assertThat(apiClientTransferMethod.getTransferMethodCountry(), is(nullValue()));
assertThat(apiClientTransferMethod.getTransferMethodCurrency(), is(nullValue()));
assertThat(apiClientTransferMethod.getBankName(), is(nullValue()));
assertThat(apiClientTransferMethod.getBankId(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchName(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchId(), is(nullValue()));
assertThat(apiClientTransferMethod.getBankAccountId(), is(nullValue()));
assertThat(apiClientTransferMethod.getBankAccountRelationship(), is(nullValue()));
assertThat(apiClientTransferMethod.getBankAccountPurpose(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchAddressLine1(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchAddressLine2(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchCity(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchStateProvince(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchCountry(), is(nullValue()));
assertThat(apiClientTransferMethod.getBranchPostalCode(), is(nullValue()));
assertThat(apiClientTransferMethod.getWireInstructions(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankId(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankName(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankAccountId(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankAddressLine1(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankAddressLine2(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankCity(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankStateProvince(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankCountry(), is(nullValue()));
assertThat(apiClientTransferMethod.getIntermediaryBankPostalCode(), is(nullValue()));
assertThat(apiClientTransferMethod.getCardType(), is(nullValue()));
assertThat(apiClientTransferMethod.getCardPackage(), is(nullValue()));
assertThat(apiClientTransferMethod.getCardNumber(), is(nullValue()));
assertThat(apiClientTransferMethod.getCardBrand(), is(nullValue()));
assertThat(apiClientTransferMethod.getProfileType(), is(nullValue()));
assertThat(apiClientTransferMethod.getBusinessName(), is(nullValue()));
assertThat(apiClientTransferMethod.getBusinessRegistrationId(), is(nullValue()));
assertThat(apiClientTransferMethod.getBusinessRegistrationStateProvince(), is(nullValue()));
assertThat(apiClientTransferMethod.getBusinessRegistrationCountry(), is(nullValue()));
assertThat(apiClientTransferMethod.getBusinessContactRole(), is(nullValue()));
assertThat(apiClientTransferMethod.getFirstName(), is(nullValue()));
assertThat(apiClientTransferMethod.getMiddleName(), is(nullValue()));
assertThat(apiClientTransferMethod.getLastName(), is(nullValue()));
assertThat(apiClientTransferMethod.getCountryOfBirth(), is(nullValue()));
assertThat(apiClientTransferMethod.getCountryOfNationality(), is(nullValue()));
assertThat(apiClientTransferMethod.getGender(), is(nullValue()));
assertThat(apiClientTransferMethod.getMobileNumber(), is(nullValue()));
assertThat(apiClientTransferMethod.getEmail(), is(nullValue()));
assertThat(apiClientTransferMethod.getGovernmentId(), is(nullValue()));
assertThat(apiClientTransferMethod.getPassportId(), is(nullValue()));
assertThat(apiClientTransferMethod.getDriversLicenseId(), is(nullValue()));
assertThat(apiClientTransferMethod.getAddressLine1(), is(nullValue()));
assertThat(apiClientTransferMethod.getAddressLine2(), is(nullValue()));
assertThat(apiClientTransferMethod.getCity(), is(nullValue()));
assertThat(apiClientTransferMethod.getStateProvince(), is(nullValue()));
assertThat(apiClientTransferMethod.getPostalCode(), is(nullValue()));
assertThat(apiClientTransferMethod.getCountry(), is(nullValue()));
}

@Test
public void testCreateTransferMethod_Completed() throws Exception{
HyperwalletTransferMethod transferMethod = createPrePopulatedTransferMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,10 +724,10 @@ public void testPost_OverwriteHeaders() throws Exception {
.withStatusCode(400)
.withBody("{ \"errors\": [{ \"code\": \"test1\", \"fieldName\": \"test2\", \"message\": \"test3\" }, { \"code\": \"test4\", \"message\": \"test5\" }] }")
);

HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Basic wrong_password");


try {
hyperwalletApiClient.post(baseUrl + "/test?test-query=test-value", requestBody, TestBody.class, headers);
fail("Expected HyperwalletException");
Expand Down Expand Up @@ -768,4 +768,27 @@ public void testPost_NullHeaders() throws Exception {
assertThat(body.test2, is(nullValue()));
}


@Test
public void testPost_noConnectionWithHeaders() {
TestBody requestBody = new TestBody();
requestBody.test1 = "value1";
requestBody.getInclusions().add("test1");

mockServer.stop();
if (mockServer.isRunning()) {
fail("Mockserver still running");
}

HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Basic wrong_password");

try {
hyperwalletApiClient.post(baseUrl + "/test?test-query=test-value", requestBody, TestBody.class,headers);
fail("Expect HyperwalletException");
} catch (HyperwalletException e) {
assertThat(e.getMessage(), is(equalTo("java.net.ConnectException: Connection refused")));
}
}

}

0 comments on commit 0a4dfeb

Please sign in to comment.