Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Petar Tonev <petar.tonev@limechain.tech>
  • Loading branch information
petreze committed Dec 5, 2024
1 parent f03d8db commit b8d0dd7
Showing 1 changed file with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.hedera.hapi.node.base.ResponseCodeEnum;
import com.hedera.hapi.node.base.TokenTransferList;
import com.hedera.hapi.node.base.TransferList;
import com.hedera.hapi.node.state.token.TokenRelation;
import com.hedera.hapi.node.token.CryptoTransferTransactionBody;
import com.hedera.node.app.service.token.impl.handlers.transfer.AssociateTokenRecipientsStep;
import com.hedera.node.app.service.token.impl.handlers.transfer.TransferContextImpl;
Expand Down Expand Up @@ -65,8 +66,10 @@ public class AssociateTokenRecipientsStepTest extends StepsBase {

private AssociateTokenRecipientsStep subject;
private AssociateTokenRecipientsStep subjectWithApproval;
private AssociateTokenRecipientsStep subjectNFTWithApproval;
private CryptoTransferTransactionBody txn;
private CryptoTransferTransactionBody txnWithApproval;
private CryptoTransferTransactionBody txnNFTWithApproval;
private TransferContextImpl transferContext;

@BeforeEach
Expand All @@ -77,6 +80,7 @@ public void setUp() {
givenStoresAndConfig(handleContext);
subject = new AssociateTokenRecipientsStep(txn);
subjectWithApproval = new AssociateTokenRecipientsStep(txnWithApproval);
subjectNFTWithApproval = new AssociateTokenRecipientsStep(txnNFTWithApproval);
transferContext = new TransferContextImpl(handleContext);
writableTokenStore.put(givenValidFungibleToken(ownerId, false, false, false, false, false));
writableTokenStore.put(givenValidNonFungibleToken(false));
Expand Down Expand Up @@ -123,6 +127,45 @@ void validateFungibleAllowancesAndOtherPrivateChecks() {
.isInstanceOf(HandleException.class);
}

@Test
void validateNFTAllowancesAndOtherPrivateChecks() {
assertThat(writableTokenRelStore.get(ownerId, fungibleTokenId)).isNotNull();
assertThat(writableTokenRelStore.get(ownerId, nonFungibleTokenId)).isNotNull();
assertThat(writableTokenRelStore.get(spenderId, fungibleTokenId)).isNull();
assertThat(writableTokenRelStore.get(spenderId, nonFungibleTokenId)).isNull();

final var modifiedConfiguration = HederaTestConfigBuilder.create()
.withValue("entities.unlimitedAutoAssociationsEnabled", true)
.getOrCreateConfig();
given(handleContext.configuration()).willReturn(modifiedConfiguration);
given(handleContext.savepointStack()).willReturn(stack);
given(stack.getBaseBuilder(any())).willReturn(builder);
given(builder.isUserDispatch()).willReturn(true);

AssertionsForClassTypes.assertThatThrownBy(() -> subjectNFTWithApproval.doIn(transferContext))
.isInstanceOf(HandleException.class);
}

@Test
void validateNFTAllowancesAndForceToThrow() {
assertThat(writableTokenRelStore.get(ownerId, fungibleTokenId)).isNotNull();
assertThat(writableTokenRelStore.get(ownerId, nonFungibleTokenId)).isNotNull();
assertThat(writableTokenRelStore.get(spenderId, fungibleTokenId)).isNull();
assertThat(writableTokenRelStore.get(spenderId, nonFungibleTokenId)).isNull();
writableTokenRelStore.remove(TokenRelation.newBuilder()
.accountId(ownerId)
.tokenId(nonFungibleTokenId)
.build());
final var modifiedConfiguration = HederaTestConfigBuilder.create()
.withValue("entities.unlimitedAutoAssociationsEnabled", true)
.getOrCreateConfig();
given(handleContext.configuration()).willReturn(modifiedConfiguration);
given(handleContext.savepointStack()).willReturn(stack);

AssertionsForClassTypes.assertThatThrownBy(() -> subjectNFTWithApproval.doIn(transferContext))
.isInstanceOf(HandleException.class);
}

void givenValidTxn() {
txn = CryptoTransferTransactionBody.newBuilder()
.transfers(TransferList.newBuilder()
Expand All @@ -145,16 +188,20 @@ void givenValidTxn() {
.accountAmounts(adjustFrom(ownerId, -1_000, true))
.accountAmounts(adjustFrom(spenderId, -1_000, true))
.build())
.tokenTransfers(
TokenTransferList.newBuilder()
.token(fungibleTokenId)
.transfers(
List.of(adjustFrom(ownerId, -1_000, true), adjustFrom(spenderId, -1_000, true)))
.build(),
TokenTransferList.newBuilder()
.token(nonFungibleTokenId)
.nftTransfers(nftTransferWith(ownerId, spenderId, 1, true))
.build())
.tokenTransfers(TokenTransferList.newBuilder()
.token(fungibleTokenId)
.transfers(List.of(adjustFrom(ownerId, -1_000, true), adjustFrom(spenderId, -1_000, true)))
.build())
.build();
txnNFTWithApproval = CryptoTransferTransactionBody.newBuilder()
.transfers(TransferList.newBuilder()
.accountAmounts(adjustFrom(ownerId, -1_000, true))
.accountAmounts(adjustFrom(spenderId, -1_000, true))
.build())
.tokenTransfers(TokenTransferList.newBuilder()
.token(nonFungibleTokenId)
.nftTransfers(nftTransferWith(ownerId, spenderId, 1, true))
.build())
.build();
given(handleContext.configuration()).willReturn(configuration);
given(handleContext.expiryValidator()).willReturn(expiryValidator);
Expand Down

0 comments on commit b8d0dd7

Please sign in to comment.