From a5679ac88c214d4571f6fbf044a8ae7cae3e6c76 Mon Sep 17 00:00:00 2001 From: ankitdas13 Date: Sun, 14 Aug 2022 14:39:52 +0530 Subject: [PATCH] qrcode entity and mock --- src/main/java/com/razorpay/QrCodeClient.java | 29 ++--- .../java/com/razorpay/QrCodeClientTest.java | 113 ++++++++++-------- 2 files changed, 75 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/razorpay/QrCodeClient.java b/src/main/java/com/razorpay/QrCodeClient.java index 4251944b..ed7b727e 100644 --- a/src/main/java/com/razorpay/QrCodeClient.java +++ b/src/main/java/com/razorpay/QrCodeClient.java @@ -1,22 +1,23 @@ package com.razorpay; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.List; +import org.json.JSONException; import org.json.JSONObject; -import okhttp3.Response; - public class QrCodeClient extends ApiClient { - QrCodeClient(String auth) { - super(auth); + QrCodeClient(String auth, ApiUtils apiUtils) { + super(auth,apiUtils); } - public QrCode create(JSONObject request) throws RazorpayException { + public QrCode create(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException { return post(Constants.QRCODE_CREATE, request); } - public QrCode fetch(String id) throws RazorpayException { + public QrCode fetch(String id) throws RazorpayException, JSONException, IOException, URISyntaxException { return get(String.format(Constants.QRCODE_FETCH, id), null); } @@ -25,7 +26,7 @@ public QrCode 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); } @@ -33,20 +34,20 @@ public List fetchAll() throws RazorpayException { * This method get list of QrCodes filtered by parameters @request * @throws RazorpayException */ - public List fetchAll(JSONObject request) throws RazorpayException { - return getCollection(Constants.QRCODE_LIST, request); + public List fetchAll(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException { + return getCollection(Constants.QRCODE_LIST, request); } - public List fetchAllPayments(String id) throws RazorpayException { + public List fetchAllPayments(String id) throws RazorpayException, JSONException, IOException, URISyntaxException { return fetchAllPayments(id,null); } - public List fetchAllPayments(String id,JSONObject request) throws RazorpayException { + public List fetchAllPayments(String id, JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException { return getCollection(Constants.QRCODE_LIST+"/"+id+"/"+Constants.QRCODE_FETCH_PAYMENT, request); } - - public QrCode close(String id) throws RazorpayException { + + public QrCode close(String id) throws RazorpayException, JSONException, IOException, URISyntaxException { return post(String.format(Constants.QRCODE_CLOSE, id), null); } -} +} \ No newline at end of file diff --git a/src/test/java/com/razorpay/QrCodeClientTest.java b/src/test/java/com/razorpay/QrCodeClientTest.java index 4f756467..8339fef8 100644 --- a/src/test/java/com/razorpay/QrCodeClientTest.java +++ b/src/test/java/com/razorpay/QrCodeClientTest.java @@ -3,16 +3,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 QrCodeClientTest extends BaseTest{ - @InjectMocks - protected QrCodeClient qrCodeClient = new QrCodeClient(TEST_SECRET_KEY); + @Mock + ApiUtils apiUtils; private static final String QRCODE_ID = "qr_HMsVL8HOpbMcjU"; @@ -24,53 +27,54 @@ public class QrCodeClientTest extends BaseTest{ * @throws RazorpayException */ @Test - public void create() throws RazorpayException { + public void create() throws Exception { JSONObject request = new JSONObject("{\n" + - " \"type\": \"upi_qr\",\n" + - " \"name\": \"Store_1\",\n" + - " \"usage\": \"single_use\",\n" + - " \"fixed_amount\": true,\n" + - " \"payment_amount\": 300,\n" + - " \"description\": \"For Store 1\",\n" + - " \"customer_id\": \"cust_HKsR5se84c5LTO\",\n" + - " \"close_by\": 1681615838,\n" + - " \"notes\": {\n" + - " \"purpose\": \"Test UPI QR code notes\"\n" + - " }\n" + - "}"); + " \"type\": \"upi_qr\",\n" + + " \"name\": \"Store_1\",\n" + + " \"usage\": \"single_use\",\n" + + " \"fixed_amount\": true,\n" + + " \"payment_amount\": 300,\n" + + " \"description\": \"For Store 1\",\n" + + " \"customer_id\": \"cust_HKsR5se84c5LTO\",\n" + + " \"close_by\": 1681615838,\n" + + " \"notes\": {\n" + + " \"purpose\": \"Test UPI QR code notes\"\n" + + " }\n" + + "}"); String mockedResponseJson = "{\n" + - " \"id\": "+QRCODE_ID+",\n" + - " \"entity\": \"qr_code\",\n" + - " \"created_at\": 1623660301,\n" + - " \"name\": \"Store_1\",\n" + - " \"usage\": \"single_use\",\n" + - " \"type\": \"upi_qr\",\n" + - " \"image_url\": \"https://rzp.io/i/BWcUVrLp\",\n" + - " \"payment_amount\": 300,\n" + - " \"status\": \"active\",\n" + - " \"description\": \"For Store 1\",\n" + - " \"fixed_amount\": true,\n" + - " \"payments_amount_received\": 0,\n" + - " \"payments_count_received\": 0,\n" + - " \"notes\": {\n" + - " \"purpose\": \"Test UPI QR code notes\"\n" + - " },\n" + - " \"customer_id\": \"cust_HKsR5se84c5LTO\",\n" + - " \"close_by\": 1681615838\n" + - "}"; + " \"id\": "+QRCODE_ID+",\n" + + " \"entity\": \"qr_code\",\n" + + " \"created_at\": 1623660301,\n" + + " \"name\": \"Store_1\",\n" + + " \"usage\": \"single_use\",\n" + + " \"type\": \"upi_qr\",\n" + + " \"image_url\": \"https://rzp.io/i/BWcUVrLp\",\n" + + " \"payment_amount\": 300,\n" + + " \"status\": \"active\",\n" + + " \"description\": \"For Store 1\",\n" + + " \"fixed_amount\": true,\n" + + " \"payments_amount_received\": 0,\n" + + " \"payments_count_received\": 0,\n" + + " \"notes\": {\n" + + " \"purpose\": \"Test UPI QR code notes\"\n" + + " },\n" + + " \"customer_id\": \"cust_HKsR5se84c5LTO\",\n" + + " \"close_by\": 1681615838\n" + + "}"; try { - mockResponseFromExternalClient(mockedResponseJson); - mockResponseHTTPCodeFromExternalClient(200); + apiUtils = mock(ApiUtils.class); + URL builder = ApiClient.getBuilder(Constants.QRCODE_CREATE, null); + mockPostRequest(apiUtils,builder,request.toString(), mockedResponseJson); + + QrCodeClient qrCodeClient = new QrCodeClient("test",apiUtils); QrCode fetch = qrCodeClient.create(request); assertNotNull(fetch); assertEquals("qr_code",fetch.get("entity")); assertEquals(QRCODE_ID,fetch.get("id")); assertTrue(fetch.has("close_by")); assertTrue(fetch.has("fixed_amount")); - String createRequest = getHost(Constants.QRCODE_CREATE); - verifySentRequest(true, request.toString(), createRequest); } catch (IOException e) { assertTrue(false); } @@ -81,7 +85,7 @@ public void create() throws RazorpayException { * @throws RazorpayException */ @Test - public void fetch() throws RazorpayException{ + public void fetch() throws Exception{ String mockedResponseJson = "{\n" + " \"id\": "+QRCODE_ID+",\n" + " \"entity\": \"qr_code\",\n" + @@ -106,8 +110,11 @@ public void fetch() throws RazorpayException{ "}"; try { - mockResponseFromExternalClient(mockedResponseJson); - mockResponseHTTPCodeFromExternalClient(200); + apiUtils = mock(ApiUtils.class); + URL builder = ApiClient.getBuilder(String.format(Constants.QRCODE_FETCH, QRCODE_ID), null); + mockGetRequest(apiUtils,builder,null, mockedResponseJson); + + QrCodeClient qrCodeClient = new QrCodeClient("test",apiUtils); QrCode fetch = qrCodeClient.fetch(QRCODE_ID); assertNotNull(fetch); assertEquals("upi_qr",fetch.get("type")); @@ -115,8 +122,6 @@ public void fetch() throws RazorpayException{ assertEquals(CUSTOMER_ID,fetch.get("customer_id")); assertTrue(fetch.has("close_by")); assertTrue(fetch.has("fixed_amount")); - String fetchRequest = getHost(String.format(Constants.QRCODE_FETCH, QRCODE_ID)); - verifySentRequest(false, null, fetchRequest); } catch (IOException e) { assertTrue(false); } @@ -128,7 +133,7 @@ public void fetch() throws RazorpayException{ * @throws RazorpayException */ @Test - public void fetchAll() throws RazorpayException{ + public void fetchAll() throws Exception{ String mockedResponseJson = "{\n" + " \"entity\": \"collection\",\n" + " \"count\": 2,\n" + @@ -159,16 +164,17 @@ public void fetchAll() throws RazorpayException{ "}"; try { - mockResponseFromExternalClient(mockedResponseJson); - mockResponseHTTPCodeFromExternalClient(200); + apiUtils = mock(ApiUtils.class); + URL builder = ApiClient.getBuilder(Constants.QRCODE_LIST, null); + mockGetRequest(apiUtils,builder,null, mockedResponseJson); + + QrCodeClient qrCodeClient = new QrCodeClient("test",apiUtils); List fetch = qrCodeClient.fetchAll(); assertNotNull(fetch); assertTrue(fetch.get(0).has("type")); assertTrue(fetch.get(0).has("id")); assertTrue(fetch.get(0).has("customer_id")); assertTrue(fetch.get(0).has("close_by")); - String fetchRequest = getHost(Constants.QRCODE_LIST); - verifySentRequest(false, null, fetchRequest); } catch (IOException e) { assertTrue(false); } @@ -179,7 +185,7 @@ public void fetchAll() throws RazorpayException{ * @throws RazorpayException */ @Test - public void close() throws RazorpayException{ + public void close() throws Exception{ String mockedResponseJson = "{\n" + " \"id\": "+QRCODE_ID+",\n" + @@ -205,8 +211,11 @@ public void close() throws RazorpayException{ "}"; try { - mockResponseFromExternalClient(mockedResponseJson); - mockResponseHTTPCodeFromExternalClient(200); + apiUtils = mock(ApiUtils.class); + URL builder = ApiClient.getBuilder(String.format(Constants.QRCODE_CLOSE, QRCODE_ID), null); + mockPostRequest(apiUtils,builder,null, mockedResponseJson); + + QrCodeClient qrCodeClient = new QrCodeClient("test",apiUtils); QrCode fetch = qrCodeClient.close(QRCODE_ID); assertNotNull(fetch); assertEquals("upi_qr",fetch.get("type")); @@ -214,8 +223,6 @@ public void close() throws RazorpayException{ assertEquals(CUSTOMER_ID,fetch.get("customer_id")); assertTrue(fetch.has("close_by")); assertTrue(fetch.has("fixed_amount")); - String closeRequest = getHost(String.format(Constants.QRCODE_CLOSE,QRCODE_ID)); - verifySentRequest(false, null, closeRequest); } catch (IOException e) { assertTrue(false); }