Skip to content

Commit

Permalink
Rename variables in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-iov committed Aug 28, 2024
1 parent 6f2efc8 commit 62961df
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 153 deletions.
40 changes: 25 additions & 15 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ void setUpOnEachTest() {
signatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());

Repository repository = createRepository();
bridgeStorageProvider = new BridgeStorageProvider(repository, contractAddress, btcMainnetParams, activationsAfterForks);
bridgeStorageProvider = new BridgeStorageProvider(
repository,
contractAddress,
btcMainnetParams,
activationsAfterForks
);
bridgeSupportBuilder = new BridgeSupportBuilder();

StorageAccessor bridgeStorageAccessor = new InMemoryStorage();
Expand All @@ -155,14 +160,23 @@ void setUpOnEachTest() {
.withFederationStorageProvider(federationStorageProvider)
.withActivations(activationsAfterForks)
.build();
whitelistSupport = new WhitelistSupportImpl(WhitelistMainNetConstants.getInstance(), whitelistStorageProvider, mock(ActivationConfig.ForBlock.class), signatureCache);
lockingCapSupport = new LockingCapSupportImpl(lockingCapStorageProvider, mock(ActivationConfig.ForBlock.class), LockingCapMainNetConstants.getInstance(), signatureCache);
whitelistSupport = new WhitelistSupportImpl(
WhitelistMainNetConstants.getInstance(),
whitelistStorageProvider,
activationsAfterForks,
signatureCache
);
lockingCapSupport = new LockingCapSupportImpl(
lockingCapStorageProvider,
activationsAfterForks,
LockingCapMainNetConstants.getInstance(),
signatureCache
);
}

