|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package ttx |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-smart-client/platform/view/view" |
| 13 | + "github.com/hyperledger-labs/fabric-token-sdk/token" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestRecipients_Identities(t *testing.T) { |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + input Recipients |
| 22 | + expected []view.Identity |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "empty recipients", |
| 26 | + input: Recipients{}, |
| 27 | + expected: []view.Identity{}, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "single recipient", |
| 31 | + input: Recipients{ |
| 32 | + {Identity: view.Identity("alice")}, |
| 33 | + }, |
| 34 | + expected: []view.Identity{view.Identity("alice")}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "multiple recipients preserve order", |
| 38 | + input: Recipients{ |
| 39 | + {Identity: view.Identity("alice")}, |
| 40 | + {Identity: view.Identity("bob")}, |
| 41 | + {Identity: view.Identity("charlie")}, |
| 42 | + }, |
| 43 | + expected: []view.Identity{ |
| 44 | + view.Identity("alice"), |
| 45 | + view.Identity("bob"), |
| 46 | + view.Identity("charlie"), |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + for _, tc := range tests { |
| 51 | + t.Run(tc.name, func(t *testing.T) { |
| 52 | + got := tc.input.Identities() |
| 53 | + assert.Equal(t, tc.expected, got) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestExchangeRecipientRequest_BytesRoundTrip(t *testing.T) { |
| 59 | + original := &ExchangeRecipientRequest{ |
| 60 | + TMSID: token.TMSID{Network: "net1", Channel: "ch1", Namespace: "ns1"}, |
| 61 | + WalletID: []byte("my-wallet"), |
| 62 | + RecipientData: &RecipientData{ |
| 63 | + Identity: view.Identity("alice"), |
| 64 | + AuditInfo: []byte("audit-info"), |
| 65 | + TokenMetadata: []byte("token-meta"), |
| 66 | + TokenMetadataAuditInfo: []byte("meta-audit"), |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + raw, err := original.Bytes() |
| 71 | + require.NoError(t, err) |
| 72 | + require.NotEmpty(t, raw) |
| 73 | + |
| 74 | + decoded := &ExchangeRecipientRequest{} |
| 75 | + require.NoError(t, decoded.FromBytes(raw)) |
| 76 | + |
| 77 | + assert.Equal(t, original.TMSID, decoded.TMSID) |
| 78 | + assert.Equal(t, original.WalletID, decoded.WalletID) |
| 79 | + require.NotNil(t, decoded.RecipientData) |
| 80 | + assert.Equal(t, original.RecipientData.Identity, decoded.RecipientData.Identity) |
| 81 | + assert.Equal(t, original.RecipientData.AuditInfo, decoded.RecipientData.AuditInfo) |
| 82 | + assert.Equal(t, original.RecipientData.TokenMetadata, decoded.RecipientData.TokenMetadata) |
| 83 | + assert.Equal(t, original.RecipientData.TokenMetadataAuditInfo, decoded.RecipientData.TokenMetadataAuditInfo) |
| 84 | +} |
| 85 | + |
| 86 | +func TestExchangeRecipientRequest_FromBytes_InvalidInput(t *testing.T) { |
| 87 | + r := &ExchangeRecipientRequest{} |
| 88 | + err := r.FromBytes([]byte("not valid json {{")) |
| 89 | + require.Error(t, err) |
| 90 | +} |
| 91 | + |
| 92 | +func TestRecipientRequest_BytesRoundTrip(t *testing.T) { |
| 93 | + original := &RecipientRequest{ |
| 94 | + TMSID: token.TMSID{Network: "net2", Channel: "ch2", Namespace: "ns2"}, |
| 95 | + WalletID: []byte("wallet-id"), |
| 96 | + RecipientData: &RecipientData{ |
| 97 | + Identity: view.Identity("bob"), |
| 98 | + AuditInfo: []byte("bob-audit"), |
| 99 | + }, |
| 100 | + MultiSig: true, |
| 101 | + } |
| 102 | + |
| 103 | + raw, err := original.Bytes() |
| 104 | + require.NoError(t, err) |
| 105 | + require.NotEmpty(t, raw) |
| 106 | + |
| 107 | + decoded := &RecipientRequest{} |
| 108 | + require.NoError(t, decoded.FromBytes(raw)) |
| 109 | + |
| 110 | + assert.Equal(t, original.TMSID, decoded.TMSID) |
| 111 | + assert.Equal(t, original.WalletID, decoded.WalletID) |
| 112 | + assert.Equal(t, original.MultiSig, decoded.MultiSig) |
| 113 | + require.NotNil(t, decoded.RecipientData) |
| 114 | + assert.Equal(t, original.RecipientData.Identity, decoded.RecipientData.Identity) |
| 115 | + assert.Equal(t, original.RecipientData.AuditInfo, decoded.RecipientData.AuditInfo) |
| 116 | +} |
| 117 | + |
| 118 | +func TestRecipientRequest_FromBytes_InvalidInput(t *testing.T) { |
| 119 | + r := &RecipientRequest{} |
| 120 | + err := r.FromBytes([]byte("not valid json {{")) |
| 121 | + require.Error(t, err) |
| 122 | +} |
| 123 | + |
| 124 | +func TestGetRecipientData(t *testing.T) { |
| 125 | + t.Run("nil params map returns nil", func(t *testing.T) { |
| 126 | + opts := &token.ServiceOptions{} |
| 127 | + assert.Nil(t, getRecipientData(opts)) |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("missing key returns nil", func(t *testing.T) { |
| 131 | + opts := &token.ServiceOptions{ |
| 132 | + Params: map[string]interface{}{ |
| 133 | + "SomeOtherKey": "value", |
| 134 | + }, |
| 135 | + } |
| 136 | + assert.Nil(t, getRecipientData(opts)) |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("key present returns recipient data", func(t *testing.T) { |
| 140 | + rd := &RecipientData{ |
| 141 | + Identity: view.Identity("alice"), |
| 142 | + AuditInfo: []byte("audit"), |
| 143 | + } |
| 144 | + opts := &token.ServiceOptions{ |
| 145 | + Params: map[string]interface{}{ |
| 146 | + "RecipientData": rd, |
| 147 | + }, |
| 148 | + } |
| 149 | + assert.Equal(t, rd, getRecipientData(opts)) |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +func TestGetRecipientWalletID(t *testing.T) { |
| 154 | + t.Run("nil params map returns empty string", func(t *testing.T) { |
| 155 | + opts := &token.ServiceOptions{} |
| 156 | + assert.Empty(t, getRecipientWalletID(opts)) |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("missing key returns empty string", func(t *testing.T) { |
| 160 | + opts := &token.ServiceOptions{ |
| 161 | + Params: map[string]interface{}{ |
| 162 | + "SomeOtherKey": "value", |
| 163 | + }, |
| 164 | + } |
| 165 | + assert.Empty(t, getRecipientWalletID(opts)) |
| 166 | + }) |
| 167 | + |
| 168 | + t.Run("key present returns wallet id", func(t *testing.T) { |
| 169 | + opts := &token.ServiceOptions{ |
| 170 | + Params: map[string]interface{}{ |
| 171 | + "RecipientWalletID": "my-wallet-id", |
| 172 | + }, |
| 173 | + } |
| 174 | + assert.Equal(t, "my-wallet-id", getRecipientWalletID(opts)) |
| 175 | + }) |
| 176 | +} |
0 commit comments