Skip to content

Commit dee83e4

Browse files
committed
REST spec: Validate that api name matches file name that contains it
This commit validates that each spec json file contains an API that has the same name as the file
1 parent 2e86357 commit dee83e4

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public ClientYamlSuiteRestApi parse(String location, XContentParser parser) thro
4040
//move to first field name
4141
}
4242

43-
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApi(location, parser.currentName());
43+
String apiName = parser.currentName();
44+
if (location.endsWith(apiName + ".json") == false) {
45+
throw new IllegalArgumentException("API [" + apiName + "] should have the same name as its file [" + location + "]");
46+
}
47+
48+
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApi(location, apiName);
4449

4550
int level = -1;
4651
while (parser.nextToken() != XContentParser.Token.END_OBJECT || level >= 0) {

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.elasticsearch.common.xcontent.XContent;
2222
import org.elasticsearch.common.xcontent.XContentParser;
23-
import org.elasticsearch.common.xcontent.json.JsonXContent;
2423
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
2524
import org.elasticsearch.test.ESTestCase;
2625

@@ -49,7 +48,7 @@ public void testDuplicateMethods() throws Exception {
4948
" }," +
5049
" \"body\": null" +
5150
" }" +
52-
"}", "Found duplicate method [PUT]");
51+
"}", "ping.json", "Found duplicate method [PUT]");
5352
}
5453

5554
public void testDuplicatePaths() throws Exception {
@@ -69,7 +68,7 @@ public void testDuplicatePaths() throws Exception {
6968
" }," +
7069
" \"body\": null" +
7170
" }" +
72-
"}", "Found duplicate path [/pingtwo]");
71+
"}", "ping.json", "Found duplicate path [/pingtwo]");
7372
}
7473

7574
public void testDuplicateParts() throws Exception {
@@ -103,7 +102,7 @@ public void testDuplicateParts() throws Exception {
103102
" }," +
104103
" \"body\": null" +
105104
" }" +
106-
"}", "Found duplicate part [index]");
105+
"}", "ping.json", "Found duplicate part [index]");
107106
}
108107

109108
public void testDuplicateParams() throws Exception {
@@ -135,22 +134,26 @@ public void testDuplicateParams() throws Exception {
135134
" }," +
136135
" \"body\": null" +
137136
" }" +
138-
"}", "Found duplicate param [timeout]");
137+
"}", "ping.json", "Found duplicate param [timeout]");
139138
}
140139

141140
public void testBrokenSpecShouldThrowUsefulExceptionWhenParsingFailsOnParams() throws Exception {
142-
parseAndExpectFailure(BROKEN_SPEC_PARAMS, "Expected params field in rest api definition to contain an object");
141+
parseAndExpectFailure(BROKEN_SPEC_PARAMS, "ping.json", "Expected params field in rest api definition to contain an object");
143142
}
144143

145144
public void testBrokenSpecShouldThrowUsefulExceptionWhenParsingFailsOnParts() throws Exception {
146-
parseAndExpectFailure(BROKEN_SPEC_PARTS, "Expected parts field in rest api definition to contain an object");
145+
parseAndExpectFailure(BROKEN_SPEC_PARTS, "ping.json", "Expected parts field in rest api definition to contain an object");
147146
}
148147

149-
private void parseAndExpectFailure(String brokenJson, String expectedErrorMessage) throws Exception {
148+
public void testSpecNameMatchesFilename() throws Exception {
149+
parseAndExpectFailure("{\"ping\":{}}", "not_matching.json", "API [ping] should have the same name as its file [not_matching.json]");
150+
}
151+
152+
private void parseAndExpectFailure(String brokenJson, String location, String expectedErrorMessage) throws Exception {
150153
XContentParser parser = createParser(YamlXContent.yamlXContent, brokenJson);
151154
ClientYamlSuiteRestApiParser restApiParser = new ClientYamlSuiteRestApiParser();
152155

153-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> restApiParser.parse("location", parser));
156+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> restApiParser.parse(location, parser));
154157
assertThat(e.getMessage(), containsString(expectedErrorMessage));
155158
}
156159

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class ClientYamlSuiteRestApiParserTests extends AbstractClientYamlTestFragmentParserTestCase {
3131
public void testParseRestSpecIndexApi() throws Exception {
3232
parser = createParser(YamlXContent.yamlXContent, REST_SPEC_INDEX_API);
33-
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser);
33+
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("index.json", parser);
3434

3535
assertThat(restApi, notNullValue());
3636
assertThat(restApi.getName(), equalTo("index"));
@@ -47,14 +47,14 @@ public void testParseRestSpecIndexApi() throws Exception {
4747
assertThat(restApi.getPathParts(), hasEntry("id", false));
4848
assertThat(restApi.getParams().size(), equalTo(4));
4949
assertThat(restApi.getParams().keySet(), containsInAnyOrder("wait_for_active_shards", "op_type", "parent", "refresh"));
50-
restApi.getParams().entrySet().stream().forEach(e -> assertThat(e.getValue(), equalTo(false)));
50+
restApi.getParams().entrySet().forEach(e -> assertThat(e.getValue(), equalTo(false)));
5151
assertThat(restApi.isBodySupported(), equalTo(true));
5252
assertThat(restApi.isBodyRequired(), equalTo(true));
5353
}
5454

5555
public void testParseRestSpecGetTemplateApi() throws Exception {
5656
parser = createParser(YamlXContent.yamlXContent, REST_SPEC_GET_TEMPLATE_API);
57-
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser);
57+
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("indices.get_template.json", parser);
5858
assertThat(restApi, notNullValue());
5959
assertThat(restApi.getName(), equalTo("indices.get_template"));
6060
assertThat(restApi.getMethods().size(), equalTo(1));
@@ -71,7 +71,7 @@ public void testParseRestSpecGetTemplateApi() throws Exception {
7171

7272
public void testParseRestSpecCountApi() throws Exception {
7373
parser = createParser(YamlXContent.yamlXContent, REST_SPEC_COUNT_API);
74-
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("location", parser);
74+
ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("count.json", parser);
7575
assertThat(restApi, notNullValue());
7676
assertThat(restApi.getName(), equalTo("count"));
7777
assertThat(restApi.getMethods().size(), equalTo(2));
@@ -83,7 +83,7 @@ public void testParseRestSpecCountApi() throws Exception {
8383
assertThat(restApi.getPaths().get(2), equalTo("/{index}/{type}/_count"));
8484
assertThat(restApi.getPathParts().size(), equalTo(2));
8585
assertThat(restApi.getPathParts().keySet(), containsInAnyOrder("index", "type"));
86-
restApi.getPathParts().entrySet().stream().forEach(e -> assertThat(e.getValue(), equalTo(false)));
86+
restApi.getPathParts().entrySet().forEach(e -> assertThat(e.getValue(), equalTo(false)));
8787
assertThat(restApi.getParams().size(), equalTo(1));
8888
assertThat(restApi.getParams().keySet(), contains("ignore_unavailable"));
8989
assertThat(restApi.getParams(), hasEntry("ignore_unavailable", false));

0 commit comments

Comments
 (0)