@@ -130,13 +130,15 @@ public void initTest() {
130
130
dbManager .getDynamicPropertiesStore ().saveNextMaintenanceTime (2000000 );
131
131
}
132
132
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 ) {
134
135
return Any .pack (
135
136
Contract .ExchangeTransactionContract .newBuilder ()
136
137
.setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (address )))
137
138
.setExchangeId (exchangeId )
138
139
.setTokenId (ByteString .copyFrom (tokenId .getBytes ()))
139
140
.setQuant (quant )
141
+ .setExpected (expected )
140
142
.build ());
141
143
}
142
144
@@ -156,7 +158,7 @@ public void successExchangeTransaction() {
156
158
Assert .assertEquals (null , assetMap .get ("def" ));
157
159
158
160
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
159
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
161
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
160
162
dbManager );
161
163
TransactionResultCapsule ret = new TransactionResultCapsule ();
162
164
@@ -221,7 +223,7 @@ public void successExchangeTransaction2() {
221
223
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
222
224
223
225
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
224
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
226
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
225
227
dbManager );
226
228
TransactionResultCapsule ret = new TransactionResultCapsule ();
227
229
@@ -286,7 +288,7 @@ public void invalidAddress() {
286
288
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
287
289
288
290
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
289
- OWNER_ADDRESS_INVALID , exchangeId , tokenId , quant ),
291
+ OWNER_ADDRESS_INVALID , exchangeId , tokenId , quant , 1 ),
290
292
dbManager );
291
293
TransactionResultCapsule ret = new TransactionResultCapsule ();
292
294
@@ -321,7 +323,7 @@ public void noAccount() {
321
323
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
322
324
323
325
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
324
- OWNER_ADDRESS_NOACCOUNT , exchangeId , tokenId , quant ),
326
+ OWNER_ADDRESS_NOACCOUNT , exchangeId , tokenId , quant , 1 ),
325
327
dbManager );
326
328
TransactionResultCapsule ret = new TransactionResultCapsule ();
327
329
@@ -357,7 +359,7 @@ public void exchangeNotExist() {
357
359
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
358
360
359
361
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
360
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
362
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
361
363
dbManager );
362
364
TransactionResultCapsule ret = new TransactionResultCapsule ();
363
365
@@ -393,7 +395,7 @@ public void tokenIsNotInExchange() {
393
395
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
394
396
395
397
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
396
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
398
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
397
399
dbManager );
398
400
TransactionResultCapsule ret = new TransactionResultCapsule ();
399
401
@@ -429,7 +431,7 @@ public void tokenBalanceZero() {
429
431
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
430
432
431
433
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
432
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
434
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
433
435
dbManager );
434
436
TransactionResultCapsule ret = new TransactionResultCapsule ();
435
437
@@ -455,7 +457,7 @@ public void tokenBalanceZero() {
455
457
}
456
458
457
459
/**
458
- * withdraw token quant must greater than zero
460
+ * token quant must greater than zero
459
461
*/
460
462
@ Test
461
463
public void tokenQuantLessThanZero () {
@@ -473,7 +475,7 @@ public void tokenQuantLessThanZero() {
473
475
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
474
476
475
477
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
476
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
478
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
477
479
dbManager );
478
480
TransactionResultCapsule ret = new TransactionResultCapsule ();
479
481
@@ -483,7 +485,7 @@ public void tokenQuantLessThanZero() {
483
485
fail ();
484
486
} catch (ContractValidateException e ) {
485
487
Assert .assertTrue (e instanceof ContractValidateException );
486
- Assert .assertEquals ("transaction token balance must greater than zero" ,
488
+ Assert .assertEquals ("token quant must greater than zero" ,
487
489
e .getMessage ());
488
490
} catch (ContractExeException e ) {
489
491
Assert .assertFalse (e instanceof ContractExeException );
@@ -509,7 +511,7 @@ public void tokenBalanceGreaterThanBalanceLimit() {
509
511
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
510
512
511
513
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
512
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
514
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
513
515
dbManager );
514
516
TransactionResultCapsule ret = new TransactionResultCapsule ();
515
517
@@ -544,7 +546,7 @@ public void balanceNotEnough() {
544
546
dbManager .getAccountStore ().put (ownerAddress , accountCapsule );
545
547
546
548
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
547
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
549
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
548
550
dbManager );
549
551
TransactionResultCapsule ret = new TransactionResultCapsule ();
550
552
@@ -562,7 +564,7 @@ public void balanceNotEnough() {
562
564
}
563
565
564
566
/**
565
- * first token balance is not enough
567
+ * token balance is not enough
566
568
*/
567
569
@ Test
568
570
public void tokenBalanceNotEnough () {
@@ -580,7 +582,7 @@ public void tokenBalanceNotEnough() {
580
582
dbManager .getAccountStore ().put (accountCapsule .createDbKey (), accountCapsule );
581
583
582
584
ExchangeTransactionActuator actuator = new ExchangeTransactionActuator (getContract (
583
- OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant ),
585
+ OWNER_ADDRESS_SECOND , exchangeId , tokenId , quant , 1 ),
584
586
dbManager );
585
587
TransactionResultCapsule ret = new TransactionResultCapsule ();
586
588
@@ -596,4 +598,49 @@ public void tokenBalanceNotEnough() {
596
598
Assert .assertFalse (e instanceof ContractExeException );
597
599
}
598
600
}
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
+ }
599
646
}
0 commit comments