-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsource_ir_test.go
More file actions
181 lines (165 loc) · 5.15 KB
/
Copy pathsource_ir_test.go
File metadata and controls
181 lines (165 loc) · 5.15 KB
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
package raml
import (
"context"
"testing"
"gopkg.in/yaml.v3"
)
// mappingNodeFromYAML unmarshals src and returns the top-level mapping node.
func mappingNodeFromYAML(t *testing.T, src string) *yaml.Node {
t.Helper()
var doc yaml.Node
if err := yaml.Unmarshal([]byte(src), &doc); err != nil {
t.Fatalf("unmarshal yaml: %v", err)
}
if doc.Kind != yaml.DocumentNode || len(doc.Content) == 0 {
t.Fatalf("expected document node with content, got kind=%v", doc.Kind)
}
return doc.Content[0]
}
// scalarKeyNode builds a scalar key node with the given value, for use as the
// method/uri key passed alongside a value mapping.
func scalarKeyNode(value string) *yaml.Node {
return &yaml.Node{Kind: yaml.ScalarNode, Tag: TagStr, Value: value}
}
// bodyKeys returns the ordered list of top-level keys in a stage-1 body node.
func bodyKeys(body *yaml.Node) []string {
if body == nil {
return nil
}
keys := make([]string, 0, len(body.Content)/2)
for i := 0; i < len(body.Content); i += 2 {
keys = append(keys, body.Content[i].Value)
}
return keys
}
func containsStr(s []string, v string) bool {
for _, e := range s {
if e == v {
return true
}
}
return false
}
func TestMakeSourceOperation_DirectivesAndBody(t *testing.T) {
r := New(context.Background())
body := mappingNodeFromYAML(t, `
is: [ pageable, secured ]
securedBy: [ oauth ]
displayName: List things
headers:
X-Trace:
type: string
responses:
200:
body:
application/json: Thing
`)
op, err := r.makeSourceOperation("api.raml", scalarKeyNode("get"), body)
if err != nil {
t.Fatalf("makeSourceOperation: %v", err)
}
if op.Method != "get" {
t.Errorf("Method = %q, want get", op.Method)
}
if len(op.Traits) != 2 || op.Traits[0].Name != "pageable" || op.Traits[1].Name != "secured" {
t.Errorf("Traits = %+v, want [pageable secured]", op.Traits)
}
if !op.explicitSecuredBy || len(op.SecuredBy) != 1 || op.SecuredBy[0].Name != "oauth" {
t.Errorf("SecuredBy = %+v explicit=%v, want [oauth] explicit=true", op.SecuredBy, op.explicitSecuredBy)
}
keys := bodyKeys(op.body)
if containsStr(keys, FacetIs) || containsStr(keys, FacetSecuredBy) {
t.Errorf("body must not contain directive keys, got %v", keys)
}
for _, want := range []string{"displayName", "headers", "responses"} {
if !containsStr(keys, want) {
t.Errorf("body missing type-bearing key %q, got %v", want, keys)
}
}
}
func TestMakeSourceOperation_EmptyNullBody(t *testing.T) {
r := New(context.Background())
nullVal := &yaml.Node{Kind: yaml.ScalarNode, Tag: TagNull}
op, err := r.makeSourceOperation("api.raml", scalarKeyNode("delete"), nullVal)
if err != nil {
t.Fatalf("makeSourceOperation: %v", err)
}
if op.body != nil {
t.Errorf("body = %+v, want nil for null operation", op.body)
}
if len(op.Traits) != 0 || len(op.SecuredBy) != 0 {
t.Errorf("expected no directives, got Traits=%v SecuredBy=%v", op.Traits, op.SecuredBy)
}
}
func TestMakeSourceEndPoint_DirectivesOperationsAndNesting(t *testing.T) {
r := New(context.Background())
body := mappingNodeFromYAML(t, `
type: collection
is: [ secured ]
securedBy: [ oauth ]
displayName: Products
uriParameters:
id:
type: string
get:
description: list
/items:
get:
description: nested list
`)
ep, err := r.makeSourceEndPoint(scalarKeyNode("/products"), body, "api.raml", "")
if err != nil {
t.Fatalf("makeSourceEndPoint: %v", err)
}
if ep.URI != "/products" || ep.FullURI != "/products" {
t.Errorf("URI=%q FullURI=%q, want /products /products", ep.URI, ep.FullURI)
}
if ep.ResourceType == nil || ep.ResourceType.Name != "collection" {
t.Errorf("ResourceType = %+v, want collection", ep.ResourceType)
}
if len(ep.Traits) != 1 || ep.Traits[0].Name != "secured" {
t.Errorf("Traits = %+v, want [secured]", ep.Traits)
}
if !ep.explicitSecuredBy || len(ep.SecuredBy) != 1 || ep.SecuredBy[0].Name != "oauth" {
t.Errorf("SecuredBy = %+v explicit=%v, want [oauth] true", ep.SecuredBy, ep.explicitSecuredBy)
}
if _, ok := ep.Operations.Get("get"); !ok {
t.Errorf("expected get operation, ops=%v", ep.Operations.Len())
}
child, ok := ep.EndPoints.Get("/items")
if !ok {
t.Fatalf("expected nested /items endpoint")
}
if child.FullURI != "/products/items" {
t.Errorf("nested FullURI = %q, want /products/items", child.FullURI)
}
if _, ok := child.Operations.Get("get"); !ok {
t.Errorf("expected get on nested endpoint")
}
keys := bodyKeys(ep.body)
for _, forbidden := range []string{FacetType, FacetIs, FacetSecuredBy, "get", "/items"} {
if containsStr(keys, forbidden) {
t.Errorf("body must not contain %q, got %v", forbidden, keys)
}
}
for _, want := range []string{"displayName", "uriParameters"} {
if !containsStr(keys, want) {
t.Errorf("body missing %q, got %v", want, keys)
}
}
}
func TestMakeSourceOperation_CapturesScope(t *testing.T) {
r := New(context.Background())
lib := &Library{Location: "lib.raml"}
want := ParseCtx{AnchorFrag: lib}
r.pushParseCtx(want)
defer r.popParseCtx()
op, err := r.makeSourceOperation("api.raml", scalarKeyNode("get"),
mappingNodeFromYAML(t, "description: x"))
if err != nil {
t.Fatalf("makeSourceOperation: %v", err)
}
if op.scope != want {
t.Errorf("scope = %+v, want %+v", op.scope, want)
}
}