Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer entity nmock #243

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/main/java/com/razorpay/TransferClient.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package com.razorpay;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class TransferClient extends ApiClient {

TransferClient(String auth) {
super(auth);
TransferClient(String auth, ApiUtils apiUtils) {
super(auth,apiUtils);
}

public Transfer create(JSONObject request) throws RazorpayException {
public Transfer create(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return post(Constants.TRANSFER_CREATE, request);
}

public Transfer edit(String id, JSONObject request) throws RazorpayException {
public Transfer edit(String id, JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return patch(String.format(Constants.TRANSFER_EDIT, id), request);
}

public Reversal reversal(String id, JSONObject request) throws RazorpayException {
public Reversal reversal(String id, JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return post(String.format(Constants.TRANSFER_REVERSAL_CREATE, id), request);
}

public Transfer fetch(String id) throws RazorpayException {
public Transfer fetch(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return get(String.format(Constants.TRANSFER_GET, id), null);
}

public List<Transfer> fetchAll() throws RazorpayException {
public List<Transfer> fetchAll() throws RazorpayException, JSONException, IOException, URISyntaxException {
return fetchAll(null);
}

public List<Transfer> fetchAll(JSONObject request) throws RazorpayException {
public List<Transfer> fetchAll(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return getCollection(Constants.TRANSFER_LIST, request);
}
}
57 changes: 31 additions & 26 deletions src/test/java/com/razorpay/TransferClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import org.json.JSONObject;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

public class TransferClientTest extends BaseTest{

@InjectMocks
protected TransferClient transferClient = new TransferClient(TEST_SECRET_KEY);
@Mock
ApiUtils apiUtils;

private static final String TRANSFER_ID = "trf_E9uhYLFLLZ2pks";

Expand All @@ -23,7 +25,7 @@ public class TransferClientTest extends BaseTest{
* @throws RazorpayException
*/
@Test
public void create() throws RazorpayException{
public void create() throws Exception{

JSONObject request = new JSONObject("{\n" +
" \"amount\": 500,\n" +
Expand All @@ -49,15 +51,16 @@ public void create() throws RazorpayException{
" \"processed_at\": 1580219046\n" +
"}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.TRANSFER_CREATE, null);
mockPostRequest(apiUtils,builder,request.toString(), mockedResponseJson);

TransferClient transferClient = new TransferClient("test",apiUtils);
Transfer fetch = transferClient.create(request);
assertNotNull(fetch);
assertEquals(TRANSFER_ID,fetch.get("id"));
assertTrue(fetch.has("amount"));
assertTrue(fetch.has("currency"));
String createRequest = getHost(Constants.TRANSFER_CREATE);
verifySentRequest(true, request.toString(), createRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -68,9 +71,9 @@ public void create() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void fetch() throws RazorpayException{
public void fetch() throws Exception{

String json = "{\n" +
String mockedResponseJson = "{\n" +
" \"id\": "+TRANSFER_ID+",\n" +
" \"entity\": \"transfer\",\n" +
" \"source\": \"pay_E6j30Iu1R7XbIG\",\n" +
Expand All @@ -89,15 +92,16 @@ public void fetch() throws RazorpayException{
" \"processed_at\": 1579691505\n" +
"}";
try {
mockResponseFromExternalClient(json);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.TRANSFER_GET, TRANSFER_ID), null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

TransferClient transferClient = new TransferClient("test",apiUtils);
Transfer fetch = transferClient.fetch(TRANSFER_ID);
assertNotNull(fetch);
assertEquals(TRANSFER_ID,fetch.get("id"));
assertEquals("INR",fetch.get("currency"));
assertTrue(fetch.has("amount_reversed"));
String addonCreate = getHost(String.format(Constants.TRANSFER_GET, TRANSFER_ID));
verifySentRequest(false, null, addonCreate);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -108,9 +112,9 @@ public void fetch() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void fetchAll() throws RazorpayException{
public void fetchAll() throws Exception{

String json = "{\n" +
String mockedResponseJson = "{\n" +
" \"entity\": \"collection\",\n" +
" \"count\": 1,\n" +
" \"items\": [\n" +
Expand Down Expand Up @@ -145,16 +149,16 @@ public void fetchAll() throws RazorpayException{
" ]\n" +
"}";
try {
mockResponseFromExternalClient(json);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.TRANSFER_LIST, null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);
TransferClient transferClient = new TransferClient("test",apiUtils);
List<Transfer> fetch = transferClient.fetchAll();
assertNotNull(fetch);
assertEquals(true,fetch.get(0).has("id"));
assertEquals(true,fetch.get(0).has("entity"));
assertEquals(true,fetch.get(0).has("amount_reversed"));
assertEquals(true,fetch.get(0).toJson().getJSONObject("recipient_settlement").has("amount"));
String transferList = getHost(Constants.TRANSFER_LIST);
verifySentRequest(false, null, transferList);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -165,14 +169,14 @@ public void fetchAll() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void edit() throws RazorpayException{
public void edit() throws Exception{

JSONObject request = new JSONObject("{\n" +
" \"on_hold\": \"1\",\n" +
" \"on_hold_until\": \"1679691505\"\n" +
"}");

String json = "{\n" +
String mockedResponseJson = "{\n" +
" \"id\": "+TRANSFER_ID+",\n" +
" \"entity\": \"transfer\",\n" +
" \"source\": \"pay_EAeSM2Xul8xYRo\",\n" +
Expand All @@ -191,15 +195,16 @@ public void edit() throws RazorpayException{
" \"processed_at\": 1580459321\n" +
"}\n";
try {
mockResponseFromExternalClient(json);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.TRANSFER_EDIT, TRANSFER_ID), null);
mockPatchRequest(apiUtils,builder,request.toString(), mockedResponseJson);
TransferClient transferClient = new TransferClient("test",apiUtils);

Transfer fetch = transferClient.edit(TRANSFER_ID,request);
assertNotNull(fetch);
assertEquals(TRANSFER_ID,fetch.get("id"));
assertEquals("INR",fetch.get("currency"));
assertTrue(fetch.has("amount_reversed"));
String editRequest = getHost(String.format(Constants.TRANSFER_EDIT, TRANSFER_ID));
verifySentRequest(true, request.toString(), editRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand Down