Skip to content

Commit

Permalink
Refactoring: Fix Sonar issues, flatten services interfaces and implem…
Browse files Browse the repository at this point in the history
…entations
  • Loading branch information
BerezinD committed May 9, 2024
1 parent 5ea5d8a commit 146ceaf
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.account.service.impl;
package org.cardanofoundation.rosetta.api.account.service;

import java.util.List;
import java.util.Objects;
Expand All @@ -17,7 +17,6 @@

import org.cardanofoundation.rosetta.api.account.model.domain.AddressBalance;
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
import org.cardanofoundation.rosetta.api.account.service.AccountService;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.StakeAddressBalance;
import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import jakarta.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -39,7 +38,7 @@ protected List<Operation> convert(List<T> stakeReg, OperationStatus status,
.stream()
.flatMap(List::stream)
.map(t -> toDto(t, status, indexHolder.getAndIncrement()))
.collect(Collectors.toList());
.toList();
}

protected OperationMetadata mapToOperationMetaData(boolean spent, List<Amt> amounts) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cardanofoundation.rosetta.api.block.mapper;

import java.util.List;
import java.util.stream.Collectors;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

Expand Down Expand Up @@ -81,6 +80,6 @@ public static List<OperationIdentifier> getOperationIndexes(List<Operation> oper
.map(operation -> OperationIdentifier
.builder()
.index(operation.getOperationIdentifier().getIndex()).build())
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import jakarta.validation.constraints.NotNull;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -138,14 +137,14 @@ private void populateTransaction(BlockTx transaction) {
.findByTxHash(transaction.getHash())
.stream()
.map(m -> mapper.map(m, StakeRegistration.class))
.collect(Collectors.toList()));
.toList());

transaction.setDelegations(
delegationRepository
.findByTxHash(transaction.getHash())
.stream()
.map(m -> mapper.map(m, Delegation.class))
.collect(Collectors.toList()));
.toList());

transaction.setPoolRegistrations(poolRegistrationRepository
.findByTxHash(transaction.getHash())
Expand All @@ -154,13 +153,13 @@ private void populateTransaction(BlockTx transaction) {
.addMappings(mp ->
mp.map(PoolRegistrationEntity::getPoolOwners, PoolRegistration::setOwners))
.map(poolReg))
.collect(Collectors.toList()));
.toList());

transaction.setPoolRetirements(poolRetirementRepository
.findByTxHash(transaction.getHash())
.stream()
.map(m -> mapper.map(m, PoolRetirement.class))
.collect(Collectors.toList()));
.toList());

