Skip to content

Commit

Permalink
add Expected validation unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
nanfengpo committed Sep 5, 2018
1 parent 9b83706 commit 21c94cd
Showing 1 changed file with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ public void initTest() {
dbManager.getDynamicPropertiesStore().saveNextMaintenanceTime(2000000);
}

private Any getContract(String address, long exchangeId, String tokenId, long quant) {
private Any getContract(String address, long exchangeId, String tokenId,
long quant, long expected) {
return Any.pack(
Contract.ExchangeTransactionContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(address)))
.setExchangeId(exchangeId)
.setTokenId(ByteString.copyFrom(tokenId.getBytes()))
.setQuant(quant)
.setExpected(expected)
.build());
}

Expand All @@ -156,7 +158,7 @@ public void successExchangeTransaction() {
Assert.assertEquals(null, assetMap.get("def"));

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -221,7 +223,7 @@ public void successExchangeTransaction2() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -286,7 +288,7 @@ public void invalidAddress() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_INVALID, exchangeId, tokenId, quant),
OWNER_ADDRESS_INVALID, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -321,7 +323,7 @@ public void noAccount() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_NOACCOUNT, exchangeId, tokenId, quant),
OWNER_ADDRESS_NOACCOUNT, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -357,7 +359,7 @@ public void exchangeNotExist() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -393,7 +395,7 @@ public void tokenIsNotInExchange() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -429,7 +431,7 @@ public void tokenBalanceZero() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -473,7 +475,7 @@ public void tokenQuantLessThanZero() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand All @@ -483,7 +485,7 @@ public void tokenQuantLessThanZero() {
fail();
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("transaction token balance must greater than zero",
Assert.assertEquals("token quant must greater than zero",
e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
Expand All @@ -509,7 +511,7 @@ public void tokenBalanceGreaterThanBalanceLimit() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -544,7 +546,7 @@ public void balanceNotEnough() {
dbManager.getAccountStore().put(ownerAddress, accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand Down Expand Up @@ -580,7 +582,7 @@ public void tokenBalanceNotEnough() {
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant),
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

Expand All @@ -596,4 +598,49 @@ public void tokenBalanceNotEnough() {
Assert.assertFalse(e instanceof ContractExeException);
}
}

/**
* token required must greater than expected
*/
@Test
public void tokenRequiredNotEnough() {
long exchangeId = 2;
String tokenId = "abc";
long quant = 1_000L;
String buyTokenId = "def";

byte[] ownerAddress = ByteArray.fromHexString(OWNER_ADDRESS_SECOND);
AccountCapsule accountCapsule = dbManager.getAccountStore().get(ownerAddress);
accountCapsule.addAssetAmount(tokenId.getBytes(), quant);
Map<String, Long> assetMap = accountCapsule.getAssetMap();
Assert.assertEquals(20000_000000L, accountCapsule.getBalance());
Assert.assertEquals(null, assetMap.get(buyTokenId));
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

long expected = 0;
try {
ExchangeCapsule exchangeCapsule = dbManager.getExchangeStore()
.get(ByteArray.fromLong(exchangeId));
expected = exchangeCapsule.transaction(tokenId.getBytes(), quant);
} catch (ItemNotFoundException e) {
fail();
}

ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(getContract(
OWNER_ADDRESS_SECOND, exchangeId, tokenId, quant, expected + 1),
dbManager);
TransactionResultCapsule ret = new TransactionResultCapsule();

try {
actuator.validate();
actuator.execute(ret);
fail("should not run here");
} catch (ContractValidateException e) {
Assert.assertTrue(e instanceof ContractValidateException);
Assert.assertEquals("token required must greater than expected",
e.getMessage());
} catch (ContractExeException e) {
Assert.assertFalse(e instanceof ContractExeException);
}
}
}

0 comments on commit 21c94cd

Please sign in to comment.