/
testutil.go
139 lines (101 loc) · 3.55 KB
/
testutil.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package testutil
import (
"encoding/json"
"fmt"
"net/url"
"testing"
"time"
"github.com/hyperledger/aries-framework-go/component/storageutil/mem"
"github.com/hyperledger/aries-framework-go/pkg/doc/ld"
ldstore "github.com/hyperledger/aries-framework-go/pkg/store/ld"
"github.com/stretchr/testify/require"
"github.com/trustbloc/sidetree-core-go/pkg/canonicalizer"
"github.com/trustbloc/orb/internal/pkg/ldcontext"
"github.com/trustbloc/orb/pkg/store/expiry"
"github.com/trustbloc/orb/pkg/taskmgr"
)
// MustParseURL parses the given string and returns the URL.
// If the given string is not a valid URL then the function panics.
func MustParseURL(raw string) *url.URL {
u, err := url.Parse(raw)
if err != nil {
panic(err)
}
return u
}
// NewMockID returns a URL using the base IRI and the given path.
func NewMockID(iri fmt.Stringer, path string) *url.URL {
return MustParseURL(fmt.Sprintf("%s%s", iri, path))
}
// NewMockURLs returns the given number of URLs using the given function to format each one.
func NewMockURLs(num int, getURI func(i int) string) []*url.URL {
results := make([]*url.URL, num)
for i := 0; i < num; i++ {
results[i] = MustParseURL(getURI(i))
}
return results
}
// GetCanonical converts the given JSON string into a canonical JSON.
func GetCanonical(t *testing.T, raw string) string {
t.Helper()
return string(GetCanonicalBytes(t, raw))
}
// GetCanonicalBytes converts the given JSON string into a canonical JSON (returning the bytes).
func GetCanonicalBytes(t *testing.T, raw string) []byte {
t.Helper()
var expectedDoc map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(raw), &expectedDoc))
bytes, err := canonicalizer.MarshalCanonical(expectedDoc)
require.NoError(t, err)
return bytes
}
// MarshalCanonical marshals the given object to a canonical JSON.
func MarshalCanonical(t *testing.T, i interface{}) []byte {
t.Helper()
bytes, err := canonicalizer.MarshalCanonical(i)
require.NoError(t, err)
return bytes
}
type provider struct {
ContextStore ldstore.ContextStore
RemoteProviderStore ldstore.RemoteProviderStore
}
func (p *provider) JSONLDContextStore() ldstore.ContextStore {
return p.ContextStore
}
func (p *provider) JSONLDRemoteProviderStore() ldstore.RemoteProviderStore {
return p.RemoteProviderStore
}
// GetLoader returns document loader.
func GetLoader(t *testing.T) *ld.DocumentLoader {
t.Helper()
contextStore, err := ldstore.NewContextStore(mem.NewProvider())
require.NoError(t, err)
remoteProviderStore, err := ldstore.NewRemoteProviderStore(mem.NewProvider())
require.NoError(t, err)
p := &provider{
ContextStore: contextStore,
RemoteProviderStore: remoteProviderStore,
}
documentLoader, err := ld.NewDocumentLoader(p, ld.WithExtraContexts(ldcontext.MustGetAll()...))
require.NoError(t, err)
return documentLoader
}
// GetTaskMgr returns a test task manager service.
func GetTaskMgr(t *testing.T) *taskmgr.Manager {
t.Helper()
const checkInterval = 500 * time.Millisecond
coordinationStore, err := mem.NewProvider().OpenStore("coordination")
require.NoError(t, err)
return taskmgr.New(coordinationStore, checkInterval)
}
// GetExpiryService returns test expiry service object. For most tests, the expiry service used doesn't really matter
// this object is just needed to ensure that no nil pointer errors happen when initializing the store.
func GetExpiryService(t *testing.T) *expiry.Service {
t.Helper()
return expiry.NewService(GetTaskMgr(t), time.Second)
}