-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Angelo De Caro <[email protected]>
- Loading branch information
Showing
2 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package pledge | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common" | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/driver" | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/token" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIssueActionMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
attributes map[string][]byte | ||
opts *driver.IssueOptions | ||
expected map[string][]byte | ||
errExpected bool | ||
}{ | ||
{ | ||
name: "empty attributes", | ||
attributes: map[string][]byte{}, | ||
opts: &driver.IssueOptions{ | ||
Attributes: map[interface{}]interface{}{}, | ||
}, | ||
expected: map[string][]byte{}, | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "missing tokenID", | ||
attributes: map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"), | ||
}, | ||
opts: &driver.IssueOptions{ | ||
Attributes: map[interface{}]interface{}{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network", | ||
}, | ||
}, | ||
expected: map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"), | ||
}, | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "missing network", | ||
attributes: map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"), | ||
}, | ||
opts: &driver.IssueOptions{ | ||
Attributes: map[interface{}]interface{}{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": "tokenID", | ||
}, | ||
}, | ||
expected: map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"), | ||
}, | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "valid attributes", | ||
attributes: map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"), | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"), | ||
}, | ||
opts: &driver.IssueOptions{ | ||
Attributes: map[interface{}]interface{}{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network", | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": &token.ID{ | ||
TxId: "a_transaction", | ||
Index: 2, | ||
}, | ||
}, | ||
}, | ||
expected: func() map[string][]byte { | ||
metadata := &IssueMetadata{ | ||
OriginTokenID: &token.ID{ | ||
TxId: "a_transaction", | ||
Index: 2, | ||
}, | ||
OriginNetwork: "network", | ||
} | ||
marshalled, err := json.Marshal(metadata) | ||
assert.NoError(t, err) | ||
key := common.Hashable(marshalled).String() | ||
res := map[string][]byte{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": []byte("network"), | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": []byte("tokenID"), | ||
key: marshalled, | ||
key + "proof_of_claim": nil, | ||
} | ||
return res | ||
}(), | ||
errExpected: false, | ||
}, | ||
{ | ||
name: "valid attributes with proof", | ||
attributes: map[string][]byte{}, | ||
opts: &driver.IssueOptions{ | ||
Attributes: map[interface{}]interface{}{ | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/network": "network", | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/tokenID": &token.ID{ | ||
TxId: "a_transaction", | ||
Index: 2, | ||
}, | ||
"github.com/hyperledger-labs/fabric-token-sdk/token/services/interop/pledge/proof": []byte("proof"), | ||
}, | ||
}, | ||
expected: func() map[string][]byte { | ||
metadata := &IssueMetadata{ | ||
OriginTokenID: &token.ID{ | ||
TxId: "a_transaction", | ||
Index: 2, | ||
}, | ||
OriginNetwork: "network", | ||
} | ||
marshalled, err := json.Marshal(metadata) | ||
assert.NoError(t, err) | ||
key := common.Hashable(marshalled).String() | ||
res := map[string][]byte{ | ||
key: marshalled, | ||
key + "proof_of_claim": []byte("proof"), | ||
} | ||
return res | ||
}(), | ||
errExpected: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result, err := IssueActionMetadata(test.attributes, test.opts) | ||
if test.errExpected && err == nil { | ||
t.Errorf("expected error but got none") | ||
} | ||
if !test.errExpected && err != nil { | ||
t.Errorf("got error %v", err) | ||
} | ||
assert.Equal(t, test.expected, result) | ||
}) | ||
} | ||
} |