From 70b251a665393ddf88566d8d6859cb28461966fa Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 9 Nov 2017 18:21:41 -0500 Subject: [PATCH] Adding support for a string-based query when using the LookupAssetSymbols API --- .../graphenej/api/LookupAssetSymbols.java | 15 +++-- .../graphenej/api/LookupAssetSymbolsTest.java | 60 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 graphenej/src/test/java/cy/agorise/graphenej/api/LookupAssetSymbolsTest.java diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/LookupAssetSymbols.java b/graphenej/src/main/java/cy/agorise/graphenej/api/LookupAssetSymbols.java index d60f90c..d467dde 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/LookupAssetSymbols.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/LookupAssetSymbols.java @@ -28,7 +28,7 @@ import cy.agorise.graphenej.models.WitnessResponse; */ public class LookupAssetSymbols extends BaseGrapheneHandler { private WitnessResponseListener mListener; - private List assets; + private List assets; private boolean mOneTime; @@ -42,7 +42,7 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { * be implemented by the party interested in being notified about the * success/failure of the operation. */ - public LookupAssetSymbols(List assets, boolean oneTime, WitnessResponseListener listener){ + public LookupAssetSymbols(List assets, boolean oneTime, WitnessResponseListener listener){ super(listener); this.assets = assets; this.mOneTime = oneTime; @@ -57,7 +57,7 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { * be implemented by the party interested in being notified about the * success/failure of the operation. */ - public LookupAssetSymbols(List assets, WitnessResponseListener listener){ + public LookupAssetSymbols(List assets, WitnessResponseListener listener){ this(assets, true, listener); } @@ -65,8 +65,13 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList params = new ArrayList<>(); ArrayList subArray = new ArrayList<>(); - for(Asset asset : this.assets){ - subArray.add(asset.getObjectId()); + for(int i = 0; i < assets.size(); i++){ + Object obj = assets.get(i); + if(obj instanceof String){ + subArray.add((String) obj); + }else{ + subArray.add(((Asset) obj).getObjectId()); + } params.add(subArray); } ApiCall loginCall = new ApiCall(0, RPC.CALL_LOOKUP_ASSET_SYMBOLS, params, RPC.VERSION, 0); diff --git a/graphenej/src/test/java/cy/agorise/graphenej/api/LookupAssetSymbolsTest.java b/graphenej/src/test/java/cy/agorise/graphenej/api/LookupAssetSymbolsTest.java new file mode 100644 index 0000000..de0d13c --- /dev/null +++ b/graphenej/src/test/java/cy/agorise/graphenej/api/LookupAssetSymbolsTest.java @@ -0,0 +1,60 @@ +package cy.agorise.graphenej.api; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import cy.agorise.graphenej.Asset; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.BaseResponse; +import cy.agorise.graphenej.models.WitnessResponse; + +/** + * Testing the standalone usage of the {@link LookupAssetSymbols} API handler. + */ + +public class LookupAssetSymbolsTest extends BaseApiTest { + + @Test + public void testLookupAssetSymbolsWithString(){ + ArrayList assetSymbols = new ArrayList<>(); + assetSymbols.add("USD"); + mWebSocket.addListener(new LookupAssetSymbols(assetSymbols, true, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + List assets = (List) response.result; + Assert.assertEquals(1, assets.size()); + Assert.assertEquals("1.3.121", assets.get(0).getObjectId()); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + })); + } + + @Test + public void testLookupAssetSymbolsWithAsset(){ + ArrayList assetSymbols = new ArrayList<>(); + assetSymbols.add(new Asset("1.3.121")); + mWebSocket.addListener(new LookupAssetSymbols(assetSymbols, true, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + List assets = (List) response.result; + Assert.assertEquals(1, assets.size()); + Assert.assertEquals("1.3.121", assets.get(0).getObjectId()); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + })); + } +}