OSDN Git Service

add BalanceTest and some list methods
authorsuccessli <successli@outlook.com>
Wed, 30 May 2018 11:58:01 +0000 (19:58 +0800)
committersuccessli <successli@outlook.com>
Wed, 30 May 2018 11:58:01 +0000 (19:58 +0800)
src/main/java/io/bytom/api/Balance.java
src/test/java/io/bytom/integration/BalanceTest.java [new file with mode: 0644]
src/test/java/io/bytom/integration/TransactionTest.java

index 319cbc9..f1478f0 100644 (file)
@@ -7,6 +7,7 @@ import io.bytom.http.Client;
 import org.apache.log4j.Logger;
 
 import java.lang.reflect.Type;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -68,14 +69,56 @@ public class Balance {
          */
         public List<Balance> list(Client client) throws BytomException {
 
-            // TODO: 2018/5/23 need tx and test
             Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Balance.class});
             List<Balance> balanceList = client.request("list-balances", null, listType);
             logger.info("list-balances:");
             logger.info("size of :" + balanceList.size());
-            logger.info(balanceList.get(0).toJson());
+            for (Balance result : balanceList) {
+                logger.info(result.toJson());
+            }
 
             return balanceList;
         }
+
+        /**
+         * sum of all Asset alias amount
+         *
+         * @param client
+         * @param assetAlias
+         * @return
+         * @throws BytomException
+         */
+        public long listByAssetAlias(Client client, String assetAlias) throws BytomException {
+            List<Balance> balanceList = list(client);
+            Balance assetBalance = new Balance();
+            long amount = 0;
+            for (Balance result : balanceList) {
+                if (result.assetAlias.equals(assetAlias)) {
+                    amount += result.amount;
+                }
+            }
+            logger.info("amount:"+amount);
+            return amount;
+        }
+
+        /**
+         * sum of all Account alias amount
+         *
+         * @param client
+         * @param accountAlias
+         * @return
+         * @throws BytomException
+         */
+        public List<Balance> listByAccountAlias(Client client, String accountAlias) throws BytomException {
+            List<Balance> balanceList = list(client);
+            List<Balance> accountBalance = new ArrayList<>();
+            for (Balance result : balanceList) {
+                if (result.accountAlias.equals(accountAlias)) {
+                    accountBalance.add(result);
+                    logger.info(result.toJson());
+                }
+            }
+            return accountBalance;
+        }
     }
 }
diff --git a/src/test/java/io/bytom/integration/BalanceTest.java b/src/test/java/io/bytom/integration/BalanceTest.java
new file mode 100644 (file)
index 0000000..4442a24
--- /dev/null
@@ -0,0 +1,46 @@
+package io.bytom.integration;
+
+import io.bytom.TestUtils;
+import io.bytom.api.Balance;
+import io.bytom.api.Wallet;
+import io.bytom.exception.BytomException;
+import io.bytom.http.Client;
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class BalanceTest {
+
+    static Client client;
+
+    static {
+        try {
+            client = TestUtils.generateClient();
+        } catch (BytomException e) {
+            e.printStackTrace();
+        }
+    }
+
+    static Balance balance;
+
+    @Test
+    public void testBalanceList() throws Exception {
+        List<Balance> balanceList = new Balance.QueryBuilder().list(client);
+        Assert.assertNotNull(balanceList);
+    }
+
+    @Test
+    public void testBalanceByAssetAlias() throws Exception {
+        long amount = new Balance.QueryBuilder().listByAssetAlias(client, "BTM");
+        Assert.assertNotNull(amount);
+    }
+
+    @Test
+    public void testBalanceByAccountAlias() throws Exception {
+        List<Balance> balanceList = new Balance.QueryBuilder().listByAccountAlias(client, "test");
+        Assert.assertNotNull(balanceList);
+    }
+
+}
index a922ecb..a03b405 100644 (file)
@@ -24,8 +24,8 @@ public class TransactionTest {
     static Account receiverAccount;
     static Receiver senderReceiver;
     static Receiver receiverReceiver;
-    static Address senderAddress;
-    static Address receiverAddress;
+    static Account.Address senderAddress;
+    static Account.Address receiverAddress;
     static Asset senderAsset;
     static Asset receiverAsset;