Skip to content

Commit

Permalink
[FAB-7862] Add WithConfig helper method to fabsdk
Browse files Browse the repository at this point in the history
For those that want to directly provide the underlying Config, this
new helper converts a Config to a ConfigProvider.

Example: sdk, err := fabsdk.New(fabsdk.WithConfig(c))

Change-Id: I5d6b370163bc80388f72d89766c197a3c19908f4
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jan 23, 2018
1 parent 40b253e commit 48a3c93
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/fabsdk/client_test.go
Expand Up @@ -40,7 +40,7 @@ func TestFromConfigGoodClientOpt(t *testing.T) {
t.Fatalf("Unexpected error from config: %v", err)
}

sdk, err := fromConfig(c)
sdk, err := New(WithConfig(c))
if err != nil {
t.Fatalf("Expected no error from New, but got %v", err)
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/fabsdk/fabsdk.go
Expand Up @@ -45,7 +45,7 @@ type options struct {
type Option func(opts *options) error

// New initializes the SDK based on the set of options provided.
// configProvider provides the application configuration and is required.
// configProvider provides the application configuration.
func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) {
pkgSuite := defPkgSuite{}
config, err := cp()
Expand All @@ -55,11 +55,13 @@ func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) {
return fromPkgSuite(config, &pkgSuite, opts...)
}

// fromConfig initializes the SDK based on the set of options provided.
// configProvider provides the application configuration and is required.
func fromConfig(config apiconfig.Config, opts ...Option) (*FabricSDK, error) {
pkgSuite := defPkgSuite{}
return fromPkgSuite(config, &pkgSuite, opts...)
// WithConfig converts a Config interface to a ConfigProvider.
// This is a helper function for those who already loaded the config
// prior to instantiating the SDK.
func WithConfig(config apiconfig.Config) apiconfig.ConfigProvider {
return func() (apiconfig.Config, error) {
return config, nil
}
}

// fromPkgSuite creates an SDK based on the implementations in the provided pkg suite.
Expand Down
36 changes: 27 additions & 9 deletions pkg/fabsdk/fabsdk_test.go
Expand Up @@ -58,8 +58,12 @@ func TestNewDefaultSDK(t *testing.T) {
t.Fatalf("Error initializing SDK: %s", err)
}

verifySDK(t, sdk)
}

func verifySDK(t *testing.T, sdk *FabricSDK) {
// Default channel client (uses organisation from client configuration)
_, err = sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel")
_, err := sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
Expand All @@ -78,7 +82,21 @@ func TestNewDefaultSDK(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
}

func TestWithConfigOpt(t *testing.T) {
// Test New SDK with valid config file
c, err := configImpl.FromFile(sdkConfigFile)()
if err != nil {
t.Fatalf("Unexpected error from config: %v", err)
}

sdk, err := New(WithConfig(c))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}

verifySDK(t, sdk)
}

func TestWithCorePkg(t *testing.T) {
Expand All @@ -88,7 +106,7 @@ func TestWithCorePkg(t *testing.T) {
t.Fatalf("Unexpected error from config: %v", err)
}

_, err = fromConfig(c)
_, err = New(WithConfig(c))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -102,7 +120,7 @@ func TestWithCorePkg(t *testing.T) {
factory.EXPECT().NewSigningManager(nil, c).Return(nil, nil)
factory.EXPECT().NewFabricProvider(c, nil, nil, nil).Return(nil, nil)

_, err = fromConfig(c, WithCorePkg(factory))
_, err = New(WithConfig(c), WithCorePkg(factory))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -115,7 +133,7 @@ func TestWithServicePkg(t *testing.T) {
t.Fatalf("Unexpected error from config: %v", err)
}

_, err = fromConfig(c)
_, err = New(WithConfig(c))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -127,7 +145,7 @@ func TestWithServicePkg(t *testing.T) {
factory.EXPECT().NewDiscoveryProvider(c).Return(nil, nil)
factory.EXPECT().NewSelectionProvider(c).Return(nil, nil)

_, err = fromConfig(c, WithServicePkg(factory))
_, err = New(WithConfig(c), WithServicePkg(factory))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -145,7 +163,7 @@ func TestWithContextPkg(t *testing.T) {
t.Fatalf("Error initializing core factory: %s", err)
}

_, err = fromConfig(c)
sdk, err := New(WithConfig(c))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -169,7 +187,7 @@ func TestWithContextPkg(t *testing.T) {

factory.EXPECT().NewCredentialManager(sdkValidClientOrg1, c, core.cryptoSuite).Return(cm, nil)

sdk, err := fromConfig(c, WithCorePkg(core), WithContextPkg(factory))
sdk, err = New(WithConfig(c), WithCorePkg(core), WithContextPkg(factory))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -193,7 +211,7 @@ func TestWithSessionPkg(t *testing.T) {
t.Fatalf("Error initializing core factory: %s", err)
}

_, err = fromConfig(c)
_, err = New(WithConfig(c))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand All @@ -203,7 +221,7 @@ func TestWithSessionPkg(t *testing.T) {
defer mockCtrl.Finish()
factory := mockapisdk.NewMockSessionClientFactory(mockCtrl)

sdk, err := fromConfig(c, WithCorePkg(core), WithSessionPkg(factory))
sdk, err := New(WithConfig(c), WithCorePkg(core), WithSessionPkg(factory))
if err != nil {
t.Fatalf("Error initializing SDK: %s", err)
}
Expand Down

0 comments on commit 48a3c93

Please sign in to comment.