-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2256 from rsksmart/fix-snapshot-unit-tests
Fix snapshot sonarcloud and unit tests
- Loading branch information
Showing
19 changed files
with
774 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
rskj-core/src/test/java/co/rsk/net/messages/SnapBlocksRequestMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package co.rsk.net.messages; | ||
|
||
import co.rsk.blockchain.utils.BlockGenerator; | ||
import org.ethereum.core.Block; | ||
import org.ethereum.util.RLP; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
class SnapBlocksRequestMessageTest { | ||
Check notice Code scanning / CodeQL Unused classes and interfaces Note test
Unused class: SnapBlocksRequestMessageTest is not referenced within this codebase. If not used as an external API it should be removed.
|
||
|
||
private final Block block4Test = new BlockGenerator().getBlock(1); | ||
private final SnapBlocksRequestMessage underTest = new SnapBlocksRequestMessage(block4Test.getNumber()); | ||
|
||
|
||
@Test | ||
void getMessageType_returnCorrectMessageType() { | ||
//given-when | ||
MessageType messageType = underTest.getMessageType(); | ||
|
||
//then | ||
assertEquals(MessageType.SNAP_BLOCKS_REQUEST_MESSAGE, messageType); | ||
} | ||
|
||
@Test | ||
void getEncodedMessage_returnExpectedByteArray() { | ||
//given default block 4 test | ||
|
||
//when | ||
byte[] encodedMessage = underTest.getEncodedMessage(); | ||
|
||
//then | ||
assertThat(encodedMessage, equalTo(RLP.encodeList(RLP.encodeBigInteger(BigInteger.ONE)))); | ||
} | ||
|
||
@Test | ||
void getBlockNumber_returnTheExpectedValue() { | ||
//given default block 4 test | ||
|
||
//when | ||
long blockNumber = underTest.getBlockNumber(); | ||
|
||
//then | ||
assertThat(blockNumber, equalTo(block4Test.getNumber())); | ||
} | ||
|
||
@Test | ||
void givenAcceptIsCalled_messageVisitorIsAppliedFormessage() { | ||
//given | ||
Block block = new BlockGenerator().getBlock(1); | ||
SnapBlocksRequestMessage message = new SnapBlocksRequestMessage(block.getNumber()); | ||
MessageVisitor visitor = mock(MessageVisitor.class); | ||
|
||
//when | ||
message.accept(visitor); | ||
|
||
//then | ||
verify(visitor, times(1)).apply(message); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
rskj-core/src/test/java/co/rsk/net/messages/SnapBlocksResponseMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package co.rsk.net.messages; | ||
|
||
import co.rsk.blockchain.utils.BlockGenerator; | ||
import co.rsk.config.TestSystemProperties; | ||
import co.rsk.core.BlockDifficulty; | ||
import co.rsk.db.HashMapBlocksIndex; | ||
import org.ethereum.core.Block; | ||
import org.ethereum.core.BlockFactory; | ||
import org.ethereum.datasource.HashMapDB; | ||
import org.ethereum.db.BlockStore; | ||
import org.ethereum.db.IndexedBlockStore; | ||
import org.ethereum.util.RLP; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
class SnapBlocksResponseMessageTest { | ||
Check notice Code scanning / CodeQL Unused classes and interfaces Note test
Unused class: SnapBlocksResponseMessageTest is not referenced within this codebase. If not used as an external API it should be removed.
|
||
|
||
private final TestSystemProperties config = new TestSystemProperties(); | ||
private final BlockFactory blockFactory = new BlockFactory(config.getActivationConfig()); | ||
private final BlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex()); | ||
private final Block block4Test = new BlockGenerator().getBlock(1); | ||
private final List<Block> blockList = Collections.singletonList(new BlockGenerator().getBlock(1)); | ||
private final List<BlockDifficulty> blockDifficulties = Collections.singletonList(indexedBlockStore.getTotalDifficultyForHash(block4Test.getHash().getBytes())); | ||
private final SnapBlocksResponseMessage underTest = new SnapBlocksResponseMessage(blockList, blockDifficulties); | ||
|
||
|
||
@Test | ||
void getMessageType_returnCorrectMessageType() { | ||
//given-when | ||
MessageType messageType = underTest.getMessageType(); | ||
|
||
//then | ||
assertEquals(MessageType.SNAP_BLOCKS_RESPONSE_MESSAGE, messageType); | ||
} | ||
|
||
@Test | ||
void getEncodedMessage_returnExpectedByteArray() { | ||
//given default block 4 test | ||
byte[] expectedEncodedMessage = RLP.encodeList( | ||
RLP.encodeList(RLP.encode(block4Test.getEncoded())), | ||
RLP.encodeList(RLP.encode(blockDifficulties.get(0).getBytes()))); | ||
//when | ||
byte[] encodedMessage = underTest.getEncodedMessage(); | ||
|
||
//then | ||
assertThat(encodedMessage, equalTo(expectedEncodedMessage)); | ||
} | ||
|
||
@Test | ||
void getDifficulties_returnTheExpectedValue() { | ||
//given default block 4 test | ||
|
||
//when | ||
List<BlockDifficulty> difficultiesReturned = underTest.getDifficulties(); | ||
//then | ||
assertThat(difficultiesReturned, equalTo(blockDifficulties)); | ||
} | ||
|
||
@Test | ||
void getBlocks_returnTheExpectedValue() { | ||
//given default block 4 test | ||
|
||
//when | ||
List<Block> blocksReturned = underTest.getBlocks(); | ||
//then | ||
assertThat(blocksReturned, equalTo(blockList)); | ||
} | ||
|
||
@Test | ||
void givenAcceptIsCalled_messageVisitorIsAppliedForMessage() { | ||
//given | ||
SnapBlocksResponseMessage message = new SnapBlocksResponseMessage(blockList, blockDifficulties); | ||
MessageVisitor visitor = mock(MessageVisitor.class); | ||
|
||
//when | ||
message.accept(visitor); | ||
|
||
//then | ||
verify(visitor, times(1)).apply(message); | ||
} | ||
|
||
} |
Oops, something went wrong.