11package jfyg.account
22
3- import org.junit.After
3+ import com.google.gson.Gson
4+ import com.google.gson.GsonBuilder
5+ import jfyg.response.BaseResponse
6+ import jfyg.response.account.AccountBalanceResponse
7+ import jfyg.response.account.AccountBlockResponse
8+ import jfyg.response.account.AccountMultiBalanceResponse
9+ import jfyg.response.account.AccountTransactionResponse
10+ import org.junit.Assert.assertEquals
11+ import org.junit.Assert
412import org.junit.Before
513import org.junit.Test
614
715class AccountTest {
16+
17+ lateinit var gson: Gson
18+
19+ private val accountBalance = """
20+ {
21+ "status": "1",
22+ "message": "OK",
23+ "result": "670456215218885498951364"
24+ }"""
25+
26+ private val accountMultiBalance = """
27+ {
28+ "status": "1",
29+ "message": "OK",
30+ "result": [
31+ {
32+ "account": "0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
33+ "balance": "40807168564070000000000"
34+ },
35+ {
36+ "account": "0x63a9975ba31b0b9626b34300f7f627147df1f526",
37+ "balance": "332567136222827062478"
38+ },
39+ {
40+ "account": "0x198ef1ec325a96cc354c7266a038be8b5c558f67",
41+ "balance": "0"
42+ }
43+ ]
44+ }"""
45+ private val accountBlocksMined = """
46+ {
47+ "status": "1",
48+ "message": "OK",
49+ "result": [
50+ {
51+ "blockNumber": "3462296",
52+ "timeStamp": "1491118514",
53+ "blockReward": "5194770940000000000"
54+ },
55+ {
56+ "blockNumber": "2691400",
57+ "timeStamp": "1480072029",
58+ "blockReward": "5086562212310617100"
59+ },
60+ {
61+ "blockNumber": "70606",
62+ "timeStamp": "1439322608",
63+ "blockReward": "5001164799033398000"
64+ }
65+ ]
66+ }"""
67+
68+ private val accountTransactions = """
69+ {
70+ "status": "1",
71+ "message": "OK",
72+ "result": [
73+ {
74+ "blockNumber": "5273167",
75+ "timeStamp": "1521316197",
76+ "hash": "0x308d39f4223154fdf2db3b803400dea0da7468592aded1c6e9c01fd35835f23f",
77+ "nonce": "0",
78+ "blockHash": "0x315ee5a947d4c7519bd817465711fbb0b56c8ecafc8d9766aef1350e83c41521",
79+ "transactionIndex": "28",
80+ "from": "0x047250bd5ac59e6c45473cc0036d71737c885f6a",
81+ "to": "0x82e4499d4b2a669831a3881d61bb24f7b620c61a",
82+ "value": "18034800000000000",
83+ "gas": "21000",
84+ "gasPrice": "4000000000",
85+ "isError": "0",
86+ "txreceipt_status": "1",
87+ "input": "0x",
88+ "contractAddress": "",
89+ "cumulativeGasUsed": "1376343",
90+ "gasUsed": "21000",
91+ "confirmations": "150836"
92+ },
93+ {
94+ "blockNumber": "5273704",
95+ "timeStamp": "1521323646",
96+ "hash": "0x55a6922f90f0fce43896b9b2239ddddd3f829fb06291724bfb4be6fcc895d202",
97+ "nonce": "0",
98+ "blockHash": "0xeb96c9b68ae66c42dbf35f323e66534858d88dd1b107f77bb2c65504140e9752",
99+ "transactionIndex": "14",
100+ "from": "0x4a7341f16107521adca5d24009e7c2d8829787d3",
101+ "to": "0x82e4499d4b2a669831a3881d61bb24f7b620c61a",
102+ "value": "53297010000000000",
103+ "gas": "21000",
104+ "gasPrice": "4000000000",
105+ "isError": "0",
106+ "txreceipt_status": "1",
107+ "input": "0x",
108+ "contractAddress": "",
109+ "cumulativeGasUsed": "391843",
110+ "gasUsed": "21000",
111+ "confirmations": "150299"
112+ }
113+ ]
114+ }"""
115+
116+ private val inputBadResponse = """
117+ {
118+ "status": "0",
119+ "message": "NOTOK",
120+ "result": "Error!"
121+ }"""
122+
123+
8124 @Before
9125 fun setUp () {
126+ val gb = GsonBuilder ()
127+ gson = gb.create()
10128 }
11129
12- @After
13- fun tearDown () {
14- }
15130
16131 @Test
17132 fun getBalance () {
133+ val response = gson.fromJson(accountBalance, AccountBalanceResponse ::class .java)
134+ assertEquals(" 670456215218885498951364" , response.result)
18135 }
19136
20137 @Test
21138 fun getMultiBalance () {
139+ val response = gson.fromJson(accountMultiBalance, AccountMultiBalanceResponse ::class .java)
140+ assertEquals(" 0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a" , response.result?.get(0 )?.account)
141+ assertEquals(" 40807168564070000000000" , response.result?.get(0 )?.balance)
142+ assertEquals(" 0x63a9975ba31b0b9626b34300f7f627147df1f526" , response.result?.get(1 )?.account)
143+ assertEquals(" 332567136222827062478" , response.result?.get(1 )?.balance)
144+ assertEquals(" 0x198ef1ec325a96cc354c7266a038be8b5c558f67" , response.result?.get(2 )?.account)
145+ assertEquals(" 0" , response.result?.get(2 )?.balance)
22146 }
23147
24148 @Test
25149 fun getBlocks () {
150+ val response = gson.fromJson(accountBlocksMined, AccountBlockResponse ::class .java)
151+ assertEquals(" 3462296" , response.result?.get(0 )?.blockNumber)
152+ assertEquals(" 1491118514" , response.result?.get(0 )?.timeStamp)
153+ assertEquals(" 5194770940000000000" , response.result?.get(0 )?.blockReward)
154+ assertEquals(" 2691400" , response.result?.get(1 )?.blockNumber)
155+ assertEquals(" 1480072029" , response.result?.get(1 )?.timeStamp)
156+ assertEquals(" 5086562212310617100" , response.result?.get(1 )?.blockReward)
26157 }
27158
28159 @Test
29160 fun getTransactions () {
161+ val response = gson.fromJson(accountTransactions, AccountTransactionResponse ::class .java)
162+ assertEquals(" 0x047250bd5ac59e6c45473cc0036d71737c885f6a" , response.result?.get(0 )?.transactionFrom)
163+ assertEquals(" 0x82e4499d4b2a669831a3881d61bb24f7b620c61a" , response.result?.get(0 )?.transactionTo)
164+ assertEquals(" 18034800000000000" , response.result?.get(0 )?.value)
165+ assertEquals(" 21000" , response.result?.get(1 )?.gas)
166+ assertEquals(" 4000000000" , response.result?.get(1 )?.gasPrice)
167+ assertEquals(" 0" , response.result?.get(1 )?.isError)
168+ }
169+
170+ @Test
171+ fun networkStatusIsDown () {
172+ val response = gson.fromJson(inputBadResponse, BaseResponse ::class .java)
173+ Assert .assertEquals(" 0" , response.status)
30174 }
31175
32176 @Test
33- fun getNetworkStatus () {
177+ fun networkStatusIsUp () {
178+ val response = gson.fromJson(inputBadResponse, BaseResponse ::class .java)
179+ Assert .assertNotEquals(" 1" , response.status)
34180 }
35181
36182 @Test
37- fun getNetworkMessage () {
183+ fun networkMessageNotOk () {
184+ val response = gson.fromJson(inputBadResponse, BaseResponse ::class .java)
185+ Assert .assertEquals(" NOTOK" , response.message)
38186 }
39187
188+ @Test
189+ fun networkMessageOk () {
190+ val response = gson.fromJson(inputBadResponse, BaseResponse ::class .java)
191+ Assert .assertNotEquals(" OK" , response.message)
192+ }
40193}
0 commit comments