-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
entity_test.go
265 lines (225 loc) · 7.98 KB
/
entity_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package backstage
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
)
// TestEntityServiceGet tests the retrieval of a specific entity.
func TestEntityServiceGet(t *testing.T) {
const dataFile = "testdata/entities_single.json"
const uid = "uid"
var expected Entity
expectedData, _ := os.ReadFile(dataFile)
err := json.Unmarshal(expectedData, &expected)
assert.FileExists(t, dataFile, "Test data file should exist")
assert.NoError(t, err, "Unmarshal should not return an error")
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Get(fmt.Sprintf("/catalog/entities/by-uid/%s", uid)).
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
actual, resp, err := s.Get(context.Background(), uid)
assert.NoError(t, err, "Get should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.Equal(t, http.StatusOK, resp.StatusCode, "Response status code should be 200")
assert.EqualValues(t, &expected, actual, "Response body should match the one from the server")
}
// TestEntityServiceList tests the retrieval of a list of entities.
func TestEntityServiceList(t *testing.T) {
const dataFile = "testdata/entities.json"
var expected []Entity
expectedData, _ := os.ReadFile(dataFile)
err := json.Unmarshal(expectedData, &expected)
assert.FileExists(t, dataFile, "Test data file should exist")
assert.NoError(t, err, "Unmarshal should not return an error")
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Get("/catalog/entities").
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
actual, resp, err := s.List(context.Background(), nil)
assert.NoError(t, err, "Get should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.Equal(t, http.StatusOK, resp.StatusCode, "Response status code should be 200")
assert.EqualValues(t, expected, actual, "Response body should match the one from the server")
}
// TestEntityServiceList_Filter tests the retrieval of a list of entities with a filter.
func TestEntityServiceList_Filter(t *testing.T) {
const dataFile = "testdata/entities_filter.json"
var expected []Entity
expectedData, _ := os.ReadFile(dataFile)
err := json.Unmarshal(expectedData, &expected)
assert.FileExists(t, dataFile, "Test data file should exist")
assert.NoError(t, err, "Unmarshal should not return an error")
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Get("/catalog/entities").
MatchParam("filter", "kind=User").
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
actual, resp, err := s.List(context.Background(), &ListEntityOptions{
Filters: []string{"kind=User"},
})
assert.NoError(t, err, "Get should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.EqualValues(t, expected, actual, "Response body should match the one from the server")
}
// TestEntityServiceList_Fields tests the retrieval of a list of entities containing only the specified fields.
func TestEntityServiceList_Fields(t *testing.T) {
const dataFile = "testdata/entities_fields.json"
var expected []Entity
expectedData, _ := os.ReadFile(dataFile)
err := json.Unmarshal(expectedData, &expected)
assert.FileExists(t, dataFile, "Test data file should exist")
assert.NoError(t, err, "Unmarshal should not return an error")
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Get("/catalog/entities").
MatchParam("fields", "metadata.name").
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
actual, resp, err := s.List(context.Background(), &ListEntityOptions{
Fields: []string{
"metadata.name",
},
})
assert.NoError(t, err, "Get should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.EqualValues(t, expected, actual, "Response body should match the one from the server")
}
// TestEntityServiceList_Order tests the retrieval of a list of entities sorted by the specified field.
func TestEntityServiceList_Order(t *testing.T) {
const dataFile = "testdata/entities_order.json"
var expected []Entity
expectedData, _ := os.ReadFile(dataFile)
err := json.Unmarshal(expectedData, &expected)
assert.FileExists(t, dataFile, "Test data file should exist")
assert.NoError(t, err, "Unmarshal should not return an error")
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Get("/catalog/entities").
MatchParam("order", "desc:metadata.name").
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
actual, resp, err := s.List(context.Background(), &ListEntityOptions{
Order: []ListEntityOrder{
{
Direction: OrderDescending,
Field: "metadata.name",
},
},
})
assert.NoError(t, err, "Get should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.EqualValues(t, expected, actual, "Response body should match the one from the server")
}
// TestEntityServiceList_InvalidOrder tests the retrieval of a list when invalid order is provided.
func TestEntityServiceList_InvalidOrder(t *testing.T) {
c, _ := NewClient("", "", nil)
s := newEntityService(newCatalogService(c))
_, _, err := s.List(context.Background(), &ListEntityOptions{
Order: []ListEntityOrder{
{
Direction: "InvalidOrder",
Field: "metadata.name",
},
},
})
assert.Error(t, err, "Get should return an error when the order is invalid")
}
// TestEntityServiceDelete tests the deletion of an entity.
func TestEntityServiceDelete(t *testing.T) {
const uid = "uid"
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Delete(fmt.Sprintf("/catalog/entities/by-uid/%s", uid)).
Reply(http.StatusNoContent)
c, _ := NewClient(baseURL.String(), "", nil)
s := newEntityService(newCatalogService(c))
resp, err := s.Delete(context.Background(), uid)
assert.NoError(t, err, "Delete should not return an error")
assert.NotEmpty(t, resp, "Response should not be empty")
assert.EqualValues(t, http.StatusNoContent, resp.StatusCode, "Response status code should match the one from the server")
}
// TestListEntityOrderString tests if list entity order string is correctly generated.
func TestListEntityOrderString(t *testing.T) {
tests := []struct {
name string
order ListEntityOrder
expected string
shouldErr bool
}{
{
name: "valid order",
order: ListEntityOrder{
Direction: OrderAscending,
Field: "field1",
},
expected: "asc:field1",
shouldErr: false,
},
{
name: "valid descending order",
order: ListEntityOrder{
Direction: OrderDescending,
Field: "field2",
},
expected: "desc:field2",
shouldErr: false,
},
{
name: "invalid order direction",
order: ListEntityOrder{
Direction: "invalid",
Field: "field3",
},
expected: "",
shouldErr: true,
},
{
name: "empty order",
order: ListEntityOrder{},
expected: "",
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := test.order.string()
if test.shouldErr {
assert.Error(t, err, "Expected error but got nil")
} else {
assert.NoError(t, err, "Unexpected error")
assert.Equal(t, test.expected, actual, "List entity order string should match expected value")
}
})
}
}