|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package config |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + fscconfig "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/config" |
| 13 | + "github.com/hyperledger-labs/fabric-token-sdk/token" |
| 14 | + "github.com/hyperledger-labs/fabric-token-sdk/token/driver" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func newTestConfiguration(t *testing.T) *Configuration { |
| 20 | + t.Helper() |
| 21 | + cp, err := fscconfig.NewProvider("./testdata/token0") |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + return NewConfiguration(cp, "n1c1ns1", driver.TMSID{ |
| 25 | + Network: "n1", |
| 26 | + Channel: "c1", |
| 27 | + Namespace: "ns1", |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func TestConfiguration_ID(t *testing.T) { |
| 32 | + c := newTestConfiguration(t) |
| 33 | + id := c.ID() |
| 34 | + assert.Equal(t, "n1", id.Network) |
| 35 | + assert.Equal(t, "c1", id.Channel) |
| 36 | + assert.Equal(t, "ns1", id.Namespace) |
| 37 | +} |
| 38 | + |
| 39 | +func TestConfiguration_Validate_Valid(t *testing.T) { |
| 40 | + c := newTestConfiguration(t) |
| 41 | + assert.NoError(t, c.Validate()) |
| 42 | +} |
| 43 | + |
| 44 | +func TestConfiguration_Validate_MissingNetwork(t *testing.T) { |
| 45 | + cp, err := fscconfig.NewProvider("./testdata/token0") |
| 46 | + require.NoError(t, err) |
| 47 | + c := NewConfiguration(cp, "n1c1ns1", driver.TMSID{ |
| 48 | + Channel: "c1", |
| 49 | + Namespace: "ns1", |
| 50 | + }) |
| 51 | + assert.Error(t, c.Validate()) |
| 52 | +} |
| 53 | + |
| 54 | +func TestConfiguration_Validate_MissingNamespace(t *testing.T) { |
| 55 | + cp, err := fscconfig.NewProvider("./testdata/token0") |
| 56 | + require.NoError(t, err) |
| 57 | + c := NewConfiguration(cp, "n1c1ns1", driver.TMSID{ |
| 58 | + Network: "n1", |
| 59 | + Channel: "c1", |
| 60 | + }) |
| 61 | + assert.Error(t, c.Validate()) |
| 62 | +} |
| 63 | + |
| 64 | +func TestConfiguration_GetString(t *testing.T) { |
| 65 | + c := newTestConfiguration(t) |
| 66 | + val := c.GetString("network") |
| 67 | + assert.Equal(t, "n1", val) |
| 68 | +} |
| 69 | + |
| 70 | +func TestConfiguration_GetBool(t *testing.T) { |
| 71 | + cp, err := fscconfig.NewProvider("./testdata/token0") |
| 72 | + require.NoError(t, err) |
| 73 | + c := NewConfiguration(cp, "n1c1ns1", driver.TMSID{ |
| 74 | + Network: "n1", |
| 75 | + Channel: "c1", |
| 76 | + Namespace: "ns1", |
| 77 | + }) |
| 78 | + // non-existent bool key returns false |
| 79 | + assert.False(t, c.GetBool("nonexistent")) |
| 80 | +} |
| 81 | + |
| 82 | +func TestConfiguration_IsSet(t *testing.T) { |
| 83 | + c := newTestConfiguration(t) |
| 84 | + assert.True(t, c.IsSet("network")) |
| 85 | + assert.False(t, c.IsSet("nonexistent_key_xyz")) |
| 86 | +} |
| 87 | + |
| 88 | +func TestConfiguration_TranslatePath(t *testing.T) { |
| 89 | + c := newTestConfiguration(t) |
| 90 | + result := c.TranslatePath("some/path") |
| 91 | + assert.NotEmpty(t, result) |
| 92 | +} |
| 93 | + |
| 94 | +func TestConfiguration_Serialize(t *testing.T) { |
| 95 | + c := newTestConfiguration(t) |
| 96 | + raw, err := c.Serialize(token.TMSID{ |
| 97 | + Network: "new_network", |
| 98 | + Channel: "new_channel", |
| 99 | + Namespace: "new_namespace", |
| 100 | + }) |
| 101 | + require.NoError(t, err) |
| 102 | + assert.NotEmpty(t, raw) |
| 103 | + assert.Contains(t, string(raw), "new_network") |
| 104 | + assert.Contains(t, string(raw), "new_channel") |
| 105 | + assert.Contains(t, string(raw), "new_namespace") |
| 106 | +} |
0 commit comments