/dapp-chain/side-chain/framework/src/test/java/stest/tron/wallet/dailybuild/http/HttpTestEasyAccount001.java

https://github.com/tronprotocol/sun-network · Java · 291 lines · 222 code · 35 blank · 34 comment · 0 complexity · 2d441ea8860d59ed8897336722f43848 MD5 · raw file

  1. package stest.tron.wallet.dailybuild.http;
  2. import org.tron.core.Wallet;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.http.HttpResponse;
  6. import org.junit.Assert;
  7. import org.testng.annotations.AfterClass;
  8. import org.testng.annotations.Test;
  9. import org.tron.common.crypto.ECKey;
  10. import org.tron.common.utils.ByteArray;
  11. import org.tron.common.utils.Commons;
  12. import org.tron.common.utils.Utils;
  13. import stest.tron.wallet.common.client.Configuration;
  14. import stest.tron.wallet.common.client.utils.HttpMethed;
  15. import stest.tron.wallet.common.client.utils.PublicMethed;
  16. @Slf4j
  17. public class HttpTestEasyAccount001 {
  18. private final static String tokenId = Configuration.getByPath("testng.conf")
  19. .getString("tokenFoundationAccount.slideTokenId");
  20. private static String assetIssueId;
  21. private final String testKey002 = Configuration.getByPath("testng.conf")
  22. .getString("foundationAccount.key1");
  23. private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey002);
  24. private final String tokenOwnerKey = Configuration.getByPath("testng.conf")
  25. .getString("tokenFoundationAccount.slideTokenOwnerKey");
  26. private final byte[] tokenOnwerAddress = PublicMethed.getFinalAddress(tokenOwnerKey);
  27. String description = Configuration.getByPath("testng.conf")
  28. .getString("defaultParameter.assetDescription");
  29. String url = Configuration.getByPath("testng.conf")
  30. .getString("defaultParameter.assetUrl");
  31. long beforeEasyBalance = 0L;
  32. long afterEasyBalance = 0L;
  33. long beforeGenerateBalance = 0L;
  34. long afterGenerateBalance = 0L;
  35. long beforeEasyAsset = 0L;
  36. long afterEasyAsset = 0L;
  37. long beforeGenerateAsset = 0L;
  38. long afterGenerateAsset = 0L;
  39. ECKey ecKey1 = new ECKey(Utils.getRandom());
  40. byte[] assetAddress = ecKey1.getAddress();
  41. String assetKey = ByteArray.toHexString(ecKey1.getPrivKeyBytes());
  42. private JSONObject responseContent;
  43. private HttpResponse response;
  44. private String httpnode = Configuration.getByPath("testng.conf").getStringList("httpnode.ip.list")
  45. .get(0);
  46. private long now = System.currentTimeMillis();
  47. private final long totalSupply = now;
  48. private String userPassword = "ps_" + Long.toString(now);
  49. private String assetName = "httpEasyAsset_" + Long.toString(now);
  50. private String easyAddress = null;
  51. private String generatePriKey = null;
  52. private String generateAddress = null;
  53. private String generateHexAddress = null;
  54. /**
  55. * constructor.
  56. */
  57. @Test(enabled = true, description = "Create address by http")
  58. public void test01CreateAddress() {
  59. logger.info(userPassword);
  60. response = HttpMethed.createAddress(httpnode, userPassword);
  61. logger.info("code is " + response.getStatusLine().getStatusCode());
  62. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  63. responseContent = HttpMethed.parseResponseContent(response);
  64. HttpMethed.printJsonContent(responseContent);
  65. easyAddress = responseContent.get("base58checkAddress").toString();
  66. //Send trx to easy account
  67. response = HttpMethed
  68. .sendCoin(httpnode, fromAddress, Wallet.decodeFromBase58Check(easyAddress), 5000000L,
  69. testKey002);
  70. Assert.assertTrue(HttpMethed.verificationResult(response));
  71. HttpMethed.waitToProduceOneBlock(httpnode);
  72. beforeEasyBalance = HttpMethed.getBalance(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  73. logger.info("beforeEasyBalance: " + beforeEasyBalance);
  74. }
  75. /**
  76. * constructor.
  77. */
  78. @Test(enabled = true, description = "Generate address by http")
  79. public void test02GenerateAddress() {
  80. response = HttpMethed.generateAddress(httpnode);
  81. logger.info("code is " + response.getStatusLine().getStatusCode());
  82. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  83. responseContent = HttpMethed.parseResponseContent(response);
  84. HttpMethed.printJsonContent(responseContent);
  85. generateAddress = responseContent.get("address").toString();
  86. generateHexAddress = responseContent.get("hexAddress").toString();
  87. generatePriKey = responseContent.get("privateKey").toString();
  88. //Send trx to easy account
  89. response = HttpMethed
  90. .sendCoin(httpnode, fromAddress, Wallet.decodeFromBase58Check(generateAddress), 5000000L,
  91. testKey002);
  92. Assert.assertTrue(HttpMethed.verificationResult(response));
  93. HttpMethed.waitToProduceOneBlock(httpnode);
  94. beforeGenerateBalance = HttpMethed
  95. .getBalance(httpnode, Wallet.decodeFromBase58Check(generateAddress));
  96. logger.info("beforeGenerateBalance: " + beforeGenerateBalance);
  97. }
  98. /**
  99. * constructor.
  100. */
  101. @Test(enabled = true, description = "Validate address by http")
  102. public void test03ValideteAddress() {
  103. // Base58check format
  104. response = HttpMethed.validateAddress(httpnode, generateAddress);
  105. logger.info("code is " + response.getStatusLine().getStatusCode());
  106. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  107. responseContent = HttpMethed.parseResponseContent(response);
  108. HttpMethed.printJsonContent(responseContent);
  109. // Hex string format
  110. response = HttpMethed.validateAddress(httpnode, generateHexAddress);
  111. logger.info("code is " + response.getStatusLine().getStatusCode());
  112. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  113. responseContent = HttpMethed.parseResponseContent(response);
  114. HttpMethed.printJsonContent(responseContent);
  115. }
  116. /**
  117. * constructor.
  118. */
  119. @Test(enabled = true, description = "Easy transfer by http")
  120. public void test04EasyTransfer() {
  121. response = HttpMethed
  122. .easyTransfer(httpnode, userPassword, Wallet.decodeFromBase58Check(generateAddress),
  123. 1000000L);
  124. logger.info("code is " + response.getStatusLine().getStatusCode());
  125. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  126. HttpMethed.waitToProduceOneBlock(httpnode);
  127. //Send trx to easy account
  128. afterEasyBalance = HttpMethed.getBalance(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  129. logger.info("afterEasyBalance: " + afterEasyBalance);
  130. afterGenerateBalance = HttpMethed
  131. .getBalance(httpnode, Wallet.decodeFromBase58Check(generateAddress));
  132. logger.info("afterGenerateBalance: " + afterGenerateBalance);
  133. Assert.assertEquals(beforeEasyBalance - afterEasyBalance,
  134. afterGenerateBalance - beforeGenerateBalance);
  135. beforeEasyBalance = afterEasyBalance;
  136. beforeGenerateBalance = afterGenerateBalance;
  137. }
  138. /**
  139. * constructor.
  140. */
  141. @Test(enabled = true, description = "Easy transfer by privateKey by http")
  142. public void test05EasyTransferByPrivateKey() {
  143. response = HttpMethed
  144. .easyTransferByPrivate(httpnode, generatePriKey,
  145. Wallet.decodeFromBase58Check(easyAddress),
  146. 1000000L);
  147. logger.info("code is " + response.getStatusLine().getStatusCode());
  148. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  149. HttpMethed.waitToProduceOneBlock(httpnode);
  150. //Send trx to easy account
  151. afterEasyBalance = HttpMethed.getBalance(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  152. logger.info("beforeEasyBalance: " + beforeEasyBalance);
  153. logger.info("afterEasyBalance: " + afterEasyBalance);
  154. afterGenerateBalance = HttpMethed
  155. .getBalance(httpnode, Wallet.decodeFromBase58Check(generateAddress));
  156. logger.info("beforeGenerateBalance: " + beforeGenerateBalance);
  157. logger.info("afterGenerateBalance: " + afterGenerateBalance);
  158. Assert.assertEquals(beforeGenerateBalance - afterGenerateBalance,
  159. afterEasyBalance - beforeEasyBalance);
  160. }
  161. /**
  162. * constructor.
  163. */
  164. @Test(enabled = false, description = "Create asset issue by http")
  165. public void test06CreateAssetIssue() {
  166. Long amount = 2048000000L;
  167. response = HttpMethed.sendCoin(httpnode, fromAddress, assetAddress, amount, testKey002);
  168. Assert.assertTrue(HttpMethed.verificationResult(response));
  169. HttpMethed.waitToProduceOneBlock(httpnode);
  170. //Create an asset issue
  171. response = HttpMethed
  172. .assetIssue(httpnode, assetAddress, assetName, assetName, totalSupply, 1, 1,
  173. System.currentTimeMillis() + 5000, System.currentTimeMillis() + 50000000,
  174. 2, 3, description, url, 1000L, 1000L, assetKey);
  175. Assert.assertTrue(HttpMethed.verificationResult(response));
  176. HttpMethed.waitToProduceOneBlock(httpnode);
  177. response = HttpMethed.getAccount(httpnode, assetAddress);
  178. responseContent = HttpMethed.parseResponseContent(response);
  179. HttpMethed.printJsonContent(responseContent);
  180. assetIssueId = responseContent.getString("asset_issued_ID");
  181. logger.info(assetIssueId);
  182. Assert.assertTrue(Integer.parseInt(assetIssueId) > 1000000);
  183. }
  184. /**
  185. * constructor.
  186. */
  187. @Test(enabled = true, description = "Easy transfer asset by http")
  188. public void test07EasyTransferAsset() {
  189. response = HttpMethed
  190. .transferAsset(httpnode, tokenOnwerAddress,
  191. Wallet.decodeFromBase58Check(easyAddress), tokenId, 100L, tokenOwnerKey);
  192. Assert.assertTrue(HttpMethed.verificationResult(response));
  193. HttpMethed.waitToProduceOneBlock(httpnode);
  194. response = HttpMethed.getAccount(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  195. responseContent = HttpMethed.parseResponseContent(response);
  196. HttpMethed.printJsonContent(responseContent);
  197. beforeEasyAsset = responseContent.getJSONArray("assetV2").getJSONObject(0)
  198. .getLongValue("value");
  199. logger.info("beforeEasyAsset:" + beforeEasyAsset);
  200. response = HttpMethed
  201. .easyTransferAsset(httpnode, userPassword, Wallet.decodeFromBase58Check(generateAddress),
  202. 10L, tokenId);
  203. logger.info("code is " + response.getStatusLine().getStatusCode());
  204. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  205. HttpMethed.waitToProduceOneBlock(httpnode);
  206. response = HttpMethed.getAccount(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  207. responseContent = HttpMethed.parseResponseContent(response);
  208. HttpMethed.printJsonContent(responseContent);
  209. afterEasyAsset = responseContent.getJSONArray("assetV2").getJSONObject(0)
  210. .getLongValue("value");
  211. logger.info("afterEasyAsset:" + afterEasyAsset);
  212. response = HttpMethed.getAccount(httpnode, Wallet.decodeFromBase58Check(generateAddress));
  213. responseContent = HttpMethed.parseResponseContent(response);
  214. HttpMethed.printJsonContent(responseContent);
  215. beforeGenerateAsset = responseContent.getJSONArray("assetV2").getJSONObject(0)
  216. .getLongValue("value");
  217. logger.info("beforeGenerateAsset:" + beforeGenerateAsset);
  218. Assert.assertEquals(beforeEasyAsset - afterEasyAsset, beforeGenerateAsset);
  219. beforeEasyAsset = afterEasyAsset;
  220. }
  221. /**
  222. * constructor.
  223. */
  224. @Test(enabled = true, description = "Easy transfer asset by private key by http")
  225. public void test08EasyTransferAssetByPrivateKey() {
  226. response = HttpMethed
  227. .easyTransferAssetByPrivate(httpnode, generatePriKey,
  228. Wallet.decodeFromBase58Check(easyAddress),
  229. 5L, tokenId);
  230. logger.info("code is " + response.getStatusLine().getStatusCode());
  231. Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
  232. HttpMethed.waitToProduceOneBlock(httpnode);
  233. response = HttpMethed.getAccount(httpnode, Wallet.decodeFromBase58Check(easyAddress));
  234. responseContent = HttpMethed.parseResponseContent(response);
  235. HttpMethed.printJsonContent(responseContent);
  236. afterEasyAsset = responseContent.getJSONArray("assetV2").getJSONObject(0)
  237. .getLongValue("value");
  238. logger.info("beforeEasyAsset:" + beforeEasyAsset);
  239. logger.info("afterEasyAsset:" + afterEasyAsset);
  240. response = HttpMethed.getAccount(httpnode, Wallet.decodeFromBase58Check(generateAddress));
  241. responseContent = HttpMethed.parseResponseContent(response);
  242. HttpMethed.printJsonContent(responseContent);
  243. afterGenerateAsset = responseContent.getJSONArray("assetV2").getJSONObject(0)
  244. .getLongValue("value");
  245. logger.info("afterGenerateAsset:" + afterGenerateAsset);
  246. Assert
  247. .assertEquals(beforeGenerateAsset - afterGenerateAsset, afterEasyAsset - beforeEasyAsset);
  248. }
  249. /**
  250. * constructor.
  251. */
  252. @AfterClass
  253. public void shutdown() throws InterruptedException {
  254. HttpMethed.disConnect();
  255. }
  256. }