Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor web3 tests code #10293

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
public static final long DEFAULT_ACCOUNT_BALANCE = 100_000_000_000_000_000L;
bilyana-gospodinova marked this conversation as resolved.
Show resolved Hide resolved

@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
Loading