diff --git a/src/test/java/bdledger/api/ClientTests.java b/src/test/java/bdledger/api/ClientTests.java index 7a9a759..f93f1ad 100644 --- a/src/test/java/bdledger/api/ClientTests.java +++ b/src/test/java/bdledger/api/ClientTests.java @@ -1,14 +1,20 @@ package bdledger.api; +import bdledger.api.grpc.common.Block; +import bdledger.api.grpc.common.Transaction; import bdledger.api.grpc.ledger.GetLedgersResponse; import bdledger.api.grpc.common.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.List; import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; import java.util.stream.IntStream; import static org.junit.jupiter.api.Assertions.*; @@ -17,7 +23,78 @@ import static org.junit.jupiter.api.Assertions.*; class ClientTests { private static final String ledger = "test"; - private String[] ledgers = new String[]{"first", "second", "third"}; + private static final String[] ledgers = new String[]{"first", "second", "third"}; + private static final int blockCount = 2018; + private static final int transactionCount = 2020; + + private static final String blockHashStr = "deadc0dedeadc0dedeadc0dedeadc0dedeadc0de"; + private static final ByteString blockHash = + ByteString.copyFrom(Utils.hexStringToByteArray(blockHashStr)); + private static final String[] hashStrs = { + "0404040404040404040404040404040404040404", + "1313131313131313131313131313131313131313", + "5252525252525252525252525252525252525252" + }; + private static final List hashes = + Arrays.stream(hashStrs) + .map(s -> ByteString.copyFrom(Utils.hexStringToByteArray(s))) + .collect(Collectors.toList()); + private static final List parentHashes = hashes; + private static final List witnesses = hashes; + private static final long timestamp = 2018050400000L; + private static final long size = 20180504L; + private static final int blockTransactionCount = 1000; + private static final ByteString transactionsRoot = + ByteString.copyFrom(Utils.hexStringToByteArray("babefacebabefacebabefacebabefacebabeface")); + + 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 = "0404040404040404040404040404040404040404"; + private static final ByteString txHash1 = + ByteString.copyFrom(Utils.hexStringToByteArray(txHashStr)); + private static final ByteString txHash2 = + ByteString.copyFrom(Utils.hexStringToByteArray("1313131313131313131313131313131313131313")); + + private static final Block block = + Block.newBuilder() + .setHash(blockHash) + .addAllParentHashes(parentHashes) + .addAllWitnesses(witnesses) + .setTimestamp(timestamp) + .setSize(size) + .setTransactionCount(blockTransactionCount) + .setTransactionsRoot(transactionsRoot) + .addAllTransactionHashes(Arrays.asList(txHash1, txHash2)) + .build(); + private static final Transaction tx = + Transaction.newBuilder() + .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 Client client; @@ -28,14 +105,14 @@ class ClientTests { @Test @DisplayName("ClientVersion#1") - void clientVersion1() throws InterruptedException, ExecutionException { + void clientVersion1() throws ExecutionException, InterruptedException { assertEquals( "TxLedgerGo/v0.0.1alpha/darwin/go1.11", client.clientVersion().get().getVersion()); } @Test @DisplayName("CreateLedger#1") - void createLedger1() throws InterruptedException, ExecutionException { + void createLedger1() throws ExecutionException, InterruptedException { assertTrue(client.createLedger(ledger).get().getOk()); } @@ -50,7 +127,7 @@ class ClientTests { @Test @DisplayName("GetLedgers#1") - void getLedgers1() throws InterruptedException, ExecutionException { + void getLedgers1() throws ExecutionException, InterruptedException { GetLedgersResponse r = client.getLedgers().get(); assertEquals(3, r.getLedgersCount()); String[] expected = new String[]{"first", "second", "third"}; @@ -60,7 +137,7 @@ class ClientTests { @Test @DisplayName("SendTransaction#1") - void sendTransaction1() throws InterruptedException, ExecutionException { + void sendTransaction1() throws ExecutionException, InterruptedException { String hash = Utils.byteArrayToHexString( client @@ -91,6 +168,56 @@ class ClientTests { assertEquals("Multiple invalid arguments", s.getDescription()); } + @Test + @DisplayName("GetBlockByHash#1") + void getBlockByHash1() throws ExecutionException, InterruptedException { + Block b = client.getBlockByHash(ledger, blockHashStr, false).get().getBlock(); + assertEquals(block, b); + } + + @Test + @DisplayName("CountBlocks#1") + void blockNumber1() throws ExecutionException, InterruptedException { + assertEquals(blockCount, client.countBlocks(ledger).get().getCount()); + } + + @Test + @DisplayName("CountBlocks#2") + void blockNumber2() { + Throwable e = assertThrows(Exception.class, () -> client.countBlocks("").get()); + Status s = Status.fromThrowable(e); + assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode()); + assertEquals("ledger must not be empty", s.getDescription()); + } + + @Test + @DisplayName("GetTransactionByHash#1") + void getTransactionByHash1() throws ExecutionException, InterruptedException { + Transaction t = client.getTransactionByHash(ledger, txHashStr).get().getTransaction(); + assertEquals(tx, t); + } + + @Test + @DisplayName("GetTransactionByBlockHashAndIndex#1") + void GetTransactionByBlockHashAndIndex1() throws ExecutionException, InterruptedException { + Transaction t = client.getTransactionByBlockHashAndIndex(ledger, blockHashStr, index).get().getTransaction(); + assertEquals(tx, t); + } + + @DisplayName("CountTransactions#1") + void CountTransactions1() throws ExecutionException, InterruptedException { + assertEquals(transactionCount, client.countTransactions(ledger).get().getCount()); + } + + @Test + @DisplayName("CountTransactions#2") + void CountTransactions2() { + Throwable e = assertThrows(Exception.class, () -> client.countTransactions("").get()); + Status s = Status.fromThrowable(e); + assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode()); + assertEquals("ledger must not be empty", s.getDescription()); + } + @AfterAll static void teadDwonAll() throws InterruptedException { client.shutdown();