@@ -73,7 +73,7 @@ describe('HLFQueryHandler', () => {
7373
7474 it ( 'should not switch to another peer if peer returns a payload which is an error' , async ( ) => {
7575 const response = new Error ( 'my chaincode error' ) ;
76- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
76+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
7777 let qspSpy = sinon . spy ( queryHandler , 'querySinglePeer' ) ;
7878 try {
7979 await queryHandler . queryChaincode ( mockTransactionID , 'myfunc' , [ 'arg1' , 'arg2' ] ) ;
@@ -175,11 +175,11 @@ describe('HLFQueryHandler', () => {
175175
176176 it ( 'should query a single peer' , async ( ) => {
177177 const response = Buffer . from ( 'hello world' ) ;
178- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
178+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
179179 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
180180 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 , {
183183 chaincodeId : 'org-acme-biznet' ,
184184 txId : mockTransactionID ,
185185 fcn : 'myfunc' ,
@@ -191,18 +191,18 @@ describe('HLFQueryHandler', () => {
191191 } ) ;
192192
193193 it ( 'should throw if no responses are returned' , ( ) => {
194- mockChannel . queryByChaincode . resolves ( [ ] ) ;
194+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ ] ) ;
195195 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
196196 . 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 / ) ;
197197 } ) ;
198198
199199 it ( 'should return any responses that are errors and not UNAVAILABLE' , async ( ) => {
200200 const response = new Error ( 'such error' ) ;
201- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
201+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
202202 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
203203 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 , {
206206 chaincodeId : 'org-acme-biznet' ,
207207 txId : mockTransactionID ,
208208 fcn : 'myfunc' ,
@@ -216,7 +216,7 @@ describe('HLFQueryHandler', () => {
216216 it ( 'should throw any responses that are errors and code 14 being unavailable.' , ( ) => {
217217 const response = new Error ( '14 UNAVAILABLE: Connect Failed' ) ;
218218 response . code = 14 ;
219- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
219+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
220220 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
221221 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
222222 . should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -225,7 +225,7 @@ describe('HLFQueryHandler', () => {
225225 it ( 'should throw any responses that are errors and code 1 being unavailable.' , ( ) => {
226226 const response = new Error ( '1 UNAVAILABLE: Connect Failed' ) ;
227227 response . code = 1 ;
228- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
228+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
229229 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
230230 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
231231 . should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
@@ -234,17 +234,136 @@ describe('HLFQueryHandler', () => {
234234 it ( 'should throw any responses that are errors and code 4 being unavailable.' , ( ) => {
235235 const response = new Error ( '4 UNAVAILABLE: Connect Failed' ) ;
236236 response . code = 4 ;
237- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
237+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
238238 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
239239 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
240240 . should . be . rejectedWith ( / C o n n e c t F a i l e d / ) ;
241241 } ) ;
242242
243243 it ( 'should throw if query request fails' , ( ) => {
244- mockChannel . queryByChaincode . rejects ( new Error ( 'Query Failed' ) ) ;
244+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . rejects ( new Error ( 'Query Failed' ) ) ;
245245 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
246246 . should . be . rejectedWith ( / Q u e r y F a i l e d / ) ;
247247 } ) ;
248248 } ) ;
249249
250+ describe ( '#queryByChaincode' , ( ) => {
251+ it ( 'should handle single good response' , async ( ) => {
252+ const request = {
253+ id : 1
254+ } ;
255+ const results = [
256+ [ {
257+ response : {
258+ payload : 'some payload'
259+ }
260+ } ]
261+ ] ;
262+ mockChannel . sendTransactionProposal . resolves ( results ) ;
263+ const responses = await queryHandler . queryByChaincode ( request ) ;
264+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
265+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
266+ responses . length . should . equal ( 1 ) ;
267+ responses [ 0 ] . should . equal ( 'some payload' ) ;
268+ } ) ;
269+
270+ it ( 'should handle multiple good responses' , async ( ) => {
271+ const request = {
272+ id : 1
273+ } ;
274+ const results = [ [
275+ {
276+ response : {
277+ payload : 'some payload'
278+ }
279+ } ,
280+ {
281+ response : {
282+ payload : 'another payload'
283+ }
284+ } ,
285+ {
286+ response : {
287+ payload : 'final payload'
288+ }
289+ }
290+ ] ] ;
291+ mockChannel . sendTransactionProposal . resolves ( results ) ;
292+ const responses = await queryHandler . queryByChaincode ( request ) ;
293+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
294+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
295+ responses . length . should . equal ( 3 ) ;
296+ responses [ 0 ] . should . equal ( 'some payload' ) ;
297+ responses [ 1 ] . should . equal ( 'another payload' ) ;
298+ responses [ 2 ] . should . equal ( 'final payload' ) ;
299+ } ) ;
300+
301+ it ( 'should handle single error response' , async ( ) => {
302+ const request = {
303+ id : 1
304+ } ;
305+ const results = [
306+ [ new Error ( 'some error' ) ]
307+ ] ;
308+ mockChannel . sendTransactionProposal . resolves ( results ) ;
309+ const responses = await queryHandler . queryByChaincode ( request ) ;
310+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
311+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
312+ responses . length . should . equal ( 1 ) ;
313+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
314+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
315+ } ) ;
316+
317+ it ( 'should handle multiple different response types' , async ( ) => {
318+ const request = {
319+ id : 1
320+ } ;
321+ const results = [ [
322+
323+ new Error ( 'some error' ) ,
324+
325+ {
326+ response : {
327+ payload : 'another payload'
328+ }
329+ } ,
330+ {
331+ response : 'a strange error'
332+ } ,
333+ {
334+ data : 'I am not just an android'
335+ }
336+ ] ] ;
337+ mockChannel . sendTransactionProposal . resolves ( results ) ;
338+ const responses = await queryHandler . queryByChaincode ( request ) ;
339+ sinon . assert . calledOnce ( mockChannel . sendTransactionProposal ) ;
340+ sinon . assert . calledWith ( mockChannel . sendTransactionProposal , request ) ;
341+ responses . length . should . equal ( 4 ) ;
342+ responses [ 0 ] . should . be . instanceOf ( Error ) ;
343+ responses [ 0 ] . message . should . equal ( 'some error' ) ;
344+ responses [ 1 ] . should . equal ( 'another payload' ) ;
345+ responses [ 2 ] . should . be . instanceOf ( Error ) ;
346+ responses [ 3 ] . should . be . instanceOf ( Error ) ;
347+ } ) ;
348+
349+ it ( 'should handle no responses' , async ( ) => {
350+ const request = {
351+ id : 1
352+ } ;
353+ let results = [ ] ;
354+ mockChannel . sendTransactionProposal . resolves ( results ) ;
355+ 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 / ) ;
356+ results = [ 'not an array' ] ;
357+ mockChannel . sendTransactionProposal . resolves ( results ) ;
358+ 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 / ) ;
359+ } ) ;
360+
361+ it ( 'should handle error from sendTransactionProposal' , async ( ) => {
362+ const request = {
363+ id : 1
364+ } ;
365+ mockChannel . sendTransactionProposal . rejects ( new Error ( 'sendTxProp error' ) ) ;
366+ await queryHandler . queryByChaincode ( request ) . should . be . rejectedWith ( / s e n d T x P r o p e r r o r / ) ;
367+ } ) ;
368+ } ) ;
250369} ) ;
0 commit comments