@@ -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,26 +234,144 @@ 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 any responses that are exceeded deadline responses.' , ( ) => {
244244 const response = new Error ( 'Failed to connect before the deadline' ) ;
245- mockChannel . queryByChaincode . resolves ( [ response ] ) ;
245+ sandbox . stub ( queryHandler , ' queryByChaincode' ) . resolves ( [ response ] ) ;
246246 mockConnection . businessNetworkIdentifier = 'org-acme-biznet' ;
247247 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
248248 . 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 / ) ;
249249 } ) ;
250250
251-
252251 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' ) ) ;
254253 return queryHandler . querySinglePeer ( mockPeer2 , 'txid' , 'myfunc' , [ 'arg1' , 'arg2' ] )
255254 . should . be . rejectedWith ( / Q u e r y F a i l e d / ) ;
256255 } ) ;
257256 } ) ;
258257
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+ } ) ;
259377} ) ;
0 commit comments