1414
1515package org .hyperledger .fabric .sdk ;
1616
17+ //Allow throwing undeclared checked execeptions in mock code.
18+ //CHECKSTYLE.OFF: IllegalImport
19+
20+ import java .lang .reflect .Field ;
1721import java .util .Arrays ;
1822import java .util .Collection ;
1923import java .util .concurrent .CompletableFuture ;
2024
25+ import com .google .common .util .concurrent .ListenableFuture ;
26+ import com .google .common .util .concurrent .SettableFuture ;
27+ import io .grpc .Status ;
28+ import io .grpc .StatusRuntimeException ;
2129import org .hyperledger .fabric .protos .common .Common ;
2230import org .hyperledger .fabric .protos .orderer .Ab ;
31+ import org .hyperledger .fabric .protos .peer .FabricProposal ;
32+ import org .hyperledger .fabric .protos .peer .FabricProposalResponse ;
2333import org .hyperledger .fabric .sdk .exception .InvalidArgumentException ;
34+ import org .hyperledger .fabric .sdk .exception .PeerException ;
2435import org .hyperledger .fabric .sdk .exception .ProposalException ;
2536import org .hyperledger .fabric .sdk .exception .TransactionException ;
2637import org .junit .Assert ;
2738import org .junit .BeforeClass ;
2839import org .junit .Rule ;
2940import org .junit .Test ;
3041import org .junit .rules .ExpectedException ;
42+ import sun .misc .Unsafe ;
3143
3244import static org .hyperledger .fabric .sdk .testutils .TestUtils .setField ;
3345
46+ //CHECKSTYLE.ON: IllegalImport
47+
48+
49+
50+
3451public class ChannelTest {
3552 private static HFClient hfclient = null ;
3653 private static Channel shutdownChannel = null ;
@@ -258,7 +275,6 @@ protected void parseConfigBlock() {
258275
259276 @ Test
260277 public void testChannelShutdown () {
261- Channel testChannel = null ;
262278
263279 try {
264280
@@ -267,7 +283,7 @@ public void testChannelShutdown() {
267283 } catch (Exception e ) {
268284
269285 Assert .assertTrue (e .getClass () == InvalidArgumentException .class );
270- Assert .assertTrue (testChannel .isInitialized ());
286+ Assert .assertTrue (shutdownChannel .isInitialized ());
271287 }
272288
273289 }
@@ -605,4 +621,130 @@ public void testChannelQueryTransactionByIDNull() throws Exception {
605621
606622 }
607623
624+ @ Test
625+ public void testQueryInstalledChaincodesThrowInterrupted () throws Exception {
626+
627+ thrown .expect (ProposalException .class );
628+ thrown .expectMessage ("You interrupting me?" );
629+
630+ final Channel channel = createRunningChannel (null );
631+ Peer peer = channel .getPeers ().iterator ().next ();
632+
633+ setField (peer , "endorserClent" , new MockEndorserClient (new InterruptedException ("You interrupting me?" )));
634+
635+ hfclient .queryChannels (peer );
636+
637+ }
638+
639+ @ Test
640+ public void testQueryInstalledChaincodesThrowPeerException () throws Exception {
641+
642+ thrown .expect (ProposalException .class );
643+ thrown .expectMessage ("rick did this:)" );
644+
645+ final Channel channel = createRunningChannel (null );
646+ Peer peer = channel .getPeers ().iterator ().next ();
647+
648+ setField (peer , "endorserClent" , new MockEndorserClient (new PeerException ("rick did this:)" )));
649+
650+ hfclient .queryChannels (peer );
651+
652+ }
653+
654+ @ Test
655+ public void testQueryInstalledChaincodesThrowTimeoutException () throws Exception {
656+
657+ thrown .expect (ProposalException .class );
658+ thrown .expectMessage ("What time is it?" );
659+
660+ final Channel channel = createRunningChannel (null );
661+ Peer peer = channel .getPeers ().iterator ().next ();
662+
663+ setField (peer , "endorserClent" , new MockEndorserClient (new PeerException ("What time is it?" )));
664+
665+ hfclient .queryChannels (peer );
666+
667+ }
668+
669+ @ Test
670+ public void testQueryInstalledChaincodesERROR () throws Exception {
671+
672+ thrown .expect (Error .class );
673+ thrown .expectMessage ("Error bad bad bad" );
674+
675+ final Channel channel = createRunningChannel (null );
676+ Peer peer = channel .getPeers ().iterator ().next ();
677+
678+ final SettableFuture <FabricProposalResponse .ProposalResponse > settableFuture = SettableFuture .create ();
679+ settableFuture .setException (new Error ("Error bad bad bad" ));
680+ setField (peer , "endorserClent" , new MockEndorserClient (settableFuture ));
681+
682+ hfclient .queryChannels (peer );
683+
684+ }
685+
686+ @ Test
687+ public void testQueryInstalledChaincodesStatusRuntimeException () throws Exception {
688+
689+ thrown .expect (ProposalException .class );
690+ thrown .expectMessage ("ABORTED" );
691+
692+ final Channel channel = createRunningChannel (null );
693+ Peer peer = channel .getPeers ().iterator ().next ();
694+
695+ final SettableFuture <FabricProposalResponse .ProposalResponse > settableFuture = SettableFuture .create ();
696+ settableFuture .setException (new StatusRuntimeException (Status .ABORTED ));
697+ setField (peer , "endorserClent" , new MockEndorserClient (settableFuture ));
698+
699+ hfclient .queryChannels (peer );
700+
701+ }
702+
703+ class MockEndorserClient extends EndorserClient {
704+ final Throwable throwThis ;
705+ private final ListenableFuture <FabricProposalResponse .ProposalResponse > returnedFuture ;
706+
707+ MockEndorserClient (Throwable throwThis ) {
708+ super (new Endpoint ("grpc://loclhost:99" , null ).getChannelBuilder ());
709+ if (throwThis == null ) {
710+ throw new IllegalArgumentException ("Can't throw a null!" );
711+ }
712+ this .throwThis = throwThis ;
713+ this .returnedFuture = null ;
714+ }
715+
716+ MockEndorserClient (ListenableFuture <FabricProposalResponse .ProposalResponse > returnedFuture ) {
717+ super (new Endpoint ("grpc://loclhost:99" , null ).getChannelBuilder ());
718+ this .throwThis = null ;
719+ this .returnedFuture = returnedFuture ;
720+ }
721+
722+ @ Override
723+ public ListenableFuture <FabricProposalResponse .ProposalResponse > sendProposalAsync (FabricProposal .SignedProposal proposal ) throws PeerException {
724+ if (throwThis != null ) {
725+ getUnsafe ().throwException (throwThis );
726+ }
727+ return returnedFuture ;
728+
729+ }
730+
731+ @ Override
732+ public boolean isChannelActive () {
733+
734+ return true ;
735+
736+ }
737+
738+ private Unsafe getUnsafe () { //lets us throw undeclared exceptions.
739+ try {
740+ Field field = Unsafe .class .getDeclaredField ("theUnsafe" );
741+ field .setAccessible (true );
742+ return (Unsafe ) field .get (null );
743+ } catch (Exception e ) {
744+ throw new RuntimeException (e );
745+ }
746+ }
747+ }
748+
749+
608750}
0 commit comments