From f61c235a3c0a00050d42bf7caf107ad18765f23d Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Wed, 14 Aug 2024 12:21:02 +0200 Subject: [PATCH] chore: adding tests --- .../rosetta/api/BaseMapperSetup.java | 1 + .../service/BlockServiceImplIntTest.java | 2 +- ...tionsToSearchTransactionsResponseTest.java | 45 ++++++++++++ .../service/SearchControllerIntTest.java | 73 +++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/org/cardanofoundation/rosetta/api/search/mapper/BlockTransactionsToSearchTransactionsResponseTest.java create mode 100644 api/src/test/java/org/cardanofoundation/rosetta/api/search/service/SearchControllerIntTest.java diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/BaseMapperSetup.java b/api/src/test/java/org/cardanofoundation/rosetta/api/BaseMapperSetup.java index e97d9ee8..581ff8c0 100644 --- a/api/src/test/java/org/cardanofoundation/rosetta/api/BaseMapperSetup.java +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/BaseMapperSetup.java @@ -39,6 +39,7 @@ public void before() { @ComponentScan(basePackages = { "org.cardanofoundation.rosetta.api.block.mapper", "org.cardanofoundation.rosetta.api.account.mapper", + "org.cardanofoundation.rosetta.api.search.mapper", "org.cardanofoundation.rosetta.common.mapper"}) public static class BaseMappersConfig { diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/block/service/BlockServiceImplIntTest.java b/api/src/test/java/org/cardanofoundation/rosetta/api/block/service/BlockServiceImplIntTest.java index 83ce4791..7b28c642 100644 --- a/api/src/test/java/org/cardanofoundation/rosetta/api/block/service/BlockServiceImplIntTest.java +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/block/service/BlockServiceImplIntTest.java @@ -71,7 +71,7 @@ void getBlockTransaction_Test() { assertEquals(blockTxHash, tx.getHash()); assertEquals(blockNo, tx.getBlockNo()); assertEquals(fee, tx.getFee()); - assertEquals(385, tx.getSize()); + assertEquals(284, tx.getSize()); assertEquals(0, tx.getScriptSize()); assertEquals(1, tx.getInputs().size()); assertEquals(2, tx.getOutputs().size()); diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/search/mapper/BlockTransactionsToSearchTransactionsResponseTest.java b/api/src/test/java/org/cardanofoundation/rosetta/api/search/mapper/BlockTransactionsToSearchTransactionsResponseTest.java new file mode 100644 index 00000000..2b7aa140 --- /dev/null +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/search/mapper/BlockTransactionsToSearchTransactionsResponseTest.java @@ -0,0 +1,45 @@ +package org.cardanofoundation.rosetta.api.search.mapper; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import org.cardanofoundation.rosetta.api.BaseMapperSetup; +import org.junit.jupiter.api.Test; +import org.openapitools.client.model.BlockIdentifier; +import org.openapitools.client.model.BlockTransaction; +import org.openapitools.client.model.SearchTransactionsResponse; +import org.openapitools.client.model.Transaction; +import org.openapitools.client.model.TransactionMetadata; +import org.springframework.beans.factory.annotation.Autowired; + +class BlockTransactionsToSearchTransactionsResponseTest extends BaseMapperSetup { + + @Autowired + private SearchMapper my; + + @Test + void mapToSearchResponse_test_Ok() { + List blockTransactions = List.of(BlockTransaction.builder() + .blockIdentifier(BlockIdentifier.builder() + .hash("hash") + .index(1L) + .build()) + .transaction(Transaction.builder() + .metadata(TransactionMetadata.builder() + .size(10L) + .scriptSize(11L) + .build()) + .build()) + .build()); + + SearchTransactionsResponse searchTransactionsResponse = my.mapToSearchTransactionsResponse( + blockTransactions, 2L); + + assertThat(searchTransactionsResponse.getTransactions().size() == 1); + assertThat(searchTransactionsResponse.getNextOffset() == 2L); + assertThat(searchTransactionsResponse.getTotalCount() == 1); + assertThat(searchTransactionsResponse.getTransactions().get(0).getBlockIdentifier().getHash().equals("hash")); + assertThat(searchTransactionsResponse.getTransactions().get(0).getBlockIdentifier().getIndex().equals(1L)); + } + +} diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/search/service/SearchControllerIntTest.java b/api/src/test/java/org/cardanofoundation/rosetta/api/search/service/SearchControllerIntTest.java new file mode 100644 index 00000000..c2c00cd6 --- /dev/null +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/search/service/SearchControllerIntTest.java @@ -0,0 +1,73 @@ +package org.cardanofoundation.rosetta.api.search.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import org.cardanofoundation.rosetta.api.IntegrationTest; +import org.cardanofoundation.rosetta.testgenerator.common.TestConstants; +import org.cardanofoundation.rosetta.testgenerator.common.TransactionBlockDetails; +import org.junit.jupiter.api.Test; +import org.openapitools.client.model.BlockTransaction; +import org.openapitools.client.model.CoinIdentifier; +import org.openapitools.client.model.SearchTransactionsRequest; +import org.openapitools.client.model.TransactionIdentifier; +import org.springframework.beans.factory.annotation.Autowired; + +class SearchControllerIntTest extends IntegrationTest { + + @Autowired + private SearchService service; + + @Test + void searchAddressTransactions() { + SearchTransactionsRequest req = SearchTransactionsRequest.builder() + .address(TestConstants.TEST_ACCOUNT_ADDRESS) + .build(); + + List blockTransactions = service.searchTransaction(req, 0L, 5L); + assertThat(blockTransactions.size() == 5); + } + + @Test + void searchByTxHash() { + TransactionBlockDetails stakeKeyDeregistration = generatedDataMap.get( + "stake_key_deregistration"); + SearchTransactionsRequest req = SearchTransactionsRequest.builder() + .transactionIdentifier(TransactionIdentifier.builder() + .hash(stakeKeyDeregistration.txHash()) + .build()) + .build(); + List blockTransactions = service.searchTransaction(req, 0L, 10L); + assertThat(blockTransactions.size() == 1); + BlockTransaction tx = blockTransactions.getFirst(); + assertThat(tx.getBlockIdentifier().getHash().equals(stakeKeyDeregistration.blockHash())); + assertThat(tx.getBlockIdentifier().getIndex() == stakeKeyDeregistration.blockNumber()); + assertThat(tx.getTransaction().getTransactionIdentifier().getHash().equals(stakeKeyDeregistration.txHash())); + assertThat(tx.getTransaction().getOperations().size() == 4); + } + + @Test + void searchByUtxo() { + TransactionBlockDetails txDetails = generatedDataMap.get( + "simple_transaction"); + SearchTransactionsRequest req = SearchTransactionsRequest.builder() + .transactionIdentifier(TransactionIdentifier.builder() + .hash(txDetails.txHash()) + .build()) + .build(); + + List blockTransactions = service.searchTransaction(req, 0L, 10L); + assertThat(blockTransactions.size() == 1); + String identifier = blockTransactions.getFirst().getTransaction().getOperations().getFirst() + .getCoinChange().getCoinIdentifier().getIdentifier(); + req = SearchTransactionsRequest.builder() + .coinIdentifier(CoinIdentifier.builder() + .identifier(identifier) + .build()) + .build(); + + blockTransactions = service.searchTransaction(req, 0L, 10L); + assertThat(blockTransactions.size() == 2); + } + +}