@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77package chclient
88
99import (
10+ "strings"
1011 "testing"
1112 "time"
1213
@@ -20,9 +21,39 @@ import (
2021 txnmocks "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/mocks"
2122)
2223
24+ func TestTxProposalResponseFilter (t * testing.T ) {
25+ // failed if status not 200
26+ testPeer1 := fcmocks .NewMockPeer ("Peer1" , "http://peer1.com" )
27+ testPeer2 := fcmocks .NewMockPeer ("Peer2" , "http://peer2.com" )
28+ testPeer2 .Status = 300
29+ peers := []apifabclient.Peer {testPeer1 , testPeer2 }
30+ chClient := setupChannelClient (peers , t )
31+
32+ _ , err := chClient .Query (apitxn.QueryRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("query" ), []byte ("b" )}})
33+ if err == nil {
34+ t .Fatalf ("Should have failed for not success status" )
35+ }
36+ if ! strings .Contains (err .Error (), "proposal response was not successful, error code 300" ) {
37+ t .Fatalf ("Return wrong error message %v" , err )
38+ }
39+
40+ testPeer2 .Payload = []byte ("wrongPayload" )
41+ testPeer2 .Status = 200
42+ peers = []apifabclient.Peer {testPeer1 , testPeer2 }
43+ chClient = setupChannelClient (peers , t )
44+ _ , err = chClient .Query (apitxn.QueryRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("query" ), []byte ("b" )}})
45+ if err == nil {
46+ t .Fatalf ("Should have failed for not success status" )
47+ }
48+ if ! strings .Contains (err .Error (), "ProposalResponsePayloads do not match" ) {
49+ t .Fatalf ("Return wrong error message %v" , err )
50+ }
51+
52+ }
53+
2354func TestQuery (t * testing.T ) {
2455
25- chClient := setupChannelClient (t )
56+ chClient := setupChannelClient (nil , t )
2657
2758 result , err := chClient .Query (apitxn.QueryRequest {})
2859 if err == nil {
@@ -52,7 +83,7 @@ func TestQuery(t *testing.T) {
5283
5384func TestQueryDiscoveryError (t * testing.T ) {
5485
55- chClient := setupChannelClientWithError (errors .New ("Test Error" ), nil , t )
86+ chClient := setupChannelClientWithError (errors .New ("Test Error" ), nil , nil , t )
5687
5788 _ , err := chClient .Query (apitxn.QueryRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("query" ), []byte ("b" )}})
5889 if err == nil {
@@ -63,7 +94,7 @@ func TestQueryDiscoveryError(t *testing.T) {
6394
6495func TestQuerySelectionError (t * testing.T ) {
6596
66- chClient := setupChannelClientWithError (nil , errors .New ("Test Error" ), t )
97+ chClient := setupChannelClientWithError (nil , errors .New ("Test Error" ), nil , t )
6798
6899 _ , err := chClient .Query (apitxn.QueryRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("query" ), []byte ("b" )}})
69100 if err == nil {
@@ -74,7 +105,7 @@ func TestQuerySelectionError(t *testing.T) {
74105
75106func TestQueryWithOptSync (t * testing.T ) {
76107
77- chClient := setupChannelClient (t )
108+ chClient := setupChannelClient (nil , t )
78109
79110 result , err := chClient .QueryWithOpts (apitxn.QueryRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("query" ), []byte ("b" )}}, apitxn.QueryOpts {})
80111 if err != nil {
@@ -88,7 +119,7 @@ func TestQueryWithOptSync(t *testing.T) {
88119
89120func TestQueryWithOptAsync (t * testing.T ) {
90121
91- chClient := setupChannelClient (t )
122+ chClient := setupChannelClient (nil , t )
92123
93124 notifier := make (chan apitxn.QueryResponse )
94125
@@ -117,11 +148,11 @@ func TestQueryWithOptAsync(t *testing.T) {
117148
118149func TestQueryWithOptTarget (t * testing.T ) {
119150
120- chClient := setupChannelClient (t )
151+ chClient := setupChannelClient (nil , t )
121152
122- testPeer := fcmocks.MockPeer { MockName : "Peer1" , MockURL : "http://peer1.com" , MockRoles : [] string {}, MockCert : nil }
153+ testPeer := fcmocks .NewMockPeer ( "Peer1" , "http://peer1.com" )
123154
124- peers := []apifabclient.Peer {& testPeer }
155+ peers := []apifabclient.Peer {testPeer }
125156
126157 targets := peer .PeersToTxnProcessors (peers )
127158
@@ -137,7 +168,7 @@ func TestQueryWithOptTarget(t *testing.T) {
137168
138169func TestExecuteTx (t * testing.T ) {
139170
140- chClient := setupChannelClient (t )
171+ chClient := setupChannelClient (nil , t )
141172
142173 _ , err := chClient .ExecuteTx (apitxn.ExecuteTxRequest {})
143174 if err == nil {
@@ -159,7 +190,7 @@ func TestExecuteTx(t *testing.T) {
159190
160191func TestExecuteTxDiscoveryError (t * testing.T ) {
161192
162- chClient := setupChannelClientWithError (errors .New ("Test Error" ), nil , t )
193+ chClient := setupChannelClientWithError (errors .New ("Test Error" ), nil , nil , t )
163194
164195 _ , err := chClient .ExecuteTx (apitxn.ExecuteTxRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("move" ), []byte ("a" ), []byte ("b" ), []byte ("1" )}})
165196 if err == nil {
@@ -170,7 +201,7 @@ func TestExecuteTxDiscoveryError(t *testing.T) {
170201
171202func TestExecuteTxSelectionError (t * testing.T ) {
172203
173- chClient := setupChannelClientWithError (nil , errors .New ("Test Error" ), t )
204+ chClient := setupChannelClientWithError (nil , errors .New ("Test Error" ), nil , t )
174205
175206 _ , err := chClient .ExecuteTx (apitxn.ExecuteTxRequest {ChaincodeID : "testCC" , Fcn : "invoke" , Args : [][]byte {[]byte ("move" ), []byte ("a" ), []byte ("b" ), []byte ("1" )}})
176207 if err == nil {
@@ -214,12 +245,12 @@ func setupTestSelection(discErr error, peers []apifabclient.Peer) (apifabclient.
214245 return mockSelection .NewSelectionService ("mychannel" )
215246}
216247
217- func setupChannelClient (t * testing.T ) * ChannelClient {
248+ func setupChannelClient (peers []apifabclient. Peer , t * testing.T ) * ChannelClient {
218249
219- return setupChannelClientWithError (nil , nil , t )
250+ return setupChannelClientWithError (nil , nil , peers , t )
220251}
221252
222- func setupChannelClientWithError (discErr error , selectionErr error , t * testing.T ) * ChannelClient {
253+ func setupChannelClientWithError (discErr error , selectionErr error , peers []apifabclient. Peer , t * testing.T ) * ChannelClient {
223254
224255 fcClient := setupTestClient ()
225256
@@ -236,7 +267,7 @@ func setupChannelClientWithError(discErr error, selectionErr error, t *testing.T
236267 t .Fatalf ("Failed to setup discovery service: %s" , err )
237268 }
238269
239- selectionService , err := setupTestSelection (selectionErr , nil )
270+ selectionService , err := setupTestSelection (selectionErr , peers )
240271 if err != nil {
241272 t .Fatalf ("Failed to setup discovery service: %s" , err )
242273 }
0 commit comments