Skip to content

Commit

Permalink
Refactor web3 tests code (#10293)
Browse files Browse the repository at this point in the history
Removed similar code from several tests and put it in parent class so that if a change is needed it should be done in one place only. This is the first PR, more might be needed if i find more code that needs to be extracted in a similar way.

---------

Signed-off-by: filev94 <[email protected]>
  • Loading branch information
filev94 authored Feb 6, 2025
1 parent b94461e commit 51aaec4
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.common.collect.Range;
import com.google.protobuf.ByteString;
import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.balance.TokenBalance;
import com.hedera.mirror.common.domain.entity.Entity;
Expand Down Expand Up @@ -73,6 +74,7 @@
public abstract class AbstractContractCallServiceTest extends Web3IntegrationTest {

protected static final String TREASURY_ADDRESS = EvmTokenUtils.toAddress(2).toHexString();
protected static final long DEFAULT_ACCOUNT_BALANCE = 100_000_000_000_000_000L;

@Resource
protected TestWeb3jService testWeb3jService;
Expand Down Expand Up @@ -238,6 +240,53 @@ protected Entity tokenEntityPersist() {
return domainBuilder.entity().customize(e -> e.type(EntityType.TOKEN)).persist();
}

/**
*
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersist() {
return fungibleTokenCustomizable(t -> {});
}

/**
*
* @param treasuryEntityId - the treasuryEntityId that has to be set in the token
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersistWithTreasuryAccount(final EntityId treasuryEntityId) {
return fungibleTokenCustomizable(t -> t.treasuryAccountId(treasuryEntityId));
}

/**
*
* @param treasuryEntity - the treasuryEntity which has to be set in the token
* @param kycKey - the kycKey that has to be set in the token
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersistWithTreasuryAccountAndKYCKey(
final EntityId treasuryEntity, final byte[] kycKey) {
return fungibleTokenCustomizable(
t -> t.treasuryAccountId(treasuryEntity).kycKey(kycKey));
}

/**
* Method used to customize different fields of a token and persist it in db
* @param customizer - the consumer used to customize the token
* @return Token object which is persisted in the db
*/
protected Token fungibleTokenCustomizable(Consumer<Token.TokenBuilder<?, ?>> customizer) {
final var tokenEntity =
domainBuilder.entity().customize(e -> e.type(EntityType.TOKEN)).persist();

return domainBuilder
.token()
.customize(t -> {
t.tokenId(tokenEntity.getId()).type(TokenTypeEnum.FUNGIBLE_COMMON);
customizer.accept(t); // Apply any customizations provided
})
.persist();
}

/**
* Persists fungible token in the token db table.
*
Expand Down Expand Up @@ -318,19 +367,47 @@ protected NftAllowance nftAllowancePersist(Token token, Entity owner, Entity spe
.persist();
}

/**
* Creates an account with evmAddress and alias set to null and persist to db
* @return Entity object that is persisted in the db
*/
protected Entity accountEntityPersist() {
return domainBuilder
.entity()
.customize(e ->
e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(100_000_000_000_000_000L))
.persist();
return accountEntityPersistCustomizable(
e -> e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(DEFAULT_ACCOUNT_BALANCE));
}

protected Entity accountEntityWithEvmAddressPersist() {
return domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT).balance(1_000_000_000_000_000L))
.persist();
return accountEntityPersistCustomizable(e -> e.type(EntityType.ACCOUNT).balance(DEFAULT_ACCOUNT_BALANCE));
}

/**
*
* @param alias - the alias with which the account is created
* @param publicKey - the public key with which the account is created
* @return Entity object that is persisted in the db
*/
protected Entity accountPersistWithAlias(final Address alias, final ByteString publicKey) {
return accountEntityPersistCustomizable(
e -> e.evmAddress(alias.toArray()).alias(publicKey.toByteArray()));
}

/**
*
* @param balance - the balance with which the account is created
* @return Entity object that is persisted in the db
*/
protected Entity accountEntityPersistWithBalance(final long balance) {
return accountEntityPersistCustomizable(
e -> e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(balance));
}

/**
*
* @param customizer - the consumer with which to customize the entity
* @return
*/
protected Entity accountEntityPersistCustomizable(Consumer<Entity.EntityBuilder<?, ?>> customizer) {
return domainBuilder.entity().customize(customizer).persist();
}

/**
Expand Down
Loading

0 comments on commit 51aaec4

Please sign in to comment.