Skip to content

Commit

Permalink
addon entity and mock
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitdas13 committed Aug 14, 2022
1 parent a8fb17a commit ba1feef
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/razorpay/AddonClient.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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 AddonClient extends ApiClient {

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

// To create an Addon, use the createAddon method of SubscriptionClient
public Addon fetch(String id) throws RazorpayException {
public Addon fetch(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return get(String.format(Constants.ADDON_GET, id), null);
}

Expand All @@ -20,19 +23,19 @@ public Addon fetch(String id) throws RazorpayException {
* with a default values without filteration
* @throws RazorpayException
*/
public List<Addon> fetchAll() throws RazorpayException {
public List<Addon> fetchAll() throws RazorpayException, JSONException, IOException, URISyntaxException {
return fetchAll(null);
}

/**
* This method get list of Addons filtered by parameters @request
* @throws RazorpayException
*/
public List<Addon> fetchAll(JSONObject request) throws RazorpayException {
public List<Addon> fetchAll(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return getCollection(Constants.ADDON_LIST, request);
}

public List<Addon> delete(String id) throws RazorpayException {
public List<Addon> delete(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return delete(String.format(Constants.ADDON_DELETE, id), null);
}
}
66 changes: 46 additions & 20 deletions src/test/java/com/razorpay/AddonClientTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
package com.razorpay;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;



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


public class AddonClientTest extends BaseTest{
public class AddonClientTest extends BaseTest{

@InjectMocks
protected AddonClient client = new AddonClient(TEST_SECRET_KEY);
@Mock
ApiUtils apiUtils;

private static final String ADDON_ID = "ao_00000000000001";

private static final String CUSTOMER_ID = "cust_1Aa00000000004";

/**
* Retrieve all the addon details using addon id.
* @throws RazorpayException
*/


@Test
public void fetch() throws RazorpayException {
public void fetch() throws RazorpayException, JSONException, URISyntaxException {

String json = "{\n \"id\":"+ADDON_ID+",\n" +
String response = "{\n \"id\":"+ADDON_ID+",\n" +
"\"entity\":\"addon\",\n" +
"\"item\":{\n" +
"\"id\":\"item_00000000000001\",\n" +
Expand All @@ -47,32 +66,35 @@ public void fetch() throws RazorpayException {
"\"created_at\":1581597318,\n" +
"\"subscription_id\":\"sub_00000000000001\",\n" +
"\"invoice_id\":null\n}";
try {
mockResponseFromExternalClient(json);
mockResponseHTTPCodeFromExternalClient(200);
Addon fetch = client.fetch(ADDON_ID);
try{
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ADDON_GET, "ao_JniYt836HF7aQm"), null);
mockGetRequest(apiUtils,builder,null, response);

AddonClient addonClient = new AddonClient("test",apiUtils);

Addon fetch = addonClient.fetch("ao_JniYt836HF7aQm");

assertNotNull(fetch);
JSONObject item = fetch.toJson().getJSONObject("item");
assertEquals(ADDON_ID,fetch.get("id"));
assertEquals("INR",item.get("currency"));
assertEquals(30000,item.get("amount"));
assertTrue(item.getBoolean("active"));
assertTrue(item.has("unit_amount"));
String addonCreate = getHost(String.format(Constants.ADDON_GET, ADDON_ID));
verifySentRequest(false, null, addonCreate);
} catch (IOException e) {
assertTrue(false);
}
}
}

/**
* Details of all the addon can be retrieved.
* @throws RazorpayException
*/
@Test
public void fetchAll() throws RazorpayException {
public void fetchAll() throws RazorpayException, JSONException, URISyntaxException {

String json = "{\n " +
String response = "{\n " +
"\"entity\": \"collection\",\n" +
"\"count\": 1,\n" +
"\"items\": [\n {\n" +
Expand Down Expand Up @@ -103,18 +125,22 @@ public void fetchAll() throws RazorpayException {
"\"invoice_id\": \"inv_00000000000001\"\n" +
"}\n ]\n}";
try {
mockResponseFromExternalClient(json);
mockResponseHTTPCodeFromExternalClient(200);
List<Addon> fetch = client.fetchAll();

apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.ADDON_LIST, null);
mockGetRequest(apiUtils,builder,null,response);

AddonClient addonClient = new AddonClient("test",apiUtils);

List<Addon> fetch = addonClient.fetchAll();

assertNotNull(fetch);
assertEquals(true,fetch.get(0).has("id"));
assertEquals(true,fetch.get(0).has("entity"));
assertEquals(true,fetch.get(0).has("item"));
assertEquals(true,fetch.get(0).toJson().getJSONObject("item").has("hsn_code"));
String addonList = getHost(Constants.ADDON_LIST);
verifySentRequest(false, null, addonList);
} catch (IOException e) {
assertTrue(false);
}
}
}
}

0 comments on commit ba1feef

Please sign in to comment.