1010import static org .hamcrest .Matchers .equalTo ;
1111import static org .hamcrest .Matchers .is ;
1212import static org .hamcrest .Matchers .notNullValue ;
13+ import static org .hamcrest .Matchers .startsWith ;
1314import static org .junit .Assert .assertThat ;
1415
1516import java .util .ArrayList ;
@@ -36,31 +37,17 @@ public void testCreateAndScan() {
3637 r .findAllContracts ();
3738 ChaincodeStub s = new ChaincodeStubNaiveImpl ();
3839
39- // Test Init routing
40- List <String > args = new ArrayList <>();
41- args .add ("samplecontract:i1" );
42- ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
43- InvocationRequest request = ExecutionFactory .getInstance ().createRequest (s );
44- assertThat (request .getNamespace (), is (equalTo (SampleContract .class .getAnnotation (Contract .class ).namespace ())));
45- assertThat (request .getMethod (), is (equalTo ("i1" )));
46- assertThat (request .getRequestName (), is (equalTo ("samplecontract:i1" )));
47- assertThat (request .getArgs (), is (empty ()));
48- org .hyperledger .fabric .contract .routing .TxFunction .Routing routing = r .getRouting (request );
49- assertThat (routing .getContractClass ().getName (), is (equalTo (SampleContract .class .getName ())));
50- assertThat (routing .getMethod ().getName (), is (equalTo ("i1" )));
51- //
52-
5340 // Test Transaction routing
54- args = new ArrayList <>();
41+ List < String > args = new ArrayList <>();
5542 args .add ("samplecontract:t1" );
5643 args .add ("asdf" );
5744 ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
58- request = ExecutionFactory .getInstance ().createRequest (s );
45+ InvocationRequest request = ExecutionFactory .getInstance ().createRequest (s );
5946 assertThat (request .getNamespace (), is (equalTo (SampleContract .class .getAnnotation (Contract .class ).namespace ())));
6047 assertThat (request .getMethod (), is (equalTo ("t1" )));
6148 assertThat (request .getRequestName (), is (equalTo ("samplecontract:t1" )));
6249 assertThat (request .getArgs (), is (contains (s .getArgs ().get (1 ))));
63- routing = r .getRouting (request );
50+ org . hyperledger . fabric . contract . routing . TxFunction . Routing routing = r .getRouting (request );
6451 assertThat (routing .getContractClass ().getName (), is (equalTo (SampleContract .class .getName ())));
6552 assertThat (routing .getMethod ().getName (), is (equalTo ("t1" )));
6653
@@ -73,36 +60,27 @@ public void testInit() {
7360 ChaincodeStub s = new ChaincodeStubNaiveImpl ();
7461
7562 List <String > args = new ArrayList <>();
76- args .add ("samplecontract:i1" );
63+ args .add ("samplecontract:t1" );
64+ args .add ("asdf" );
7765 ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
7866
7967 SampleContract .beforeInvoked = 0 ;
8068 SampleContract .afterInvoked = 0 ;
81- SampleContract .i1Invoked = 0 ;
69+ SampleContract .doWorkInvoked = 0 ;
70+ SampleContract .t1Invoked = 0 ;
71+
8272 Chaincode .Response response = r .init (s );
8373 assertThat (response , is (notNullValue ()));
84- assertThat (response .getStringPayload (), is (equalTo ("Init done" )));
74+ assertThat (response .getStatus (), is (Chaincode .Response .Status .SUCCESS ));
75+ assertThat (response .getStringPayload (), is (equalTo ("asdf" )));
8576 assertThat (SampleContract .beforeInvoked , is (1 ));
8677 assertThat (SampleContract .afterInvoked , is (1 ));
87- assertThat (SampleContract .i1Invoked , is (1 ));
88-
89- args .set (0 , "samplecontract:i2" );
90- ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
91-
92- response = r .init (s );
93- assertThat (response , is (notNullValue ()));
94- assertThat (response .getStatus (), is (Chaincode .Response .Status .INTERNAL_SERVER_ERROR ));
95-
96- args .set (0 , "samplecontract:t1" );
97- args .add ("arg text" );
98- ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
99- response = r .init (s );
100- assertThat (response , is (notNullValue ()));
101- assertThat (response .getStatus (), is (Chaincode .Response .Status .SUCCESS ));
78+ assertThat (SampleContract .doWorkInvoked , is (1 ));
79+ assertThat (SampleContract .t1Invoked , is (1 ));
10280 }
10381
10482 @ Test
105- public void testInvoke () {
83+ public void testInvokeTxnThatExists () {
10684 ContractRouter r = new ContractRouter (new String []{"-a" , "127.0.0.1:7052" , "-i" , "testId" });
10785 r .findAllContracts ();
10886 ChaincodeStub s = new ChaincodeStubNaiveImpl ();
@@ -119,24 +97,62 @@ public void testInvoke() {
11997
12098 Chaincode .Response response = r .invoke (s );
12199 assertThat (response , is (notNullValue ()));
100+ assertThat (response .getStatus (), is (Chaincode .Response .Status .SUCCESS ));
122101 assertThat (response .getStringPayload (), is (equalTo ("asdf" )));
123102 assertThat (SampleContract .beforeInvoked , is (1 ));
124103 assertThat (SampleContract .afterInvoked , is (1 ));
125104 assertThat (SampleContract .doWorkInvoked , is (1 ));
126105 assertThat (SampleContract .t1Invoked , is (1 ));
106+ }
127107
128- args .set (0 , "samplecontract:notsupposedtoexist" );
108+ @ Test
109+ public void testInvokeTxnThatDoesNotExist () {
110+ ContractRouter r = new ContractRouter (new String []{"-a" , "127.0.0.1:7052" , "-i" , "testId" });
111+ r .findAllContracts ();
112+ ChaincodeStub s = new ChaincodeStubNaiveImpl ();
113+
114+ List <String > args = new ArrayList <>();
115+ args .add ("samplecontract:notsupposedtoexist" );
116+ args .add ("asdf" );
129117 ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
130118
131- response = r .invoke (s );
119+ SampleContract .beforeInvoked = 0 ;
120+ SampleContract .afterInvoked = 0 ;
121+ SampleContract .doWorkInvoked = 0 ;
122+ SampleContract .t1Invoked = 0 ;
123+
124+ Chaincode .Response response = r .invoke (s );
132125 assertThat (response , is (notNullValue ()));
133126 assertThat (response .getStatus (), is (Chaincode .Response .Status .INTERNAL_SERVER_ERROR ));
127+ assertThat (response .getStringPayload (), is (startsWith ("java.lang.IllegalStateException" )));
128+ assertThat (SampleContract .beforeInvoked , is (0 ));
129+ assertThat (SampleContract .afterInvoked , is (0 ));
130+ assertThat (SampleContract .doWorkInvoked , is (0 ));
131+ assertThat (SampleContract .t1Invoked , is (0 ));
132+ }
133+
134+ @ Test
135+ public void testInvokeTxnThatThrowsAnException () {
136+ ContractRouter r = new ContractRouter (new String []{"-a" , "127.0.0.1:7052" , "-i" , "testId" });
137+ r .findAllContracts ();
138+ ChaincodeStub s = new ChaincodeStubNaiveImpl ();
134139
135- args .set (0 , "samplecontract:i1" );
140+ List <String > args = new ArrayList <>();
141+ args .add ("samplecontract:t3" );
142+ args .add ("asdf" );
136143 ((ChaincodeStubNaiveImpl ) s ).setStringArgs (args );
137- response = r .invoke (s );
144+
145+ SampleContract .beforeInvoked = 0 ;
146+ SampleContract .afterInvoked = 0 ;
147+ SampleContract .doWorkInvoked = 0 ;
148+
149+ Chaincode .Response response = r .invoke (s );
138150 assertThat (response , is (notNullValue ()));
139- assertThat (response .getStatus (), is (Chaincode .Response .Status .SUCCESS ));
151+ assertThat (response .getStatus (), is (Chaincode .Response .Status .INTERNAL_SERVER_ERROR ));
152+ assertThat (response .getStringPayload (), is (startsWith ("java.lang.RuntimeException: T3 fail!" )));
153+ assertThat (SampleContract .beforeInvoked , is (1 ));
154+ assertThat (SampleContract .afterInvoked , is (0 ));
155+ assertThat (SampleContract .doWorkInvoked , is (0 ));
140156 }
141157
142158 @ Test
0 commit comments