forked from VoneChain-CS/fabric-sdk-go-gm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockconfigbackend.go
90 lines (78 loc) · 2.18 KB
/
mockconfigbackend.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"strings"
"github.com/shiqinfeng1/fabric-sdk-go-gm/pkg/core/config"
)
//MockConfigBackend mocks config backend for unit tests
type MockConfigBackend struct {
//KeyValueMap map to override CustomBackend key-values.
KeyValueMap map[string]interface{}
}
//Lookup returns or unmarshals value for given key
func (b *MockConfigBackend) Lookup(key string) (interface{}, bool) {
v, ok := b.KeyValueMap[key]
return v, ok
}
// Set sets a backend value
func (b *MockConfigBackend) Set(key string, value interface{}) bool {
if key != "" {
node := b.KeyValueMap
ss := strings.Split(key, ".")
for i, s := range ss {
if i == len(ss)-1 {
node[s] = value
return true
}
v, ok := node[s]
if !ok {
node[s] = map[string]interface{}{}
node = node[s].(map[string]interface{})
} else {
node, ok = v.(map[string]interface{})
if !ok {
return false
}
}
}
}
return false
}
// Get returns a value from the backend
func (b *MockConfigBackend) Get(key string) (interface{}, bool) {
if key != "" {
node := b.KeyValueMap
ss := strings.Split(key, ".")
for i, s := range ss {
v, ok := node[s]
if i == len(ss)-1 {
return v, ok
}
if !ok {
return nil, ok
}
node = v.(map[string]interface{})
}
}
return nil, false
}
// BackendFromFile returns MockConfigBackend populated from file
func BackendFromFile(configPath string) (*MockConfigBackend, error) {
b, err := config.FromFile(configPath)()
if err != nil {
return nil, err
}
configBackend := b[0]
backendMap := make(map[string]interface{})
backendMap["client"], _ = configBackend.Lookup("client")
backendMap["certificateAuthorities"], _ = configBackend.Lookup("certificateAuthorities")
backendMap["entityMatchers"], _ = configBackend.Lookup("entityMatchers")
backendMap["peers"], _ = configBackend.Lookup("peers")
backendMap["organizations"], _ = configBackend.Lookup("organizations")
backendMap["orderers"], _ = configBackend.Lookup("orderers")
backendMap["channels"], _ = configBackend.Lookup("channels")
return &MockConfigBackend{KeyValueMap: backendMap}, nil
}