From ba1feef06ae965f18b10752a7e221e9c02dd4758 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Sun, 14 Aug 2022 14:07:41 +0530 Subject: [PATCH] addon entity and mock --- src/main/java/com/razorpay/AddonClient.java | 15 +++-- .../java/com/razorpay/AddonClientTest.java | 66 +++++++++++++------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/razorpay/AddonClient.java b/src/main/java/com/razorpay/AddonClient.java index d5cafcea..1a52c5db 100644 --- a/src/main/java/com/razorpay/AddonClient.java +++ b/src/main/java/com/razorpay/AddonClient.java @@ -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); } @@ -20,7 +23,7 @@ public Addon fetch(String id) throws RazorpayException { * with a default values without filteration * @throws RazorpayException */ - public List fetchAll() throws RazorpayException { + public List fetchAll() throws RazorpayException, JSONException, IOException, URISyntaxException { return fetchAll(null); } @@ -28,11 +31,11 @@ public List fetchAll() throws RazorpayException { * This method get list of Addons filtered by parameters @request * @throws RazorpayException */ - public List fetchAll(JSONObject request) throws RazorpayException { + public List fetchAll(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException { return getCollection(Constants.ADDON_LIST, request); } - public List delete(String id) throws RazorpayException { + public List delete(String id) throws RazorpayException, JSONException, IOException, URISyntaxException { return delete(String.format(Constants.ADDON_DELETE, id), null); } } \ No newline at end of file diff --git a/src/test/java/com/razorpay/AddonClientTest.java b/src/test/java/com/razorpay/AddonClientTest.java index 3bcd6658..993d9d84 100644 --- a/src/test/java/com/razorpay/AddonClientTest.java +++ b/src/test/java/com/razorpay/AddonClientTest.java @@ -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" + @@ -47,10 +66,15 @@ 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")); @@ -58,11 +82,9 @@ public void fetch() throws RazorpayException { 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); - } + } } /** @@ -70,9 +92,9 @@ public void fetch() throws RazorpayException { * @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" + @@ -103,18 +125,22 @@ public void fetchAll() throws RazorpayException { "\"invoice_id\": \"inv_00000000000001\"\n" + "}\n ]\n}"; try { - mockResponseFromExternalClient(json); - mockResponseHTTPCodeFromExternalClient(200); - List 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 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); } } -} +} \ No newline at end of file