Skip to content

Commit a56fbab

Browse files
committed
feat(cts): add custom request tests
1 parent 0edc896 commit a56fbab

File tree

16 files changed

+1264
-35
lines changed

16 files changed

+1264
-35
lines changed

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.collect.ImmutableMap.Builder;
88
import com.samskivert.mustache.Mustache.Lambda;
99
import io.swagger.v3.core.util.Json;
10+
import io.swagger.v3.oas.models.media.Schema;
1011
import java.io.File;
1112
import java.io.IOException;
1213
import java.util.*;
@@ -170,6 +171,9 @@ public Map<String, Object> postProcessSupportingFileData(
170171
System.out.println(e.getMessage());
171172
System.exit(0);
172173
}
174+
175+
System.out.println(e.getMessage());
176+
System.exit(1);
173177
} catch (Exception e) {
174178
e.printStackTrace();
175179
System.exit(1);
@@ -181,15 +185,28 @@ private Map<String, Request[]> loadCTS()
181185
throws JsonParseException, JsonMappingException, IOException, CTSException {
182186
TreeMap<String, Request[]> cts = new TreeMap<>();
183187
File dir = new File("tests/CTS/methods/requests/" + client);
188+
File commonTestDir = new File("tests/CTS/methods/requests/common");
184189
if (!dir.exists()) {
185190
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true);
186191
}
192+
if (!commonTestDir.exists()) {
193+
throw new CTSException(
194+
"CTS not found at " + commonTestDir.getAbsolutePath(),
195+
true
196+
);
197+
}
187198
for (File f : dir.listFiles()) {
188199
cts.put(
189200
f.getName().replace(".json", ""),
190201
Json.mapper().readValue(f, Request[].class)
191202
);
192203
}
204+
for (File f : commonTestDir.listFiles()) {
205+
cts.put(
206+
f.getName().replace(".json", ""),
207+
Json.mapper().readValue(f, Request[].class)
208+
);
209+
}
193210
return cts;
194211
}
195212

generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ private void handleModel(
200200
String parent,
201201
int suffix
202202
) throws CTSException {
203-
assert (spec.getHasVars());
204-
assert (spec.getItems() == null);
203+
if (!spec.getHasVars()) {
204+
throw new CTSException("Spec has no vars.");
205+
}
206+
207+
if (spec.getItems() != null) {
208+
throw new CTSException("Spec has items.");
209+
}
205210

206211
if (
207212
spec instanceof CodegenModel && ((CodegenModel) spec).oneOf.size() > 0
@@ -269,8 +274,13 @@ private void handleObject(
269274
IJsonSchemaValidationProperties spec,
270275
int suffix
271276
) throws CTSException {
272-
assert (!spec.getHasVars());
273-
assert (spec.getItems() == null);
277+
if (spec.getHasVars()) {
278+
throw new CTSException("Spec has vars.");
279+
}
280+
281+
if (spec.getItems() != null) {
282+
throw new CTSException("Spec has items.");
283+
}
274284

275285
Map<String, Object> vars = (Map<String, Object>) param;
276286

@@ -300,18 +310,37 @@ private void handleMap(
300310
IJsonSchemaValidationProperties spec,
301311
int suffix
302312
) throws CTSException {
303-
assert (!spec.getHasVars());
304-
assert (spec.getItems() != null);
313+
if (spec.getHasVars()) {
314+
throw new CTSException("Spec has vars.");
315+
}
305316

306317
Map<String, Object> vars = (Map<String, Object>) param;
307318

308319
List<Object> values = new ArrayList<>();
320+
321+
CodegenProperty items = spec.getItems();
322+
309323
for (Entry<String, Object> entry : vars.entrySet()) {
324+
IJsonSchemaValidationProperties itemType = items;
325+
326+
// The generator consider a free form object type as an `object`, which
327+
// is wrong in our case, so we infer it.
328+
if (
329+
items == null ||
330+
(items.openApiType.equals("object") && items.isFreeFormObject)
331+
) {
332+
CodegenParameter maybeMatch = new CodegenParameter();
333+
String paramType = inferDataType(entry.getValue(), maybeMatch, null);
334+
335+
maybeMatch.dataType = paramType;
336+
itemType = maybeMatch;
337+
}
338+
310339
values.add(
311340
traverseParams(
312341
entry.getKey(),
313342
entry.getValue(),
314-
spec.getItems(),
343+
itemType,
315344
paramName,
316345
suffix + 1
317346
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"method": "del",
4+
"testName": "allow del method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "DELETE"
11+
}
12+
},
13+
{
14+
"method": "del",
15+
"testName": "allow del method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
},
21+
"body": {
22+
"body": "parameters"
23+
}
24+
},
25+
"request": {
26+
"path": "/1/test/all",
27+
"method": "DELETE",
28+
"data": {
29+
"body": "parameters"
30+
},
31+
"searchParams": {
32+
"query": "parameters"
33+
}
34+
}
35+
}
36+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"method": "get",
4+
"testName": "allow get method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "GET"
11+
}
12+
},
13+
{
14+
"method": "get",
15+
"testName": "allow get method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
}
21+
},
22+
"request": {
23+
"path": "/1/test/all",
24+
"method": "GET",
25+
"searchParams": {
26+
"query": "parameters"
27+
}
28+
}
29+
}
30+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"method": "post",
4+
"testName": "allow post method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "POST"
11+
}
12+
},
13+
{
14+
"method": "post",
15+
"testName": "allow post method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
},
21+
"body": {
22+
"body": "parameters"
23+
}
24+
},
25+
"request": {
26+
"path": "/1/test/all",
27+
"method": "POST",
28+
"data": {
29+
"body": "parameters"
30+
},
31+
"searchParams": {
32+
"query": "parameters"
33+
}
34+
}
35+
}
36+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"method": "put",
4+
"testName": "allow put method for a custom path with minimal parameters",
5+
"parameters": {
6+
"path": "/test/minimal"
7+
},
8+
"request": {
9+
"path": "/1/test/minimal",
10+
"method": "PUT"
11+
}
12+
},
13+
{
14+
"method": "put",
15+
"testName": "allow put method for a custom path with all parameters",
16+
"parameters": {
17+
"path": "/test/all",
18+
"parameters": {
19+
"query": "parameters"
20+
},
21+
"body": {
22+
"body": "parameters"
23+
}
24+
},
25+
"request": {
26+
"path": "/1/test/all",
27+
"method": "PUT",
28+
"data": {
29+
"body": "parameters"
30+
},
31+
"searchParams": {
32+
"query": "parameters"
33+
}
34+
}
35+
}
36+
]

tests/CTS/methods/requests/templates/java/requests.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class {{client}}Tests {
5050

5151
{{#request.searchParams}}
5252
HashMap<String, String> expectedQuery = JSON.deserialize("{{#lambda.escapequotes}}{{{request.searchParams}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
53-
List<Pair> acutalQuery = req.getQueryParams();
54-
for (Pair p : acutalQuery) {
53+
List<Pair> actualQuery = req.getQueryParams();
54+
for (Pair p : actualQuery) {
5555
assertEquals(expectedQuery.get(p.getName()), p.getValue());
5656
}
5757
{{/request.searchParams}}

0 commit comments

Comments
 (0)