|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package certifier |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors" |
| 14 | + "github.com/hyperledger-labs/fabric-token-sdk/token/services/certifier/driver/mock" |
| 15 | + "github.com/hyperledger-labs/fabric-token-sdk/token/token" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +// Test calling IsCertified |
| 21 | +func TestCertificationClient_IsCertified(t *testing.T) { |
| 22 | + mockClient := &mock.CertificationClient{} |
| 23 | + mockClient.IsCertifiedReturns(true) |
| 24 | + |
| 25 | + client := &CertificationClient{c: mockClient} |
| 26 | + |
| 27 | + id := &token.ID{ |
| 28 | + TxId: "tx1", |
| 29 | + Index: 0, |
| 30 | + } |
| 31 | + |
| 32 | + result := client.IsCertified(context.Background(), id) |
| 33 | + assert.True(t, result) |
| 34 | + |
| 35 | + // Verify the mock was called with correct arguments |
| 36 | + assert.Equal(t, 1, mockClient.IsCertifiedCallCount()) |
| 37 | + ctx, calledID := mockClient.IsCertifiedArgsForCall(0) |
| 38 | + assert.NotNil(t, ctx) |
| 39 | + assert.Equal(t, id, calledID) |
| 40 | +} |
| 41 | + |
| 42 | +// Test negative response from Certifier |
| 43 | +func TestCertificationClient_IsCertified_False(t *testing.T) { |
| 44 | + mockClient := &mock.CertificationClient{} |
| 45 | + mockClient.IsCertifiedReturns(false) |
| 46 | + |
| 47 | + client := &CertificationClient{c: mockClient} |
| 48 | + |
| 49 | + id := &token.ID{ |
| 50 | + TxId: "tx2", |
| 51 | + Index: 1, |
| 52 | + } |
| 53 | + |
| 54 | + result := client.IsCertified(context.Background(), id) |
| 55 | + assert.False(t, result) |
| 56 | + |
| 57 | + assert.Equal(t, 1, mockClient.IsCertifiedCallCount()) |
| 58 | +} |
| 59 | + |
| 60 | +// Test calling RequestCertification |
| 61 | +func TestCertificationClient_RequestCertification_Success(t *testing.T) { |
| 62 | + mockClient := &mock.CertificationClient{} |
| 63 | + mockClient.RequestCertificationReturns(nil) |
| 64 | + |
| 65 | + client := &CertificationClient{c: mockClient} |
| 66 | + |
| 67 | + ids := []*token.ID{ |
| 68 | + {TxId: "tx1", Index: 0}, |
| 69 | + {TxId: "tx2", Index: 1}, |
| 70 | + } |
| 71 | + |
| 72 | + err := client.RequestCertification(context.Background(), ids...) |
| 73 | + require.NoError(t, err, "RequestCertification should succeed when underlying client succeeds") |
| 74 | + |
| 75 | + // Verify the mock was called |
| 76 | + assert.Equal(t, 1, mockClient.RequestCertificationCallCount()) |
| 77 | + ctx, calledIDs := mockClient.RequestCertificationArgsForCall(0) |
| 78 | + assert.NotNil(t, ctx) |
| 79 | + assert.Equal(t, ids, calledIDs) |
| 80 | +} |
| 81 | + |
| 82 | +// Test failing with a certifier that returns an error |
| 83 | +func TestCertificationClient_RequestCertification_Error(t *testing.T) { |
| 84 | + expectedErr := errors.New("certification failed") |
| 85 | + mockClient := &mock.CertificationClient{} |
| 86 | + mockClient.RequestCertificationReturns(expectedErr) |
| 87 | + |
| 88 | + client := &CertificationClient{c: mockClient} |
| 89 | + |
| 90 | + ids := []*token.ID{ |
| 91 | + {TxId: "tx1", Index: 0}, |
| 92 | + } |
| 93 | + |
| 94 | + err := client.RequestCertification(context.Background(), ids...) |
| 95 | + require.Error(t, err) |
| 96 | + require.ErrorIs(t, err, expectedErr) |
| 97 | + |
| 98 | + // Verify the mock was called once |
| 99 | + assert.Equal(t, 1, mockClient.RequestCertificationCallCount()) |
| 100 | +} |
| 101 | + |
| 102 | +// Test failing when RequestCertification is called with an empty id list |
| 103 | +func TestCertificationClient_RequestCertification_NoIDs(t *testing.T) { |
| 104 | + mockClient := &mock.CertificationClient{} |
| 105 | + mockClient.RequestCertificationReturns(nil) |
| 106 | + |
| 107 | + client := &CertificationClient{c: mockClient} |
| 108 | + |
| 109 | + err := client.RequestCertification(context.Background()) |
| 110 | + require.NoError(t, err, "RequestCertification should handle empty ID list") |
| 111 | + |
| 112 | + // Verify the mock was called once with empty slice |
| 113 | + assert.Equal(t, 1, mockClient.RequestCertificationCallCount()) |
| 114 | + ctx, calledIDs := mockClient.RequestCertificationArgsForCall(0) |
| 115 | + assert.NotNil(t, ctx) |
| 116 | + assert.Empty(t, calledIDs) |
| 117 | +} |
0 commit comments