|
| 1 | +package stest.tron.wallet.dailybuild.http; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONArray; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.google.gson.JsonArray; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.apache.http.HttpResponse; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.testng.annotations.AfterClass; |
| 11 | +import org.testng.annotations.Test; |
| 12 | +import org.tron.common.crypto.ECKey; |
| 13 | +import org.tron.common.utils.ByteArray; |
| 14 | +import org.tron.common.utils.Utils; |
| 15 | +import stest.tron.wallet.common.client.Configuration; |
| 16 | +import stest.tron.wallet.common.client.utils.HttpMethed; |
| 17 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +public class HttpTestAccount003 { |
| 21 | + |
| 22 | + private final String testKey002 = Configuration.getByPath("testng.conf") |
| 23 | + .getString("foundationAccount.key1"); |
| 24 | + private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey002); |
| 25 | + private JSONObject responseContent; |
| 26 | + private HttpResponse response; |
| 27 | + private String httpnode = Configuration.getByPath("testng.conf").getStringList("httpnode.ip.list") |
| 28 | + .get(0); |
| 29 | + private final String witnessKey001 = Configuration.getByPath("testng.conf") |
| 30 | + .getString("witness.key1"); |
| 31 | + private final byte[] witness1Address = PublicMethed.getFinalAddress(witnessKey001); |
| 32 | + private final String witnessKey002 = Configuration.getByPath("testng.conf") |
| 33 | + .getString("witness.key2"); |
| 34 | + private final byte[] witness2Address = PublicMethed.getFinalAddress(witnessKey002); |
| 35 | + |
| 36 | + ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 37 | + byte[] newAccountAddress = ecKey1.getAddress(); |
| 38 | + String newAccountKey = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 39 | + |
| 40 | + |
| 41 | + ECKey ecKey2 = new ECKey(Utils.getRandom()); |
| 42 | + byte[] updateAccountAddress = ecKey2.getAddress(); |
| 43 | + String updateAccountKey = ByteArray.toHexString(ecKey2.getPrivKeyBytes()); |
| 44 | + private final Long createWitnessAmount = Configuration.getByPath("testng.conf") |
| 45 | + .getLong("defaultParameter.createWitnessAmount"); |
| 46 | + Long amount = 50000000L; |
| 47 | + private static String updateAccountName = "updateAccount_" |
| 48 | + + Long.toString(System.currentTimeMillis()); |
| 49 | + private static String updateUrl = "http://www.update.url" + Long.toString(System.currentTimeMillis()); |
| 50 | + |
| 51 | + JsonArray voteKeys = new JsonArray(); |
| 52 | + JsonObject voteElement = new JsonObject(); |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * constructor. |
| 58 | + */ |
| 59 | + @Test(enabled = true, description = "Update account by http") |
| 60 | + public void test1UpdateAccount() { |
| 61 | + response = HttpMethed.sendCoin(httpnode, fromAddress, updateAccountAddress, amount, testKey002); |
| 62 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 63 | + |
| 64 | + HttpMethed.waitToProduceOneBlock(httpnode); |
| 65 | + |
| 66 | + response = HttpMethed.updateAccount(httpnode, updateAccountAddress,updateAccountName, |
| 67 | + updateAccountKey); |
| 68 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 69 | + |
| 70 | + |
| 71 | + response = HttpMethed.getAccount(httpnode, updateAccountAddress); |
| 72 | + responseContent = HttpMethed.parseResponseContent(response); |
| 73 | + HttpMethed.printJsonContent(responseContent); |
| 74 | + Assert.assertTrue(responseContent.getString("account_name") |
| 75 | + .equalsIgnoreCase(HttpMethed.str2hex(updateAccountName))); |
| 76 | + |
| 77 | + Assert.assertFalse(responseContent.getString("active_permission").isEmpty()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * constructor. |
| 82 | + */ |
| 83 | + @Test(enabled = true, description = "Vote witness account by http") |
| 84 | + public void test2VoteWitnessAccount() { |
| 85 | + //Freeze balance |
| 86 | + response = HttpMethed.freezeBalance(httpnode,updateAccountAddress,40000000L,0, |
| 87 | + 0,updateAccountKey); |
| 88 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 89 | + |
| 90 | + voteElement.addProperty("vote_address",ByteArray.toHexString(witness1Address)); |
| 91 | + voteElement.addProperty("vote_count",11); |
| 92 | + voteKeys.add(voteElement); |
| 93 | + |
| 94 | + voteElement.remove("vote_address"); |
| 95 | + voteElement.remove("vote_count"); |
| 96 | + voteElement.addProperty("vote_address",ByteArray.toHexString(witness2Address)); |
| 97 | + voteElement.addProperty("vote_count",12); |
| 98 | + voteKeys.add(voteElement); |
| 99 | + |
| 100 | + response = HttpMethed.voteWitnessAccount(httpnode,updateAccountAddress,voteKeys, |
| 101 | + updateAccountKey); |
| 102 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 103 | + |
| 104 | + response = HttpMethed.getAccount(httpnode, updateAccountAddress); |
| 105 | + responseContent = HttpMethed.parseResponseContent(response); |
| 106 | + HttpMethed.printJsonContent(responseContent); |
| 107 | + Assert.assertTrue(!responseContent.getString("votes").isEmpty()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * constructor. |
| 112 | + */ |
| 113 | + @Test(enabled = true, description = "List witnesses by http") |
| 114 | + public void test3ListWitness() { |
| 115 | + response = HttpMethed.listwitnesses(httpnode); |
| 116 | + responseContent = HttpMethed.parseResponseContent(response); |
| 117 | + HttpMethed.printJsonContent(responseContent); |
| 118 | + JSONArray jsonArray = JSONArray.parseArray(responseContent.getString("witnesses")); |
| 119 | + Assert.assertTrue(jsonArray.size() >= 2); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * constructor. |
| 124 | + */ |
| 125 | + @Test(enabled = true, description = "Update witness by http") |
| 126 | + public void test4UpdateWitness() { |
| 127 | + response = HttpMethed.updateWitness(httpnode,witness1Address,updateUrl,witnessKey001); |
| 128 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 129 | + |
| 130 | + HttpMethed.waitToProduceOneBlock(httpnode); |
| 131 | + |
| 132 | + response = HttpMethed.listwitnesses(httpnode); |
| 133 | + responseContent = HttpMethed.parseResponseContent(response); |
| 134 | + HttpMethed.printJsonContent(responseContent); |
| 135 | + Assert.assertTrue(responseContent.getString("witnesses").indexOf(updateUrl) != -1); |
| 136 | + //logger.info("result is " + responseContent.getString("witnesses").indexOf(updateUrl)); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * constructor. |
| 141 | + */ |
| 142 | + @Test(enabled = true, description = "Create account by http") |
| 143 | + public void test5CreateAccount() { |
| 144 | + PublicMethed.printAddress(newAccountKey); |
| 145 | + response = HttpMethed.createAccount(httpnode,fromAddress,newAccountAddress,testKey002); |
| 146 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 147 | + |
| 148 | + response = HttpMethed.getAccount(httpnode, newAccountAddress); |
| 149 | + responseContent = HttpMethed.parseResponseContent(response); |
| 150 | + HttpMethed.printJsonContent(responseContent); |
| 151 | + Assert.assertTrue(responseContent.getLong("create_time") > 3); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * constructor. |
| 156 | + */ |
| 157 | + @Test(enabled = true, description = "Create witness by http") |
| 158 | + public void test6CreateWitness() { |
| 159 | + response = HttpMethed.sendCoin(httpnode,fromAddress,newAccountAddress,createWitnessAmount,testKey002); |
| 160 | + Assert.assertTrue(HttpMethed.verificationResult(response)); |
| 161 | + |
| 162 | + PublicMethed.printAddress(newAccountKey); |
| 163 | + HttpMethed.waitToProduceOneBlock(httpnode); |
| 164 | + |
| 165 | + response = HttpMethed.createWitness(httpnode,newAccountAddress,updateUrl); |
| 166 | + responseContent = HttpMethed.parseResponseContent(response); |
| 167 | + HttpMethed.printJsonContent(responseContent); |
| 168 | + Assert.assertTrue(!responseContent.getString("txID").isEmpty()); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * constructor. |
| 173 | + */ |
| 174 | + @Test(enabled = true, description = "Withdraw by http") |
| 175 | + public void test7Withdraw() { |
| 176 | + response = HttpMethed.withdrawBalance(httpnode,witness1Address); |
| 177 | + responseContent = HttpMethed.parseResponseContent(response); |
| 178 | + HttpMethed.printJsonContent(responseContent); |
| 179 | + Assert.assertTrue(responseContent.getString("Error").indexOf("is a guard representative") != -1); |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + /** |
| 188 | + * constructor. |
| 189 | + */ |
| 190 | + @AfterClass |
| 191 | + public void shutdown() throws InterruptedException { |
| 192 | + HttpMethed.disConnect(); |
| 193 | + } |
| 194 | +} |
0 commit comments