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

Item entity nmock #233

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

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

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

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

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

public Item edit(String id, JSONObject request) throws RazorpayException {
public Item edit(String id, JSONObject request) throws RazorpayException, JSONException, IOException, URISyntaxException {
return patch(String.format(Constants.ITEM, id), request);
}

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

public List<Item> delete(String id) throws RazorpayException {
public List<Item> delete(String id) throws RazorpayException, JSONException, IOException, URISyntaxException {
return delete(String.format(Constants.ITEM, id), null);
}
}
89 changes: 51 additions & 38 deletions src/test/java/com/razorpay/ItemClientTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
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.ArrayList;
import java.util.List;

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

public class ItemClientTest extends BaseTest{

@InjectMocks
protected ItemClient itemClient = new ItemClient("test");
@Mock
ApiUtils apiUtils;

public static final String ITEM_ID = "item_IJol6jPh1ummTK";

/**
* Create item with basic details such as name and amount details
* @throws RazorpayException
*/
@Test
public void create() throws RazorpayException {
public void create() throws RazorpayException, JSONException, URISyntaxException {
JSONObject request = new JSONObject("{\n" +
"\"name\": \"Book English August\",\n" +
"\"description\": \"An indian story, Booker prize winner.\",\n" +
Expand All @@ -40,17 +45,19 @@ public void create() throws RazorpayException {
"\"currency\": \"INR\"\n" +
"}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.ITEMS, null);
mockPostRequest(apiUtils,builder,request.toString(), mockedResponseJson);

ItemClient itemClient = new ItemClient("test",apiUtils);

Item item = itemClient.create(request);
assertNotNull(item);
assertEquals(ITEM_ID,item.get("id"));
assertEquals("item",item.get("entity"));
assertTrue(item.has("active"));
assertTrue(item.has("name"));
assertTrue(item.has("name"));
String createRequest = getHost(Constants.ITEMS);
verifySentRequest(true, request.toString(), createRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -61,7 +68,7 @@ public void create() throws RazorpayException {
* @throws RazorpayException
*/
@Test
public void fetch() throws RazorpayException {
public void fetch() throws RazorpayException, JSONException, URISyntaxException {

String mockedResponseJson = "{\n " +
"\"entity\": \"item\",\n" +
Expand All @@ -73,62 +80,66 @@ public void fetch() throws RazorpayException {
"\"currency\": \"INR\"\n" +
"}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ITEM, ITEM_ID), null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

ItemClient itemClient = new ItemClient("test",apiUtils);

Item fetch = itemClient.fetch(ITEM_ID);
assertNotNull(fetch);
assertEquals(ITEM_ID,fetch.get("id"));
assertEquals("item",fetch.get("entity"));
assertTrue(fetch.has("amount"));
assertTrue(fetch.has("currency"));
String fetchRequest = getHost(String.format(Constants.ITEM,ITEM_ID));
verifySentRequest(false, null, fetchRequest);
} catch (IOException e) {
assertTrue(false);
}
}

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

String mockedResponseJson = "{\n " +
"\"entity\": \"collection\",\n" +
"\"count\": 1,\n" +
"\"items\": [\n" +
"{\n" +
"\"id\": \"item_7OxoGnoxCuUKbo\",\n" +
"\"entity\" : \"item\",\n" +
"\"active\": true,\n" +
"\"name\": \"Book English August\",\n" +
"\"description\": null,\n" +
"\"amount\": 20000,\n" +
"\"currency\": \"INR\"\n" +
"}\n]\n}";
"\"id\": \"item_7OxoGnoxCuUKbo\",\n" +
"\"entity\" : \"item\",\n" +
"\"active\": true,\n" +
"\"name\": \"Book English August\",\n" +
"\"description\": null,\n" +
"\"amount\": 20000,\n" +
"\"currency\": \"INR\"\n" +
"}\n]\n}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(Constants.ITEMS, null);
mockGetRequest(apiUtils,builder,null, mockedResponseJson);

ItemClient itemClient = new ItemClient("test",apiUtils);
List<Item> fetch = itemClient.fetchAll();
assertNotNull(fetch);
assertTrue(fetch.get(0).has("entity"));
assertTrue(fetch.get(0).has("id"));
assertTrue(fetch.get(0).has("active"));
String fetchRequest = getHost(Constants.ITEMS);
verifySentRequest(false, null, fetchRequest);

} catch (IOException e) {
assertTrue(false);
}
}

/**
* Update an item details using item id with object of that properties
* @throws RazorpayException
*/
@Test
public void edit() throws RazorpayException{
public void edit() throws RazorpayException, JSONException, URISyntaxException {

JSONObject request = new JSONObject("{\n" +
"\"name\": \"Book Ignited Minds - Updated name!\",\n" +
Expand All @@ -146,16 +157,17 @@ public void edit() throws RazorpayException{
"\"currency\": \"INR\"\n" +
"}";
try {
mockResponseFromExternalClient(mockedResponseJson);
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ITEM, ITEM_ID), null);
mockPatchRequest(apiUtils,builder,request.toString(), mockedResponseJson);

ItemClient itemClient = new ItemClient("test",apiUtils);
Item item = itemClient.edit(ITEM_ID,request);
assertNotNull(item);
assertEquals(ITEM_ID,item.get("id"));
assertEquals("item",item.get("entity"));
assertTrue(item.has("amount"));
assertTrue(item.has("description"));
String editRequest = getHost(String.format(Constants.ITEM, ITEM_ID));
verifySentRequest(true, request.toString(), editRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand All @@ -166,15 +178,16 @@ public void edit() throws RazorpayException{
* @throws RazorpayException
*/
@Test
public void testDeleteItem() throws IOException, RazorpayException {
public void testDeleteItem() throws RazorpayException, JSONException, URISyntaxException {
try {
ArrayList<String> mockedResponseJson = new ArrayList<String>();
mockResponseFromExternalClient(String.valueOf(mockedResponseJson));
mockResponseHTTPCodeFromExternalClient(200);
apiUtils = mock(ApiUtils.class);
URL builder = ApiClient.getBuilder(String.format(Constants.ITEM, ITEM_ID), null);
mockDeleteRequest(apiUtils,builder,null, mockedResponseJson.toString());

ItemClient itemClient = new ItemClient("test",apiUtils);
List<Item> item = itemClient.delete(ITEM_ID);
assertNotNull(item);
String editRequest = getHost(String.format(Constants.ITEM,ITEM_ID));
verifySentRequest(false, null, editRequest);
} catch (IOException e) {
assertTrue(false);
}
Expand Down