import { FabricGatewayConnectionManager } from '../../extension/fabric/FabricGatewayConnectionManager' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as path from 'path' ; import { FabricWalletRegistryEntry , FabricGatewayRegistryEntry } from 'ibm-blockchain-platform-common' ; import { FabricGatewayConnection } from 'ibm-blockchain-platform-gateway-v1' ; const should = chai . should ( ) ; describe ( 'FabricGatewayConnectionManager' , ( ) => { const connectionManager = FabricGatewayConnectionManager . instance ( ) ; let mockFabricConnection ; let sandbox ; let registryEntry ; let walletRegistryEntry ; let fromEnvGateway ; beforeEach ( async ( ) => { sandbox = sinon . createSandbox ( ) ; mockFabricConnection = sandbox . createStubInstance ( FabricGatewayConnection ) ; connectionManager [ 'connection' ] = null ; registryEntry = new FabricGatewayRegistryEntry ( ) ; registryEntry . name = 'myConnection' ; walletRegistryEntry = new FabricWalletRegistryEntry ( { name : 'congaWallet' , walletPath : '/some/path' } ) ; fromEnvGateway = new FabricGatewayRegistryEntry ( { name : 'myGateway' , associatedWallet : '' , fromEnvironment : 'myEnvironment' , connectionProfilePath : path . join ( 'blockchain' , 'extension' , 'directory' , 'gatewayOne' , 'connection.json' ) } ) ; } ) ; afterEach ( async ( ) => { sandbox . restore ( ) ; connectionManager [ 'connection' ] = null ; } ) ; describe ( '#getConnection' , ( ) => { it ( 'should get the connection' , ( ) => { connectionManager [ 'connection' ] = ( ( mockFabricConnection as any ) as FabricGatewayConnection ) ; connectionManager . getConnection ( ) . should . equal ( mockFabricConnection ) ; } ) ; } ) ; describe ( '#getGatewayRegistryEntry' , ( ) => { it ( 'should get the registry entry' , async ( ) => { connectionManager [ 'gatewayRegistryEntry' ] = registryEntry ; connectionManager . getGatewayRegistryEntry ( ) . should . eventually . equal ( registryEntry ) ; } ) ; it ( 'should return undefined if the registry entry is not present' , async ( ) => { connectionManager [ 'gatewayRegistryEntry' ] = undefined ; const result = await connectionManager . getGatewayRegistryEntry ( ) ; should . equal ( result , undefined ) ; } ) ; it ( 'should get the registry entry from a config file' , async ( ) => { const rootPath = path . dirname ( __dirname ) ; const connectionProfilePath = path . join ( rootPath , '../../test/data/gatewayWithConfig/myOrg.json' ) ; fromEnvGateway . connectionProfilePath = connectionProfilePath ; connectionManager [ 'gatewayRegistryEntry' ] = fromEnvGateway ; connectionManager . getGatewayRegistryEntry ( ) . should . eventually . deep . equal ( { name : 'myGatewayWithConfig' , associatedWallet : '' , fromEnvironment : 'myEnvironemt' , transactionDataDirectories : [ ] } ) ; } ) ; it ( 'should not try to get a registry entry from a config file if there isn\'t one' , async ( ) => { fromEnvGateway . connectionProfilePath = '' ; connectionManager [ 'gatewayRegistryEntry' ] = fromEnvGateway ; connectionManager . getGatewayRegistryEntry ( ) . should . eventually . equal ( fromEnvGateway ) ; } ) ; } ) ; describe ( '#connect' , ( ) => { it ( 'should store the connection and emit an event' , ( ) => { const listenerStub = sandbox . stub ( ) ; connectionManager . once ( 'connected' , listenerStub ) ; connectionManager . connect ( ( mockFabricConnection as any ) as FabricGatewayConnection , registryEntry , walletRegistryEntry ) ; connectionManager . getConnection ( ) . should . equal ( mockFabricConnection ) ; connectionManager . getGatewayRegistryEntry ( ) . should . eventually . equal ( registryEntry ) ; listenerStub . should . have . been . calledOnceWithExactly ( mockFabricConnection ) ; } ) ; } ) ; describe ( '#disconnect' , ( ) => { it ( 'should clear the connection and emit an event' , ( ) => { const listenerStub = sandbox . stub ( ) ; connectionManager . once ( 'disconnected' , listenerStub ) ; connectionManager . disconnect ( ) ; should . equal ( connectionManager . getConnection ( ) , null ) ; listenerStub . should . have . been . calledOnceWithExactly ( ) ; } ) ; } ) ; describe ( '#getConnectionIdentity' , ( ) => { it ( 'should get the name of the identity used to connect' , ( ) => { mockFabricConnection . identityName = 'admin@conga' ; connectionManager [ 'connection' ] = ( ( mockFabricConnection as any ) as FabricGatewayConnection ) ; connectionManager . getConnectionIdentity ( ) . should . equal ( 'admin@conga' ) ; } ) ; } ) ; describe ( '#getConnectionWallet' , ( ) => { it ( 'should get the wallet registry entry used to connect' , ( ) => { connectionManager [ 'walletRegistryEntry' ] = walletRegistryEntry ; connectionManager . getConnectionWallet ( ) . should . deep . equal ( walletRegistryEntry ) ; } ) ; } ) ; } ) ;