|
| 1 | +package io.topiacoin.eosrpcadapter; |
| 2 | + |
| 3 | +import io.topiacoin.eosrpcadapter.exceptions.EOSException; |
| 4 | +import io.topiacoin.eosrpcadapter.messages.RequiredKeys; |
| 5 | +import io.topiacoin.eosrpcadapter.messages.SignedTransaction; |
| 6 | +import io.topiacoin.eosrpcadapter.messages.TableRows; |
| 7 | +import io.topiacoin.eosrpcadapter.messages.Transaction; |
| 8 | +import org.junit.Ignore; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.io.InputStream; |
| 12 | +import java.net.URL; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Date; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Random; |
| 19 | + |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +@Ignore |
| 23 | +public class EOSRPCAdapterIntegrationTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testIntegration() throws Exception { |
| 27 | + |
| 28 | + URL nodeURL = new URL("http://127.0.0.1:8888/"); |
| 29 | + URL walletURL = new URL ("http://127.0.0.1:8899/"); |
| 30 | + |
| 31 | + String accountName = "inita"; |
| 32 | + String walletName = "default"; |
| 33 | + String walletPassword = "PW5KhNWRhmkmSV718ivUgvtDdsG5Rd291MrJm6PJESeDVpo5AQ7gV" ; |
| 34 | + |
| 35 | + long guid = new Random().nextLong(); |
| 36 | + String workspaceName = "workspace-" + guid; |
| 37 | + String workspaceDescription = "Description! " + guid ; |
| 38 | + |
| 39 | + EOSRPCAdapter eosrpcAdapter = new EOSRPCAdapter(nodeURL, walletURL); |
| 40 | + |
| 41 | + Chain chain = eosrpcAdapter.chain(); |
| 42 | + Wallet wallet = eosrpcAdapter.wallet(); |
| 43 | + |
| 44 | + String chainID = chain.getInfo().chain_id; |
| 45 | + |
| 46 | + // Unlock the Wallet |
| 47 | + wallet.unlock(walletName, walletPassword) ; |
| 48 | + List<String> availableKeys = wallet.getPublicKeys(walletName); |
| 49 | + |
| 50 | + // Load the Smart Contract into the inita account |
| 51 | + try |
| 52 | + { |
| 53 | + InputStream abiStream = ClassLoader.getSystemResourceAsStream("contract.abi"); |
| 54 | + InputStream wasmStream = ClassLoader.getSystemResourceAsStream("contract.wasm"); |
| 55 | + Transaction smartContractTx = chain.createSetContractTransaction(accountName, abiStream, wasmStream); |
| 56 | + |
| 57 | + RequiredKeys requiredKeys = chain.getRequiredKeys(smartContractTx, availableKeys); |
| 58 | + |
| 59 | + SignedTransaction signedContractTx = wallet.signTransaction(smartContractTx, requiredKeys.required_keys, chainID); |
| 60 | + |
| 61 | + chain.pushTransaction(signedContractTx); |
| 62 | + } catch ( EOSException e ){ |
| 63 | + System.out.println ( "Exception loading Contract: " + e.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + // Invoke the Smart Contract to Create the Workspace |
| 67 | + { |
| 68 | + Map args = new HashMap(); |
| 69 | + args.put("owner", accountName); |
| 70 | + args.put("guid", guid); |
| 71 | + args.put("workspaceName", workspaceName); |
| 72 | + args.put("workspaceDescription", workspaceDescription); |
| 73 | + args.put("key", "sekretKey"); |
| 74 | + List<String> scope = new ArrayList<>(); |
| 75 | + List<Transaction.Authorization> authorizations = new ArrayList<>(); |
| 76 | + authorizations.add(new Transaction.Authorization(accountName, "active")); |
| 77 | + Date expirationDate = new Date(System.currentTimeMillis() + 60000); |
| 78 | + Transaction createContainerTx = chain.createRawTransaction(accountName, "create", args, scope, authorizations, expirationDate); |
| 79 | + |
| 80 | + RequiredKeys requiredKeys = chain.getRequiredKeys(createContainerTx, availableKeys); |
| 81 | + |
| 82 | + SignedTransaction signedContainerTx = wallet.signTransaction(createContainerTx, requiredKeys.required_keys, chainID); |
| 83 | + |
| 84 | + chain.pushTransaction(signedContainerTx); |
| 85 | + } |
| 86 | + |
| 87 | + // Read the table rows out of the contract |
| 88 | + { |
| 89 | + String guidStr = "" + guid; |
| 90 | + TableRows tableRows = chain.getTableRows(accountName, guidStr, "workspace", 10, false); |
| 91 | + |
| 92 | + System.out.println(tableRows.rows); |
| 93 | + Map<String, Object> row = tableRows.rows.get(0); |
| 94 | + assertEquals ( workspaceName, row.get("name")); |
| 95 | + assertEquals ( workspaceDescription, row.get("description")); |
| 96 | + assertEquals ( accountName, row.get("owner")); |
| 97 | + } |
| 98 | + |
| 99 | + // Invoke the Smart Contract to Delete the Workspace |
| 100 | + { |
| 101 | + Map args = new HashMap(); |
| 102 | + args.put("guid", guid); |
| 103 | + List<String> scope = new ArrayList<>(); |
| 104 | + List<Transaction.Authorization> authorizations = new ArrayList<>(); |
| 105 | + authorizations.add(new Transaction.Authorization(accountName, "active")); |
| 106 | + Date expirationDate = new Date(System.currentTimeMillis() + 60000); |
| 107 | + Transaction createContainerTx = chain.createRawTransaction(accountName, "destroy", args, scope, authorizations, expirationDate); |
| 108 | + |
| 109 | + RequiredKeys requiredKeys = chain.getRequiredKeys(createContainerTx, availableKeys); |
| 110 | + |
| 111 | + SignedTransaction signedContainerTx = wallet.signTransaction(createContainerTx, requiredKeys.required_keys, chainID); |
| 112 | + |
| 113 | + chain.pushTransaction(signedContainerTx); |
| 114 | + } |
| 115 | + |
| 116 | + // Try to Read the table rows out of the contract |
| 117 | + { |
| 118 | + String guidStr = "" + guid; |
| 119 | + TableRows tableRows = chain.getTableRows(accountName, guidStr, "workspace", 10, false); |
| 120 | + |
| 121 | + System.out.println(tableRows.rows); |
| 122 | + |
| 123 | + assertEquals ( 0, tableRows.rows.size()); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | +} |
0 commit comments