@Test
void getFeePerKb() {
Coin feePerKb = Coin.valueOf(10_000L);
FeePerKbSupport feePerKbSupport = mock(FeePerKbSupport.class);
when(feePerKbSupport.getFeePerKb()).thenReturn(feePerKb);

BridgeSupport bridgeSupport = bridgeSupportBuilder
Expand All @@ -176,7 +190,6 @@ void getFeePerKb() {

@Test
void voteFeePerKbChange_success() {
FeePerKbSupport feePerKbSupport = mock(FeePerKbSupport.class);
when(feePerKbSupport.voteFeePerKbChange(any(), any(), any())).thenReturn(1);

BridgeSupport bridgeSupport = bridgeSupportBuilder
Expand Down Expand Up @@ -274,9 +287,6 @@ void setLockWhitelistDisableBlockDelay() throws BlockStoreException, IOException
BridgeConstants bridgeConstantsMainNet = BridgeMainNetConstants.getInstance();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
FederationSupport federationSupport = federationSupportBuilder
.withFederationConstants(bridgeConstantsMainNet.getFederationConstants())
.build();

bridgeSupport = bridgeSupportBuilder
.withWhitelistSupport(whitelistSupport)
Expand Down Expand Up @@ -529,7 +539,7 @@ void getPendingFederatorPublicKeyOfType() {
}

@Test
void voteFederationChange() throws BridgeIllegalArgumentException {
void voteFederationChange() {
Transaction tx = mock(Transaction.class);
ABICallSpec callSpec = mock(ABICallSpec.class);
int result = 1;
Expand Down Expand Up @@ -936,12 +946,12 @@ void isBtcTxHashAlreadyProcessed() throws IOException {
Sha256Hash hash1 = Sha256Hash.ZERO_HASH;
Sha256Hash hash2 = Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000001");

BridgeStorageProvider bridgeStorageProvider = mock(BridgeStorageProvider.class);
when(bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L));
BridgeStorageProvider bridgeStorageProviderMock = mock(BridgeStorageProvider.class);
when(bridgeStorageProviderMock.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L));

BridgeSupport bridgeSupport = bridgeSupportBuilder
.withBridgeConstants(bridgeConstants)
.withProvider(bridgeStorageProvider)
.withProvider(bridgeStorageProviderMock)
.withActivations(activations)
.build();

Expand All @@ -957,12 +967,12 @@ void getBtcTxHashProcessedHeight() throws IOException {
Sha256Hash hash1 = Sha256Hash.ZERO_HASH;
Sha256Hash hash2 = Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000001");

BridgeStorageProvider bridgeStorageProvider = mock(BridgeStorageProvider.class);
when(bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L));
BridgeStorageProvider bridgeStorageProviderMock = mock(BridgeStorageProvider.class);
when(bridgeStorageProviderMock.getHeightIfBtcTxhashIsAlreadyProcessed(hash1)).thenReturn(Optional.of(1L));

BridgeSupport bridgeSupport = bridgeSupportBuilder
.withBridgeConstants(bridgeConstants)
.withProvider(bridgeStorageProvider)
.withProvider(bridgeStorageProviderMock)
.withActivations(activations)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import co.rsk.bitcoinj.core.UTXO;

class FederationStorageProviderImplTests {

private static final int STANDARD_MULTISIG_FEDERATION_FORMAT_VERSION = STANDARD_MULTISIG_FEDERATION.getFormatVersion();
private static final int NON_STANDARD_ERP_FEDERATION_FORMAT_VERSION = NON_STANDARD_ERP_FEDERATION.getFormatVersion();
private static final int P2SH_ERP_FEDERATION_FORMAT_VERSION = P2SH_ERP_FEDERATION.getFormatVersion();
Expand All @@ -56,11 +55,12 @@ class FederationStorageProviderImplTests {

private static final BridgeConstants bridgeConstantsRegtest = new BridgeRegTestConstants();
private static final NetworkParameters regtestBtcParams = bridgeConstantsRegtest.getBtcParams();
private static ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
private static final BridgeConstants bridgeConstants = BridgeMainNetConstants.getInstance();
private static final FederationConstants federationConstants = bridgeConstants.getFederationConstants();
private static final NetworkParameters networkParameters = federationConstants.getBtcParams();

private static ActivationConfig.ForBlock activations;

private static Stream<Arguments> provideFederationAndFormatArguments() {
return Stream.of(
Arguments.of(P2SH_ERP_FEDERATION_FORMAT_VERSION, new P2shErpFederationBuilder().build()),
Expand All @@ -76,6 +76,11 @@ private static Stream<Arguments> provideFederationAndFormatArguments() {
);
}

@BeforeEach
void setup() {
activations = mock(ActivationConfig.ForBlock.class);
}

@ParameterizedTest
@MethodSource("provideFederationAndFormatArguments")
void testGetNewFederation(
Expand Down Expand Up @@ -158,8 +163,6 @@ void testGetPendingFederation(
PendingFederation expectedFederation
) {
// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);

when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(true);

StorageAccessor storageAccessor = new InMemoryStorage();
Expand All @@ -184,14 +187,10 @@ void testGetPendingFederation(
}

@Test
void getPendingFederation_whenStorageVersionIsNotAvailable_deserializeFromBtcKeysOnly(
) {

void getPendingFederation_whenStorageVersionIsNotAvailable_deserializeFromBtcKeysOnly() {
PendingFederation expectedFederation = new PendingFederationBuilder().build();

// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);

when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false);

StorageAccessor storageAccessor = new InMemoryStorage();
Expand Down Expand Up @@ -248,8 +247,6 @@ void getPendingFederation_previouslySetToNull_returnsNull() {
@Test
void getPendingFederation_nullPendingFederationInStorage_returnsNull() {
// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);

when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false);

StorageAccessor storageAccessor = new InMemoryStorage();
Expand Down Expand Up @@ -289,7 +286,6 @@ private static Stream<Arguments> provideSaveFederationTestArguments() {
@Test
void saveNewFederation_before_RSKIP123_should_not_save_null() {
Repository repository = mock(Repository.class);
activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP123)).thenReturn(false);

// Act
Expand Down Expand Up @@ -454,7 +450,7 @@ void getNewFederationBtcUTXOs(

@Test
void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() {
ActivationConfig.ForBlock activations = ActivationConfigsForTest.only(ConsensusRule.RSKIP123).forBlock(0);
ActivationConfig.ForBlock wasabiActivations = ActivationConfigsForTest.wasabi100().forBlock(0);
StorageAccessor storageAccessor = new InMemoryStorage();
FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor);

Expand All @@ -467,7 +463,7 @@ void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() {
storageAccessor.saveToRepository(newFederationBtcUtxosKey, expectedUtxos, BridgeSerializationUtils::serializeUTXOList);

// Get utxos from method and check they are as expected
List<UTXO> actualUtxos = federationStorageProvider.getNewFederationBtcUTXOs(networkParameters, activations);
List<UTXO> actualUtxos = federationStorageProvider.getNewFederationBtcUTXOs(networkParameters, wasabiActivations);
assertEquals(1, actualUtxos.size());
assertEquals(expectedUtxos, actualUtxos);

Expand All @@ -476,7 +472,7 @@ void getNewFederationBtcUTXOs_calledTwice_returnCachedUtxos() {
storageAccessor.saveToRepository(newFederationBtcUtxosKey, extraUtxos, BridgeSerializationUtils::serializeUTXOList);

// Get utxos from method and check they are still the same as the original expected utxos since it is returning the cached utxos
List<UTXO> actualUtxosAfterSecondGet = federationStorageProvider.getNewFederationBtcUTXOs(null, activations);
List<UTXO> actualUtxosAfterSecondGet = federationStorageProvider.getNewFederationBtcUTXOs(null, wasabiActivations);
assertEquals(1, actualUtxosAfterSecondGet.size());
assertEquals(expectedUtxos, actualUtxosAfterSecondGet);

Expand Down Expand Up @@ -616,7 +612,7 @@ void getFederationElection_whenElectionIsNotInStorage_shouldReturnDefault() {
@Test
void getNextFederationCreationBlockHeight_preIris300_storageIsNotAccessedAndReturnsEmpty() {
// Arrange
ActivationConfig.ForBlock activations = ActivationConfigsForTest.papyrus200().forBlock(0L);
ActivationConfig.ForBlock papyrusActivations = ActivationConfigsForTest.papyrus200().forBlock(0L);

StorageAccessor storageAccessor = new InMemoryStorage();
// Putting some value in the storage just to then assert that before fork, the storage won't be accessed.
Expand All @@ -629,13 +625,13 @@ void getNextFederationCreationBlockHeight_preIris300_storageIsNotAccessedAndRetu
FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor);

// Assert
assertEquals(Optional.empty(), federationStorageProvider.getNextFederationCreationBlockHeight(activations));
assertEquals(Optional.empty(), federationStorageProvider.getNextFederationCreationBlockHeight(papyrusActivations));
}

@Test
void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() {
// Arrange
ActivationConfig.ForBlock activations = ActivationConfigsForTest.iris300().forBlock(0L);
ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0L);

StorageAccessor storageAccessor = new InMemoryStorage();
long expectedValue = 1_000_000L;
Expand All @@ -644,7 +640,7 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() {

// Act
FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor);
Optional<Long> actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations);
Optional<Long> actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations);

// Assert
assertTrue(actualValue.isPresent());
Expand All @@ -653,7 +649,7 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() {
// Setting in storage a different value to assert that calling the method again should return cached value
storageAccessor.saveToRepository(NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY.getKey(), RLP.encodeBigInteger(BigInteger.valueOf(2_000_000L)));

Optional<Long> actualCachedValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations);
Optional<Long> actualCachedValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations);

assertTrue(actualCachedValue.isPresent());
assertEquals(expectedValue, actualCachedValue.get());
Expand All @@ -662,14 +658,14 @@ void getNextFederationCreationBlockHeight_postIris300_getsValueFromStorage() {
@Test
void getNextFederationCreationBlockHeight_postIris300AndNoValueInStorage_returnsEmpty() {
// Arrange
ActivationConfig.ForBlock activations = ActivationConfigsForTest.iris300().forBlock(0L);
ActivationConfig.ForBlock irisActivations = ActivationConfigsForTest.iris300().forBlock(0L);

StorageAccessor storageAccessor = new InMemoryStorage();
storageAccessor.saveToRepository(NEXT_FEDERATION_CREATION_BLOCK_HEIGHT_KEY.getKey(), null);

// Act
FederationStorageProvider federationStorageProvider = new FederationStorageProviderImpl(storageAccessor);
Optional<Long> actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(activations);
Optional<Long> actualValue = federationStorageProvider.getNextFederationCreationBlockHeight(irisActivations);

// Assert
assertFalse(actualValue.isPresent());
Expand Down
Loading

0 comments on commit 62961df

Please sign in to comment.