|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package common |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-token-sdk/token/core/common/meta" |
| 13 | + "github.com/hyperledger-labs/fabric-token-sdk/token/driver/mock" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestTransferApplicationDataValidate(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + err bool |
| 21 | + errMsg string |
| 22 | + context func() (*TestContext, TestCheck) |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "no metadata", |
| 26 | + err: false, |
| 27 | + context: func() (*TestContext, TestCheck) { |
| 28 | + pp := &mock.PublicParameters{} |
| 29 | + pp.AuditorsReturns(nil) |
| 30 | + action := &mock.TransferAction{} |
| 31 | + action.GetMetadataReturns(nil) |
| 32 | + ctx := &TestContext{ |
| 33 | + PP: pp, |
| 34 | + TransferAction: action, |
| 35 | + } |
| 36 | + return ctx, func() bool { |
| 37 | + return len(ctx.MetadataCounter) == 0 |
| 38 | + } |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "one metadata without the selected prefix", |
| 43 | + err: false, |
| 44 | + context: func() (*TestContext, TestCheck) { |
| 45 | + pp := &mock.PublicParameters{} |
| 46 | + pp.AuditorsReturns(nil) |
| 47 | + action := &mock.TransferAction{} |
| 48 | + action.GetMetadataReturns(map[string][]byte{ |
| 49 | + "key1": []byte("value1"), |
| 50 | + }) |
| 51 | + ctx := &TestContext{ |
| 52 | + PP: pp, |
| 53 | + TransferAction: action, |
| 54 | + } |
| 55 | + return ctx, func() bool { |
| 56 | + return len(ctx.MetadataCounter) == 0 |
| 57 | + } |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "one metadata with the selected prefix", |
| 62 | + err: false, |
| 63 | + context: func() (*TestContext, TestCheck) { |
| 64 | + pp := &mock.PublicParameters{} |
| 65 | + pp.AuditorsReturns(nil) |
| 66 | + action := &mock.TransferAction{} |
| 67 | + k := meta.PublicMetadataPrefix + "key1" |
| 68 | + action.GetMetadataReturns(map[string][]byte{ |
| 69 | + k: []byte("value1"), |
| 70 | + "another key": []byte("value2"), |
| 71 | + }) |
| 72 | + ctx := &TestContext{ |
| 73 | + PP: pp, |
| 74 | + TransferAction: action, |
| 75 | + MetadataCounter: map[MetadataCounterID]int{}, |
| 76 | + } |
| 77 | + return ctx, func() bool { |
| 78 | + return len(ctx.MetadataCounter) == 1 && ctx.MetadataCounter[k] == 1 |
| 79 | + } |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + ctx, check := tt.context() |
| 87 | + err := TransferApplicationDataValidate(ctx) |
| 88 | + if tt.err { |
| 89 | + assert.Error(t, err) |
| 90 | + assert.EqualError(t, err, tt.errMsg) |
| 91 | + } else { |
| 92 | + assert.NoError(t, err) |
| 93 | + } |
| 94 | + if check != nil { |
| 95 | + assert.True(t, check()) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments