1+ package org .tron .common .logsfilter ;
2+
3+
4+ import com .google .protobuf .ByteString ;
5+
6+ import java .lang .reflect .Method ;
7+ import java .util .ArrayList ;
8+ import java .util .HashMap ;
9+ import java .util .List ;
10+
11+ import org .junit .After ;
12+ import org .junit .Assert ;
13+ import org .junit .Before ;
14+ import org .junit .Test ;
15+ import org .tron .common .logsfilter .capsule .TransactionLogTriggerCapsule ;
16+ import org .tron .common .logsfilter .trigger .InternalTransactionPojo ;
17+ import org .tron .common .runtime .InternalTransaction ;
18+ import org .tron .common .runtime .ProgramResult ;
19+ import org .tron .common .runtime .RuntimeImpl ;
20+ import org .tron .common .utils .Sha256Hash ;
21+ import org .tron .core .capsule .BlockCapsule ;
22+ import org .tron .core .capsule .ReceiptCapsule ;
23+ import org .tron .core .capsule .TransactionCapsule ;
24+ import org .tron .core .db .TransactionTrace ;
25+ import org .tron .p2p .utils .ByteArray ;
26+ import org .tron .protos .Protocol ;
27+ import org .tron .protos .contract .BalanceContract ;
28+
29+ import static org .mockito .Mockito .mock ;
30+ import static org .mockito .Mockito .spy ;
31+ import static org .mockito .Mockito .when ;
32+
33+
34+ public class TransactionLogTriggerCapsuleMockTest {
35+
36+ private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc" ;
37+ private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150" ;
38+ private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548" ;
39+
40+ private TransactionCapsule transactionCapsule ;
41+ private BlockCapsule blockCapsule ;
42+
43+ @ Before
44+ public void setup () {
45+ blockCapsule = new BlockCapsule (1 ,
46+ Sha256Hash .ZERO_HASH ,
47+ System .currentTimeMillis (),
48+ Sha256Hash .ZERO_HASH .getByteString ()
49+ );
50+ }
51+
52+ @ After
53+ public void clearMocks () {
54+
55+ }
56+
57+
58+ @ Test
59+ public void testConstructorWithTransactionTrace () {
60+ BalanceContract .TransferContract .Builder builder2 =
61+ BalanceContract .TransferContract .newBuilder ()
62+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
63+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (RECEIVER_ADDRESS )));
64+ transactionCapsule = spy (new TransactionCapsule (builder2 .build (),
65+ Protocol .Transaction .Contract .ContractType .TransferContract ));
66+
67+ TransactionTrace trace = mock (TransactionTrace .class );
68+ ReceiptCapsule receiptCapsule = new ReceiptCapsule (Sha256Hash .ZERO_HASH );
69+ RuntimeImpl runtime = mock (RuntimeImpl .class );
70+ List <Protocol .TransactionInfo .Log > logs = new ArrayList <>();
71+ logs .add (Protocol .TransactionInfo .Log .newBuilder ()
72+ .setAddress (ByteString .copyFrom ("address" .getBytes ()))
73+ .setData (ByteString .copyFrom ("data" .getBytes ()))
74+ .addTopics (ByteString .copyFrom ("topic" .getBytes ()))
75+ .build ());
76+
77+ Protocol .TransactionInfo .Builder builder = Protocol .TransactionInfo .newBuilder ()
78+ .addAllLog (logs );
79+
80+ ProgramResult programResult = ProgramResult .createEmpty ();
81+ programResult .setHReturn ("hreturn" .getBytes ());
82+ programResult .setContractAddress (CONTRACT_ADDRESS .getBytes ());
83+
84+ when (transactionCapsule .getTrxTrace ()).thenReturn (trace );
85+ when (trace .getReceipt ()).thenReturn (receiptCapsule );
86+ when (trace .getRuntime ()).thenReturn (runtime );
87+ when (runtime .getResult ()).thenReturn (programResult );
88+
89+ transactionCapsule .setTrxTrace (trace );
90+
91+ TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule (
92+ transactionCapsule , blockCapsule ,0 ,0 ,0 ,
93+ builder .build (),0 );
94+
95+ Assert .assertNotNull (triggerCapsule .getTransactionLogTrigger ());
96+ }
97+
98+ @ Test
99+ public void testGetInternalTransactionList () throws Exception {
100+ BalanceContract .TransferContract .Builder builder2 =
101+ BalanceContract .TransferContract .newBuilder ()
102+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
103+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (RECEIVER_ADDRESS )));
104+ transactionCapsule = new TransactionCapsule (builder2 .build (),
105+ Protocol .Transaction .Contract .ContractType .TransferContract );
106+ InternalTransaction internalTransaction = new InternalTransaction (
107+ "parentHash" .getBytes (), 10 , 0 ,
108+ "sendAddress" .getBytes (),
109+ "transferToAddress" .getBytes (),
110+ 100L , "data" .getBytes (), "note" ,
111+ 0L , new HashMap <>()
112+ );
113+ List <InternalTransaction > internalTransactionList = new ArrayList <>();
114+ internalTransactionList .add (internalTransaction );
115+ TransactionLogTriggerCapsule triggerCapsule =
116+ new TransactionLogTriggerCapsule (transactionCapsule , blockCapsule );
117+
118+ Method privateMethod = TransactionLogTriggerCapsule .class .getDeclaredMethod (
119+ "getInternalTransactionList" , List .class );
120+ privateMethod .setAccessible (true );
121+ List <InternalTransactionPojo > pojoList = (List <InternalTransactionPojo >)
122+ privateMethod .invoke (triggerCapsule , internalTransactionList );
123+
124+ Assert .assertNotNull (pojoList );
125+ }
126+
127+ }
0 commit comments