forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_mediator.go
124 lines (99 loc) · 2.97 KB
/
mock_mediator.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
121
122
123
124
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mediator
import (
"github.com/google/uuid"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
)
// MockMediatorSvc mock route service.
type MockMediatorSvc struct {
service.Action
service.Message
ProtocolName string
HandleFunc func(service.DIDCommMsg) (string, error)
HandleOutboundFunc func(msg service.DIDCommMsg, myDID, theirDID string) (string, error)
AcceptFunc func(string) bool
RegisterFunc func(connectionID string, options ...mediator.ClientOption) error
RouterEndpoint string
RoutingKeys []string
ConfigErr error
AddKeyErr error
UnregisterErr error
Connections []string
GetConnectionsErr error
AddKeyFunc func(string) error
}
// Initialize service.
func (m *MockMediatorSvc) Initialize(interface{}) error {
return nil
}
// HandleInbound msg.
func (m *MockMediatorSvc) HandleInbound(msg service.DIDCommMsg, ctx service.DIDCommContext) (string, error) {
if m.HandleFunc != nil {
return m.HandleFunc(msg)
}
return uuid.New().String(), nil
}
// HandleOutbound msg.
func (m *MockMediatorSvc) HandleOutbound(msg service.DIDCommMsg, myDID, theirDID string) (string, error) {
if m.HandleOutboundFunc != nil {
return m.HandleOutboundFunc(msg, myDID, theirDID)
}
return "", nil
}
// Accept msg checks the msg type.
func (m *MockMediatorSvc) Accept(msgType string) bool {
if m.AcceptFunc != nil {
return m.AcceptFunc(msgType)
}
return true
}
// Name return service name.
func (m *MockMediatorSvc) Name() string {
if m.ProtocolName != "" {
return m.ProtocolName
}
return "route"
}
// Register registers agent with the router.
func (m *MockMediatorSvc) Register(connectionID string, options ...mediator.ClientOption) error {
if m.RegisterFunc != nil {
return m.RegisterFunc(connectionID, options...)
}
return nil
}
// Unregister unregisters the router.
func (m *MockMediatorSvc) Unregister(connID string) error {
return m.UnregisterErr
}
// AddKey adds agents recKey to the router.
func (m *MockMediatorSvc) AddKey(connID, recKey string) error {
if m.AddKeyErr != nil {
return m.AddKeyErr
}
if m.AddKeyFunc != nil {
return m.AddKeyFunc(recKey)
}
return nil
}
// Config gives back the router configuration.
func (m *MockMediatorSvc) Config(connID string) (*mediator.Config, error) {
if m.ConfigErr != nil {
return nil, m.ConfigErr
}
// default, route not registered error
if m.RouterEndpoint == "" || m.RoutingKeys == nil {
return nil, mediator.ErrRouterNotRegistered
}
return mediator.NewConfig(m.RouterEndpoint, m.RoutingKeys), nil
}
// GetConnections returns router`s connections.
func (m *MockMediatorSvc) GetConnections(...mediator.ConnectionOption) ([]string, error) {
if m.GetConnectionsErr != nil {
return nil, m.GetConnectionsErr
}
return m.Connections, nil
}