-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecTest.java
More file actions
210 lines (183 loc) · 6.71 KB
/
Copy pathSpecTest.java
File metadata and controls
210 lines (183 loc) · 6.71 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
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
package com.retailsvc.http.spec;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.gson.Gson;
import com.retailsvc.http.spec.schema.ObjectSchema;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class SpecTest {
private final Gson gson = new Gson();
@SuppressWarnings("unchecked")
private Map<String, Object> loadJson(String resource) throws Exception {
String text = new String(SpecTest.class.getResourceAsStream("/" + resource).readAllBytes());
return (Map<String, Object>) gson.fromJson(text, Map.class);
}
@Test
void parsesMinimalSpec() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "x", "version", "1"),
"servers", List.of(Map.of("url", "http://localhost/api")),
"paths", Map.of());
Spec spec = Spec.from(raw);
assertThat(spec.openapi()).isEqualTo("3.1.0");
assertThat(spec.info().title()).isEqualTo("x");
assertThat(spec.servers()).hasSize(1);
assertThat(spec.basePath()).isEqualTo("/api");
assertThat(spec.operations()).isEmpty();
}
@Test
void basePathDefaultsToRootWhenServerUrlHasNoPath() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "x", "version", "1"),
"servers", List.of(Map.of("url", "http://127.0.0.1:4444")),
"paths", Map.of());
Spec spec = Spec.from(raw);
assertThat(spec.basePath()).isEqualTo("/");
}
@Test
void parsesPathsWithMethods() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "x", "version", "1"),
"servers", List.of(Map.of("url", "http://localhost")),
"paths",
Map.of(
"/users",
Map.of(
"get", Map.of("operationId", "list", "responses", Map.of()),
"post", Map.of("operationId", "create", "responses", Map.of()))));
Spec spec = Spec.from(raw);
assertThat(spec.operations()).hasSize(2);
assertThat(spec.operations().stream().map(Operation::operationId))
.containsExactlyInAnyOrder("list", "create");
}
@Test
void resolvesSchemaRef() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "x", "version", "1"),
"servers", List.of(Map.of("url", "/")),
"paths", Map.of(),
"components", Map.of("schemas", Map.of("User", Map.of("type", "object"))));
Spec spec = Spec.from(raw);
assertThat(spec.resolveSchema("#/components/schemas/User")).isInstanceOf(ObjectSchema.class);
}
@Test
void parsesExistingFixture() throws Exception {
Spec spec = Spec.from(loadJson("openapi.json"));
assertThat(spec.operations()).isNotEmpty();
}
@Test
void parsesSecuritySchemesFromComponents() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"paths", Map.of(),
"components",
Map.of(
"securitySchemes",
Map.of(
"apiKeyAuth",
Map.of("type", "apiKey", "name", "X-API-Key", "in", "header"))));
Spec spec = Spec.from(raw);
assertThat(spec.securitySchemes()).containsKey("apiKeyAuth");
}
@Test
void parsesRootSecurity() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"paths", Map.of(),
"security", List.of(Map.of("bearerAuth", List.of())));
Spec spec = Spec.from(raw);
assertThat(spec.security()).hasSize(1);
}
@Test
void securitySchemesDefaultsEmpty() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"paths", Map.of());
Spec spec = Spec.from(raw);
assertThat(spec.securitySchemes()).isEmpty();
assertThat(spec.security()).isEmpty();
}
@Test
void operationLevelSecurityOverridesRoot() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"security", List.of(Map.of("bearerAuth", List.of())),
"paths",
Map.of(
"/x",
Map.of(
"get",
Map.of(
"operationId", "getX",
"security", List.of(Map.of("apiKey", List.of())),
"responses", Map.of("200", Map.of("description", "ok"))))));
Spec spec = Spec.from(raw);
Operation op = spec.operations().getFirst();
assertThat(op.security()).isPresent();
assertThat(op.security().get()).hasSize(1);
assertThat(op.security().get().get(0).schemes()).containsKey("apiKey");
}
@Test
void operationEmptySecurityIsPreserved() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"security", List.of(Map.of("bearerAuth", List.of())),
"paths",
Map.of(
"/x",
Map.of(
"get",
Map.of(
"operationId", "getX",
"security", List.of(),
"responses", Map.of("200", Map.of("description", "ok"))))));
Spec spec = Spec.from(raw);
Operation op = spec.operations().getFirst();
assertThat(op.security()).isPresent();
assertThat(op.security().get()).isEmpty();
}
@Test
void operationWithoutSecurityIsEmptyOptional() {
Map<String, Object> raw =
Map.of(
"openapi", "3.1.0",
"info", Map.of("title", "T", "version", "1"),
"servers", List.of(Map.of("url", "/v1")),
"paths",
Map.of(
"/x",
Map.of(
"get",
Map.of(
"operationId",
"getX",
"responses",
Map.of("200", Map.of("description", "ok"))))));
Spec spec = Spec.from(raw);
Operation op = spec.operations().getFirst();
assertThat(op.security()).isEmpty();
}
}