From 99129bc83969ca1494a7f3846388b79c9baca6ca Mon Sep 17 00:00:00 2001 From: Bret Harrison Date: Tue, 24 Oct 2017 18:12:37 -0400 Subject: [PATCH] [FAB-6709] NodeSDK get default channel When using a connection profile it would be nice to not have to know the name of the single channel that may be in use. The getChannel() method will be enhanced to return the first channel on the list. Change-Id: Iaac0e6d2dd9d4d2235c26b3b62781cbc19eae1c0 Signed-off-by: Bret Harrison --- fabric-client/lib/Client.js | 14 +++++++++++--- test/unit/network-config.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/fabric-client/lib/Client.js b/fabric-client/lib/Client.js index c24cb3d4e4..8a3db4dea8 100644 --- a/fabric-client/lib/Client.js +++ b/fabric-client/lib/Client.js @@ -181,9 +181,11 @@ var Client = class extends BaseClient { * will be created and populated with {@link Orderer} objects and {@link Peer} objects * as defined in the network configuration. * - * @param {string} name - The name of the channel. - * @param {boolean} throwError - Indicates if this method will throw an error if the channel - * is not found. Default is true. + * @param {string} name - Optional. The name of the channel. When omitted the + * first channel defined in the loaded network configuration will be + * returned + * @param {boolean} throwError - Indicates if this method will throw an error + * if the channel is not found. Default is true. * @returns {Channel} The channel instance */ getChannel(name, throwError) { @@ -194,6 +196,12 @@ var Client = class extends BaseClient { else { // maybe it is defined in the network config if(this._network_config) { + if(!name) { + let channel_names = Object.keys(this._network_config._network_config.channels); + if(channel_names) { + name = channel_names[0]; + } + } channel = this._network_config.getChannel(name); this._channels[name] = channel; } diff --git a/test/unit/network-config.js b/test/unit/network-config.js index ac99c98e7c..74a0d6eec8 100644 --- a/test/unit/network-config.js +++ b/test/unit/network-config.js @@ -163,6 +163,12 @@ test('\n\n ** configuration testing **\n\n', function (t) { t.equals(orderer._options['request-timeout'],30000, ' check that we get this orderer timeout set'); let eventHub = client._network_config.getEventHub('peer0.org1.example.com'); t.equals(eventHub._ep._options['request-timeout'],3000, ' check that we get this eventHub timeout set'); + let first_channel = client.getChannel(); + if(first_channel && first_channel.getName() === 'mychannel2') { + t.pass('Successfully got the first channel without specifying the name'); + } else { + t.fail('Failed to get the first channel'); + } delete client._network_config._network_config.certificateAuthorities['ca-org1'].tlsCACerts; delete client._network_config._network_config.certificateAuthorities['ca-org1'].httpOptions; @@ -179,6 +185,15 @@ test('\n\n ** configuration testing **\n\n', function (t) { '2 Should be able to run a number of test without error' ); + t.throws( + () => { + var client = new Client(); + client.getChannel(); + }, + /Channel not found for name undefined./, + 'Check for Channel not found for name undefined.' + ); + t.doesNotThrow( () => { var config_loc = path.resolve('test/fixtures/network.yaml');