-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kind_location_test.go
212 lines (176 loc) · 6.71 KB
/
kind_location_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
package backstage
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
)
// TestKindLocationGet tests functionality of getting a location.
func TestKindLocationGet(t *testing.T) {
const dataFile = "testdata/location.json"
const location = "example"
expected := LocationEntityV1alpha1{}
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-name/location/default/%s", location)).
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
actual, resp, err := s.Get(context.Background(), location, "")
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")
}
// TestKindLocationCreateByID tests functionality of creating a new location.
func TestKindLocationCreateByID(t *testing.T) {
const dataFile = "testdata/location_create.json"
const target = "https://github.com/datolabs-io/go-backstage/test"
expected := LocationCreateResponse{}
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").
Post("/catalog/locations").
MatchParam("dryRun", "false").
Reply(200).
JSON(&LocationCreateResponse{
Location: &LocationResponse{
ID: "830d2354-8bbb-42d1-a751-2959f6da5416",
Type: "url",
Target: target,
},
Entities: []Entity{},
})
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
actual, resp, err := s.Create(context.Background(), target, false)
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")
}
// TestKindLocationCreateByID_DryRun tests functionality of creating a new location.
func TestKindLocationCreateByID_DryRun(t *testing.T) {
const dataFile = "testdata/location_create_dryrun.json"
const target = "https://github.com/datolabs-io/go-backstage/test"
expected := LocationCreateResponse{}
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").
Post("/catalog/locations").
MatchParam("dryRun", "true").
Reply(200).
JSON(&LocationCreateResponse{
Location: &LocationResponse{
ID: "830d2354-8bbb-42d1-a751-2959f6da5416",
Type: "url",
Target: target,
},
Entities: []Entity{},
})
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
actual, resp, err := s.Create(context.Background(), target, true)
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")
}
// TestKindLocationGetByID tests functionality of getting a location by its ID.
func TestKindLocationGetByID(t *testing.T) {
const dataFile = "testdata/location_by_id.json"
const id = "830d2354-8bbb-42d1-a751-2959f6da5416"
expected := LocationResponse{}
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/locations/%s", id)).
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
actual, resp, err := s.GetByID(context.Background(), id)
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")
}
// TestKindLocationList tests functionality of getting all locations.
func TestKindLocationList(t *testing.T) {
const dataFile = "testdata/locations.json"
var expected []LocationListResponse
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/locations").
Reply(200).
File(dataFile)
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
actual, resp, err := s.List(context.Background())
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")
}
// TestEntityServiceDelete tests the deletion of an entity.
func TestKindLocationDeleteByID(t *testing.T) {
const id = "id"
baseURL, _ := url.Parse("https://foo:1234/api")
defer gock.Off()
gock.New(baseURL.String()).
MatchHeader("Accept", "application/json").
Delete(fmt.Sprintf("/catalog/locations/%s", id)).
Reply(http.StatusNoContent)
c, _ := NewClient(baseURL.String(), "", nil)
s := newLocationService(&entityService{
client: c,
apiPath: "/catalog/entities",
})
resp, err := s.DeleteByID(context.Background(), id)
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")
}