Skip to content

Commit 63dc3c3

Browse files
authored
feat: RA-44 Integration tests for /blocks and /block/transactions (#123)
* feat: RA-44 add em to int test * feat: RA-44 rm TODOs * feat: RA-44 fix windows local start for h2 * feat: RA-44 add assert for sender2
1 parent fb0d9f2 commit 63dc3c3

File tree

6 files changed

+58
-29
lines changed

6 files changed

+58
-29
lines changed

.env.h2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
LOG=INFO
2-
NETWORK="devkit" # devkit, mainnet, testnet, prepro
2+
NETWORK=devkit # devkit, mainnet, testnet, prepro
33
PROTOCOL_MAGIC=42
44

55
#common env
@@ -9,18 +9,18 @@ DB_ADMIN_USER_SECRET=weakpwd#123_d
99
DB_IMAGE_NAME=h2
1010
DB_IMAGE_TAG=14.11-bullseye
1111
DB_NAME=rosetta-java-preprod
12-
DB_HOST="db" # Service name in docker-compose or local db
12+
DB_HOST=db # Service name in docker-compose or local db
1313
DB_PORT=5432
1414
DB_SCHEMA=${NETWORK}
1515
MAXIMUM_POOL_SIZE=80
1616
JDBC_BATCH_SIZE=1000
1717
SCHEMA=${NETWORK}
18-
CARDANO_NODE_HOST="localhost" # Service name in docker-compose or local cardano node
18+
CARDANO_NODE_HOST=localhost # Service name in docker-compose or local cardano node
1919
CARDANO_NODE_PORT=3001 # Uncomment if you are using local cardano node
2020
CARDANO_NODE_VERSION=8.9.0
21-
CARDANO_NODE_SUBMIT_HOST="cardano-submit-api"
21+
CARDANO_NODE_SUBMIT_HOST=cardano-submit-api
2222
NODE_SUBMIT_API_PORT=8090
23-
CARDANO_NODE_SOCKET="./node-ipc"
23+
CARDANO_NODE_SOCKET=./node-ipc
2424

2525
#api env
2626
API_SPRING_PROFILES_ACTIVE=h2

api/src/main/java/org/cardanofoundation/rosetta/api/block/mapper/BlockTxToRosettaTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BlockTxToRosettaTransaction {
3333
final WithdrawalToOperation withdrawalToOperation;
3434

3535
private static final OperationStatus status = OperationStatus.builder()
36-
.status(SUCCESS_OPERATION_STATUS.getStatus()) // TODO saa: need to check the right status
36+
.status(SUCCESS_OPERATION_STATUS.getStatus())
3737
.build();
3838

3939

api/src/main/java/org/cardanofoundation/rosetta/api/block/mapper/WithdrawalToOperation.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import java.math.BigInteger;
44
import java.util.Optional;
5+
56
import lombok.AllArgsConstructor;
6-
import org.cardanofoundation.rosetta.api.block.model.domain.Withdrawal;
7-
import org.cardanofoundation.rosetta.common.annotation.OpenApiMapper;
8-
import org.cardanofoundation.rosetta.common.enumeration.OperationType;
7+
98
import org.modelmapper.ModelMapper;
10-
import org.modelmapper.spi.MappingContext;
119
import org.openapitools.client.model.Amount;
1210
import org.openapitools.client.model.Operation;
1311
import org.openapitools.client.model.OperationStatus;
1412

13+
import org.cardanofoundation.rosetta.api.block.model.domain.Withdrawal;
14+
import org.cardanofoundation.rosetta.common.annotation.OpenApiMapper;
15+
import org.cardanofoundation.rosetta.common.enumeration.OperationType;
16+
1517
@OpenApiMapper
1618
@AllArgsConstructor
1719
public class WithdrawalToOperation extends AbstractToOperation<Withdrawal>{
@@ -30,7 +32,7 @@ public Operation toDto(Withdrawal model, OperationStatus status, int index) {
3032
mp.<Amount>map(f -> updateDepositAmount(
3133
Optional.ofNullable(model.getAmount())
3234
.map(BigInteger::negate)
33-
.orElse(BigInteger.ZERO)), //TODO saa: is it OK?
35+
.orElse(BigInteger.ZERO)),
3436
(d, v) -> d.getMetadata().setWithdrawalAmount(v));
3537
mp.<Long>map(f -> index, (d, v) -> d.getOperationIdentifier().setIndex(v));
3638
})

api/src/test/java/org/cardanofoundation/rosetta/api/block/mapper/BlockToBlockResponseTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ void toDto_test_Ok() {
169169
.map(Operation::getMetadata)
170170
.collect(Collectors.toList()))
171171
.allSatisfy(d -> {
172-
//TODO saa: Is it OK to have all values here is null other than depositAmount?
173172
assertAllPropertiesIsNull(d, "depositAmount");
174173
assertProperty(d, "depositAmount",
175174
Amount

api/src/test/java/org/cardanofoundation/rosetta/api/block/mapper/BlockTxToBlockTxResponseTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void toDto() {
4141
assertThat(tx.getMetadata().getScriptSize()).isEqualTo(from.getScriptSize());
4242
assertThat(tx.getTransactionIdentifier().getHash()).isEqualTo(from.getHash());
4343

44-
//TODO saa: there are no related transactions for org.openapitools.client.model.Transaction.setRelatedTransactions
4544
assertThat(tx.getRelatedTransactions()).isNull();
4645
}
4746

api/src/test/java/org/cardanofoundation/rosetta/api/block/service/BlockServiceImplIntTest.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.cardanofoundation.rosetta.api.block.service;
22

3+
import java.math.BigInteger;
4+
import jakarta.persistence.EntityManager;
5+
import jakarta.persistence.PersistenceContext;
6+
37
import org.springframework.beans.factory.annotation.Autowired;
48

59
import org.junit.jupiter.api.Test;
@@ -8,20 +12,27 @@
812
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
913
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
1014
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
15+
import org.cardanofoundation.rosetta.api.block.model.entity.BlockEntity;
16+
import org.cardanofoundation.rosetta.api.block.model.entity.UtxoKey;
1117
import org.cardanofoundation.rosetta.common.services.ProtocolParamService;
12-
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
13-
import org.cardanofoundation.rosetta.testgenerator.common.TestTransactionNames;
1418
import org.cardanofoundation.rosetta.testgenerator.common.TransactionBlockDetails;
1519

1620
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.cardanofoundation.rosetta.testgenerator.common.TestConstants.ACCOUNT_BALANCE_LOVELACE_AMOUNT;
22+
import static org.cardanofoundation.rosetta.testgenerator.common.TestConstants.SENDER_2_ADDRESS;
23+
import static org.cardanofoundation.rosetta.testgenerator.common.TestConstants.TEST_ACCOUNT_ADDRESS;
24+
import static org.cardanofoundation.rosetta.testgenerator.common.TestTransactionNames.SIMPLE_TRANSACTION;
1725
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1827

1928
class BlockServiceImplIntTest extends IntegrationTest {
2029

2130
@Autowired
2231
private BlockService blockService;
23-
final TransactionBlockDetails simpleTx = generatedDataMap.get(
24-
TestTransactionNames.SIMPLE_TRANSACTION.getName());
32+
final TransactionBlockDetails simpleTx = generatedDataMap.get(SIMPLE_TRANSACTION.getName());
33+
34+
@PersistenceContext
35+
private EntityManager entityManager;
2536

2637

2738
@Autowired
@@ -31,18 +42,17 @@ class BlockServiceImplIntTest extends IntegrationTest {
3142
void getBlockWithTransaction_Test() {
3243
//given
3344
//when
34-
Block block = blockService.findBlock(simpleTx.blockNumber(),
35-
simpleTx.blockHash());
45+
Block block = blockService.findBlock(simpleTx.blockNumber(), simpleTx.blockHash());
3646

3747
//then
3848
assertEquals(simpleTx.blockHash(), block.getHash());
3949
assertEquals(simpleTx.blockNumber(), block.getSlotNo());
4050
assertEquals(1, block.getTransactions().size());
4151

4252
Utxo receiverUtxoDto = block.getTransactions().getFirst().getOutputs().getFirst();
43-
assertEquals(TestConstants.TEST_ACCOUNT_ADDRESS, receiverUtxoDto.getOwnerAddr());
53+
assertEquals(TEST_ACCOUNT_ADDRESS, receiverUtxoDto.getOwnerAddr());
4454
assertEquals(simpleTx.txHash(), receiverUtxoDto.getTxHash());
45-
assertEquals(TestConstants.ACCOUNT_BALANCE_LOVELACE_AMOUNT,
55+
assertEquals(ACCOUNT_BALANCE_LOVELACE_AMOUNT,
4656
receiverUtxoDto.getLovelaceAmount().toString());
4757

4858
}
@@ -70,17 +80,36 @@ void getBlockTransaction_Test() {
7080
assertEquals(0, tx.getPoolRetirements().size());
7181
assertEquals(0, tx.getDelegations().size());
7282

83+
assertNotNull(entityManager);
84+
BlockEntity fromBlockB = entityManager
85+
.createQuery("from BlockEntity b where b.number=:block", BlockEntity.class)
86+
.setParameter("block", simpleTx.blockNumber())
87+
.getSingleResult();
88+
7389
Utxo inUtxo = tx.getInputs().getFirst();
90+
UtxoKey expectedInputKey = fromBlockB.getTransactions().getFirst().getInputKeys().getFirst();
91+
92+
assertEquals(SENDER_2_ADDRESS, inUtxo.getOwnerAddr());
93+
assertEquals(expectedInputKey.getTxHash(), inUtxo.getTxHash());
94+
assertEquals(expectedInputKey.getOutputIndex(), inUtxo.getOutputIndex());
95+
96+
Utxo outUtxo1 = tx.getOutputs().getFirst();
97+
assertEquals(TEST_ACCOUNT_ADDRESS, outUtxo1.getOwnerAddr());
98+
assertEquals(blockTxHash, outUtxo1.getTxHash());
99+
assertEquals(ACCOUNT_BALANCE_LOVELACE_AMOUNT, outUtxo1.getLovelaceAmount().toString());
100+
101+
Utxo outUtxo2 = tx.getOutputs().getLast();
102+
assertEquals(SENDER_2_ADDRESS, outUtxo2.getOwnerAddr());
103+
assertEquals(blockTxHash, outUtxo2.getTxHash());
74104

75-
//TODO saa: how to check?
76-
assertEquals(TestConstants.SENDER_2_ADDRESS, inUtxo.getOwnerAddr());
77-
// assertEquals(blockTxHash, inUtxo.getTxHash());
105+
// init deposit was 1000 ADA for the account1: addr_test1qp73lju...
106+
// (@see test-data-generator/README.md)
107+
BigInteger initAmountSender1 = BigInteger.valueOf(1000 * 1_000_000); //ADA to lovelace
108+
BigInteger expected = initAmountSender1
109+
.add(fromBlockB.getTotalFees().negate()) //fee
110+
.add(new BigInteger(ACCOUNT_BALANCE_LOVELACE_AMOUNT).negate()); //sent amount
78111

79-
Utxo outUtxo = tx.getOutputs().getFirst();
80-
assertEquals(TestConstants.TEST_ACCOUNT_ADDRESS, outUtxo.getOwnerAddr());
81-
assertEquals(blockTxHash, outUtxo.getTxHash());
82-
assertEquals(TestConstants.ACCOUNT_BALANCE_LOVELACE_AMOUNT,
83-
outUtxo.getLovelaceAmount().toString());
112+
assertEquals(expected, outUtxo2.getLovelaceAmount());
84113

85114
}
86115

0 commit comments

Comments
 (0)