use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.
the class SqlEntityListenerTest method onCryptoAllowanceHistory.
@ValueSource(ints = { 1, 2, 3 })
@ParameterizedTest
void onCryptoAllowanceHistory(int commitIndex) {
// given
final String idColumns = "payer_account_id, spender";
var builder = domainBuilder.cryptoAllowance();
CryptoAllowance cryptoAllowanceCreate = builder.get();
CryptoAllowance cryptoAllowanceUpdate1 = builder.customize(c -> c.amount(999L)).get();
cryptoAllowanceUpdate1.setTimestampLower(cryptoAllowanceCreate.getTimestampLower() + 1);
CryptoAllowance cryptoAllowanceUpdate2 = builder.customize(c -> c.amount(0L)).get();
cryptoAllowanceUpdate2.setTimestampLower(cryptoAllowanceCreate.getTimestampLower() + 2);
// Expected merged objects
CryptoAllowance mergedCreate = TestUtils.clone(cryptoAllowanceCreate);
CryptoAllowance mergedUpdate1 = TestUtils.merge(cryptoAllowanceCreate, cryptoAllowanceUpdate1);
CryptoAllowance mergedUpdate2 = TestUtils.merge(mergedUpdate1, cryptoAllowanceUpdate2);
mergedCreate.setTimestampUpper(cryptoAllowanceUpdate1.getTimestampLower());
// when
sqlEntityListener.onCryptoAllowance(cryptoAllowanceCreate);
if (commitIndex > 1) {
completeFileAndCommit();
assertThat(cryptoAllowanceRepository.findAll()).containsExactly(cryptoAllowanceCreate);
assertThat(findHistory(CryptoAllowance.class, idColumns)).isEmpty();
}
sqlEntityListener.onCryptoAllowance(cryptoAllowanceUpdate1);
if (commitIndex > 2) {
completeFileAndCommit();
assertThat(cryptoAllowanceRepository.findAll()).containsExactly(mergedUpdate1);
assertThat(findHistory(CryptoAllowance.class, idColumns)).containsExactly(mergedCreate);
}
sqlEntityListener.onCryptoAllowance(cryptoAllowanceUpdate2);
completeFileAndCommit();
// then
mergedUpdate1.setTimestampUpper(cryptoAllowanceUpdate2.getTimestampLower());
assertThat(cryptoAllowanceRepository.findAll()).containsExactly(mergedUpdate2);
assertThat(findHistory(CryptoAllowance.class, idColumns)).containsExactly(mergedCreate, mergedUpdate1);
}
use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.
the class SqlEntityListenerTest method onCryptoAllowance.
@Test
void onCryptoAllowance() {
// given
CryptoAllowance cryptoAllowance1 = domainBuilder.cryptoAllowance().get();
CryptoAllowance cryptoAllowance2 = domainBuilder.cryptoAllowance().get();
// when
sqlEntityListener.onCryptoAllowance(cryptoAllowance1);
sqlEntityListener.onCryptoAllowance(cryptoAllowance2);
completeFileAndCommit();
// then
assertThat(entityRepository.count()).isZero();
assertThat(cryptoAllowanceRepository.findAll()).containsExactlyInAnyOrder(cryptoAllowance1, cryptoAllowance2);
assertThat(findHistory(CryptoAllowance.class, "payer_account_id, spender")).isEmpty();
}
use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.
the class CryptoAllowanceRepositoryTest method save.
@Test
void save() {
CryptoAllowance cryptoAllowance = domainBuilder.cryptoAllowance().persist();
assertThat(cryptoAllowanceRepository.findById(cryptoAllowance.getId())).get().isEqualTo(cryptoAllowance);
}
use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.
the class CryptoAllowanceRepositoryTest method history.
/**
* This test verifies that the domain object and table definition are in sync with the history table.
*/
@Test
void history() {
CryptoAllowance cryptoAllowance = domainBuilder.cryptoAllowance().persist();
jdbcOperations.update("insert into crypto_allowance_history select * from crypto_allowance");
List<CryptoAllowance> cryptoAllowanceHistory = jdbcOperations.query("select * from crypto_allowance_history", ROW_MAPPER);
assertThat(cryptoAllowanceRepository.findAll()).containsExactly(cryptoAllowance);
assertThat(cryptoAllowanceHistory).containsExactly(cryptoAllowance);
}
use of com.hedera.mirror.common.domain.entity.CryptoAllowance in project hedera-mirror-node by hashgraph.
the class CryptoApproveAllowanceTransactionHandler method parseCryptoAllowances.
private void parseCryptoAllowances(List<com.hederahashgraph.api.proto.java.CryptoAllowance> cryptoAllowances, RecordItem recordItem) {
var consensusTimestamp = recordItem.getConsensusTimestamp();
var cryptoAllowanceState = new HashMap<CryptoAllowance.Id, CryptoAllowance>();
var payerAccountId = recordItem.getPayerAccountId();
// iterate the crypto allowance list in reverse order and honor the last allowance for the same owner and spender
var iterator = cryptoAllowances.listIterator(cryptoAllowances.size());
while (iterator.hasPrevious()) {
var cryptoApproval = iterator.previous();
EntityId ownerAccountId = getOwnerAccountId(cryptoApproval.getOwner(), payerAccountId);
if (ownerAccountId == EntityId.EMPTY) {
// and the partialDataAction is SKIP
continue;
}
CryptoAllowance cryptoAllowance = new CryptoAllowance();
cryptoAllowance.setAmount(cryptoApproval.getAmount());
cryptoAllowance.setOwner(ownerAccountId.getId());
cryptoAllowance.setPayerAccountId(payerAccountId);
cryptoAllowance.setSpender(EntityId.of(cryptoApproval.getSpender()).getId());
cryptoAllowance.setTimestampLower(consensusTimestamp);
if (cryptoAllowanceState.putIfAbsent(cryptoAllowance.getId(), cryptoAllowance) == null) {
entityListener.onCryptoAllowance(cryptoAllowance);
}
}
}
Aggregations