OSDN Git Service

modify Block class and add WalletTest MessageTest BlockTest
authorsuccessli <successli@outlook.com>
Mon, 28 May 2018 06:27:15 +0000 (14:27 +0800)
committersuccessli <successli@outlook.com>
Mon, 28 May 2018 06:27:15 +0000 (14:27 +0800)
src/main/java/io/bytom/api/Block.java
src/test/java/io/bytom/AppTest.java
src/test/java/io/bytom/integration/BlockTest.java [new file with mode: 0644]
src/test/java/io/bytom/integration/MessageTest.java [new file with mode: 0644]
src/test/java/io/bytom/integration/WalletTest.java [new file with mode: 0644]

index cde9c07..ecda5b1 100644 (file)
@@ -11,23 +11,25 @@ import java.util.Map;
 
 public class Block {
 
-    public Integer bits;
+    public String hash;
 
-    public String difficulty;
+    public Integer size;
 
-    public String hash;
+    public Integer version;
 
     public Integer height;
 
-    public Integer nonce;
-
     @SerializedName("previous_block_hash")
     public String previousBlockHash;
 
-    public Integer size;
-
     public Integer timestamp;
 
+    public Integer nonce;
+
+    public long bits;
+
+    public String difficulty;
+
     @SerializedName("transaction_merkle_root")
     public String transactionsMerkleRoot;
 
@@ -36,7 +38,6 @@ public class Block {
 
     public List<BlockTx> transactions;
 
-    public Integer version;
 
     private static Logger logger = Logger.getLogger(Block.class);
 
@@ -106,6 +107,7 @@ public class Block {
          * @throws BytomException
          */
         public Block getBlock(Client client) throws BytomException {
+
             Block block = client.request("get-block", this, Block.class);
 
             logger.info("get-block:");
@@ -197,11 +199,13 @@ public class Block {
         /**
          * List of specified inputs for a transaction.
          */
+        @SerializedName("inputs")
         private List<AnnotatedInput> inputs;
 
         /**
          * List of specified outputs for a transaction.
          */
+        @SerializedName("outputs")
         private List<AnnotatedOutput> outputs;
     }
 
@@ -221,7 +225,7 @@ public class Block {
          * The definition of the asset being issued or spent (possibly null).
          */
         @SerializedName("asset_definition")
-        private String assetDefinition;
+        private Map<String, Object> assetDefinition;
 
         /**
          * The id of the asset being issued or spent.
@@ -241,13 +245,13 @@ public class Block {
         /**
          * The number of units of the asset being controlled.
          */
-        private Integer amount;
+        private long amount;
 
         /**
          * The definition of the asset being controlled (possibly null).
          */
         @SerializedName("asset_definition")
-        private String assetDefinition;
+        private Map<String, Object> assetDefinition;
 
         /**
          * The id of the asset being controlled.
index b5d58a9..d25fadf 100644 (file)
@@ -1,6 +1,8 @@
 package io.bytom;
 
+import com.google.gson.Gson;
 import com.squareup.okhttp.*;
+import io.bytom.api.Block;
 import org.junit.Test;
 
 import java.io.IOException;
@@ -54,5 +56,54 @@ public class AppTest {
 
         System.out.println(response.body().string());
     }
+
+    @Test
+    public void testBlockGet() throws Exception {
+        String postBody = "{\n" +
+                "        \"hash\": \"37eabf4d321f43b930f52b3af2e53b2ad2dbb234949e778213a4a54e4be04ea8\",\n" +
+                "        \"size\": 388,\n" +
+                "        \"version\": 1,\n" +
+                "        \"height\": 158,\n" +
+                "        \"previous_block_hash\": \"2cfa73d6ed14a7fcb3a8ccc2d4c45b0fd4c7754ec6fafaa0964045c23e2131cd\",\n" +
+                "        \"timestamp\": 1527241075,\n" +
+                "        \"nonce\": 0,\n" +
+                "        \"bits\": 2305843009214532812,\n" +
+                "        \"difficulty\": \"5789598940468732338727519302629705571043137272394850465663635070376277442560\",\n" +
+                "        \"transaction_merkle_root\": \"62755c4770374e696c4fecd94e022ccf6f2adc00f409ac694a5cb92fe02353eb\",\n" +
+                "        \"transaction_status_hash\": \"c9c377e5192668bc0a367e4a4764f11e7c725ecced1d7b6a492974fab1b6d5bc\",\n" +
+                "        \"transactions\": [\n" +
+                "            {\n" +
+                "                \"id\": \"5c38b8107d53cbfebd19adbd11f2839d914099a74e491f1cf75f8b18320c84e2\",\n" +
+                "                \"version\": 1,\n" +
+                "                \"size\": 77,\n" +
+                "                \"time_range\": 0,\n" +
+                "                \"inputs\": [\n" +
+                "                    {\n" +
+                "                        \"type\": \"coinbase\",\n" +
+                "                        \"asset_id\": \"0000000000000000000000000000000000000000000000000000000000000000\",\n" +
+                "                        \"asset_definition\": {},\n" +
+                "                        \"amount\": 0,\n" +
+                "                        \"arbitrary\": \"c29e\"\n" +
+                "                    }\n" +
+                "                ],\n" +
+                "                \"outputs\": [\n" +
+                "                    {\n" +
+                "                        \"type\": \"control\",\n" +
+                "                        \"id\": \"9acda4f2bd83c7114a8837b596860df773a7e61b1d6b0a774addda1473361ad4\",\n" +
+                "                        \"position\": 0,\n" +
+                "                        \"asset_id\": \"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\",\n" +
+                "                        \"asset_definition\": {},\n" +
+                "                        \"amount\": 41250000000,\n" +
+                "                        \"control_program\": \"001412a47b53b3bbdaf9f3510e1d8c19d69f81ca9142\",\n" +
+                "                        \"address\": \"sm1qz2j8k5anh0d0nu63pcwccxwkn7qu4y2zjwaj5h\"\n" +
+                "                    }\n" +
+                "                ],\n" +
+                "                \"status_fail\": false\n" +
+                "            }\n" +
+                "        ]\n" +
+                "    }";
+        Gson gson = new Gson();
+        Block block = gson.fromJson(postBody, Block.class);
+    }
 }
 
diff --git a/src/test/java/io/bytom/integration/BlockTest.java b/src/test/java/io/bytom/integration/BlockTest.java
new file mode 100644 (file)
index 0000000..71b8e3f
--- /dev/null
@@ -0,0 +1,85 @@
+package io.bytom.integration;
+
+import io.bytom.TestUtils;
+import io.bytom.api.Asset;
+import io.bytom.api.Block;
+import io.bytom.api.Wallet;
+import io.bytom.exception.BytomException;
+import io.bytom.http.Client;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class BlockTest {
+
+    static Client client;
+
+    static {
+        try {
+            client = TestUtils.generateClient();
+        } catch (BytomException e) {
+            e.printStackTrace();
+        }
+    }
+
+    static Block block;
+    static Block.BlockHeader blockHeader;
+    static Block.BlockDifficulty blockDifficulty;
+    static Block.BlockHashRate blockHashRate;
+
+    @Test
+    public void testBlockCountGet() throws Exception {
+        int count = Block.getBlockCount(client);
+        Assert.assertEquals(158, count);
+    }
+
+    @Test
+    public void testBlockHashGet() throws Exception {
+        String blockHash = Block.getBlockHash(client);
+        Assert.assertNotNull(blockHash);
+    }
+
+    @Test
+    public void testBlockGet() throws Exception {
+        int height = Block.getBlockCount(client);
+        String blockHash = Block.getBlockHash(client);
+
+        block = new Block.QueryBuilder()
+                .setBlockHeight(height)
+                .setBlockHash(blockHash)
+                .getBlock(client);
+    }
+
+    @Test
+    public void testBlockHeader() throws Exception {
+        int height = Block.getBlockCount(client);
+        String blockHash = Block.getBlockHash(client);
+
+        blockHeader = new Block.QueryBuilder()
+                .setBlockHeight(height)
+                .setBlockHash(blockHash)
+                .getBlockHeader(client);
+    }
+
+    @Test
+    public void testBlockDifficulty() throws Exception {
+        int height = Block.getBlockCount(client);
+        String blockHash = Block.getBlockHash(client);
+
+        blockDifficulty = new Block.QueryBuilder()
+                .setBlockHeight(height)
+                .setBlockHash(blockHash)
+                .getBlockDifficulty(client);
+    }
+
+    @Test
+    public void testBlockHashRate() throws Exception {
+        int height = Block.getBlockCount(client);
+        String blockHash = Block.getBlockHash(client);
+
+        blockHashRate = new Block.QueryBuilder()
+                .setBlockHeight(height)
+                .setBlockHash(blockHash)
+                .getHashRate(client);
+    }
+
+}
diff --git a/src/test/java/io/bytom/integration/MessageTest.java b/src/test/java/io/bytom/integration/MessageTest.java
new file mode 100644 (file)
index 0000000..c0d9e5d
--- /dev/null
@@ -0,0 +1,48 @@
+package io.bytom.integration;
+
+import io.bytom.TestUtils;
+import io.bytom.api.Account;
+import io.bytom.api.Address;
+import io.bytom.api.Message;
+import io.bytom.api.Wallet;
+import io.bytom.exception.BytomException;
+import io.bytom.http.Client;
+import org.junit.Test;
+
+import java.util.List;
+
+public class MessageTest {
+
+    static Client client;
+
+    static {
+        try {
+            client = TestUtils.generateClient();
+        } catch (BytomException e) {
+            e.printStackTrace();
+        }
+    }
+
+    static Message message;
+
+    @Test
+    public void testMessageSign() throws Exception {
+
+        String id = "0E6K7AFF00A02";
+        String alias = "test";
+        Account.AddressBuilder addressBuilder = new Account.AddressBuilder()
+                                                            .setAccountId(id)
+                                                            .setAccountAlias(alias);
+        List<Address> addressList = addressBuilder.list(client);
+
+        String address = addressList.get(0).address;
+
+        message = new Message.SignBuilder()
+                    .setAddress("sm1qz2j8k5anh0d0nu63pcwccxwkn7qu4y2zjwaj5h")
+                    .setMessage("this is a test message")
+                    .setPassword("123456")
+                    .sign(client);
+    }
+
+
+}
diff --git a/src/test/java/io/bytom/integration/WalletTest.java b/src/test/java/io/bytom/integration/WalletTest.java
new file mode 100644 (file)
index 0000000..194fbb6
--- /dev/null
@@ -0,0 +1,43 @@
+package io.bytom.integration;
+
+import io.bytom.TestUtils;
+import io.bytom.api.Key;
+import io.bytom.api.Wallet;
+import io.bytom.exception.BytomException;
+import io.bytom.http.Client;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class WalletTest {
+
+    static Client client;
+
+    static {
+        try {
+            client = TestUtils.generateClient();
+        } catch (BytomException e) {
+            e.printStackTrace();
+        }
+    }
+
+    static Wallet wallet;
+
+    @Test
+    public void testWalletBackUp() throws Exception {
+
+        wallet = Wallet.backupWallet(client);
+    }
+
+    @Test
+    public void testWallerRestore() throws Exception {
+        // get wallet object
+        wallet = Wallet.backupWallet(client);
+        //restore
+        Wallet.restoreWallet(client, wallet.accountImage, wallet.assetImage, wallet.keyImages);
+    }
+
+
+
+}