Skip to content

Commit

Permalink
RA-44 Tests for OperationDataMapper (#106)
Browse files Browse the repository at this point in the history
* feat: RA-44 rename mappers for Tran to BlockTx

* feat: RA-44 comment getBlockTransaction_Test

* feat: RA-44 rename Tran* -> BlockTx*

* feat: RA-44 Added stakeRegistrationOperations test

* feat: RA-44 Added stake[De]RegistrationOperations test

* feat: RA-44 Added stakeDelegation test

* feat: RA-44 Added PoolRegistrationOperations test

* feat: RA-44 Added getPoolRetirementsOperations test

* feat: RA-44 Added getOutputsAsOperations test

* feat: RA-44 marge with main and fix merge conflicts
  • Loading branch information
shleger authored Apr 4, 2024
1 parent 84e6cff commit fc8effd
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.openapitools.client.model.PartialBlockIdentifier;

import org.cardanofoundation.rosetta.api.block.mapper.BlockToBlockResponse;
import org.cardanofoundation.rosetta.api.block.mapper.TranToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.mapper.BlockTxToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
import org.cardanofoundation.rosetta.api.block.service.BlockService;
Expand All @@ -25,7 +25,7 @@ public class BlockApiImpl implements BlockApi {
private final BlockService blockService;

private final BlockToBlockResponse mapperToBlockResponse;
private final TranToBlockTxResponse mapperToBlockTxResponse;
private final BlockTxToBlockTxResponse mapperToBlockTxResponse;

@Override
public ResponseEntity<BlockResponse> block(@RequestBody BlockRequest blockRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class BlockToBlockResponse {

final ModelMapper modelMapper;
final TranToRosettaTransaction mapToRosettaTransaction;
final BlockTxToRosettaTransaction mapToRosettaTransaction;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.AllArgsConstructor;

import org.modelmapper.ModelMapper;
import org.modelmapper.spi.MappingContext;

import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.entity.BlockEntity;
Expand All @@ -19,7 +18,7 @@ public class BlockToEntity {

final ModelMapper modelMapper;

final TranToEntity tranToEntity;
final BlockTxToEntity blockTxToEntity;


public Block fromEntity(BlockEntity entity) {
Expand All @@ -35,35 +34,27 @@ public Block fromEntity(BlockEntity entity) {

})
.setPostConverter(ctx -> {
BlockEntity source = ctx.getSource();
Block dest = ctx.getDestination();

dest(ctx).setCreatedAt(TimeUnit.SECONDS.toMillis(source(ctx).getBlockTimeInSeconds()));
dest.setCreatedAt(TimeUnit.SECONDS.toMillis(source.getBlockTimeInSeconds()));
dest.setSize(Math.toIntExact(source.getBlockBodySize()));

dest(ctx).setPreviousBlockHash(
ofNullable(source(ctx).getPrev())
.map(BlockEntity::getHash)
.orElse(source(ctx).getHash()));
dest.setPreviousBlockHash(ofNullable(source.getPrev())
.map(BlockEntity::getHash)
.orElse(source.getHash()));

dest(ctx).setPreviousBlockNumber(
ofNullable(source(ctx).getPrev())
.map(BlockEntity::getNumber)
.orElse(0L));
dest.setPreviousBlockNumber(ofNullable(source.getPrev())
.map(BlockEntity::getNumber)
.orElse(0L));

dest(ctx).setSize(Math.toIntExact(source(ctx).getBlockBodySize()));
dest(ctx).setTransactions(
source(ctx).getTransactions().stream().map(tranToEntity::fromEntity).toList());

return dest(ctx);
dest.setTransactions(source.getTransactions()
.stream()
.map(blockTxToEntity::fromEntity)
.toList());

return dest;
}).map(entity);

}

private static BlockEntity source(MappingContext<BlockEntity, Block> ctx) {
return ctx.getSource();
}

private static Block dest(MappingContext<BlockEntity, Block> ctx) {
return ctx.getDestination();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

@OpenApiMapper
@AllArgsConstructor
public class TranToBlockTxResponse {
public class BlockTxToBlockTxResponse {

final ModelMapper modelMapper;

final TranToRosettaTransaction tranToRosettaTransaction;
final BlockTxToRosettaTransaction blockTxToRosettaTx;


public BlockTransactionResponse toDto(BlockTx model, String poolDeposit) {
return Optional
.ofNullable(modelMapper.getTypeMap(BlockTx.class, BlockTransactionResponse.class))
.orElseGet(() -> modelMapper.createTypeMap(BlockTx.class, BlockTransactionResponse.class))
.setPostConverter(ctx -> {
ctx.getDestination().setTransaction(tranToRosettaTransaction.toDto(model, poolDeposit));
ctx.getDestination().setTransaction(blockTxToRosettaTx.toDto(model, poolDeposit));
return ctx.getDestination();
}).map(model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigInteger;
import java.util.Optional;

import lombok.AllArgsConstructor;

import org.modelmapper.ModelMapper;
Expand All @@ -16,12 +17,11 @@

@PersistenceMapper
@AllArgsConstructor
public class TranToEntity {
public class BlockTxToEntity {

final ModelMapper modelMapper;

public BlockTx fromEntity(TxnEntity model) {

return ofNullable(modelMapper.getTypeMap(TxnEntity.class, BlockTx.class))
.orElseGet(() -> modelMapper.createTypeMap(TxnEntity.class, BlockTx.class))
.addMappings(mapper -> {
Expand All @@ -36,24 +36,17 @@ public BlockTx fromEntity(TxnEntity model) {

})
.setPostConverter(ctx -> {

dest(ctx).setInputs(source(ctx).getInputKeys().stream().map(Utxo::fromUtxoKey).toList());
dest(ctx).setOutputs(source(ctx).getOutputKeys().stream().map(Utxo::fromUtxoKey).toList());
dest(ctx).setFee(Optional.ofNullable(source(ctx).getFee()).map(BigInteger::toString).orElse(null));

return dest(ctx);


TxnEntity source = ctx.getSource();
BlockTx dest = ctx.getDestination();
dest.setInputs(source.getInputKeys().stream().map(Utxo::fromUtxoKey).toList());
dest.setOutputs(source.getOutputKeys().stream().map(Utxo::fromUtxoKey).toList());
dest.setFee(Optional
.ofNullable(source.getFee())
.map(BigInteger::toString)
.orElse(null));

return dest;
})
.map(model);
}

private static TxnEntity source(MappingContext<TxnEntity, BlockTx> ctx) {
return ctx.getSource();
}

private static BlockTx dest(MappingContext<TxnEntity, BlockTx> ctx) {
return ctx.getDestination();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@OpenApiMapper
@AllArgsConstructor
public class TranToRosettaTransaction {
public class BlockTxToRosettaTransaction {

final ModelMapper modelMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public static List<Operation> getStakeRegistrationOperations(BlockTx transaction

.depositAmount(DataMapper.mapAmount("2000000", ADA, ADA_DECIMALS,
null)) // TODO need to get this from protocolparams
// Create and inject GenesisService to get the stake deposit amount
// see similar implementation in BlockService.getPoolDeposit
.build())
.build();
}).toList();
Expand All @@ -209,9 +211,7 @@ public static List<Operation> getDelegationOperations(BlockTx transaction,
.type(OperationType.STAKE_DELEGATION.getValue())
.status(status.getStatus())
.account(AccountIdentifier.builder().address(delegation.getAddress()).build())
.metadata(OperationMetadata.builder()
.poolKeyHash(delegation.getPoolId())
.build())
.metadata(OperationMetadata.builder().poolKeyHash(delegation.getPoolId()).build())
.build();
}).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public BlockTx getBlockTransaction(Long blockId, String blockHash, String txHash
.filter(tr -> tr.getHash().equals(txHash))
.findFirst()
.orElseThrow(ExceptionFactory::transactionNotFound);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.cardanofoundation.rosetta.api.account.model.repository.AddressBalanceRepository;
import org.cardanofoundation.rosetta.api.account.model.repository.AddressUtxoRepository;
import org.cardanofoundation.rosetta.api.block.mapper.BlockToEntity;
import org.cardanofoundation.rosetta.api.block.mapper.TranToEntity;
import org.cardanofoundation.rosetta.api.block.mapper.BlockTxToEntity;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.Delegation;
import org.cardanofoundation.rosetta.api.block.model.domain.GenesisBlock;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class PostgresLedgerDataProviderService implements LedgerDataProviderServ
private final CardanoConfigService cardanoConfigService;

private final BlockToEntity mapperBlock;
private final TranToEntity mapperTran;
private final BlockTxToEntity mapperTran;

@Override
public GenesisBlock findGenesisBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import org.cardanofoundation.rosetta.api.SpringMvcTest;
import org.cardanofoundation.rosetta.api.block.mapper.BlockToBlockResponse;
import org.cardanofoundation.rosetta.api.block.mapper.TranToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.mapper.BlockTxToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
import org.cardanofoundation.rosetta.api.block.service.BlockService;
Expand All @@ -41,16 +41,14 @@ class BlockApiImplTest extends SpringMvcTest {
BlockToBlockResponse blockToBlockResponse;

@MockBean
TranToBlockTxResponse mapperToBlockTxResponse;
BlockTxToBlockTxResponse mapperToBlockTxResponse;


@Test
void block_Test() throws Exception {

//given
BlockRequest blockRequest = givenBlockRequest();
when(blockService.findBlock(123L, "hash1")).thenReturn(new Block());

//when
//then
Long index = blockRequest.getBlockIdentifier().getIndex();
Expand All @@ -62,17 +60,13 @@ void block_Test() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.block.block_identifier.index").value(index))
.andExpect(jsonPath("$.block.block_identifier.hash").value(hash));


}

@Test
void blockNotFound_Test() throws Exception {

//given
BlockRequest blockRequest = givenBlockRequest();
when(blockService.findBlock(123L, "hash1")).thenThrow(ExceptionFactory.blockNotFoundException());

//when
//then
mockMvc.perform(post("/block")
Expand All @@ -83,21 +77,17 @@ void blockNotFound_Test() throws Exception {
.andExpect(jsonPath("$.code").value(4001))
.andExpect(jsonPath("$.message").value("Block not found"))
.andExpect(jsonPath("$.retriable").value(false));

}

@Test
void blockTransaction_Test() throws Exception {

//given
BlockTransactionResponse resp = newBlockTransactionResponse();
BlockTransactionRequest req = newBlockTransactionRequest();

when(blockService.getBlockTransaction(anyLong(), anyString(), anyString())).thenReturn(
new BlockTx());
when(blockService.getPoolDeposit()).thenReturn("1000"); //any string
when(mapperToBlockTxResponse.toDto(any(BlockTx.class), anyString())).thenReturn(resp);

//when
//then
String txHash = resp.getTransaction().getTransactionIdentifier().getHash();
Expand All @@ -107,18 +97,15 @@ void blockTransaction_Test() throws Exception {
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.transaction.transaction_identifier.hash").value(txHash));

}


@Test
void blockTransaction_notFound_Test() throws Exception {

//given
BlockTransactionRequest req = newBlockTransactionRequest();
when(blockService.getBlockTransaction(anyLong(), anyString(), anyString()))
.thenThrow(ExceptionFactory.transactionNotFound());

//when
//then
mockMvc.perform(post("/block/transaction")
Expand All @@ -129,7 +116,6 @@ void blockTransaction_notFound_Test() throws Exception {
.andExpect(jsonPath("$.code").value(4006))
.andExpect(jsonPath("$.message").value("Transaction not found"))
.andExpect(jsonPath("$.retriable").value(false));

}

private BlockRequest givenBlockRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BlockToBlockResponseTest extends BaseMapperTest {
void toDto_test_Ok() {

//given
TranToRosettaTransaction tx2tx = new TranToRosettaTransaction(modelMapper);
BlockTxToRosettaTransaction tx2tx = new BlockTxToRosettaTransaction(modelMapper);
BlockToBlockResponse my = new BlockToBlockResponse(modelMapper,tx2tx);
my.modelMapper.validate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class BlockToEntityTest extends BaseMapperTest {
void fromEntity_Test() {

//given
TranToEntity tranToEntity = new TranToEntity(modelMapper);
BlockToEntity my = new BlockToEntity(modelMapper, tranToEntity);
BlockTxToEntity blockTxToEntity = new BlockTxToEntity(modelMapper);
BlockToEntity my = new BlockToEntity(modelMapper, blockTxToEntity);
my.modelMapper.validate();
BlockEntity from = newBlockEntity();

Expand Down
Loading

0 comments on commit fc8effd

Please sign in to comment.