Skip to content

Commit

Permalink
Refactoring ReadableAirdropStoreImplTest and static code analysis fixes
Browse files Browse the repository at this point in the history
Signed-off-by: ibankov <[email protected]>
  • Loading branch information
ibankov committed May 29, 2024
1 parent 09861b0 commit 0955fc8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public ReadableAirdropStoreImpl(@NonNull final ReadableStates states) {
}

@Override
public boolean exists(@NonNull PendingAirdropId airdropId) {
public boolean exists(@NonNull final PendingAirdropId airdropId) {
return readableAirdropState.contains(airdropId);
}

/** {@inheritDoc} */
@Override
@Nullable
public PendingAirdropValue getFungibleAirdropAmount(@NonNull PendingAirdropId airdropId) {
public PendingAirdropValue getFungibleAirdropAmount(@NonNull final PendingAirdropId airdropId) {
requireNonNull(airdropId);
if (airdropId.hasNonFungibleToken()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void put(@NonNull final PendingAirdropId airdropId, @NonNull final Pendin
*
* @param airdropId the {@code PendingAirdropId} to be removed
*/
public void remove(@NonNull PendingAirdropId airdropId) {
public void remove(@NonNull final PendingAirdropId airdropId) {
airdropState.remove(requireNonNull(airdropId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,29 @@
import com.hedera.hapi.node.base.TokenID;
import com.hedera.node.app.service.token.impl.ReadableAirdropStoreImpl;
import com.hedera.node.app.service.token.impl.test.handlers.util.StateBuilderUtil;
import com.hedera.node.app.service.token.impl.test.handlers.util.TokenHandlerTestBase;
import com.swirlds.state.spi.ReadableKVState;
import com.swirlds.state.spi.ReadableStates;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class ReadableAirdropStoreImplTest extends TokenHandlerTestBase {
class ReadableAirdropStoreImplTest extends StateBuilderUtil {

@Mock
private ReadableStates readableStates;

private ReadableKVState<PendingAirdropId, PendingAirdropValue> airdrops;

private ReadableAirdropStoreImpl subject;

@BeforeEach
public void setUp() {
initializeAirdrops();
subject = new ReadableAirdropStoreImpl(readableStates);
}

private void initializeAirdrops() {
given(readableStates.<PendingAirdropId, PendingAirdropValue>get(StateBuilderUtil.AIRDROPS))
given(readableStates.<PendingAirdropId, PendingAirdropValue>get(AIRDROPS))
.willReturn(airdrops);
subject = new ReadableAirdropStoreImpl(readableStates);
}

@Test
Expand All @@ -63,23 +61,38 @@ void getsValueIfAirdropContainsFungibleToken() {
var fungibleAirdrop = getFungibleAirdrop();
var airdropValue = PendingAirdropValue.newBuilder().amount(5).build();

given(airdrops.get(fungibleAirdrop)).willReturn(airdropValue);
airdrops = emptyReadableAirdropStateBuilder()
.value(fungibleAirdrop,airdropValue)
.build();
given(readableStates.<PendingAirdropId, PendingAirdropValue>get(AIRDROPS))
.willReturn(airdrops);
subject = new ReadableAirdropStoreImpl(readableStates);

assertThat(subject.getFungibleAirdropAmount(fungibleAirdrop)).isNotNull();
assertThat(subject.getFungibleAirdropAmount(fungibleAirdrop)).isEqualTo(airdropValue);
}

@Test
void testSizeOfState() {
final var store = new ReadableAirdropStoreImpl(readableStates);
assertThat(readableStates.get(StateBuilderUtil.AIRDROPS).size()).isEqualTo(store.sizeOfState());
airdrops = emptyReadableAirdropStateBuilder()
.value(getNonFungibleAirDrop(), PendingAirdropValue.newBuilder().build())
.build();
given(readableStates.<PendingAirdropId, PendingAirdropValue>get(AIRDROPS))
.willReturn(airdrops);
subject = new ReadableAirdropStoreImpl(readableStates);
assertThat(readableStates.get(StateBuilderUtil.AIRDROPS).size()).isEqualTo(subject.sizeOfState());
}

@Test
void testExists() {
var fungibleAirdrop = getFungibleAirdrop();

given(airdrops.contains(fungibleAirdrop)).willReturn(true);
airdrops = emptyReadableAirdropStateBuilder()
.value(fungibleAirdrop, PendingAirdropValue.newBuilder().amount(5).build())
.build();
given(readableStates.<PendingAirdropId, PendingAirdropValue>get(AIRDROPS))
.willReturn(airdrops);
subject = new ReadableAirdropStoreImpl(readableStates);

final var store = new ReadableAirdropStoreImpl(readableStates);
assertThat(readableStates.get(StateBuilderUtil.AIRDROPS).contains(fungibleAirdrop))
Expand All @@ -89,7 +102,7 @@ void testExists() {
private PendingAirdropId getNonFungibleAirDrop() {
return PendingAirdropId.newBuilder()
.nonFungibleToken(NftID.newBuilder()
.serialNumber(123456)
.serialNumber(123_456)
.tokenId(TokenID.newBuilder()
.tokenNum(1)
.shardNum(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class WritableAirdropStoreTest extends StateBuilderUtil {
class WritableAirdropStoreTest extends StateBuilderUtil {

@Mock
private WritableStates writableStates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected MapWritableKVState.Builder<AccountID, Account> emptyWritableAccountSta
return MapWritableKVState.builder(ACCOUNTS);
}

@NonNull
protected MapReadableKVState.Builder<PendingAirdropId, PendingAirdropValue> emptyReadableAirdropStateBuilder() {
return MapReadableKVState.builder(AIRDROPS);
}

@NonNull
protected MapWritableKVState.Builder<PendingAirdropId, PendingAirdropValue> emptyWritableAirdropStateBuilder() {
return MapWritableKVState.builder(AIRDROPS);
Expand Down

0 comments on commit 0955fc8

Please sign in to comment.