11package org .cardanofoundation .rosetta .api .block .service ;
22
3+ import java .math .BigInteger ;
4+ import jakarta .persistence .EntityManager ;
5+ import jakarta .persistence .PersistenceContext ;
6+
37import org .springframework .beans .factory .annotation .Autowired ;
48
59import org .junit .jupiter .api .Test ;
812import org .cardanofoundation .rosetta .api .account .model .domain .Utxo ;
913import org .cardanofoundation .rosetta .api .block .model .domain .Block ;
1014import 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 ;
1117import org .cardanofoundation .rosetta .common .services .ProtocolParamService ;
12- import org .cardanofoundation .rosetta .testgenerator .common .TestConstants ;
13- import org .cardanofoundation .rosetta .testgenerator .common .TestTransactionNames ;
1418import org .cardanofoundation .rosetta .testgenerator .common .TransactionBlockDetails ;
1519
1620import 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 ;
1725import static org .junit .jupiter .api .Assertions .assertEquals ;
26+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
1827
1928class 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