Skip to content

Commit 268e5e9

Browse files
committed
[1.1.0-SNAPSHOT]
ApiRunner added All tests refactored to use ApiRunner ApiRunner correct key retrival and queue set added All tests fixed and improved
1 parent 664cb72 commit 268e5e9

34 files changed

+266
-366
lines changed

src/main/java/io/api/etherscan/core/impl/EtherScanApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public EtherScanApi(final String apiKey,
6767
final Supplier<IHttpExecutor> executorSupplier) {
6868
this(apiKey, network, executorSupplier,
6969
DEFAULT_KEY.equals(apiKey)
70-
? new QueueManager(1, 6)
70+
? QueueManager.DEFAULT_KEY_QUEUE
7171
: new FakeQueueManager()
7272
);
7373
}

src/main/java/io/api/etherscan/manager/impl/QueueManager.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.api.etherscan.manager.impl;
22

3+
import io.api.etherscan.core.impl.EtherScanApi;
34
import io.api.etherscan.manager.IQueueManager;
45

56
import java.util.concurrent.*;
@@ -14,6 +15,9 @@
1415
*/
1516
public class QueueManager implements IQueueManager {
1617

18+
public static final QueueManager DEFAULT_KEY_QUEUE = new QueueManager(1, 6);
19+
public static final QueueManager PERSONAL_KEY_QUEUE = new QueueManager(5, 1);
20+
1721
private final Semaphore semaphore;
1822

1923
public QueueManager(int queueSize, int queueResetTimeInSec) {

src/test/java/io/api/ApiRunner.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.api;
2+
3+
import io.api.etherscan.core.impl.EtherScanApi;
4+
import io.api.etherscan.manager.impl.QueueManager;
5+
import io.api.etherscan.model.EthNetwork;
6+
import org.junit.Assert;
7+
8+
public class ApiRunner extends Assert {
9+
10+
private static final EtherScanApi api;
11+
private static final EtherScanApi apiRopsten;
12+
private static final EtherScanApi apiRinkeby;
13+
private static final EtherScanApi apiKovan;
14+
15+
static {
16+
final String apiKey = System.getenv("API_KEY");
17+
final String keyOrDefault = (apiKey == null || apiKey.isEmpty())
18+
? EtherScanApi.DEFAULT_KEY
19+
: apiKey;
20+
21+
final QueueManager queue = keyOrDefault.equals(EtherScanApi.DEFAULT_KEY)
22+
? QueueManager.DEFAULT_KEY_QUEUE
23+
: QueueManager.PERSONAL_KEY_QUEUE;
24+
25+
api = new EtherScanApi(keyOrDefault, EthNetwork.MAINNET, queue);
26+
apiRopsten = new EtherScanApi(keyOrDefault, EthNetwork.ROPSTEN, queue);
27+
apiRinkeby = new EtherScanApi(keyOrDefault, EthNetwork.RINKEBY, queue);
28+
apiKovan = new EtherScanApi(keyOrDefault, EthNetwork.KOVAN, queue);
29+
}
30+
31+
public static EtherScanApi getApi() {
32+
return api;
33+
}
34+
35+
public static EtherScanApi getApiRopsten() {
36+
return apiRopsten;
37+
}
38+
39+
public static EtherScanApi getApiRinkeby() {
40+
return apiRinkeby;
41+
}
42+
43+
public static EtherScanApi getApiKovan() {
44+
return apiKovan;
45+
}
46+
}

src/test/java/io/api/etherscan/EtherScanApiTest.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.api.etherscan;
22

3+
import io.api.ApiRunner;
34
import io.api.etherscan.core.impl.EtherScanApi;
45
import io.api.etherscan.error.ApiException;
56
import io.api.etherscan.error.ApiKeyException;
@@ -9,22 +10,19 @@
910
import io.api.etherscan.model.Balance;
1011
import io.api.etherscan.model.Block;
1112
import io.api.etherscan.model.EthNetwork;
12-
import org.junit.Assert;
1313
import org.junit.Test;
1414

1515
import java.util.List;
1616
import java.util.function.Supplier;
1717

1818
/**
19-
* ! NO DESCRIPTION !
20-
*
2119
* @author GoodforGod
2220
* @since 05.11.2018
2321
*/
24-
public class EtherScanApiTest extends Assert {
22+
public class EtherScanApiTest extends ApiRunner {
2523

26-
private EthNetwork network = EthNetwork.KOVAN;
27-
private String validKey = "YourKey";
24+
private final EthNetwork network = EthNetwork.KOVAN;
25+
private final String validKey = "YourKey";
2826

2927
@Test
3028
public void validKey() {
@@ -59,24 +57,20 @@ public void noTimeoutOnRead() {
5957
@Test
6058
public void noTimeoutOnReadGroli() {
6159
Supplier<IHttpExecutor> supplier = () -> new HttpExecutor(300);
62-
EtherScanApi api = new EtherScanApi(EthNetwork.GORLI, supplier);
63-
Balance balance = api.account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
60+
Balance balance = getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
6461
assertNotNull(balance);
6562
}
6663

6764
@Test
6865
public void noTimeoutOnReadTobalala() {
6966
Supplier<IHttpExecutor> supplier = () -> new HttpExecutor(30000);
70-
EtherScanApi api = new EtherScanApi(EthNetwork.TOBALABA, supplier);
71-
Balance balance = api.account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
67+
Balance balance = getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
7268
assertNotNull(balance);
7369
}
7470

7571
@Test
7672
public void noTimeoutUnlimitedAwait() {
77-
Supplier<IHttpExecutor> supplier = () -> new HttpExecutor(-30, -300);
78-
EtherScanApi api = new EtherScanApi(EthNetwork.MAINNET, supplier);
79-
Balance balance = api.account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
73+
Balance balance = getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
8074
assertNotNull(balance);
8175
}
8276

src/test/java/io/api/etherscan/account/AccountBalanceListTest.java

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.api.etherscan.account;
22

3-
import io.api.etherscan.core.impl.EtherScanApi;
3+
import io.api.ApiRunner;
44
import io.api.etherscan.error.InvalidAddressException;
55
import io.api.etherscan.model.Balance;
66
import io.api.support.AddressUtil;
7-
import org.junit.Assert;
87
import org.junit.Test;
98

9+
import java.math.BigInteger;
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

@@ -16,31 +16,29 @@
1616
* @author GoodforGod
1717
* @since 03.11.2018
1818
*/
19-
public class AccountBalanceListTest extends Assert {
20-
21-
private EtherScanApi api = new EtherScanApi();
19+
public class AccountBalanceListTest extends ApiRunner {
2220

2321
@Test
2422
public void correct() {
2523
List<String> addresses = new ArrayList<>();
2624
addresses.add("0x9327cb34984c3992ec1EA0eAE98Ccf80A74f95B9");
2725
addresses.add("0xC9F32CE1127e44C51cbD182D6364F3D707Fd0d47");
2826

29-
List<Balance> balances = api.account().balances(addresses);
27+
List<Balance> balances = getApi().account().balances(addresses);
3028
assertNotNull(balances);
3129
assertFalse(balances.isEmpty());
3230
assertEquals(2, balances.size());
3331
assertNotEquals(balances.get(0), balances.get(1));
3432
assertNotEquals(balances.get(0).hashCode(), balances.get(1).hashCode());
35-
for(Balance balance : balances) {
33+
for (Balance balance : balances) {
3634
assertNotNull(balance.getAddress());
3735
assertNotNull(balance.getGwei());
3836
assertNotNull(balance.getKwei());
3937
assertNotNull(balance.getMwei());
4038
assertNotNull(balance.getEther());
4139
assertNotNull(balance.getGwei());
4240
assertNotNull(balance.getAddress());
43-
assertNotEquals(0, balance.getWei());
41+
assertNotEquals(BigInteger.ZERO, balance.getWei());
4442
assertNotNull(balance.toString());
4543
}
4644
}
@@ -49,20 +47,15 @@ public void correct() {
4947
public void correctMoreThat20Addresses() {
5048
List<String> addresses = AddressUtil.genRealAddresses();
5149

52-
List<Balance> balances = api.account().balances(addresses);
50+
List<Balance> balances = getApi().account().balances(addresses);
5351
assertNotNull(balances);
5452
assertFalse(balances.isEmpty());
5553
assertEquals(25, balances.size());
56-
for(Balance balance : balances) {
54+
for (Balance balance : balances) {
5755
assertNotNull(balance.getAddress());
58-
assertNotEquals(0, balance.getWei());
59-
assertNotEquals(0, balance.getKwei());
60-
assertNotEquals(0, balance.getMwei());
61-
assertNotEquals(0, balance.getEther());
62-
assertNotEquals(0, balance.getGwei());
6356
}
6457

65-
assertFalse(balances.get(0).equals(balances.get(1)));
58+
assertNotEquals(balances.get(0), balances.get(1));
6659
}
6760

6861
@Test(expected = InvalidAddressException.class)
@@ -71,13 +64,13 @@ public void invalidParamWithError() {
7164
addresses.add("0x9327cb34984c3992ec1EA0eAE98Ccf80A74f95B9");
7265
addresses.add("C9F32CE1127e44C51cbD182D6364F3D707Fd0d47");
7366

74-
api.account().balances(addresses);
67+
getApi().account().balances(addresses);
7568
}
7669

7770
@Test
7871
public void emptyParamList() {
7972
List<String> addresses = new ArrayList<>();
80-
List<Balance> balances = api.account().balances(addresses);
73+
List<Balance> balances = getApi().account().balances(addresses);
8174
assertNotNull(balances);
8275
assertTrue(balances.isEmpty());
8376
}
@@ -88,11 +81,11 @@ public void correctParamWithEmptyExpectedResult() {
8881
addresses.add("0x1327cb34984c3992ec1EA0eAE98Ccf80A74f95B9");
8982
addresses.add("0xC1F32CE1127e44C51cbD182D6364F3D707Fd0d47");
9083

91-
List<Balance> balances = api.account().balances(addresses);
84+
List<Balance> balances = getApi().account().balances(addresses);
9285
assertNotNull(balances);
9386
assertFalse(balances.isEmpty());
9487
assertEquals(2, balances.size());
95-
for(Balance balance : balances) {
88+
for (Balance balance : balances) {
9689
assertNotNull(balance.getAddress());
9790
assertEquals(0, balance.getWei().intValue());
9891
}

src/test/java/io/api/etherscan/account/AccountBalanceTest.java

+10-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package io.api.etherscan.account;
22

3+
import io.api.ApiRunner;
34
import io.api.etherscan.core.impl.EtherScanApi;
45
import io.api.etherscan.error.InvalidAddressException;
56
import io.api.etherscan.model.Balance;
6-
import io.api.etherscan.model.EthNetwork;
7-
import org.junit.Assert;
87
import org.junit.Test;
98
import org.junit.runner.RunWith;
109
import org.junit.runners.Parameterized;
@@ -20,12 +19,12 @@
2019
* @since 03.11.2018
2120
*/
2221
@RunWith(Parameterized.class)
23-
public class AccountBalanceTest extends Assert {
22+
public class AccountBalanceTest extends ApiRunner {
2423

25-
private EtherScanApi api;
26-
private String addressCorrect;
27-
private String addressInvalid;
28-
private String addressNoResponse;
24+
private final EtherScanApi api;
25+
private final String addressCorrect;
26+
private final String addressInvalid;
27+
private final String addressNoResponse;
2928

3029
public AccountBalanceTest(EtherScanApi api, String addressCorrect, String addressInvalid, String addressNoResponse) {
3130
this.api = api;
@@ -36,31 +35,13 @@ public AccountBalanceTest(EtherScanApi api, String addressCorrect, String addres
3635

3736
@Parameters
3837
public static Collection data() {
39-
return Arrays.asList(new Object[][]{
38+
return Arrays.asList(new Object[][] {
4039
{
41-
new EtherScanApi(),
40+
getApi(),
4241
"0x8d4426f94e42f721C7116E81d6688cd935cB3b4F",
4342
"8d4426f94e42f721C7116E81d6688cd935cB3b4F",
4443
"0x1d4426f94e42f721C7116E81d6688cd935cB3b4F"
45-
},
46-
{
47-
new EtherScanApi(EthNetwork.ROPSTEN),
48-
"0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
49-
"xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
50-
"0x1dbd2b932c763ba5b1b7ae3b362eac3e8d40121a"
51-
},
52-
{
53-
new EtherScanApi(EthNetwork.RINKEBY),
54-
"0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
55-
"xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
56-
"0x1dbd2b932c763ba5b1b7ae3b362eac3e8d40121a"
57-
},
58-
{
59-
new EtherScanApi(EthNetwork.KOVAN),
60-
"0xB9F36EE9df7E2A24B61b1738F4127BFDe8bA1A87",
61-
"xB9F36EE9df7E2A24B61b1738F4127BFDe8bA1A87",
62-
"0xB1F36EE9df7E2A24B61b1738F4127BFDe8bA1A87"
63-
},
44+
}
6445
});
6546
}
6647

@@ -74,13 +55,12 @@ public void correct() {
7455
assertNotNull(balance.getGwei());
7556
assertNotNull(balance.getEther());
7657
assertNotNull(balance.getAddress());
77-
assertNotEquals(0, balance.getWei());
7858
assertNotNull(balance.toString());
7959
}
8060

8161
@Test(expected = InvalidAddressException.class)
8262
public void invalidParamWithError() {
83-
Balance balance = api.account().balance(addressInvalid);
63+
Balance balance = getApi().account().balance(addressInvalid);
8464
}
8565

8666
@Test

src/test/java/io/api/etherscan/account/AccountMinedBlocksTest.java

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package io.api.etherscan.account;
22

3+
import io.api.ApiRunner;
34
import io.api.etherscan.core.impl.EtherScanApi;
45
import io.api.etherscan.error.InvalidAddressException;
56
import io.api.etherscan.model.Block;
6-
import io.api.etherscan.model.EthNetwork;
7-
import org.junit.Assert;
87
import org.junit.Test;
98
import org.junit.runner.RunWith;
109
import org.junit.runners.Parameterized;
@@ -21,13 +20,13 @@
2120
* @since 03.11.2018
2221
*/
2322
@RunWith(Parameterized.class)
24-
public class AccountMinedBlocksTest extends Assert {
23+
public class AccountMinedBlocksTest extends ApiRunner {
2524

26-
private EtherScanApi api;
27-
private int blocksMined;
28-
private String addressCorrect;
29-
private String addressInvalid;
30-
private String addressNoResponse;
25+
private final EtherScanApi api;
26+
private final int blocksMined;
27+
private final String addressCorrect;
28+
private final String addressInvalid;
29+
private final String addressNoResponse;
3130

3231
public AccountMinedBlocksTest(EtherScanApi api,
3332
int blocksMined,
@@ -43,22 +42,14 @@ public AccountMinedBlocksTest(EtherScanApi api,
4342

4443
@Parameters
4544
public static Collection data() {
46-
return Arrays.asList(new Object[][]{
45+
return Arrays.asList(new Object[][] {
4746
{
48-
new EtherScanApi(),
47+
getApi(),
4948
223,
5049
"0xE4C6175183029A0f039bf2DFffa5C6e8F3cA9B23",
5150
"xE4C6175183029A0f039bf2DFffa5C6e8F3cA9B23",
5251
"0xE1C6175183029A0f039bf2DFffa5C6e8F3cA9B23",
53-
},
54-
{
55-
new EtherScanApi(EthNetwork.ROPSTEN),
56-
1,
57-
"0x0923DafEB5A5d11a83E188d5dbCdEd14f9b161a7",
58-
"00923DafEB5A5d11a83E188d5dbCdEd14f9b161a7",
59-
"0x1923DafEB5A5d11a83E188d5dbCdEd14f9b161a7",
6052
}
61-
// Other netWorks not presented due to 30k+ mined blocks, tests runs forever
6253
});
6354
}
6455

@@ -71,15 +62,15 @@ public void correct() {
7162
assertBlocks(blocks);
7263
assertNotNull(blocks.get(0).toString());
7364

74-
if(blocks.size() > 1) {
65+
if (blocks.size() > 1) {
7566
assertNotEquals(blocks.get(0), blocks.get(1));
7667
assertNotEquals(blocks.get(0).hashCode(), blocks.get(1).hashCode());
7768
}
7869
}
7970

8071
@Test(expected = InvalidAddressException.class)
8172
public void invalidParamWithError() {
82-
List<Block> txs = api.account().minedBlocks(addressInvalid);
73+
List<Block> txs = getApi().account().minedBlocks(addressInvalid);
8374
}
8475

8576
@Test

0 commit comments

Comments
 (0)