Update client tests
This commit is contained in:
parent
4d5f392539
commit
7b25e6a773
10
README.md
10
README.md
@ -1,4 +1,12 @@
|
||||
Dependencies:
|
||||
## Dependencies:
|
||||
|
||||
- io.grpc:grpc-netty-shaded
|
||||
- io.grpc:grpc-protobuf
|
||||
- io.grpc:grpc-stub
|
||||
|
||||
## Testing
|
||||
|
||||
1. Download [mock servers](https://public.internetapi.cn/?dir=bdchain/test) for Transaction Ledger &
|
||||
Accounting Chain
|
||||
2. Run mock servers with ```--port={{port}}```
|
||||
3. Run ```gradle test```
|
||||
|
12
build.gradle
12
build.gradle
@ -18,11 +18,19 @@ version '1.0-SNAPSHOT'
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
def grpc_java_version = '1.15.0'
|
||||
def junit_version = '5.3.1'
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
options.encoding = "UTF-8"
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
// testLogging {
|
||||
// events "passed", "skipped", "failed"
|
||||
// }
|
||||
}
|
||||
|
||||
javadoc {
|
||||
source = sourceSets.main.allJava
|
||||
classpath = configurations.compile
|
||||
@ -60,5 +68,7 @@ dependencies {
|
||||
compile 'io.grpc:grpc-netty-shaded:' + grpc_java_version
|
||||
compile 'io.grpc:grpc-protobuf:' + grpc_java_version
|
||||
compile 'io.grpc:grpc-stub:' + grpc_java_version
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:' + junit_version
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-params:' + junit_version
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:' + junit_version
|
||||
}
|
||||
|
183
src/test/java/bdchain/api/AccountingChainClientTests.java
Normal file
183
src/test/java/bdchain/api/AccountingChainClientTests.java
Normal file
@ -0,0 +1,183 @@
|
||||
package bdchain.api;
|
||||
|
||||
import bdchain.api.grpc.Block;
|
||||
import bdchain.api.grpc.Transaction;
|
||||
import bdchain.api.grpc.TransactionType;
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.grpc.Status;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@DisplayName("Accounting Chain tests")
|
||||
class AccountingChainClientTests {
|
||||
|
||||
private static final String ledger = "test";
|
||||
private static final int blockNumber = 2018;
|
||||
private static final String blockHashStr =
|
||||
"deadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0de";
|
||||
private static final ByteString blockHash =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray(blockHashStr));
|
||||
private static final ByteString parentHash =
|
||||
ByteString.copyFrom(
|
||||
Utils.hexStringToByteArray(
|
||||
"babefacebabefacebabefacebabefacebabefacebabefacebabefacebabeface"));
|
||||
private static final ByteString witness =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray("1fee1bad1fee1bad1fee1bad1fee1bad1fee1bad"));
|
||||
private static final long timestamp = 2018050400000L;
|
||||
private static final long size = 20180504L;
|
||||
private static final ByteString transactionsRoot =
|
||||
ByteString.copyFrom(
|
||||
Utils.hexStringToByteArray(
|
||||
"50bada5550bada5550bada5550bada5550bada5550bada5550bada5550bada55"));
|
||||
|
||||
private static final int index = 0;
|
||||
private static final ByteString from =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray("f00dcafef00dcafef00dcafef00dcafef00dcafe"));
|
||||
private static final long nonce = 2018L;
|
||||
private static final ByteString to =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray("feedbabefeedbabefeedbabefeedbabefeedbabe"));
|
||||
private static final ByteString data =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray("deadbeef"));
|
||||
private static final ByteString v = ByteString.copyFrom(Utils.hexStringToByteArray("25"));
|
||||
private static final ByteString r =
|
||||
ByteString.copyFrom(
|
||||
Utils.hexStringToByteArray(
|
||||
"1b5e176d927f8e9ab405058b2d2457392da3e20f328b16ddabcebc33eaac5fea"));
|
||||
private static final ByteString s =
|
||||
ByteString.copyFrom(
|
||||
Utils.hexStringToByteArray(
|
||||
"4ba69724e8f69de52f0125ad8b3c5c2cef33019bac3249e2c0a2192766d1721c"));
|
||||
private static final String txHashStr =
|
||||
"0404040404040404040404040404040404040404040404040404040404040404";
|
||||
private static final ByteString txHash1 =
|
||||
ByteString.copyFrom(Utils.hexStringToByteArray(txHashStr));
|
||||
private static final ByteString txHash2 =
|
||||
ByteString.copyFrom(
|
||||
Utils.hexStringToByteArray(
|
||||
"1313131313131313131313131313131313131313131313131313131313131313"));
|
||||
private static final Block block =
|
||||
Block.newBuilder()
|
||||
.setNumber(blockNumber)
|
||||
.setHash(blockHash)
|
||||
.setParentHash(parentHash)
|
||||
.setWitness(witness)
|
||||
.setTimestamp(timestamp)
|
||||
.setSize(size)
|
||||
.setTransactionsRoot(transactionsRoot)
|
||||
.addAllTransactionHashes(Arrays.asList(txHash1, txHash2))
|
||||
.build();
|
||||
private static final Transaction tx =
|
||||
Transaction.newBuilder()
|
||||
.setBlockNumber(blockNumber)
|
||||
.setBlockHash(blockHash)
|
||||
.setIndex(index)
|
||||
.setHash(txHash1)
|
||||
.setType(TransactionType.RECORD)
|
||||
.setFrom(from)
|
||||
.setNonce(nonce)
|
||||
.setTo(from)
|
||||
.setData(data)
|
||||
.setV(v)
|
||||
.setR(r)
|
||||
.setS(s)
|
||||
.build();
|
||||
|
||||
private static AccountingChainClient acClient;
|
||||
|
||||
@BeforeAll
|
||||
static void initAll() {
|
||||
acClient = new AccountingChainClient("localhost", 10001);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ClientVersion#1")
|
||||
void clientVersion1() throws InterruptedException, ExecutionException {
|
||||
assertEquals(
|
||||
"AcChainGo/v0.0.1alpha/darwin/go1.11", acClient.clientVersion().get().getVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("BlockNumber#1")
|
||||
void blockNumber1() throws InterruptedException, ExecutionException {
|
||||
assertEquals(blockNumber, acClient.blockNumber(ledger).get().getBlockNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("BlockNumber#2")
|
||||
void blockNumber2() {
|
||||
Throwable e = assertThrows(Exception.class, () -> acClient.blockNumber("").get());
|
||||
Status s = Status.fromThrowable(e);
|
||||
assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode());
|
||||
assertEquals("ledger must not be empty", s.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetBlockByNumber#1")
|
||||
void getBlockByNumber1() throws ExecutionException, InterruptedException {
|
||||
Block b = acClient.getBlockByNumber(ledger, blockNumber, true).get();
|
||||
Block blockFull =
|
||||
Block.newBuilder(block)
|
||||
.addAllTransactions(
|
||||
Arrays.asList(
|
||||
Transaction.newBuilder(tx).setIndex(0).build(),
|
||||
Transaction.newBuilder(tx)
|
||||
.setIndex(1)
|
||||
.setHash(txHash2)
|
||||
.setType(TransactionType.MESSAGE)
|
||||
.setNonce(2019L)
|
||||
.setTo(to)
|
||||
.clearData()
|
||||
.build()))
|
||||
.clearTransactionHashes()
|
||||
.build();
|
||||
assertEquals(blockFull, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetBlockByNumber#2")
|
||||
void GetBlockByNumber2() throws ExecutionException, InterruptedException {
|
||||
Block b = acClient.getBlockByNumber(ledger, blockNumber, false).get();
|
||||
assertEquals(block, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetBlockByHash#1")
|
||||
void getBlockByHash1() throws ExecutionException, InterruptedException {
|
||||
Block b = acClient.getBlockByHash(ledger, blockHashStr, false).get();
|
||||
assertEquals(block, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetTransactionByHash#1")
|
||||
void getTransactionByHash1() throws ExecutionException, InterruptedException {
|
||||
Transaction t = acClient.getTransactionByHash(ledger, txHashStr).get();
|
||||
assertEquals(tx, t);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetTransactionByBlockNumberAndIndex#1")
|
||||
void GetTransactionByBlockNumberAndIndex1() throws ExecutionException, InterruptedException {
|
||||
Transaction t = acClient.getTransactionByBlockNumberAndIndex(ledger, blockNumber, index).get();
|
||||
assertEquals(tx, t);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetTransactionByBlockHashAndIndex#1")
|
||||
void GetTransactionByBlockHashAndIndex1() throws ExecutionException, InterruptedException {
|
||||
Transaction t = acClient.getTransactionByBlockHashAndIndex(ledger, blockHashStr, index).get();
|
||||
assertEquals(tx, t);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void teadDwonAll() throws InterruptedException {
|
||||
acClient.shutdown();
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package bdchain.api;
|
||||
|
||||
import bdchain.api.grpc.GetLedgersResponse;
|
||||
import bdchain.api.grpc.TransactionType;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class Test {
|
||||
|
||||
static final String ledger = "test";
|
||||
static final int block_number = 1;
|
||||
static final String block_hash = "0x0";
|
||||
static final int index = 0;
|
||||
|
||||
private static void testFutureApi() throws InterruptedException {
|
||||
|
||||
TransactionLedgerClient tlClient = new TransactionLedgerClient("localhost", 8004);
|
||||
try {
|
||||
System.out.println(tlClient.createLedger(ledger).get().getOk());
|
||||
GetLedgersResponse r = tlClient.getLedgers().get();
|
||||
for (int i = 0; i < r.getLedgersCount(); i++) {
|
||||
System.out.println(r.getLedgers(i));
|
||||
}
|
||||
System.out.println(
|
||||
tlClient
|
||||
.sendTransaction(ledger, TransactionType.RECORD, "0x0", "0x1", "data".getBytes())
|
||||
.get()
|
||||
.getHash());
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
tlClient.shutdown();
|
||||
|
||||
AccountingChainClient acClient = new AccountingChainClient("localhost", 8013);
|
||||
try {
|
||||
System.out.println(acClient.blockNumber(ledger).get().getBlockNumber());
|
||||
System.out.println(acClient.getBlockByNumber(ledger, block_number, false).get().toString());
|
||||
System.out.println(acClient.getBlockByNumber(ledger, block_number, true).get().toString());
|
||||
System.out.println(acClient.getBlockByHash(ledger, block_hash, false).get().toString());
|
||||
System.out.println(acClient.getBlockByHash(ledger, block_hash, true).get().toString());
|
||||
System.out.println(acClient.getTransactionByHash(ledger, "0x0").get().toString());
|
||||
System.out.println(
|
||||
acClient
|
||||
.getTransactionByBlockNumberAndIndex(ledger, block_number, index)
|
||||
.get()
|
||||
.toString());
|
||||
System.out.println(
|
||||
acClient.getTransactionByBlockHashAndIndex(ledger, block_hash, index).get().toString());
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
acClient.shutdown();
|
||||
}
|
||||
|
||||
private static void testSyncApi() throws InterruptedException {
|
||||
|
||||
TransactionLedgerClient tlClient = new TransactionLedgerClient("localhost", 8004);
|
||||
System.out.println(tlClient.createLedgerSync(ledger).getOk());
|
||||
GetLedgersResponse r = tlClient.getLedgersSync();
|
||||
for (int i = 0; i < r.getLedgersCount(); i++) {
|
||||
System.out.println(r.getLedgers(i));
|
||||
}
|
||||
System.out.println(
|
||||
tlClient
|
||||
.sendTransactionSync(ledger, TransactionType.RECORD, "0x0", "0x1", "data".getBytes())
|
||||
.getHash());
|
||||
tlClient.shutdown();
|
||||
|
||||
AccountingChainClient acClient = new AccountingChainClient("localhost", 8013);
|
||||
System.out.println(acClient.blockNumberSync(ledger).getBlockNumber());
|
||||
System.out.println(acClient.getBlockByNumberSync(ledger, block_number, false).toString());
|
||||
System.out.println(acClient.getBlockByNumberSync(ledger, block_number, true).toString());
|
||||
System.out.println(acClient.getBlockByHashSync(ledger, block_hash, false).toString());
|
||||
System.out.println(acClient.getBlockByHashSync(ledger, block_hash, true).toString());
|
||||
System.out.println(acClient.getTransactionByHashSync(ledger, "0x0").toString());
|
||||
System.out.println(
|
||||
acClient.getTransactionByBlockNumberAndIndexSync(ledger, block_number, index).toString());
|
||||
System.out.println(
|
||||
acClient.getTransactionByBlockHashAndIndexSync(ledger, block_hash, index).toString());
|
||||
acClient.shutdown();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
testFutureApi();
|
||||
testSyncApi();
|
||||
}
|
||||
}
|
98
src/test/java/bdchain/api/TransactionLedgerClientTests.java
Normal file
98
src/test/java/bdchain/api/TransactionLedgerClientTests.java
Normal file
@ -0,0 +1,98 @@
|
||||
package bdchain.api;
|
||||
|
||||
import bdchain.api.grpc.GetLedgersResponse;
|
||||
import bdchain.api.grpc.TransactionType;
|
||||
import io.grpc.Status;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@DisplayName("Transaction Ledger tests")
|
||||
class TransactionLedgerClientTests {
|
||||
|
||||
private static final String ledger = "test";
|
||||
private String[] ledgers = new String[] {"first", "second", "third"};
|
||||
|
||||
private static TransactionLedgerClient tlClient;
|
||||
|
||||
@BeforeAll
|
||||
static void initAll() {
|
||||
tlClient = new TransactionLedgerClient("localhost", 10000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ClientVersion#1")
|
||||
void clientVersion1() throws InterruptedException, ExecutionException {
|
||||
assertEquals(
|
||||
"TxLedgerGo/v0.0.1alpha/darwin/go1.11", tlClient.clientVersion().get().getVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CreateLedger#1")
|
||||
void createLedger1() throws InterruptedException, ExecutionException {
|
||||
assertTrue(tlClient.createLedger(ledger).get().getOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CreateLedger#2")
|
||||
void createLedger2() {
|
||||
Throwable e = assertThrows(Exception.class, () -> tlClient.createLedger("").get());
|
||||
Status s = Status.fromThrowable(e);
|
||||
assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode());
|
||||
assertEquals("name must not be empty", s.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetLedgers#1")
|
||||
void getLedgers1() throws InterruptedException, ExecutionException {
|
||||
GetLedgersResponse r = tlClient.getLedgers().get();
|
||||
assertEquals(3, r.getLedgersCount());
|
||||
String[] expected = new String[] {"first", "second", "third"};
|
||||
assertAll(
|
||||
IntStream.range(0, 3).boxed().map(i -> () -> assertEquals(ledgers[i], r.getLedgers(i))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("SendTransaction#1")
|
||||
void sendTransaction1() throws InterruptedException, ExecutionException {
|
||||
String hash =
|
||||
Utils.byteArrayToHexString(
|
||||
tlClient
|
||||
.sendTransaction(
|
||||
ledger,
|
||||
TransactionType.MESSAGE,
|
||||
"f00dcafef00dcafef00dcafef00dcafef00dcafe",
|
||||
"feedbabefeedbabefeedbabefeedbabefeedbabe",
|
||||
Utils.hexStringToByteArray("deadbeef"))
|
||||
.get()
|
||||
.getHash()
|
||||
.toByteArray());
|
||||
assertEquals("d15ea5edd15ea5edd15ea5edd15ea5edd15ea5edd15ea5edd15ea5edd15ea5ed", hash);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("SendTransaction#2")
|
||||
void sendTransaction2() {
|
||||
Throwable e =
|
||||
assertThrows(
|
||||
Exception.class,
|
||||
() ->
|
||||
tlClient
|
||||
.sendTransaction(ledger, TransactionType.MESSAGE, null, "50bada55", null)
|
||||
.get());
|
||||
Status s = Status.fromThrowable(e);
|
||||
assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode());
|
||||
assertEquals("Multiple invalid arguments", s.getDescription());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void teadDwonAll() throws InterruptedException {
|
||||
tlClient.shutdown();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user