Skip to content

Commit e2cf17d

Browse files
authored
test(certifier): increase unit-test coverage from 41% to 62% (#1508)
Signed-off-by: atharrva01 <atharvaborade568@gamil.com>
1 parent 2b0ba32 commit e2cf17d

File tree

15 files changed

+974
-191
lines changed

15 files changed

+974
-191
lines changed

token/services/certifier/interactive/certification_test.go

Lines changed: 196 additions & 126 deletions
Large diffs are not rendered by default.

token/services/certifier/interactive/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ const (
3030

3131
var logger = logging.MustGetLogger()
3232

33+
//go:generate counterfeiter -o mock/query_engine.go -fake-name QueryEngineMock . QueryEngine
3334
type QueryEngine interface {
3435
UnspentTokensIterator(ctx context.Context) (*token2.UnspentTokensIterator, error)
3536
}
3637

38+
//go:generate counterfeiter -o mock/certification_storage.go -fake-name CertificationStorageMock . CertificationStorage
3739
type CertificationStorage interface {
3840
Exists(ctx context.Context, id *token.ID) bool
3941
Store(ctx context.Context, certifications map[*token.ID][]byte) error
4042
}
4143

44+
//go:generate counterfeiter -o mock/view_manager.go -fake-name ViewManagerMock . ViewManager
4245
type ViewManager interface {
4346
InitiateView(view view.View) (interface{}, error)
4447
}

token/services/certifier/interactive/client_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Copyright IBM Corp. All Rights Reserved.
44
SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
package interactive
7+
package interactive_test
88

99
import (
1010
"context"
@@ -19,6 +19,7 @@ import (
1919
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
2020
token2 "github.com/hyperledger-labs/fabric-token-sdk/token"
2121
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
22+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/certifier/interactive"
2223
"github.com/hyperledger-labs/fabric-token-sdk/token/services/tokens"
2324
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
2425
"github.com/stretchr/testify/assert"
@@ -90,13 +91,13 @@ func (f *fakeViewManager) InitiateView(v view.View) (interface{}, error) {
9091
}
9192

9293
// Build a success result using the IDs from the CertificationRequestView.
93-
crv, ok := v.(*CertificationRequestView)
94+
crv, ok := v.(*interactive.CertificationRequestView)
9495
if !ok {
9596
return nil, errors.Errorf("unexpected view type %T", v)
9697
}
9798

98-
result := make(map[*token.ID][]byte, len(crv.ids))
99-
for _, id := range crv.ids {
99+
result := make(map[*token.ID][]byte, len(interactive.CRVIDs(crv)))
100+
for _, id := range interactive.CRVIDs(crv) {
100101
result[id] = []byte("cert:" + id.TxId)
101102
}
102103

@@ -120,15 +121,15 @@ var _ = (*fakeViewManager).callCount
120121
// newTestClient creates a CertificationClient with defaults suitable for tests.
121122
func newTestClient(
122123
t *testing.T,
123-
vm ViewManager,
124-
storage CertificationStorage,
124+
vm interactive.ViewManager,
125+
storage interactive.CertificationStorage,
125126
batchSize int,
126127
flushInterval time.Duration,
127128
workers int,
128-
) *CertificationClient {
129+
) *interactive.CertificationClient {
129130
t.Helper()
130131

131-
return NewCertificationClient(
132+
return interactive.NewCertificationClient(
132133
context.Background(),
133134
"test-network",
134135
"test-channel",
@@ -144,15 +145,15 @@ func newTestClient(
144145
1000,
145146
flushInterval,
146147
workers,
147-
DefaultResponseTimeout,
148+
interactive.DefaultResponseTimeout,
148149
&disabled.Provider{},
149150
)
150151
}
151152

152153
// injectToken sends a token ID directly into the client's input channel.
153154
// This simulates what OnReceive does without going through the events system.
154-
func injectToken(cc *CertificationClient, id *token.ID) {
155-
cc.tokens <- id
155+
func injectToken(cc *interactive.CertificationClient, id *token.ID) {
156+
interactive.ClientTokensChan(cc) <- id
156157
}
157158

158159
// --- tests ---
@@ -236,7 +237,7 @@ func TestCertificationClient_OnReceive_BufferFull_DoesNotBlock(t *testing.T) {
236237
storage := newFakeCertificationStorage()
237238

238239
// batchSize=1000 and flushInterval=10min so tokens accumulate but are never batched.
239-
cc := NewCertificationClient(
240+
cc := interactive.NewCertificationClient(
240241
context.Background(),
241242
"net", "ch", "ns",
242243
&fakeQueryEngine{},
@@ -249,7 +250,7 @@ func TestCertificationClient_OnReceive_BufferFull_DoesNotBlock(t *testing.T) {
249250
2, // tiny buffer — fills immediately
250251
10*time.Minute,
251252
1,
252-
DefaultResponseTimeout,
253+
interactive.DefaultResponseTimeout,
253254
&disabled.Provider{},
254255
)
255256
cc.Start()
@@ -280,7 +281,7 @@ func TestCertificationClient_PushbackNonBlocking(t *testing.T) {
280281
vm := &fakeViewManager{failErr: errors.New("simulated failure")}
281282
storage := newFakeCertificationStorage()
282283

283-
cc := NewCertificationClient(
284+
cc := interactive.NewCertificationClient(
284285
context.Background(),
285286
"net", "ch", "ns",
286287
&fakeQueryEngine{},
@@ -294,7 +295,7 @@ func TestCertificationClient_PushbackNonBlocking(t *testing.T) {
294295
1, // bufferSize=1 — push-back will overflow
295296
20*time.Millisecond,
296297
1,
297-
DefaultResponseTimeout,
298+
interactive.DefaultResponseTimeout,
298299
&disabled.Provider{},
299300
)
300301
cc.Start()
@@ -327,7 +328,7 @@ func TestCertificationClient_MultipleWorkers(t *testing.T) {
327328

328329
storage := newFakeCertificationStorage()
329330

330-
cc := NewCertificationClient(
331+
cc := interactive.NewCertificationClient(
331332
context.Background(),
332333
"net", "ch", "ns",
333334
&fakeQueryEngine{},
@@ -340,7 +341,7 @@ func TestCertificationClient_MultipleWorkers(t *testing.T) {
340341
1000,
341342
5*time.Millisecond,
342343
workers,
343-
DefaultResponseTimeout,
344+
interactive.DefaultResponseTimeout,
344345
&disabled.Provider{},
345346
)
346347
cc.Start()
@@ -399,7 +400,7 @@ func TestCertificationClient_TimerResets_MultipleFlushes(t *testing.T) {
399400

400401
// countingViewManager wraps a fakeViewManager and counts successful certifications.
401402
type countingViewManager struct {
402-
inner ViewManager
403+
inner interactive.ViewManager
403404
counter *atomic.Int32
404405
}
405406

token/services/certifier/interactive/config_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ Copyright IBM Corp. All Rights Reserved.
44
SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
package interactive
7+
package interactive_test
88

99
import (
1010
"testing"
1111
"time"
1212

13+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/certifier/interactive"
1314
"github.com/stretchr/testify/assert"
1415
)
1516

1617
// TestDefaultConstants guards that the operational defaults don't change accidentally.
1718
// Changing them is a behaviour-breaking deployment change, not just a refactor.
1819
func TestDefaultConstants(t *testing.T) {
19-
assert.Equal(t, 3, DefaultMaxAttempts, "DefaultMaxAttempts")
20-
assert.Equal(t, 10*time.Second, DefaultWaitTime, "DefaultWaitTime")
21-
assert.Equal(t, 10, DefaultBatchSize, "DefaultBatchSize")
22-
assert.Equal(t, 1000, DefaultBufferSize, "DefaultBufferSize")
23-
assert.Equal(t, 5*time.Second, DefaultFlushInterval, "DefaultFlushInterval")
24-
assert.Equal(t, 1, DefaultWorkers, "DefaultWorkers")
20+
assert.Equal(t, 3, interactive.DefaultMaxAttempts, "DefaultMaxAttempts")
21+
assert.Equal(t, 10*time.Second, interactive.DefaultWaitTime, "DefaultWaitTime")
22+
assert.Equal(t, 10, interactive.DefaultBatchSize, "DefaultBatchSize")
23+
assert.Equal(t, 1000, interactive.DefaultBufferSize, "DefaultBufferSize")
24+
assert.Equal(t, 5*time.Second, interactive.DefaultFlushInterval, "DefaultFlushInterval")
25+
assert.Equal(t, 1, interactive.DefaultWorkers, "DefaultWorkers")
2526
}

token/services/certifier/interactive/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Resolver interface {
6565
ResolveIdentities(endpoints ...string) ([]view.Identity, error)
6666
}
6767

68+
//go:generate counterfeiter -o mock/subscriber.go -fake-name SubscriberMock . Subscriber
6869
type Subscriber = events.Subscriber
6970

7071
type Driver struct {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package interactive_test
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
13+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/metrics/disabled"
14+
token "github.com/hyperledger-labs/fabric-token-sdk/token"
15+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/certifier/interactive"
16+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/certifier/interactive/mock"
17+
"github.com/stretchr/testify/require"
18+
)
19+
20+
// TestDriver_NewCertificationService_BackendFactoryError verifies that when
21+
// the BackendFactory returns an error, NewCertificationService propagates it
22+
// and leaves CertificationService nil.
23+
func TestDriver_NewCertificationService_BackendFactoryError(t *testing.T) {
24+
factoryErr := errors.New("backend init failed")
25+
26+
d := interactive.NewDriver(
27+
func(_ *token.ManagementService, _ string) (interactive.Backend, error) {
28+
return nil, factoryErr
29+
},
30+
nil, nil, &fakeViewManager{},
31+
&mock.ResponderRegistryMock{},
32+
&disabled.Provider{},
33+
)
34+
35+
_, err := d.NewCertificationService(nil, "wallet")
36+
require.Error(t, err)
37+
require.ErrorIs(t, err, factoryErr)
38+
require.Nil(t, d.CertificationService, "service must not be set after factory failure")
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// export_test.go exposes unexported fields and helpers for use by the external
8+
// test package (package interactive_test). Compiled only during testing.
9+
package interactive
10+
11+
import (
12+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
13+
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
14+
)
15+
16+
// ClientTokensChan returns the raw tokens channel of a CertificationClient.
17+
func ClientTokensChan(cc *CertificationClient) chan *token.ID { return cc.tokens }
18+
19+
// ClientProcessBatch calls the unexported processBatch method.
20+
func ClientProcessBatch(cc *CertificationClient, ids []*token.ID) { cc.processBatch(ids) }
21+
22+
// ServiceBackend returns the backend wired into a CertificationService.
23+
func ServiceBackend(s *CertificationService) Backend { return s.backend }
24+
25+
// ServiceWallets returns the wallets map of a CertificationService.
26+
func ServiceWallets(s *CertificationService) map[string]string { return s.wallets }
27+
28+
// ServiceMetrics returns the metrics of a CertificationService.
29+
func ServiceMetrics(s *CertificationService) *Metrics { return s.metrics }
30+
31+
// CRVNetwork returns the network field of a CertificationRequestView.
32+
func CRVNetwork(v *CertificationRequestView) string { return v.network }
33+
34+
// CRVChannel returns the channel field of a CertificationRequestView.
35+
func CRVChannel(v *CertificationRequestView) string { return v.channel }
36+
37+
// CRVNamespace returns the ns field of a CertificationRequestView.
38+
func CRVNamespace(v *CertificationRequestView) string { return v.ns }
39+
40+
// CRVCertifier returns the certifier field of a CertificationRequestView.
41+
func CRVCertifier(v *CertificationRequestView) view.Identity { return v.certifier }
42+
43+
// CRVIDs returns the ids field of a CertificationRequestView.
44+
func CRVIDs(v *CertificationRequestView) []*token.ID { return v.ids }

token/services/certifier/interactive/backend_mock.go renamed to token/services/certifier/interactive/mock/backend.go

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)