From f367eb0021cca1a92e612d92a0ee8fe0677c5635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Wed, 19 Jul 2017 13:48:47 -0300 Subject: [PATCH] Adding docs and tests for some API handler classes (GetAccountBalances and GetAccountByName) --- .../graphenej/api/GetAccountBalances.java | 33 ++++++++-- .../graphenej/api/GetAccountByName.java | 26 ++++++++ .../graphenej/api/GetLimitOrders.java | 32 +++++++++- .../api/android/NodeConnectionTest.java | 63 +++++++++++++++---- 4 files changed, 137 insertions(+), 17 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java index cace112..1d8a5d7 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java @@ -19,6 +19,14 @@ import java.util.List; import java.util.Map; /** + * Class that implements get_account_balances request handler. + * + * Get an account’s balances in various assets. + * + * The request returns the balances of the account + * + * @see get_account_balances API doc + * * Created by nelson on 1/13/17. */ public class GetAccountBalances extends BaseGrapheneHandler { @@ -27,18 +35,35 @@ public class GetAccountBalances extends BaseGrapheneHandler { private List mAssetList; private boolean mOneTime; - /* - * Constructor + /** + * Default Constructor + * + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @param listener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. */ - public GetAccountBalances(UserAccount userAccount, boolean oneTime, List assets, WitnessResponseListener listener) { + public GetAccountBalances(UserAccount userAccount, List assets, boolean oneTime, WitnessResponseListener listener) { super(listener); this.mUserAccount = userAccount; this.mAssetList = assets; this.mOneTime = oneTime; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in + * @param listener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. + */ public GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { - this(userAccount, true, assets, listener); + this(userAccount, assets, true, listener); } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index 2d9c6bf..a5fe247 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -20,6 +20,14 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** + * Class that implements get_account_by_name request handler. + * + * Get an account’s info by name. + * + * The request returns account data that refer to the name. + * + * @see get_account_by_name API doc + * * Created by nelson on 11/15/16. */ public class GetAccountByName extends BaseGrapheneHandler { @@ -28,6 +36,16 @@ public class GetAccountByName extends BaseGrapheneHandler { private WitnessResponseListener mListener; private boolean mOneTime; + /** + * Default Constructor + * + * @param accountName name of the account to get info + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @param listener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. + */ public GetAccountByName(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountName = accountName; @@ -35,6 +53,14 @@ public class GetAccountByName extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param accountName name of the account to get info + * @param listener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. + */ public GetAccountByName(String accountName, WitnessResponseListener listener){ this(accountName, true, listener); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java index 5429d13..b73429c 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -22,7 +22,15 @@ import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 11/15/16. + * Class that implements get_limit_orders request handler. + * + * Get limit orders in a given market. + * + * The request returns the limit orders, ordered from least price to greatest + * + * @see get_limit_orders API doc + * + * Created by nelson on 1/5/17. */ public class GetLimitOrders extends BaseGrapheneHandler { @@ -34,6 +42,18 @@ public class GetLimitOrders extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Default Constructor + * + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @param mListener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. + */ public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener mListener) { super(mListener); this.a = a; @@ -43,6 +63,16 @@ public class GetLimitOrders extends BaseGrapheneHandler { this.mListener = mListener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve + * @param mListener A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + * of the transaction broadcast operation. + */ public GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { this(a, b, limit, true, mListener); } diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java index 1d1d596..9536b1f 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java @@ -27,10 +27,10 @@ import de.bitsharesmunich.graphenej.Address; * Created by nelson on 6/26/17. */ public class NodeConnectionTest { - private String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); private String NODE_URL_1 = System.getenv("NODE_URL_1"); private String NODE_URL_2 = System.getenv("NODE_URL_2"); private String NODE_URL_3 = System.getenv("NODE_URL_3"); + private String NODE_URL_4 = System.getenv("NODE_URL_4"); private String ACCOUNT_ID = System.getenv("ACCOUNT_ID"); private String ACCOUNT_NAME = System.getenv("ACCOUNT_NAME"); private long BlOCK_TEST_NUMBER = Long.parseLong(System.getenv("BlOCK_TEST_NUMBER")); @@ -75,7 +75,7 @@ public class NodeConnectionTest { @Test public void testNodeConnection(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", true, mErrorListener); Timer timer = new Timer(); @@ -93,15 +93,22 @@ public class NodeConnectionTest { } @Test + /** + * Test for NodeConnection's addNodeUrl and addNodeUrls working together. + * + * Need to setup the NODE_URL_(1 to 4) env to work. Some of the nodes may have invalid nodes + * websockets URL just to test the hop. + * + */ public void testNodeHopFeature(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.addNodeUrl(NODE_URL_4); //Test adding a "sublist" ArrayList urlList = new ArrayList(){{ - add(NODE_URL_1); - add(NODE_URL_2); + add(NODE_URL_3); + add(NODE_URL_3); }}; - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", true, mErrorListener); @@ -119,10 +126,16 @@ public class NodeConnectionTest { } } + /** + * Test for GetAccountBalances Handler. + * + * Request balances for a valid account (Need to setup the ACCOUNT_ID env with desired account id) + * + */ @Test public void testGetAccountBalancesRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAccountBalances here"); @@ -132,7 +145,26 @@ public class NodeConnectionTest { assetList.add(BTS); assetList.add(BITDOLAR); assetList.add(BITEURO); - nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, false, assetList, new WitnessResponseListener(){ + System.out.println("Test: Request to discrete asset list"); + nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, assetList, false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("getAccountBalances.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("getAccountBalances.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } + + try{ + UserAccount userAccount = new UserAccount(ACCOUNT_ID); + System.out.println("Test: Request to all account' assets balance"); + nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, null, false, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { System.out.println("getAccountBalances.onSuccess"); @@ -158,9 +190,16 @@ public class NodeConnectionTest { } @Test + /** + * Test for GetAccountByName Handler. + * + * Request for a valid account name by name (Need to setup the ACCOUNT_NAME env with desired + * account name) + * + */ public void testGetAccountByNameRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAccountByName here"); @@ -193,7 +232,7 @@ public class NodeConnectionTest { @Test public void testGetAllAssetHoldersRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAllAssetHolders request"); @@ -226,7 +265,7 @@ public class NodeConnectionTest { @Test public void testGetBlockHeaderRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); @@ -261,7 +300,7 @@ public class NodeConnectionTest { @Test public void testGetKeyReferencesRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); Address address1 = null;