Skip to content

Commit

Permalink
chore: Add code coverage to CustomRoyaltyFeeAssessor, `CustomFixedF…
Browse files Browse the repository at this point in the history
…eeAssessor` (#13224)

Signed-off-by: Neeharika-Sompalli <neeharika.sompalli@swirldslabs.com>
  • Loading branch information
Neeharika-Sompalli committed May 14, 2024
1 parent ea0b875 commit 40b6658
Show file tree
Hide file tree
Showing 5 changed files with 539 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void assessRoyaltyFees(
}
}
// We don't want to charge the fallback fee for each nft transfer, if the receiver has already
// paid it for this token
// paid it for this token. This should be added only once per token transfer, so this is
// added here.
if (exchangedValue.isEmpty()) {
// Receiver pays fallback fees
result.addToRoyaltiesPaid(Pair.of(receiver, tokenId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.node.app.service.token.impl.test.handlers.transfer.customfees;

import static com.hedera.node.app.service.token.impl.handlers.BaseCryptoHandler.asAccount;
import static com.hedera.node.app.service.token.impl.handlers.BaseTokenHandler.asToken;
import static com.hedera.node.app.service.token.impl.test.handlers.util.CryptoTokenHandlerTestBase.withFixedFee;
import static com.hedera.node.app.service.token.impl.test.handlers.util.TransferUtil.asNftTransferList;
import static org.assertj.core.api.Assertions.assertThat;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.TokenID;
import com.hedera.hapi.node.base.TokenTransferList;
import com.hedera.hapi.node.base.TokenType;
import com.hedera.hapi.node.transaction.AssessedCustomFee;
import com.hedera.hapi.node.transaction.CustomFee;
import com.hedera.hapi.node.transaction.FixedFee;
import com.hedera.node.app.service.token.impl.handlers.transfer.customfees.AssessmentResult;
import com.hedera.node.app.service.token.impl.handlers.transfer.customfees.CustomFeeMeta;
import com.hedera.node.app.service.token.impl.handlers.transfer.customfees.CustomFixedFeeAssessor;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class CustomFixedFeeAssessorTest {

private CustomFixedFeeAssessor subject;

private AssessmentResult result;

private final AccountID payer = asAccount(4001);
private final AccountID otherCollector = asAccount(1001);
private final AccountID funding = asAccount(98);
private final TokenID firstFungibleTokenId = asToken(3000);
private final AccountID minter = asAccount(6000);
private final TokenID nonFungibleTokenId = asToken(70000);
private final TokenTransferList nftTransferList = asNftTransferList(nonFungibleTokenId, payer, funding, 1);
final FixedFee htsFixedFee = FixedFee.newBuilder()
.denominatingTokenId(firstFungibleTokenId)
.amount(1)
.build();
final FixedFee hbarFixedFee = FixedFee.newBuilder().amount(1).build();
private final AssessedCustomFee htsAssessedFee = AssessedCustomFee.newBuilder()
.amount(1)
.tokenId(firstFungibleTokenId)
.effectivePayerAccountId(payer)
.feeCollectorAccountId(otherCollector)
.build();
private final AssessedCustomFee hbarAssessedFee = AssessedCustomFee.newBuilder()
.amount(1)
.effectivePayerAccountId(payer)
.feeCollectorAccountId(otherCollector)
.build();

@BeforeEach
void setUp() {
subject = new CustomFixedFeeAssessor();
}

@Test
void delegatesToHbarWhenDenomIsNull() {
result = new AssessmentResult(List.of(nftTransferList), List.of());
final var hbarFee = withFixedFee(hbarFixedFee, otherCollector);
final var feeMeta = withCustomFeeMeta(List.of(hbarFee), TokenType.FUNGIBLE_COMMON);

subject.assessFixedFees(feeMeta, payer, result);
assertThat(result.getAssessedCustomFees()).isNotEmpty();
assertThat(result.getAssessedCustomFees()).contains(hbarAssessedFee);
}

@Test
void delegatesToHtsWhenDenomIsNonNull() {
result = new AssessmentResult(List.of(nftTransferList), List.of());
final var hbarFee = withFixedFee(htsFixedFee, otherCollector);
final var feeMeta = withCustomFeeMeta(List.of(hbarFee), TokenType.FUNGIBLE_COMMON);

subject.assessFixedFees(feeMeta, payer, result);
assertThat(result.getAssessedCustomFees()).isNotEmpty();
assertThat(result.getAssessedCustomFees()).contains(htsAssessedFee);
}

@Test
void fixedCustomFeeExemptIsOk() {
result = new AssessmentResult(List.of(nftTransferList), List.of());
final var hbarFee = withFixedFee(htsFixedFee, payer);
final var feeMeta = withCustomFeeMeta(List.of(hbarFee), TokenType.FUNGIBLE_COMMON);

subject.assessFixedFees(feeMeta, payer, result);
assertThat(result.getAssessedCustomFees()).isEmpty();
}

@Test
void exemptsAssessmentWhenSenderSameAsCollector() {
result = new AssessmentResult(List.of(nftTransferList), List.of());
final var hbarFee = withFixedFee(htsFixedFee, payer);
final var feeMeta = withCustomFeeMeta(List.of(hbarFee), TokenType.FUNGIBLE_COMMON);

subject.assessFixedFees(feeMeta, payer, result);
assertThat(result.getAssessedCustomFees()).isEmpty();
}

@Test
void ignoresIfPayerExempt() {
result = new AssessmentResult(List.of(nftTransferList), List.of());
final var hbarFee = withFixedFee(htsFixedFee, payer);
final var feeMeta = withCustomFeeMeta(List.of(hbarFee), TokenType.FUNGIBLE_COMMON);

subject.assessFixedFee(feeMeta, payer, hbarFee, result);
assertThat(result.getAssessedCustomFees()).isEmpty();
}

private CustomFeeMeta withCustomFeeMeta(List<CustomFee> customFees, TokenType tokenType) {
return new CustomFeeMeta(firstFungibleTokenId, minter, customFees, tokenType);
}
}

0 comments on commit 40b6658

Please sign in to comment.