Skip to content

Commit

Permalink
chore: add unit test for UtilPrngHandler (#13254)
Browse files Browse the repository at this point in the history
Signed-off-by: Lev Povolotsky <lev@swirldslabs.com>
  • Loading branch information
povolev15 committed May 14, 2024
1 parent 40b6658 commit b09fc8a
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mock.Strictness.LENIENT;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.hedera.hapi.node.base.SubType;
import com.hedera.hapi.node.transaction.TransactionBody;
Expand All @@ -31,6 +35,8 @@
import com.hedera.node.app.service.util.impl.records.PrngRecordBuilder;
import com.hedera.node.app.spi.fees.FeeAccumulator;
import com.hedera.node.app.spi.fees.FeeCalculator;
import com.hedera.node.app.spi.fees.FeeContext;
import com.hedera.node.app.spi.fees.Fees;
import com.hedera.node.app.spi.fixtures.fees.FakeFeeAccumulator;
import com.hedera.node.app.spi.fixtures.fees.FakeFeeCalculator;
import com.hedera.node.app.spi.records.BlockRecordInfo;
Expand Down Expand Up @@ -137,6 +143,26 @@ void followsHappyPathWithNoRange() {
assertThat(recordBuilder.entropyBytes).isEqualTo(hash);
}

@Test
void calculateFeesHappyPath() {
givenTxnWithoutRange();
final var body = TransactionBody.newBuilder().utilPrng(txn).build();

final var feeCtx = mock(FeeContext.class);
given(feeCtx.body()).willReturn(body);

final var feeCalc = mock(FeeCalculator.class);
given(feeCtx.feeCalculator(notNull())).willReturn(feeCalc);
given(feeCalc.addBytesPerTransaction(anyLong())).willReturn(feeCalc);
// The fees wouldn't be free in this scenario, but we don't care about the actual return
// value here since we're using a mock calculator
given(feeCalc.calculate()).willReturn(Fees.FREE);

subject.calculateFees(feeCtx);

verify(feeCalc).addBytesPerTransaction(0);
}

@ParameterizedTest
@ValueSource(
ints = {
Expand Down Expand Up @@ -301,6 +327,18 @@ void emptyHashFromRunningHashReturnsAllZeros() {
assertThat(recordBuilder.entropyBytes).isEqualTo(Bytes.wrap(new byte[48]));
}

@Test
void verifyModThrowException() {
assertThatThrownBy(() -> UtilPrngHandler.mod(0, 0)).isInstanceOf(ArithmeticException.class);
}

@Test
void verifyModHappyPath() {
assertThatCode(() -> UtilPrngHandler.mod(2, 4)).doesNotThrowAnyException();

assertThat(UtilPrngHandler.mod(2, 4)).isEqualTo(2);
}

private void givenTxnWithRange(int range) {
txn = UtilPrngTransactionBody.newBuilder().range(range).build();
given(handleContext.body())
Expand Down

0 comments on commit b09fc8a

Please sign in to comment.