From b60fffdc3b7e59db0755379a575dc86af1802c3f Mon Sep 17 00:00:00 2001 From: Kostya Shpachenko Date: Mon, 18 Dec 2017 15:41:21 +0200 Subject: [PATCH] Added test of custom operation deserialization --- .../agorise/graphenej/api/GetBlockTest.java | 133 ++++++++++++++---- 1 file changed, 102 insertions(+), 31 deletions(-) diff --git a/graphenej/src/test/java/cy/agorise/graphenej/api/GetBlockTest.java b/graphenej/src/test/java/cy/agorise/graphenej/api/GetBlockTest.java index 1689e04..f6637a5 100644 --- a/graphenej/src/test/java/cy/agorise/graphenej/api/GetBlockTest.java +++ b/graphenej/src/test/java/cy/agorise/graphenej/api/GetBlockTest.java @@ -9,6 +9,7 @@ import cy.agorise.graphenej.interfaces.WitnessResponseListener; import cy.agorise.graphenej.models.BaseResponse; import cy.agorise.graphenej.models.Block; import cy.agorise.graphenej.models.WitnessResponse; +import cy.agorise.graphenej.operations.CustomOperation; import cy.agorise.graphenej.operations.TransferOperation; import org.junit.Assert; import org.junit.Test; @@ -21,29 +22,42 @@ import java.util.concurrent.TimeoutException; import static org.hamcrest.CoreMatchers.instanceOf; public class GetBlockTest extends BaseApiTest { - private final static long BLOCK_NUMBER = 15776988L; + // data for the test with transfer operation + private final static long TRANSFER_BLOCK_NUMBER = 15776988L; - private final static String EXPECTED_PREVIOUS = "00f0bcdbe3d9dc66d8597e7b013a16d8dea5f778"; - private final static String EXPECTED_TIMESTAMP = "2017-04-18T04:14:51"; - private final static String EXPECTED_WITNESS = "1.6.17"; - private final static String EXPECTED_TRANSACTION_MERKLE_ROOT = "fb87a3f8af907a2450c327d181b2833b8270a504"; - private final static int EXPECTED_EXTENSIONS_SIZE = 0; - private final static String EXPECTED_WITNESS_SIGNATURE = "20160921c8ba0d312dee14bdc780c3d05b27e406e2d014b8a2415e9843bf7075cb7abbc45f5173ffefac69cecf4dd2afa5dce6076bdf24cc577ff49427babe75e1"; - private final static int EXPECTED_TRANSACTIONS_SIZE = 1; - private final static int EXPECTED_OPERATIONS_SIZE = 1; + private final static String TRANSFER_EXPECTED_PREVIOUS = "00f0bcdbe3d9dc66d8597e7b013a16d8dea5f778"; + private final static String TRANSFER_EXPECTED_TIMESTAMP = "2017-04-18T04:14:51"; + private final static String TRANSFER_EXPECTED_WITNESS = "1.6.17"; + private final static String TRANSFER_EXPECTED_TRANSACTION_MERKLE_ROOT = "fb87a3f8af907a2450c327d181b2833b8270a504"; + private final static int TRANSFER_EXPECTED_EXTENSIONS_SIZE = 0; + private final static String TRANSFER_EXPECTED_WITNESS_SIGNATURE = "20160921c8ba0d312dee14bdc780c3d05b27e406e2d014b8a2415e9843bf7075cb7abbc45f5173ffefac69cecf4dd2afa5dce6076bdf24cc577ff49427babe75e1"; + private final static int TRANSFER_EXPECTED_TRANSACTIONS_SIZE = 1; + private final static int TRANSFER_EXPECTED_OPERATIONS_SIZE = 1; - private final static UnsignedLong EXPECTED_FEE_AMOUNT = UnsignedLong.valueOf(2048); - private final static String EXPECTED_FEE_ASSET_ID = "1.3.113"; - private final static String EXPECTED_FROM = "1.2.151069"; - private final static String EXPECTED_TO = "1.2.116354"; - private final static UnsignedLong EXPECTED_AMOUNT = UnsignedLong.valueOf(13700); - private final static String EXPECTED_ASSET_ID = "1.3.113"; + private final static UnsignedLong TRANSFER_EXPECTED_FEE_AMOUNT = UnsignedLong.valueOf(2048); + private final static String TRANSFER_EXPECTED_FEE_ASSET_ID = "1.3.113"; + private final static String TRANSFER_EXPECTED_FROM = "1.2.151069"; + private final static String TRANSFER_EXPECTED_TO = "1.2.116354"; + private final static UnsignedLong TRANSFER_EXPECTED_AMOUNT = UnsignedLong.valueOf(13700); + private final static String TRANSFER_EXPECTED_ASSET_ID = "1.3.113"; + + // data for the test with custom operation + private final static long CUSTOM_BLOCK_NUMBER = 22754473L; + private final static int CUSTOM_TRANSACTION_INDEX = 8; + + private final static UnsignedLong CUSTOM_EXPECTED_FEE_AMOUNT = UnsignedLong.valueOf(13798); + private final static String CUSTOM_EXPECTED_FEE_ASSET_ID = "1.3.0"; + private final static String CUSTOM_EXPECTED_PAYER = "1.2.140994"; + private final static int CUSTOM_EXPECTED_REQUIRED_AUTHS_SIZE = 1; + private final static String CUSTOM_EXPECTED_REQUIRED_AUTH = "1.2.140994"; + private final static int CUSTOM_EXPECTED_ID = 61166; + private final static String CUSTOM_EXPECTED_DATA = "some data"; @Test - public void testGetBlock() { + public void testGetBlockWithTransferOperation() { try { final Exchanger responseExchanger = new Exchanger<>(); - mWebSocket.addListener(new GetBlock(BLOCK_NUMBER, new WitnessResponseListener() { + mWebSocket.addListener(new GetBlock(TRANSFER_BLOCK_NUMBER, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { System.out.println("onSuccess"); @@ -71,31 +85,88 @@ public class GetBlockTest extends BaseApiTest { Block block = (Block) responseResult; Assert.assertNotNull(block); - Assert.assertEquals(EXPECTED_PREVIOUS, block.getPrevious()); - Assert.assertEquals(EXPECTED_TIMESTAMP, block.getTimestamp()); - Assert.assertEquals(EXPECTED_WITNESS, block.getWitness()); - Assert.assertEquals(EXPECTED_TRANSACTION_MERKLE_ROOT, block.getTransaction_merkle_root()); - Assert.assertEquals(EXPECTED_EXTENSIONS_SIZE, block.getExtensions().length); - Assert.assertEquals(EXPECTED_WITNESS_SIGNATURE, block.getWitness_signature()); + Assert.assertEquals(TRANSFER_EXPECTED_PREVIOUS, block.getPrevious()); + Assert.assertEquals(TRANSFER_EXPECTED_TIMESTAMP, block.getTimestamp()); + Assert.assertEquals(TRANSFER_EXPECTED_WITNESS, block.getWitness()); + Assert.assertEquals(TRANSFER_EXPECTED_TRANSACTION_MERKLE_ROOT, block.getTransaction_merkle_root()); + Assert.assertEquals(TRANSFER_EXPECTED_EXTENSIONS_SIZE, block.getExtensions().length); + Assert.assertEquals(TRANSFER_EXPECTED_WITNESS_SIGNATURE, block.getWitness_signature()); List transactions = block.getTransactions(); - Assert.assertEquals(EXPECTED_TRANSACTIONS_SIZE, transactions.size()); + Assert.assertEquals(TRANSFER_EXPECTED_TRANSACTIONS_SIZE, transactions.size()); List operations = transactions.get(0).getOperations(); - Assert.assertEquals(EXPECTED_OPERATIONS_SIZE, operations.size()); + Assert.assertEquals(TRANSFER_EXPECTED_OPERATIONS_SIZE, operations.size()); BaseOperation operation = operations.get(0); Assert.assertThat(operation, instanceOf(TransferOperation.class)); TransferOperation transferOperation = (TransferOperation) operation; AssetAmount fee = transferOperation.getFee(); - Assert.assertEquals(EXPECTED_FEE_AMOUNT, fee.getAmount()); - Assert.assertEquals(EXPECTED_FEE_ASSET_ID, fee.getAsset().getObjectId()); - Assert.assertEquals(EXPECTED_FROM, transferOperation.getFrom().getObjectId()); - Assert.assertEquals(EXPECTED_TO, transferOperation.getTo().getObjectId()); + Assert.assertEquals(TRANSFER_EXPECTED_FEE_AMOUNT, fee.getAmount()); + Assert.assertEquals(TRANSFER_EXPECTED_FEE_ASSET_ID, fee.getAsset().getObjectId()); + Assert.assertEquals(TRANSFER_EXPECTED_FROM, transferOperation.getFrom().getObjectId()); + Assert.assertEquals(TRANSFER_EXPECTED_TO, transferOperation.getTo().getObjectId()); AssetAmount assetAmount = transferOperation.getAssetAmount(); - Assert.assertEquals(EXPECTED_AMOUNT, assetAmount.getAmount()); - Assert.assertEquals(EXPECTED_ASSET_ID, assetAmount.getAsset().getObjectId()); + Assert.assertEquals(TRANSFER_EXPECTED_AMOUNT, assetAmount.getAmount()); + Assert.assertEquals(TRANSFER_EXPECTED_ASSET_ID, assetAmount.getAsset().getObjectId()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + Assert.fail("Fail because of WebSocketException"); + } catch (InterruptedException e) { + System.out.println("InterruptedException. Msg: " + e.getMessage()); + Assert.fail("Fail because of InterruptedException"); + } catch (TimeoutException e) { + System.out.println("TimeoutException. Msg: " + e.getMessage()); + Assert.fail("Fail because of TimeoutException"); + } + } + + @Test + public void testGetBlockWithCustomOperation() { + try { + final Exchanger responseExchanger = new Exchanger<>(); + mWebSocket.addListener(new GetBlock(CUSTOM_BLOCK_NUMBER, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + try { + responseExchanger.exchange(response.result); + } catch (InterruptedException e) { + System.out.println("InterruptedException in success handler. Msg: " + e.getMessage()); + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + try { + responseExchanger.exchange(null); + } catch (InterruptedException e) { + System.out.println("InterruptedException in error handler. Msg: " + e.getMessage()); + } + } + })); + + mWebSocket.connect(); + + Object responseResult = responseExchanger.exchange(null, 5, TimeUnit.SECONDS); + + Block block = (Block) responseResult; + List transactions = block.getTransactions(); + List operations = transactions.get(CUSTOM_TRANSACTION_INDEX).getOperations(); + BaseOperation operation = operations.get(0); + Assert.assertThat(operation, instanceOf(CustomOperation.class)); + + CustomOperation customOperation = (CustomOperation) operation; + AssetAmount fee = customOperation.getFee(); + Assert.assertEquals(CUSTOM_EXPECTED_FEE_AMOUNT, fee.getAmount()); + Assert.assertEquals(CUSTOM_EXPECTED_FEE_ASSET_ID, fee.getAsset().getObjectId()); + Assert.assertEquals(CUSTOM_EXPECTED_PAYER, customOperation.getPayer().getObjectId()); + Assert.assertEquals(CUSTOM_EXPECTED_REQUIRED_AUTHS_SIZE, customOperation.getRequiredAuths().size()); + Assert.assertEquals(CUSTOM_EXPECTED_REQUIRED_AUTH, customOperation.getRequiredAuths().get(0).getObjectId()); + Assert.assertEquals(CUSTOM_EXPECTED_ID, customOperation.getOperationId()); + Assert.assertEquals(CUSTOM_EXPECTED_DATA, customOperation.getData()); } catch (WebSocketException e) { System.out.println("WebSocketException. Msg: " + e.getMessage()); Assert.fail("Fail because of WebSocketException");