Skip to content

Commit

Permalink
Merge pull request #2271 from rsksmart/fixing-sonar-issues-and-adding…
Browse files Browse the repository at this point in the history
…-unit-tests

Fixing sonar issue and adding unit test
  • Loading branch information
Vovchyk authored Mar 20, 2024
2 parents ab921c1 + c0ea9ee commit 2c4ac29
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class TrieDTOInOrderRecoverer {

private static final Logger logger = LoggerFactory.getLogger(TrieDTOInOrderRecoverer.class);

private TrieDTOInOrderRecoverer() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
Expand Down Expand Up @@ -102,10 +103,6 @@ private static TrieDTO fromTrieDTO(TrieDTO result,
return result;
}

private static Keccak256 getHash(byte[] recoveredBytes) {
return new Keccak256(Keccak256Helper.keccak256(recoveredBytes));
}

private static long getValue(TrieDTO trieCollection) {
return trieCollection.getChildrenSize().value;
}
Expand Down
3 changes: 0 additions & 3 deletions rskj-core/src/test/java/co/rsk/trie/TrieDTOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@
import org.ethereum.datasource.HashMapDB;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.bouncycastle.util.encoders.Hex.decode;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class TrieDTOTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: TrieDTOTest is not referenced within this codebase. If not used as an external API it should be removed.


Expand Down
54 changes: 52 additions & 2 deletions rskj-core/src/test/java/co/rsk/trie/TrieStoreImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;

/**
* Created by ajlopez on 08/01/2017.
Expand Down Expand Up @@ -188,7 +191,7 @@ void saveFullTrieUpdateAndSaveAgain() {

@Test
void retrieveTrieNotFound() {
Assertions.assertFalse(store.retrieve(new byte[] { 0x01, 0x02, 0x03, 0x04 }).isPresent());
Assertions.assertFalse(store.retrieve(new byte[]{0x01, 0x02, 0x03, 0x04}).isPresent());
}

@Test
Expand Down Expand Up @@ -239,4 +242,51 @@ void retrieveTrieWithLongValuesByHash() {

verify(map, times(1)).get(any());
}

@Test
void saveAndRetrieveTrieDTO() {
Trie trie = new Trie(store).put("foo", "bar".getBytes());

TrieDTO dto = TrieDTO.decodeFromMessage(trie.toMessage(), store);
store.saveDTO(dto);

verify(map, times(trie.trieSize())).put(any(), any());
verifyNoMoreInteractions(map);

Optional<TrieDTO> optStoredDto = store.retrieveDTO(trie.getHash().getBytes());
assertTrue(optStoredDto.isPresent());

TrieDTO storedDto = optStoredDto.get();
assertArrayEquals("bar".getBytes(), storedDto.getValue());
}

@Test
void saveAndRetrieveTrieDTOLongValue() {
byte[] longValue = TrieValueTest.makeValue(100);
Trie trie = new Trie(store).put("foo", longValue);
store.save(trie);
TrieDTO dto = TrieDTO.decodeFromMessage(trie.toMessage(), store);
store.saveDTO(dto);

verify(map, times(4)).put(any(), any());

Optional<TrieDTO> optStoredDto = store.retrieveDTO(trie.getHash().getBytes());
assertTrue(optStoredDto.isPresent());

TrieDTO storedDto = optStoredDto.get();
assertArrayEquals(longValue, storedDto.getValue());
}

@Test
void saveComposedTrieDtoWithLongValues() {
Trie trie = new Trie(store)
.put("foo", TrieValueTest.makeValue(100))
.put("bar", TrieValueTest.makeValue(200));
store.save(trie);
verify(map, times(trie.trieSize())).put(any(), any());

TrieDTO dto = TrieDTO.decodeFromMessage(trie.toMessage(), store, true, null);
store.saveDTO(dto);
verify(map, times(6)).put(any(), any());
}
}

0 comments on commit 2c4ac29

Please sign in to comment.