Skip to content

Commit 0ddc8e7

Browse files
matiwinnetouMateusz Czeladka
authored andcommitted
refactor: slice replaced with a list to fix a valid sonar issue (#418)
2 parents 20304ce + 795d0b4 commit 0ddc8e7

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

api/src/main/java/org/cardanofoundation/rosetta/api/block/model/repository/TxRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cardanofoundation.rosetta.api.block.model.repository;
22

3+
import java.util.List;
34
import java.util.Set;
45
import javax.annotation.Nullable;
56

@@ -15,7 +16,7 @@
1516
@Repository
1617
public interface TxRepository extends JpaRepository<TxnEntity, Long> {
1718

18-
Slice<TxnEntity> findTransactionsByBlockHash(@Param("blockHash") String blockHash);
19+
List<TxnEntity> findTransactionsByBlockHash(@Param("blockHash") String blockHash);
1920

2021
@Query(value = """
2122
SELECT DISTINCT tx FROM TxnEntity tx

api/src/main/java/org/cardanofoundation/rosetta/api/block/service/LedgerBlockService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cardanofoundation.rosetta.api.block.service;
22

3+
import java.util.List;
34
import java.util.Optional;
45

56
import org.springframework.data.domain.Slice;
@@ -27,7 +28,9 @@ public interface LedgerBlockService {
2728
* @param hash block hash
2829
* @return the list of transactions
2930
*/
30-
Slice<BlockTx> findTransactionsByBlock(Long number, String hash);
31+
List<BlockTx> findTransactionsByBlock(Long number, String hash);
32+
33+
List<BlockTx> mapTxnEntitiesToBlockTxList(List<TxnEntity> txList);
3134

3235
Slice<BlockTx> mapTxnEntitiesToBlockTxList(Slice<TxnEntity> txList);
3336

api/src/main/java/org/cardanofoundation/rosetta/api/block/service/LedgerBlockServiceImpl.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
import lombok.extern.slf4j.Slf4j;
1818

1919
import org.springframework.beans.factory.annotation.Value;
20-
import org.springframework.data.domain.Page;
2120
import org.springframework.data.domain.Slice;
2221
import org.springframework.stereotype.Component;
2322
import org.springframework.transaction.annotation.Transactional;
24-
import org.apache.commons.lang3.ObjectUtils;
2523

2624
import org.cardanofoundation.rosetta.api.account.mapper.AddressUtxoEntityToUtxo;
2725
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
@@ -108,24 +106,32 @@ private Block toModelFrom(BlockEntity blockEntity) {
108106
}
109107

110108
@Override
111-
public Slice<BlockTx> findTransactionsByBlock(Long blk, String blkHash) {
109+
public List<BlockTx> findTransactionsByBlock(Long blk, String blkHash) {
112110
log.debug("query blockNumber: {} blockHash: {}", blk, blkHash);
113111
Optional<BlockEntity> blkEntity = blockRepository.findByNumberAndHash(blk, blkHash);
114112

115113
if (blkEntity.isEmpty()) {
116114
log.debug("Block Not found: {} blockHash: {}", blk, blkHash);
117115

118-
return Page.empty();
116+
return List.of();
119117
}
120118

121-
Slice<TxnEntity> txList = txRepository.findTransactionsByBlockHash(blkEntity.get().getHash());
122-
log.debug("Found {} transactions", txList.getNumberOfElements());
119+
List<TxnEntity> txList = txRepository.findTransactionsByBlockHash(blkEntity.get().getHash());
120+
log.debug("Found {} transactions", txList.size());
123121

124-
if (ObjectUtils.isNotEmpty(txList)) {
125-
return mapTxnEntitiesToBlockTxList(txList);
126-
}
122+
return mapTxnEntitiesToBlockTxList(txList);
123+
}
124+
125+
@Override
126+
public List<BlockTx> mapTxnEntitiesToBlockTxList(List<TxnEntity> txList) {
127+
List<BlockTx> transactions = txList.stream().map(blockMapper::mapToBlockTx).toList();
128+
129+
TransactionInfo fetched = findByTxHash(transactions);
130+
Map<UtxoKey, AddressUtxoEntity> utxoMap = getUtxoMapFromEntities(fetched);
127131

128-
return Page.empty();
132+
transactions.forEach(tx -> populateTransaction(tx, fetched, utxoMap));
133+
134+
return transactions;
129135
}
130136

131137
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.List;
55
import java.util.Optional;
66

7-
import org.springframework.data.domain.PageImpl;
87
import org.mockito.InjectMocks;
98
import org.mockito.Mock;
109
import org.mockito.junit.jupiter.MockitoExtension;
@@ -124,7 +123,7 @@ void getBlockTransaction_Test_OK() {
124123
long blockId = 1L;
125124
String blockHash = "hash1";
126125
when(ledgerBlockService.findTransactionsByBlock(blockId, blockHash))
127-
.thenReturn(new PageImpl<>(List.of(tx)));
126+
.thenReturn(List.of(tx));
128127
//when
129128
BlockTx blockTransaction = blockService.getBlockTransaction(blockId, blockHash, txHash);
130129
//then
@@ -138,7 +137,7 @@ void getBlockTransaction_Test_notFoundTransaction() {
138137
long blockId = 1L;
139138
String blockHash = "hash1";
140139
when(ledgerBlockService.findTransactionsByBlock(blockId, blockHash))
141-
.thenReturn(new PageImpl<>(List.of(newTran("any"))));
140+
.thenReturn(List.of(newTran("any")));
142141
try {
143142
//when
144143
blockService.getBlockTransaction(blockId, blockHash, "differentFromAny");

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import jakarta.persistence.PersistenceContext;
77

88
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.data.domain.Slice;
109
import org.springframework.transaction.annotation.Transactional;
1110

1211
import org.junit.jupiter.api.RepeatedTest;
@@ -116,7 +115,7 @@ void findBlock_Test_OK_empty() {
116115
void findTransactionsByBlock_Test_empty_tx() {
117116
//given
118117
//when
119-
Slice<BlockTx> txs = ledgerBlockService.findTransactionsByBlock(-123L, "#####");
118+
List<BlockTx> txs = ledgerBlockService.findTransactionsByBlock(-123L, "#####");
120119
//then
121120
assertThat(txs).isEmpty();
122121
}
@@ -128,7 +127,7 @@ void findTransactionsByBlock_Test_delegation_tx() {
128127
//when
129128

130129
List<BlockTx> txs =
131-
ledgerBlockService.findTransactionsByBlock(tx.blockNumber(), tx.blockHash()).getContent();
130+
ledgerBlockService.findTransactionsByBlock(tx.blockNumber(), tx.blockHash());
132131

133132
//then
134133
assertThat(txs).isNotNull().hasSize(1);
@@ -162,12 +161,12 @@ void findTransactionsByBlock_Test_pool_reg_tx() {
162161
//given
163162
TransactionBlockDetails tx = generatedDataMap.get(POOL_REGISTRATION_TRANSACTION.getName());
164163
//when
165-
Slice<BlockTx> txs =
164+
List<BlockTx> txs =
166165
ledgerBlockService.findTransactionsByBlock(tx.blockNumber(), tx.blockHash());
167166
//then
168167
assertThat(txs).isNotNull().hasSize(1);
169168

170-
BlockTx blockTx = txs.getContent().getFirst();
169+
BlockTx blockTx = txs.getFirst();
171170
assertThat(blockTx.getHash()).isEqualTo(tx.txHash());
172171
assertThat(blockTx.getBlockNo()).isEqualTo(tx.blockNumber());
173172
assertThat(blockTx.getBlockHash()).isEqualTo(tx.blockHash());
@@ -206,12 +205,12 @@ void findTransactionsByBlock_Test_pool_ret_tx() {
206205
//given
207206
TransactionBlockDetails tx = generatedDataMap.get(POOL_RETIREMENT_TRANSACTION.getName());
208207
//when
209-
Slice<BlockTx> txs =
208+
List<BlockTx> txs =
210209
ledgerBlockService.findTransactionsByBlock(tx.blockNumber(), tx.blockHash());
211210
//then
212211
assertThat(txs).isNotNull().hasSize(1);
213212

214-
BlockTx blockTx = txs.getContent().getFirst();
213+
BlockTx blockTx = txs.getFirst();
215214
assertThat(blockTx.getHash()).isEqualTo(tx.txHash());
216215
assertThat(blockTx.getBlockNo()).isEqualTo(tx.blockNumber());
217216
assertThat(blockTx.getBlockHash()).isEqualTo(tx.blockHash());
@@ -236,12 +235,12 @@ void findTransactionsByBlock_Test_stake_pool_tx() {
236235
//given
237236
TransactionBlockDetails tx = generatedDataMap.get(STAKE_KEY_REGISTRATION_TRANSACTION.getName());
238237
//when
239-
Slice<BlockTx> txs =
238+
List<BlockTx> txs =
240239
ledgerBlockService.findTransactionsByBlock(tx.blockNumber(), tx.blockHash());
241240
//then
242241
assertThat(txs).isNotNull().hasSize(1);
243242

244-
BlockTx blockTx = txs.getContent().getFirst();
243+
BlockTx blockTx = txs.getFirst();
245244
assertThat(blockTx.getHash()).isEqualTo(tx.txHash());
246245
assertThat(blockTx.getBlockNo()).isEqualTo(tx.blockNumber());
247246
assertThat(blockTx.getBlockHash()).isEqualTo(tx.blockHash());

api/src/test/java/org/cardanofoundation/rosetta/api/search/controller/SearchControllerTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.mockito.InjectMocks;
1010
import org.mockito.Mock;
1111
import org.mockito.Mockito;
12+
import org.openapitools.client.model.AccountIdentifier;
1213
import org.openapitools.client.model.NetworkIdentifier;
1314
import org.openapitools.client.model.SearchTransactionsRequest;
1415

@@ -20,21 +21,23 @@
2021
import org.cardanofoundation.rosetta.common.exception.ExceptionFactory;
2122
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
2223

24+
import static org.cardanofoundation.rosetta.testgenerator.common.TestConstants.TEST_ACCOUNT_ADDRESS;
2325
import static org.junit.jupiter.api.Assertions.assertThrows;
24-
import static org.mockito.ArgumentMatchers.any;
26+
import static org.mockito.ArgumentMatchers.*;
2527
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2628
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2729
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2830

2931
class SearchControllerTest extends BaseSpringMvcSetup {
3032

3133
@Mock
32-
SearchService service;
34+
private SearchService service;
35+
3336
@Mock
34-
NetworkService networkService;
37+
private NetworkService networkService;
3538

3639
@InjectMocks
37-
SearchApiImpl searchApi;
40+
private SearchApiImpl searchApi;
3841

3942
@Test
4043
void searchOfflineModeTest() throws NoSuchFieldException, IllegalAccessException {
@@ -65,7 +68,6 @@ void searchControllerOK() {
6568
.network(TestConstants.TEST_NETWORK)
6669
.build())
6770
.build();
68-
Mockito.when(service.searchTransaction(any(), any(), any())).thenReturn(Page.empty());
6971

7072
mockMvc.perform(post("/search/transactions")
7173
.contentType(MediaType.APPLICATION_JSON)
@@ -74,4 +76,26 @@ void searchControllerOK() {
7476
.andExpect(jsonPath("$.total_count").value(10))
7577
.andExpect(jsonPath("$.next_offset").value(10));
7678
}
79+
80+
@SneakyThrows
81+
@Test
82+
void searchControllerOnLastPage() {
83+
SearchTransactionsRequest request = SearchTransactionsRequest.builder()
84+
.networkIdentifier(NetworkIdentifier.builder()
85+
.blockchain(TestConstants.TEST_BLOCKCHAIN)
86+
.network(TestConstants.TEST_NETWORK)
87+
.build())
88+
.accountIdentifier(AccountIdentifier.builder()
89+
.address(TEST_ACCOUNT_ADDRESS)
90+
.build())
91+
.build();
92+
93+
mockMvc.perform(post("/search/transactions")
94+
.contentType(MediaType.APPLICATION_JSON)
95+
.content(objectMapper.writeValueAsString(request)))
96+
.andExpect(status().isOk())
97+
.andExpect(jsonPath("$.total_count").value(4))
98+
.andExpect(jsonPath("$.next_offset").doesNotExist());
99+
}
100+
77101
}

0 commit comments

Comments
 (0)