@@ -73,7 +73,7 @@ describe('HLFQueryHandler', () => {
73
73
74
74
it ( 'should not switch to another peer if peer returns a payload which is an error' , async ( ) => {
75
75
const response = new Error ( 'my chaincode error' ) ;
76
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
76
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
77
77
let qspSpy = sinon . spy ( queryHandler , 'querySinglePeer' ) ;
78
78
try {
79
79
await queryHandler . queryChaincode ( mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
@@ -175,11 +175,11 @@ describe('HLFQueryHandler', () => {
175
175
176
176
it ( 'should query a single peer' , async ( ) => {
177
177
const response = Buffer . from ( 'hello world' ) ;
178
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
178
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
179
179
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
180
180
let result = await queryHandler . querySinglePeer ( mockPeer2 , mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
181
- sinon . assert . calledOnce ( mockChannel . queryByChaincode ) ;
182
- sinon . assert . calledWith ( mockChannel . queryByChaincode , {
181
+ sinon . assert . calledOnce ( queryHandler . queryByChaincode ) ;
182
+ sinon . assert . calledWith ( queryHandler . queryByChaincode , {
183
183
chaincodeId : 'org-acme-biznet' ,
184
184
txId : mockTransactionID ,
185
185
fcn : 'myfunc' ,
@@ -191,18 +191,18 @@ describe('HLFQueryHandler', () => {
191
191
} ) ;
192
192
193
193
it ( 'should throw if no responses are returned' , ( ) => {
194
- mockChannel . queryByChaincode . resolves ( [ ] ) ;
194
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ ] ) ;
195
195
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
196
196
. should . be . rejectedWith ( / N o p a y l o a d s w e r e r e t u r n e d f r o m t h e q u e r y r e q u e s t / ) ;
197
197
} ) ;
198
198
199
199
it ( 'should return any responses that are errors and not UNAVAILABLE' , async ( ) => {
200
200
const response = new Error ( 'such error' ) ;
201
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
201
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
202
202
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
203
203
let result = await queryHandler . querySinglePeer ( mockPeer2 , mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
204
- sinon . assert . calledOnce ( mockChannel . queryByChaincode ) ;
205
- sinon . assert . calledWith ( mockChannel . queryByChaincode , {
204
+ sinon . assert . calledOnce ( queryHandler . queryByChaincode ) ;
205
+ sinon . assert . calledWith ( queryHandler . queryByChaincode , {
206
206
chaincodeId : 'org-acme-biznet' ,
207
207
txId : mockTransactionID ,
208
208
fcn : 'myfunc' ,
@@ -216,7 +216,7 @@ describe('HLFQueryHandler', () => {
216
216
it ( 'should throw any responses that are errors and code 14 being unavailable.' , ( ) => {
217
217
const response = new Error ( '14 UNAVAILABLE: Connect Failed' ) ;
218
218
response . code = 14 ;
219
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
219
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
220
220
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
221
221
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
222
222
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -225,7 +225,7 @@ describe('HLFQueryHandler', () => {
225
225
it ( 'should throw any responses that are errors and code 1 being unavailable.' , ( ) => {
226
226
const response = new Error ( '1 UNAVAILABLE: Connect Failed' ) ;
227
227
response . code = 1 ;
228
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
228
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
229
229
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
230
230
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
231
231
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -234,26 +234,144 @@ describe('HLFQueryHandler', () => {
234
234
it ( 'should throw any responses that are errors and code 4 being unavailable.' , ( ) => {
235
235
const response = new Error ( '4 UNAVAILABLE: Connect Failed' ) ;
236
236
response . code = 4 ;
237
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
237
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
238
238
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
239
239
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
240
240
. should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
241
241
} ) ;
242
242
243
243
it ( 'should throw any responses that are exceeded deadline responses.' , ( ) => {
244
244
const response = new Error ( 'Failed to connect before the deadline' ) ;
245
- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
245
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
246
246
mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
247
247
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
248
248
. should . be . rejectedWith ( / F a i l e d t o c o n n e c t b e f o r e t h e d e a d l i n e / ) ;
249
249
} ) ;
250
250
251
-
252
251
it ( 'should throw if query request fails' , ( ) => {
253
- mockChannel . queryByChaincode . rejects ( new Error ( 'Query Failed' ) ) ;
252
+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . rejects ( new Error ( 'Query Failed' ) ) ;
254
253
return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
255
254
. should . be . rejectedWith ( / Q u e r y F a i l e d / ) ;
256
255
} ) ;
257
256
} ) ;
258
257
258
+ describe ( '#queryByChaincode' , ( ) => {
259
+ it ( 'should handle single good response' , async ( ) => {
260
+ const request = {
261
+ id : 1
262
+ } ;
263
+ const results = [
264
+ [ {
265
+ response : {
266
+ payload : 'some payload'
267
+ }
268
+ } ]
269
+ ] ;
270
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
271
+ const responses = await queryHandler . queryByChaincode ( request ) ;
272
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
273
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
274
+ responses . length . should . equal ( 1 ) ;
275
+ responses [ 0 ] . should . equal ( 'some payload' ) ;
276
+ } ) ;
277
+
278
+ it ( 'should handle multiple good responses' , async ( ) => {
279
+ const request = {
280
+ id : 1
281
+ } ;
282
+ const results = [ [
283
+ {
284
+ response : {
285
+ payload : 'some payload'
286
+ }
287
+ } ,
288
+ {
289
+ response : {
290
+ payload : 'another payload'
291
+ }
292
+ } ,
293
+ {
294
+ response : {
295
+ payload : 'final payload'
296
+ }
297
+ }
298
+ ] ] ;
299
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
300
+ const responses = await queryHandler . queryByChaincode ( request ) ;
301
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
302
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
303
+ responses . length . should . equal ( 3 ) ;
304
+ responses [ 0 ] . should . equal ( 'some payload' ) ;
305
+ responses [ 1 ] . should . equal ( 'another payload' ) ;
306
+ responses [ 2 ] . should . equal ( 'final payload' ) ;
307
+ } ) ;
308
+
309
+ it ( 'should handle single error response' , async ( ) => {
310
+ const request = {
311
+ id : 1
312
+ } ;
313
+ const results = [
314
+ [ new Error ( 'some error' ) ]
315
+ ] ;
316
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
317
+ const responses = await queryHandler . queryByChaincode ( request ) ;
318
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
319
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
320
+ responses . length . should . equal ( 1 ) ;
321
+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
322
+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
323
+ } ) ;
324
+
325
+ it ( 'should handle multiple different response types' , async ( ) => {
326
+ const request = {
327
+ id : 1
328
+ } ;
329
+ const results = [ [
330
+
331
+ new Error ( 'some error' ) ,
332
+
333
+ {
334
+ response : {
335
+ payload : 'another payload'
336
+ }
337
+ } ,
338
+ {
339
+ response : 'a strange error'
340
+ } ,
341
+ {
342
+ data : 'I am not just an android'
343
+ }
344
+ ] ] ;
345
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
346
+ const responses = await queryHandler . queryByChaincode ( request ) ;
347
+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
348
+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
349
+ responses . length . should . equal ( 4 ) ;
350
+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
351
+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
352
+ responses [ 1 ] . should . equal ( 'another payload' ) ;
353
+ responses [ 2 ] . should . be . instanceOf ( Error ) ;
354
+ responses [ 3 ] . should . be . instanceOf ( Error ) ;
355
+ } ) ;
356
+
357
+ it ( 'should handle no responses' , async ( ) => {
358
+ const request = {
359
+ id : 1
360
+ } ;
361
+ let results = [ ] ;
362
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
363
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / P a y l o a d r e s u l t s a r e m i s s i n g / ) ;
364
+ results = [ 'not an array' ] ;
365
+ mockChannel . sendTransactionProposal . resolves ( results ) ;
366
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / P a y l o a d r e s u l t s a r e m i s s i n g / ) ;
367
+ } ) ;
368
+
369
+ it ( 'should handle error from sendTransactionProposal' , async ( ) => {
370
+ const request = {
371
+ id : 1
372
+ } ;
373
+ mockChannel . sendTransactionProposal . rejects ( new Error ( 'sendTxProp error' ) ) ;
374
+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / s e n d T x P r o p e r r o r / ) ;
375
+ } ) ;
376
+ } ) ;
259
377
} ) ;
0 commit comments