@@ -130,13 +130,15 @@ public void initTest() {
130130 dbManager .getDynamicPropertiesStore ().saveNextMaintenanceTime (2000000 );
131131 }
132132
133- private Any getContract (String address , long exchangeId , String tokenId , long quant ) {
133+ private Any getContract (String address , long exchangeId , String tokenId ,
134+ long quant , long expected ) {
134135 return Any .pack (
135136 Contract .ExchangeTransactionContract .newBuilder ()
136137 .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (address )))
137138 .setExchangeId (exchangeId )
138139 .setTokenId (ByteString .copyFrom (tokenId .getBytes ()))
139140 .setQuant (quant )
141+ .setExpected (expected )
140142 .build ());
141143 }
142144
@@ -156,7 +158,7 @@ public void successExchangeTransaction() {
156158 Assert .assertEquals (null , assetMap .get ("def" ));
157159
158160 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
159- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
161+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
160162 dbManager );
161163 TransactionResultCapsule ret = new TransactionResultCapsule ();
162164
@@ -221,7 +223,7 @@ public void successExchangeTransaction2() {
221223 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
222224
223225 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
224- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
226+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
225227 dbManager );
226228 TransactionResultCapsule ret = new TransactionResultCapsule ();
227229
@@ -286,7 +288,7 @@ public void invalidAddress() {
286288 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
287289
288290 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
289- OWNER_ADDRESS_INVALID , exchangeId , tokenId , quant ),
291+ OWNER_ADDRESS_INVALID , exchangeId , tokenId , quant , 1 ),
290292 dbManager );
291293 TransactionResultCapsule ret = new TransactionResultCapsule ();
292294
@@ -321,7 +323,7 @@ public void noAccount() {
321323 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
322324
323325 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
324- OWNER_ADDRESS_NOACCOUNT , exchangeId , tokenId , quant ),
326+ OWNER_ADDRESS_NOACCOUNT , exchangeId , tokenId , quant , 1 ),
325327 dbManager );
326328 TransactionResultCapsule ret = new TransactionResultCapsule ();
327329
@@ -357,7 +359,7 @@ public void exchangeNotExist() {
357359 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
358360
359361 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
360- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
362+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
361363 dbManager );
362364 TransactionResultCapsule ret = new TransactionResultCapsule ();
363365
@@ -393,7 +395,7 @@ public void tokenIsNotInExchange() {
393395 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
394396
395397 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
396- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
398+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
397399 dbManager );
398400 TransactionResultCapsule ret = new TransactionResultCapsule ();
399401
@@ -429,7 +431,7 @@ public void tokenBalanceZero() {
429431 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
430432
431433 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
432- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
434+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
433435 dbManager );
434436 TransactionResultCapsule ret = new TransactionResultCapsule ();
435437
@@ -455,7 +457,7 @@ public void tokenBalanceZero() {
455457 }
456458
457459 /**
458- * withdraw token quant must greater than zero
460+ * token quant must greater than zero
459461 */
460462 @ Test
461463 public void tokenQuantLessThanZero () {
@@ -473,7 +475,7 @@ public void tokenQuantLessThanZero() {
473475 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
474476
475477 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
476- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
478+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
477479 dbManager );
478480 TransactionResultCapsule ret = new TransactionResultCapsule ();
479481
@@ -483,7 +485,7 @@ public void tokenQuantLessThanZero() {
483485 fail ();
484486 } catch (ContractValidateException e ) {
485487 Assert .assertTrue (e instanceof ContractValidateException );
486- Assert .assertEquals ("transaction token balance must greater than zero" ,
488+ Assert .assertEquals ("token quant must greater than zero" ,
487489 e .getMessage ());
488490 } catch (ContractExeException e ) {
489491 Assert .assertFalse (e instanceof ContractExeException );
@@ -509,7 +511,7 @@ public void tokenBalanceGreaterThanBalanceLimit() {
509511 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
510512
511513 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
512- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
514+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
513515 dbManager );
514516 TransactionResultCapsule ret = new TransactionResultCapsule ();
515517
@@ -544,7 +546,7 @@ public void balanceNotEnough() {
544546 dbManager .getAccountStore ().put (ownerAddress , accountCapsule );
545547
546548 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
547- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
549+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
548550 dbManager );
549551 TransactionResultCapsule ret = new TransactionResultCapsule ();
550552
@@ -562,7 +564,7 @@ public void balanceNotEnough() {
562564 }
563565
564566 /**
565- * first token balance is not enough
567+ * token balance is not enough
566568 */
567569 @ Test
568570 public void tokenBalanceNotEnough () {
@@ -580,7 +582,7 @@ public void tokenBalanceNotEnough() {
580582 dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
581583
582584 ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
583- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
585+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
584586 dbManager );
585587 TransactionResultCapsule ret = new TransactionResultCapsule ();
586588
@@ -596,4 +598,49 @@ public void tokenBalanceNotEnough() {
596598 Assert .assertFalse (e instanceof ContractExeException );
597599 }
598600 }
601+
602+ /**
603+ * token required must greater than expected
604+ */
605+ @ Test
606+ public void tokenRequiredNotEnough () {
607+ long exchangeId = 2 ;
608+ String tokenId = "abc" ;
609+ long quant = 1_000L ;
610+ String buyTokenId = "def" ;
611+
612+ byte [] ownerAddress = ByteArray .fromHexString (OWNER_ADDRESS_SECOND );
613+ AccountCapsule accountCapsule = dbManager .getAccountStore ().get (ownerAddress );
614+ accountCapsule .addAssetAmount (tokenId .getBytes (), quant );
615+ Map <String , Long > assetMap = accountCapsule .getAssetMap ();
616+ Assert .assertEquals (20000_000000L , accountCapsule .getBalance ());
617+ Assert .assertEquals (null , assetMap .get (buyTokenId ));
618+ dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
619+
620+ long expected = 0 ;
621+ try {
622+ ExchangeCapsule exchangeCapsule = dbManager .getExchangeStore ()
623+ .get (ByteArray .fromLong (exchangeId ));
624+ expected = exchangeCapsule .transaction (tokenId .getBytes (), quant );
625+ } catch (ItemNotFoundException e ) {
626+ fail ();
627+ }
628+
629+ ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
630+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , expected + 1 ),
631+ dbManager );
632+ TransactionResultCapsule ret = new TransactionResultCapsule ();
633+
634+ try {
635+ actuator .validate ();
636+ actuator .execute (ret );
637+ fail ("should not run here" );
638+ } catch (ContractValidateException e ) {
639+ Assert .assertTrue (e instanceof ContractValidateException );
640+ Assert .assertEquals ("token required must greater than expected" ,
641+ e .getMessage ());
642+ } catch (ContractExeException e ) {
643+ Assert .assertFalse (e instanceof ContractExeException );
644+ }
645+ }
599646}
0 commit comments