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

Order entity nmock #232

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/OrderClient.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 OrderClient extends ApiClient {

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

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

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

public List<Order> fetchAll(JSONObject request) throws RazorpayException {
public List<Order> fetchAll(JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return getCollection(Constants.ORDER_LIST, request);
}

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

public List<Payment> fetchPayments(String id) throws RazorpayException {
public List<Payment> fetchPayments(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return getCollection(String.format(Constants.ORDER_PAYMENT_LIST, id), null);
}

public Order edit(String id, JSONObject request) throws RazorpayException {
public Order edit(String id, JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return patch(String.format(Constants.ORDER_EDIT, id), request);
}
}
69 changes: 40 additions & 29 deletions src/test/java/com/razorpay/OrderClientTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.razorpay;

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

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

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

public class OrderClientTest extends BaseTest{

@InjectMocks
protected OrderClient orderClient = new OrderClient(TEST_SECRET_KEY);
@Mock
ApiUtils apiUtils;

private static final String ORDER_ID = "order_EKwxwAgItmmXdp";

Expand All @@ -21,7 +25,7 @@ public class OrderClientTest extends BaseTest{
* @throws RazorpayException
*/
@Test
public void create() throws RazorpayException {
public void create() throws RazorpayException, JSONException, URISyntaxException {
JSONObject request = new JSONObject("{" +
"amount:50000," +
"currency:\"INR\"," +
Expand All @@ -44,16 +48,18 @@ public void create() throws RazorpayException {
"\"notes\":[]," +
"\"created_at\":1582628071}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.ORDER_CREATE, null);
mockPostRequest(apiUtils,builder,request.toString(), mockedResponseJson);

OrderClient orderClient = new OrderClient("test",apiUtils);

Order fetch = orderClient.create(request);
assertNotNull(fetch);
assertEquals(ORDER_ID,fetch.get("id"));
assertEquals("order",fetch.get("entity"));
assertTrue(fetch.has("amount"));
assertTrue(fetch.has("amount_paid"));
String createRequest = getHost(Constants.ORDER_CREATE);
verifySentRequest(true, request.toString(), createRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -64,7 +70,7 @@ public void create() throws RazorpayException {
* @throws RazorpayException
*/
@Test
public void fetchAll() throws RazorpayException{
public void fetchAll() throws RazorpayException, JSONException, URISyntaxException {
String mockedResponseJson = "{" +
"\"entity\":\"collection\"," +
"\"count\":1,\"items\":" +
Expand All @@ -81,15 +87,16 @@ public void fetchAll() throws RazorpayException{
"\"notes\":[]," +
"\"created_at\":1582637108}]}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.ORDER_LIST, null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

OrderClient orderClient = new OrderClient("test",apiUtils);
List<Order> fetch = orderClient.fetchAll();
assertNotNull(fetch);
assertTrue(fetch.get(0).has("entity"));
assertTrue(fetch.get(0).has("amount"));
assertTrue(fetch.get(0).has("amount_paid"));
String fetchRequest = getHost(Constants.ORDER_LIST);
verifySentRequest(false, null, fetchRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -100,7 +107,7 @@ public void fetchAll() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void fetch() throws RazorpayException{
public void fetch() throws RazorpayException, JSONException, URISyntaxException {
String mockedResponseJson = "{\"id\":"+ORDER_ID+"," +
"\"entity\":\"order\"," +
"\"amount\":2200," +
Expand All @@ -113,16 +120,17 @@ public void fetch() throws RazorpayException{
"\"notes\":[]," +
"\"created_at\":1572505143}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ORDER_GET, ORDER_ID), null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

OrderClient orderClient = new OrderClient("test",apiUtils);
Order fetch = orderClient.fetch(ORDER_ID);
assertNotNull(fetch);
assertEquals(true,fetch.has("id"));
assertTrue(fetch.has("entity"));
assertTrue(fetch.has("amount"));
assertTrue(fetch.has("amount_paid"));
String fetchRequest = getHost(String.format(Constants.ORDER_GET,ORDER_ID));
verifySentRequest(false, null, fetchRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -133,7 +141,8 @@ public void fetch() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void fetchPayments() throws RazorpayException{
public void fetchPayments() throws RazorpayException, JSONException, URISyntaxException {

String mockedResponseJson = "{" +
"\"entity\":\"collection\"," +
"\"count\":1," +
Expand Down Expand Up @@ -163,15 +172,16 @@ public void fetchPayments() throws RazorpayException{
"\"error_description\":null," +
"\"created_at\":1572505160}]}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ORDER_PAYMENT_LIST, ORDER_ID), null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

OrderClient orderClient = new OrderClient("test",apiUtils);
List<Payment> fetch = orderClient.fetchPayments(ORDER_ID);
assertNotNull(fetch);
assertTrue(fetch.get(0).has("id"));
assertTrue(fetch.get(0).has("amount"));
assertTrue(fetch.get(0).has("currency"));
String fetchRequest = getHost(String.format(Constants.ORDER_PAYMENT_LIST, ORDER_ID));
verifySentRequest(false, null, fetchRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -182,7 +192,8 @@ public void fetchPayments() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void edit() throws RazorpayException {
public void edit() throws RazorpayException, JSONException, URISyntaxException {

JSONObject request = new JSONObject("{" +
"\"notes\":{" +
"\"key1\":\"value3\"," +
Expand All @@ -207,14 +218,14 @@ public void edit() throws RazorpayException {
"\"created_at\":1572505143" +
"}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ORDER_EDIT, ORDER_ID), null);
mockPatchRequest(apiUtils,builder,request.toString(), mockedResponseJson);

OrderClient orderClient = new OrderClient("test",apiUtils);
Order fetch = orderClient.edit(ORDER_ID,request);
assertNotNull(fetch);
assertEquals(ORDER_ID,fetch.get("id"));
assertEquals(ORDER_ID,fetch.get("id"));
String editRequest = getHost(String.format(Constants.ORDER_EDIT, ORDER_ID));
verifySentRequest(true, request.toString(), editRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand Down