-
Notifications
You must be signed in to change notification settings - Fork 4
/
addon_test.go
188 lines (174 loc) · 5.98 KB
/
addon_test.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package gonnect
import (
"io"
"log"
"strings"
"sync"
"testing"
"github.com/craftamap/atlas-gonnect/store"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/jinzhu/gorm"
)
func TestNewAddon(t *testing.T) {
memoryGorm1, err := gorm.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
}
nameL := "example"
name := &nameL
keyL := "com.github.craftamap.atlassian-gonnect.example"
key := &keyL
testCases := []struct {
descriptorReader io.Reader
configReader io.Reader
addon *Addon
expectError bool
}{
// It should fail because no descriptor nor config
{
descriptorReader: strings.NewReader(""),
configReader: strings.NewReader(""),
addon: &Addon{},
expectError: true,
},
// It should fail because no descriptor nor config
{
descriptorReader: strings.NewReader("{}"),
configReader: strings.NewReader("{}"),
addon: &Addon{},
expectError: true,
},
// It should fail because no valid descriptor
{
descriptorReader: strings.NewReader("THIS IS SPARTA!"),
configReader: strings.NewReader(`{"currentProfile": "dev", "profiles": {"dev": {"baseUrl": "http://test/","port": 8080,"store": {"type": "sqlite3","databaseUrl": ":memory:"}}}}`),
addon: &Addon{},
expectError: true,
},
// It should fail because an invalid Descriptor is provided
{
descriptorReader: strings.NewReader("{}"),
configReader: strings.NewReader(`{"currentProfile": "dev", "profiles": {"dev": {"baseUrl": "http://test/","port": 8080,"store": {"type": "sqlite3","databaseUrl": ":memory:"}}}}`),
addon: &Addon{},
expectError: true,
},
// No key
{
descriptorReader: strings.NewReader(`{"name":"example"}`),
configReader: strings.NewReader(`{"currentProfile": "dev", "profiles": {"dev": {"baseUrl": "http://test/","port": 8080,"store": {"type": "sqlite3","databaseUrl": ":memory:"}}}}`),
expectError: true,
},
// there wil hopefully never be sqlite99
{
descriptorReader: strings.NewReader(`{"name":"example"}`),
configReader: strings.NewReader(`{"currentProfile": "dev", "profiles": {"dev": {"baseUrl": "http://test/","port": 8080,"store": {"type": "sqlite99","databaseUrl": ":memory:"}}}}`),
expectError: true,
},
// Successful test
{
descriptorReader: strings.NewReader(`{"name":"example", "key": "com.github.craftamap.atlassian-gonnect.example"}`),
configReader: strings.NewReader(`{"currentProfile": "dev", "profiles": {"dev": {"baseUrl": "http://test/","port": 8080,"store": {"type": "sqlite3","databaseUrl": ":memory:"}}}}`),
addon: &Addon{
CurrentProfile: "dev",
Store: &store.Store{Database: memoryGorm1},
Config: &Profile{
Port: 8080,
BaseUrl: "http://test/",
Store: StoreConfiguration{
Type: "sqlite3",
DatabaseUrl: ":memory:",
},
},
AddonDescriptor: map[string]interface{}{
"name": "example",
"key": "com.github.craftamap.atlassian-gonnect.example",
},
Name: name,
Key: key,
},
expectError: false,
},
}
for _, testCase := range testCases {
addon, err := NewAddon(testCase.configReader, testCase.descriptorReader)
if err != nil {
if testCase.expectError {
log.Printf("Expected error: %s", err)
} else {
t.Error(err)
}
continue
} else if testCase.expectError {
t.Error("Expected error, but got no error")
continue
}
// The equal check of an addon will be funny to code...
if testCase.addon == nil && addon != nil {
t.Errorf("Expected error to be nil, but addon is %+v", addon)
}
if addon != nil {
if addon.Config == nil || testCase.addon.Config == nil || !cmp.Equal(*addon.Config, *testCase.addon.Config) {
t.Errorf("Expected addon.Config to be %+v, but got %+v", testCase.addon.Config, addon.Config)
}
if addon.Store == nil || testCase.addon.Store == nil || !cmp.Equal(*addon.Store, *testCase.addon.Store, cmpopts.IgnoreUnexported(gorm.DB{}, sync.RWMutex{})) {
t.Errorf("Expected addon.Config to be %+v, but got %+v", testCase.addon.Store, addon.Store)
}
if addon.CurrentProfile != testCase.addon.CurrentProfile {
t.Errorf("Expected addon.CurrentProfile to be %s, but got %s", testCase.addon.CurrentProfile, addon.CurrentProfile)
}
if !cmp.Equal(addon.AddonDescriptor, testCase.addon.AddonDescriptor) {
t.Errorf("Expected addon.AddonDescriptor to be %+v, but got %+v", testCase.addon.AddonDescriptor, addon.AddonDescriptor)
}
if *addon.Key != *testCase.addon.Key || *addon.Name != *testCase.addon.Name {
t.Errorf("Expected addon.Key to be %s, but got %s. Expected addon.Name to be %s, but got %s.",
*testCase.addon.Key, *addon.Key, *testCase.addon.Name, *addon.Name)
}
}
}
}
func TestReadAddonDescriptor(t *testing.T) {
testCases := []struct {
DescriptorReader io.Reader
BaseUrl string
DescriptorMap map[string]interface{}
ExpectError bool
}{
{
DescriptorReader: strings.NewReader("THIS IS INVALID JSON"),
BaseUrl: "",
DescriptorMap: map[string]interface{}{},
ExpectError: true,
},
{
DescriptorReader: strings.NewReader("{}"),
BaseUrl: "",
DescriptorMap: map[string]interface{}{},
ExpectError: false,
},
{
DescriptorReader: strings.NewReader(`{"abc":"def"}`),
BaseUrl: "",
DescriptorMap: map[string]interface{}{"abc": "def"},
ExpectError: false,
},
{
DescriptorReader: strings.NewReader(`{"abc":"{{BaseUrl}}"}`),
BaseUrl: "",
DescriptorMap: map[string]interface{}{"abc": "def"},
ExpectError: true,
},
}
for _, testCase := range testCases {
descriptorMap, err := readAddonDescriptor(testCase.DescriptorReader, testCase.BaseUrl)
if err != nil {
if !testCase.ExpectError {
t.Error(err)
}
continue
}
if !cmp.Equal(descriptorMap, testCase.DescriptorMap) {
t.Errorf("Expected Descriptor to be %s, but got %s", testCase.DescriptorMap, descriptorMap)
}
}
}