Skip to content

Commit

Permalink
Merge pull request #245 from razorpay/qrcode_entity_nmock
Browse files Browse the repository at this point in the history
qrcode entity and mock
  • Loading branch information
ankitdas13 authored Aug 24, 2022
2 parents f695661 + a5679ac commit 57631fa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
29 changes: 15 additions & 14 deletions src/main/java/com/razorpay/QrCodeClient.java
Original file line number Diff line number Diff line change
@@ -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);
}

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

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

public List<QrCode> fetchAllPayments(String id) throws RazorpayException {
public List<QrCode> fetchAllPayments(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return fetchAllPayments(id,null);
}

public List<QrCode> fetchAllPayments(String id,JSONObject request) throws RazorpayException {
public List<QrCode> 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);
}

}
}
113 changes: 60 additions & 53 deletions src/test/java/com/razorpay/QrCodeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
Expand All @@ -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" +
Expand All @@ -106,17 +110,18 @@ 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"));
assertEquals(QRCODE_ID,fetch.get("id"));
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);
}
Expand All @@ -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" +
Expand Down Expand Up @@ -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 <QrCode> 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);
}
Expand All @@ -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" +
Expand All @@ -205,17 +211,18 @@ 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"));
assertEquals(QRCODE_ID,fetch.get("id"));
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);
}
Expand Down

0 comments on commit 57631fa

Please sign in to comment.