transaction.setWithdrawals(withdrawalRepository
.findByTxHash(transaction.getHash())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.construction.service.impl;
package org.cardanofoundation.rosetta.api.construction.service;

import java.io.IOException;
import java.util.List;
Expand All @@ -17,7 +17,6 @@
import org.openapitools.client.model.*;

import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.api.construction.service.ConstructionApiService;
import org.cardanofoundation.rosetta.common.enumeration.AddressType;
import org.cardanofoundation.rosetta.common.enumeration.NetworkEnum;
import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.mempool.service.impl;
package org.cardanofoundation.rosetta.api.mempool.service;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,7 +17,6 @@
import com.bloxbean.cardano.yaci.helper.LocalTxMonitorClient;
import org.openapitools.client.model.TransactionIdentifier;

import org.cardanofoundation.rosetta.api.mempool.service.MempoolService;
import org.cardanofoundation.rosetta.api.network.service.NetworkService;
import org.cardanofoundation.rosetta.common.model.cardano.transaction.MemPoolTransaction;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.network.service.impl;
package org.cardanofoundation.rosetta.api.network.service;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -30,7 +30,6 @@
import org.cardanofoundation.rosetta.api.block.model.domain.GenesisBlock;
import org.cardanofoundation.rosetta.api.block.model.domain.NetworkStatus;
import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService;
import org.cardanofoundation.rosetta.api.network.service.NetworkService;
import org.cardanofoundation.rosetta.common.enumeration.OperationType;
import org.cardanofoundation.rosetta.common.enumeration.OperationTypeStatus;
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ConfigurationMapper.class, SpringMappersTestConfig.class})
public class BaseMapperTest {
public class BaseMapperSetup {

@MockBean
protected ProtocolParamService protocolParamService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@SpringBootTest
@AutoConfigureMockMvc
public class SpringMvcTest {
public class SpringMvcSetup {

@Autowired
protected ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseMapperTest;
import org.cardanofoundation.rosetta.api.BaseMapperSetup;
import org.cardanofoundation.rosetta.api.account.model.entity.AddressUtxoEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.UtxoKey;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class UtxoTest extends BaseMapperTest {
class UtxoTest extends BaseMapperSetup {

@Inject
ModelMapper mapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.cardanofoundation.rosetta.api.account.service.impl;
package org.cardanofoundation.rosetta.api.account.service;

import java.math.BigInteger;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.SpringMvcTest;
import org.cardanofoundation.rosetta.api.SpringMvcSetup;
import org.cardanofoundation.rosetta.api.block.mapper.BlockToBlockResponse;
import org.cardanofoundation.rosetta.api.block.mapper.BlockTxToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
Expand All @@ -36,7 +36,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


class BlockApiImplTest extends SpringMvcTest {
class BlockApiImplTest extends SpringMvcSetup {

@MockBean
private BlockService blockService;
Expand All @@ -55,7 +55,6 @@ class BlockApiImplTest extends SpringMvcTest {
private ProtocolParams protocolParams;



@Test
void block_Test() throws Exception {
//given
Expand All @@ -78,7 +77,8 @@ void block_Test() throws Exception {
void blockNotFound_Test() throws Exception {
//given
BlockRequest blockRequest = givenBlockRequest();
when(blockService.findBlock(123L, "hash1")).thenThrow(ExceptionFactory.blockNotFoundException());
when(blockService.findBlock(123L, "hash1")).thenThrow(
ExceptionFactory.blockNotFoundException());
//when
//then
mockMvc.perform(post("/block")
Expand Down Expand Up @@ -207,6 +207,4 @@ private static NetworkIdentifier newNetworkId() {
.network("devkit")
.build();
}


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

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseMapperTest;
import org.cardanofoundation.rosetta.api.BaseMapperSetup;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
import org.cardanofoundation.rosetta.api.block.model.domain.Delegation;
Expand All @@ -36,7 +36,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.cardanofoundation.rosetta.common.util.RosettaConstants.SUCCESS_OPERATION_STATUS;

class BlockToBlockResponseTest extends BaseMapperTest {
class BlockToBlockResponseTest extends BaseMapperSetup {


@Autowired
Expand Down Expand Up @@ -69,8 +69,7 @@ void toDto_test_Ok() {
assertThat(from.getSize()).isEqualTo(into.getBlock().getMetadata().getSize());
assertThat(from.getSlotNo()).isEqualTo(into.getBlock().getMetadata().getSlotNo());

assertThat(from.getTransactions().size()).isEqualTo(
into.getBlock().getTransactions().size());
assertThat(from.getTransactions()).hasSameSizeAs(into.getBlock().getTransactions());
assertThat(from.getTransactions())
.extracting(BlockTx::getHash)
.isEqualTo(into.getBlock().getTransactions().stream()
Expand Down Expand Up @@ -222,11 +221,13 @@ void toDto_test_Ok() {
g -> g.getType().equals("stakeDelegation"))
.map(Operation::getMetadata)
.collect(Collectors.toList()))
.allSatisfy(d->
.allSatisfy(d ->
{
assertAllPropertiesIsNull(d, "poolKeyHash");
assertProperty(List.of(d.getFirst()), "poolKeyHash", "poolDlg"+poolKeyHash.incrementAndGet());
assertProperty(List.of(d.getLast()), "poolKeyHash", "poolDlg"+poolKeyHash.incrementAndGet());
assertProperty(List.of(d.getFirst()), "poolKeyHash",
"poolDlg" + poolKeyHash.incrementAndGet());
assertProperty(List.of(d.getLast()), "poolKeyHash",
"poolDlg" + poolKeyHash.incrementAndGet());
});


Expand Down Expand Up @@ -289,17 +290,17 @@ private Block newBlock() {
private List<BlockTx> newTransactions() {
return List.of(
new BlockTx("hash1", "blockHash1", 1L, "fee1", 1L,
1L, null, null,
1L, null, null,
newStakeRegistrations(1, 2), newDelegations(1, 2), newPoolRegistrations(1, 2),
newPoolRetirements(1, 2), null),

new BlockTx("hash2", "blockHash2", 2L, "fee2", 2L,
2L, null, null,
2L, null, null,
newStakeRegistrations(3, 4), newDelegations(3, 4), newPoolRegistrations(3, 4),
newPoolRetirements(3, 4), null),

new BlockTx("hash3", "blockHash3", 3L, "fee3", 3L,
3L, null, null,
3L, null, null,
newStakeRegistrations(5, 6), newDelegations(5, 6), newPoolRegistrations(5, 6),
newPoolRetirements(5, 6), null));
}
Expand Down Expand Up @@ -364,6 +365,4 @@ private List<StakeRegistration> newStakeRegistrations(int... instances) {


}


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

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseMapperTest;
import org.cardanofoundation.rosetta.api.BaseMapperSetup;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
import org.cardanofoundation.rosetta.api.block.model.entity.BlockEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.TxnEntity;

import static org.assertj.core.api.Assertions.assertThat;

class BlockToEntityTest extends BaseMapperTest {
class BlockToEntityTest extends BaseMapperSetup {

@Autowired
private BlockToEntity my;

@Test
void fromEntity_Test() {
//given
Expand All @@ -44,16 +45,16 @@ void fromEntity_Test() {
assertThat(into.getCreatedBy()).isEqualTo(from.getSlotLeader());
assertThat(into.getSlotNo()).isEqualTo(from.getSlot());

assertThat(into.getTransactions().size()).isEqualTo(from.getTransactions().size());
assertThat(into.getTransactions().size()).isEqualTo(1);
assertThat(into.getTransactions()).hasSameSizeAs(from.getTransactions());
assertThat(into.getTransactions()).hasSize(1);

assertThat(to(into).getHash()).isEqualTo(got(from).getTxHash());
assertThat(to(into).getBlockHash()).isEqualTo(got(from).getBlock().getHash());
assertThat(to(into).getBlockNo()).isEqualTo(got(from).getBlock().getNumber());
assertThat(to(into).getSize()).isZero();
assertThat(to(into).getScriptSize()).isZero();
assertThat(to(into).getInputs().size()).isEqualTo(got(from).getInputKeys().size());
assertThat(to(into).getOutputs().size()).isEqualTo(got(from).getOutputKeys().size());
assertThat(to(into).getInputs()).hasSameSizeAs(got(from).getInputKeys());
assertThat(to(into).getOutputs()).hasSameSizeAs(got(from).getOutputKeys());
assertThat(to(into).getFee()).isEqualTo(got(from).getFee().toString());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseMapperTest;
import org.cardanofoundation.rosetta.api.BaseMapperSetup;
import org.cardanofoundation.rosetta.api.account.model.domain.Amt;
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
Expand All @@ -23,7 +23,7 @@

import static org.assertj.core.api.Assertions.assertThat;

class BlockTxToBlockTxResponseTest extends BaseMapperTest {
class BlockTxToBlockTxResponseTest extends BaseMapperSetup {

@Autowired
private BlockTxToBlockTxResponse my;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseMapperTest;
import org.cardanofoundation.rosetta.api.BaseMapperSetup;
import org.cardanofoundation.rosetta.api.block.model.domain.BlockTx;
import org.cardanofoundation.rosetta.api.block.model.entity.BlockEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.TxnEntity;
import org.cardanofoundation.rosetta.api.block.model.entity.UtxoKey;

import static org.assertj.core.api.Assertions.assertThat;

class BlockTxToEntityTest extends BaseMapperTest {
class BlockTxToEntityTest extends BaseMapperSetup {

@Autowired
private BlockTxToEntity my;
Expand All @@ -36,13 +36,13 @@ void fromEntity_Test() {
assertThat(into.getSize()).isZero();
assertThat(into.getScriptSize()).isZero();

assertThat(into.getInputs().size()).isEqualTo(from.getInputKeys().size());
assertThat(into.getInputs()).hasSameSizeAs(from.getInputKeys());
assertThat(into.getInputs()).extracting("txHash")
.isEqualTo(List.of(inUtxKey.getTxHash()));
assertThat(into.getInputs()).extracting("outputIndex")
.isEqualTo(List.of(inUtxKey.getOutputIndex()));

assertThat(into.getOutputs().size()).isEqualTo(from.getOutputKeys().size());
assertThat(into.getOutputs()).hasSameSizeAs(from.getOutputKeys());
assertThat(into.getOutputs()).extracting("txHash")
.isEqualTo(List.of(outUtxKey.getTxHash()));
assertThat(into.getOutputs()).extracting("outputIndex")
Expand Down

0 comments on commit 146ceaf

Please sign in to comment.