forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockchprovider.go
120 lines (98 loc) · 3.43 KB
/
mockchprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
reqContext "context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)
// MockChannelProvider holds a mock channel provider.
type MockChannelProvider struct {
ctx core.Providers
customChannelService fab.ChannelService
}
// MockChannelService holds a mock channel service.
type MockChannelService struct {
provider *MockChannelProvider
channelID string
transactor fab.Transactor
mockOrderers []string
discovery fab.DiscoveryService
selection fab.SelectionService
}
// NewMockChannelProvider returns a mock ChannelProvider
func NewMockChannelProvider(ctx core.Providers) (*MockChannelProvider, error) {
// Create a mock client with the mock channel
cp := MockChannelProvider{
ctx: ctx,
}
return &cp, nil
}
// ChannelService returns a mock ChannelService
func (cp *MockChannelProvider) ChannelService(ctx fab.ClientContext, channelID string) (fab.ChannelService, error) {
if cp.customChannelService != nil {
return cp.customChannelService, nil
}
cs := MockChannelService{
provider: cp,
channelID: channelID,
transactor: &MockTransactor{},
discovery: NewMockDiscoveryService(nil),
selection: NewMockSelectionService(nil),
}
return &cs, nil
}
// SetCustomChannelService sets custom channel service for unit-test purposes
func (cp *MockChannelProvider) SetCustomChannelService(customChannelService fab.ChannelService) {
cp.customChannelService = customChannelService
}
// SetOrderers sets orderes to mock channel service for unit-test purposes
func (cs *MockChannelService) SetOrderers(orderers []string) {
cs.mockOrderers = orderers
}
// EventService returns a mock event service
func (cs *MockChannelService) EventService(opts ...options.Opt) (fab.EventService, error) {
return NewMockEventService(), nil
}
// SetTransactor changes the return value of Transactor
func (cs *MockChannelService) SetTransactor(t fab.Transactor) {
cs.transactor = t
}
// Transactor returns a mock transactor
func (cs *MockChannelService) Transactor(reqCtx reqContext.Context) (fab.Transactor, error) {
if cs.transactor != nil {
return cs.transactor, nil
}
return &MockTransactor{ChannelID: cs.channelID, Ctx: reqCtx}, nil
}
// Config ...
func (cs *MockChannelService) Config() (fab.ChannelConfig, error) {
return nil, nil
}
// Membership returns member identification
func (cs *MockChannelService) Membership() (fab.ChannelMembership, error) {
return NewMockMembership(), nil
}
//ChannelConfig returns channel config
func (cs *MockChannelService) ChannelConfig() (fab.ChannelCfg, error) {
return &MockChannelCfg{MockID: cs.channelID, MockOrderers: cs.mockOrderers}, nil
}
// Discovery returns a mock DiscoveryService
func (cs *MockChannelService) Discovery() (fab.DiscoveryService, error) {
return cs.discovery, nil
}
// SetDiscovery sets the mock DiscoveryService
func (cs *MockChannelService) SetDiscovery(discovery fab.DiscoveryService) {
cs.discovery = discovery
}
// Selection returns a mock SelectionService
func (cs *MockChannelService) Selection() (fab.SelectionService, error) {
return cs.selection, nil
}
// SetSelection sets the mock SelectionService
func (cs *MockChannelService) SetSelection(selection fab.SelectionService) {
cs.selection = selection
}