-
Notifications
You must be signed in to change notification settings - Fork 5
/
merge_test.go
322 lines (291 loc) · 11.4 KB
/
merge_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package vervet_test
import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/getkin/kin-openapi/openapi3"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/snyk/vervet/v8"
"github.com/snyk/vervet/v8/testdata"
)
var openapiCmp = qt.CmpEquals(cmpopts.IgnoreUnexported(
openapi3.ExampleRef{},
openapi3.HeaderRef{},
openapi3.ParameterRef{},
openapi3.RequestBodyRef{},
openapi3.ResponseRef{},
openapi3.Schema{},
openapi3.SchemaRef{},
openapi3.SecuritySchemeRef{},
))
func TestMergeComponents(t *testing.T) {
c := qt.New(t)
c.Run("component without replace and conflict", func(c *qt.C) {
src := mustLoadFile(c, "merge_test_conflict.yaml")
dst := mustLoadFile(c, "merge_test_dst.yaml")
err := vervet.Merge(dst, src, false)
c.Assert(err, qt.IsNotNil)
})
c.Run("component without replace", func(c *qt.C) {
src := mustLoadFile(c, "merge_test_src.yaml")
dstOrig := mustLoadFile(c, "merge_test_dst.yaml")
dst := mustLoadFile(c, "merge_test_dst.yaml")
err := vervet.Merge(dst, src, false)
c.Assert(err, qt.IsNil)
c.Assert(dst.Components.Schemas["Foo"], openapiCmp, dstOrig.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
c.Assert(dst.Components.Parameters["Foo"], openapiCmp, dstOrig.Components.Parameters["Foo"])
c.Assert(dst.Components.Parameters["Bar"], openapiCmp, src.Components.Parameters["Bar"])
c.Assert(dst.Components.Parameters["Baz"], openapiCmp, dstOrig.Components.Parameters["Baz"])
c.Assert(dst.Components.Headers["Foo"], openapiCmp, dstOrig.Components.Headers["Foo"])
c.Assert(dst.Components.Headers["Bar"], openapiCmp, src.Components.Headers["Bar"])
c.Assert(dst.Components.Headers["Baz"], openapiCmp, dstOrig.Components.Headers["Baz"])
c.Assert(dst.Components.RequestBodies["Foo"], openapiCmp, dstOrig.Components.RequestBodies["Foo"])
c.Assert(dst.Components.RequestBodies["Bar"], openapiCmp, src.Components.RequestBodies["Bar"])
c.Assert(dst.Components.RequestBodies["Baz"], openapiCmp, dstOrig.Components.RequestBodies["Baz"])
c.Assert(dst.Components.Responses["200"], openapiCmp, dstOrig.Components.Responses["200"])
c.Assert(dst.Components.Responses["201"], openapiCmp, src.Components.Responses["201"])
c.Assert(dst.Components.Responses["202"], openapiCmp, dstOrig.Components.Responses["202"])
c.Assert(dst.Components.SecuritySchemes["Foo"], openapiCmp, dstOrig.Components.SecuritySchemes["Foo"])
c.Assert(dst.Components.SecuritySchemes["Bar"], openapiCmp, src.Components.SecuritySchemes["Bar"])
c.Assert(dst.Components.SecuritySchemes["Baz"], openapiCmp, dstOrig.Components.SecuritySchemes["Baz"])
c.Assert(dst.Components.Examples["Foo"], openapiCmp, dstOrig.Components.Examples["Foo"])
c.Assert(dst.Components.Examples["Bar"], openapiCmp, src.Components.Examples["Bar"])
c.Assert(dst.Components.Examples["Baz"], openapiCmp, dstOrig.Components.Examples["Baz"])
})
c.Run("component with replace", func(c *qt.C) {
src := mustLoadFile(c, "merge_test_src.yaml")
dstOrig := mustLoadFile(c, "merge_test_dst.yaml")
dst := mustLoadFile(c, "merge_test_dst.yaml")
err := vervet.Merge(dst, src, true)
c.Assert(err, qt.IsNil)
c.Assert(dst.Components.Schemas["Foo"], openapiCmp, src.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
c.Assert(dst.Components.Parameters["Foo"], openapiCmp, src.Components.Parameters["Foo"])
c.Assert(dst.Components.Parameters["Bar"], openapiCmp, src.Components.Parameters["Bar"])
c.Assert(dst.Components.Parameters["Baz"], openapiCmp, dstOrig.Components.Parameters["Baz"])
c.Assert(dst.Components.Headers["Foo"], openapiCmp, src.Components.Headers["Foo"])
c.Assert(dst.Components.Headers["Bar"], openapiCmp, src.Components.Headers["Bar"])
c.Assert(dst.Components.Headers["Baz"], openapiCmp, dstOrig.Components.Headers["Baz"])
c.Assert(dst.Components.RequestBodies["Foo"], openapiCmp, src.Components.RequestBodies["Foo"])
c.Assert(dst.Components.RequestBodies["Bar"], openapiCmp, src.Components.RequestBodies["Bar"])
c.Assert(dst.Components.RequestBodies["Baz"], openapiCmp, dstOrig.Components.RequestBodies["Baz"])
c.Assert(dst.Components.RequestBodies["200"], openapiCmp, src.Components.RequestBodies["200"])
c.Assert(dst.Components.RequestBodies["201"], openapiCmp, src.Components.RequestBodies["201"])
c.Assert(dst.Components.RequestBodies["202"], openapiCmp, dstOrig.Components.RequestBodies["202"])
c.Assert(dst.Components.SecuritySchemes["Foo"], openapiCmp, src.Components.SecuritySchemes["Foo"])
c.Assert(dst.Components.SecuritySchemes["Bar"], openapiCmp, src.Components.SecuritySchemes["Bar"])
c.Assert(dst.Components.SecuritySchemes["Baz"], openapiCmp, dstOrig.Components.SecuritySchemes["Baz"])
c.Assert(dst.Components.Examples["Foo"], openapiCmp, src.Components.Examples["Foo"])
c.Assert(dst.Components.Examples["Bar"], openapiCmp, src.Components.Examples["Bar"])
c.Assert(dst.Components.Examples["Baz"], openapiCmp, dstOrig.Components.Examples["Baz"])
})
c.Run("component with missing sections", func(c *qt.C) {
src := mustLoadFile(c, "merge_test_src.yaml")
dstOrig := mustLoadFile(c, "merge_test_dst_missing_components.yaml")
dst := mustLoadFile(c, "merge_test_dst_missing_components.yaml")
err := vervet.Merge(dst, src, true)
c.Assert(err, qt.IsNil)
c.Assert(dst.Components.Schemas["Foo"], openapiCmp, src.Components.Schemas["Foo"])
c.Assert(dst.Components.Schemas["Bar"], openapiCmp, src.Components.Schemas["Bar"])
c.Assert(dst.Components.Schemas["Baz"], openapiCmp, dstOrig.Components.Schemas["Baz"])
c.Assert(dst.Components.Parameters["Foo"], openapiCmp, src.Components.Parameters["Foo"])
c.Assert(dst.Components.Parameters["Bar"], openapiCmp, src.Components.Parameters["Bar"])
c.Assert(dst.Components.Parameters["Baz"], openapiCmp, dstOrig.Components.Parameters["Baz"])
c.Assert(dst.Components.Headers["Foo"], openapiCmp, src.Components.Headers["Foo"])
c.Assert(dst.Components.Headers["Bar"], openapiCmp, src.Components.Headers["Bar"])
c.Assert(dst.Components.Headers["Baz"], openapiCmp, dstOrig.Components.Headers["Baz"])
c.Assert(dst.Components.RequestBodies["Foo"], openapiCmp, src.Components.RequestBodies["Foo"])
c.Assert(dst.Components.RequestBodies["Bar"], openapiCmp, src.Components.RequestBodies["Bar"])
c.Assert(dst.Components.RequestBodies["Baz"], openapiCmp, dstOrig.Components.RequestBodies["Baz"])
c.Assert(dst.Components.RequestBodies["200"], openapiCmp, src.Components.RequestBodies["200"])
c.Assert(dst.Components.RequestBodies["201"], openapiCmp, src.Components.RequestBodies["201"])
c.Assert(dst.Components.RequestBodies["202"], openapiCmp, dstOrig.Components.RequestBodies["202"])
c.Assert(dst.Components.SecuritySchemes["Foo"], openapiCmp, src.Components.SecuritySchemes["Foo"])
c.Assert(dst.Components.SecuritySchemes["Bar"], openapiCmp, src.Components.SecuritySchemes["Bar"])
c.Assert(dst.Components.SecuritySchemes["Baz"], openapiCmp, dstOrig.Components.SecuritySchemes["Baz"])
c.Assert(dst.Components.Examples["Foo"], openapiCmp, src.Components.Examples["Foo"])
c.Assert(dst.Components.Examples["Bar"], openapiCmp, src.Components.Examples["Bar"])
c.Assert(dst.Components.Examples["Baz"], openapiCmp, dstOrig.Components.Examples["Baz"])
})
}
func TestMergeTags(t *testing.T) {
srcYaml := `
tags:
- name: foo
description: foo resource (src)
- name: bar
description: bar resource (src)
`
dstYaml := `
tags:
- name: foo
description: foo resource (dst)
- name: baz
description: baz resource (dst)
`
c := qt.New(t)
c.Run("tags without replace", func(c *qt.C) {
src := mustLoad(c, srcYaml)
dst := mustLoad(c, dstYaml)
err := vervet.Merge(dst, src, false)
c.Assert(err, qt.IsNil)
c.Assert(dst.Tags, qt.DeepEquals, openapi3.Tags{{
Name: "bar",
Description: "bar resource (src)",
}, {
Name: "baz",
Description: "baz resource (dst)",
}, {
Name: "foo",
Description: "foo resource (dst)",
}})
})
c.Run("tags with replace", func(c *qt.C) {
src := mustLoad(c, srcYaml)
dst := mustLoad(c, dstYaml)
err := vervet.Merge(dst, src, true)
c.Assert(err, qt.IsNil)
c.Assert(dst.Tags, qt.DeepEquals, openapi3.Tags{{
Name: "bar",
Description: "bar resource (src)",
}, {
Name: "baz",
Description: "baz resource (dst)",
}, {
Name: "foo",
Description: "foo resource (src)",
}})
})
}
func TestMergeTopLevel(t *testing.T) {
srcYaml := `
info:
title: Src
version: src
security:
- Foo: []
- Bar:
- read
- write
servers:
- url: https://example.com/foo
description: Foo (src)
- url: https://example.com/bar
description: Bar (src)
x-extension:
key0: value0
key1: value1
`
dstYaml := `
info:
title: Dst
version: dst
security:
- Foo:
- up
- down
- Baz:
- strange
- crunchy
servers:
- url: https://example.com/foo
description: Foo (dst)
- url: https://example.com/baz
description: Baz (dst)
x-extension:
key1: value11
key2: value2
`
c := qt.New(t)
c.Run("without replace", func(c *qt.C) {
src := mustLoad(c, srcYaml)
dst := mustLoad(c, dstYaml)
err := vervet.Merge(dst, src, false)
c.Assert(err, qt.IsNil)
c.Assert(dst.Info, qt.DeepEquals, &openapi3.Info{
Title: "Dst",
Version: "dst",
})
c.Assert(dst.Security, qt.DeepEquals, openapi3.SecurityRequirements{{
"Foo": []string{"up", "down"},
}, {
"Baz": []string{"strange", "crunchy"},
}})
c.Assert(dst.Servers, qt.DeepEquals, openapi3.Servers{{
URL: "https://example.com/foo",
Description: "Foo (dst)",
}, {
URL: "https://example.com/baz",
Description: "Baz (dst)",
}})
c.Assert(dst.Extensions, qt.DeepEquals, map[string]interface{}{
"x-extension": map[string]interface{}{
"key1": "value11",
"key2": "value2",
},
})
})
c.Run("with replace", func(c *qt.C) {
src := mustLoad(c, srcYaml)
dst := mustLoad(c, dstYaml)
err := vervet.Merge(dst, src, true)
c.Assert(err, qt.IsNil)
c.Assert(dst.Info, qt.DeepEquals, &openapi3.Info{
Title: "Src",
Version: "src",
})
c.Assert(dst.Security, qt.DeepEquals, openapi3.SecurityRequirements{{
"Foo": []string{},
}, {
"Bar": []string{"read", "write"},
}})
c.Assert(dst.Servers, qt.DeepEquals, openapi3.Servers{{
URL: "https://example.com/foo",
Description: "Foo (src)",
}, {
URL: "https://example.com/bar",
Description: "Bar (src)",
}})
c.Assert(dst.Extensions, qt.DeepEquals, map[string]interface{}{
"x-extension": map[string]interface{}{
"key0": "value0",
"key1": "value1",
},
})
})
}
func TestMergeIntoEmpty(t *testing.T) {
c := qt.New(t)
srcYaml := `
info:
title: Src
version: src
paths:
/foo:
get:
description: get a foo
responses:
200:
contents:
application/json:
schema:
type: object
`
src := mustLoad(c, srcYaml)
dst := &openapi3.T{}
err := vervet.Merge(dst, src, false)
c.Assert(err, qt.IsNil)
c.Assert(dst.Paths.Len(), qt.Equals, 1)
}
func mustLoadFile(c *qt.C, path string) *openapi3.T {
doc, err := vervet.NewDocumentFile(testdata.Path(path))
c.Assert(err, qt.IsNil)
return doc.T
}
func mustLoad(c *qt.C, s string) *openapi3.T {
doc, err := openapi3.NewLoader().LoadFromData([]byte(s))
c.Assert(err, qt.IsNil)
return doc
}