From c0ea9eeb2983a26b6cf8b9539f52fc59baf61c16 Mon Sep 17 00:00:00 2001 From: Angel Soto Date: Wed, 20 Mar 2024 12:42:46 +0100 Subject: [PATCH] Fixing sonar issue and adding unit test --- .../co/rsk/trie/TrieDTOInOrderRecoverer.java | 5 +- .../test/java/co/rsk/trie/TrieDTOTest.java | 3 -- .../java/co/rsk/trie/TrieStoreImplTest.java | 54 ++++++++++++++++++- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/trie/TrieDTOInOrderRecoverer.java b/rskj-core/src/main/java/co/rsk/trie/TrieDTOInOrderRecoverer.java index 7446441f96b..32fd83e52c6 100644 --- a/rskj-core/src/main/java/co/rsk/trie/TrieDTOInOrderRecoverer.java +++ b/rskj-core/src/main/java/co/rsk/trie/TrieDTOInOrderRecoverer.java @@ -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"); } @@ -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; } diff --git a/rskj-core/src/test/java/co/rsk/trie/TrieDTOTest.java b/rskj-core/src/test/java/co/rsk/trie/TrieDTOTest.java index d8969693488..1e51a69b6f9 100644 --- a/rskj-core/src/test/java/co/rsk/trie/TrieDTOTest.java +++ b/rskj-core/src/test/java/co/rsk/trie/TrieDTOTest.java @@ -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 { diff --git a/rskj-core/src/test/java/co/rsk/trie/TrieStoreImplTest.java b/rskj-core/src/test/java/co/rsk/trie/TrieStoreImplTest.java index 7ce8ae62879..4faf9b264f0 100644 --- a/rskj-core/src/test/java/co/rsk/trie/TrieStoreImplTest.java +++ b/rskj-core/src/test/java/co/rsk/trie/TrieStoreImplTest.java @@ -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. @@ -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 @@ -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 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 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()); + } }