Skip to content

Commit 9b3037b

Browse files
RocMarshal1996fanruisnuyanzin
authored
[FLINK-39359][docs] Introduce integration test cases to ensure that the generated REST-API related documentation stays consistent with the code. (#27884)
Co-authored-by: 1996fanrui <1996fanrui@gmail.com> Co-authored-by: Sergey Nuyanzin <snuyanzin@gmail.com>
1 parent 463330c commit 9b3037b

File tree

8 files changed

+188
-16
lines changed

8 files changed

+188
-16
lines changed

flink-docs/src/main/java/org/apache/flink/docs/rest/RuntimeOpenApiSpecGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
public class RuntimeOpenApiSpecGenerator {
3838

39+
public static final String RUNTIME_OPEN_API_TITLE = "Flink JobManager REST API";
40+
3941
/**
4042
* Generates the Runtime REST API OpenAPI spec.
4143
*
@@ -51,7 +53,7 @@ public static void main(String[] args) throws IOException, ConfigurationExceptio
5153
continue;
5254
}
5355
createDocumentationFile(
54-
"Flink JobManager REST API",
56+
RUNTIME_OPEN_API_TITLE,
5557
new DocumentingDispatcherRestEndpoint(),
5658
apiVersion,
5759
Paths.get(

flink-docs/src/main/java/org/apache/flink/docs/rest/SqlGatewayOpenApiSpecGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
public class SqlGatewayOpenApiSpecGenerator {
3838

39+
public static final String SQL_GATEWAY_OPEN_API_TITLE = "Flink SQL Gateway REST API";
40+
3941
/**
4042
* Generates the Sql Gateway REST API OpenAPI spec.
4143
*
@@ -51,7 +53,7 @@ public static void main(String[] args) throws IOException, ConfigurationExceptio
5153
continue;
5254
}
5355
createDocumentationFile(
54-
"Flink SQL Gateway REST API",
56+
SQL_GATEWAY_OPEN_API_TITLE,
5557
new DocumentingSqlGatewayRestEndpoint(),
5658
apiVersion,
5759
Paths.get(

flink-docs/src/main/java/org/apache/flink/docs/util/Utils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.flink.docs.util;
2020

21+
import java.io.File;
22+
2123
/** Contains various shared utility functions. */
2224
public enum Utils {
2325
;
@@ -34,4 +36,15 @@ public static String escapeCharacters(String value) {
3436
.replaceAll(">", "&gt;")
3537
.replaceAll(TEMPORARY_PLACEHOLDER, "<wbr>");
3638
}
39+
40+
public static String getProjectRootDir() {
41+
final String dirFromProperty = System.getProperty("rootDir");
42+
if (dirFromProperty != null) {
43+
return dirFromProperty;
44+
}
45+
46+
// to make this work in the IDE use a default fallback
47+
final String currentDir = System.getProperty("user.dir");
48+
return new File(currentDir).getParent();
49+
}
3750
}

flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocGeneratorTest.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Map;
5050

5151
import static org.apache.flink.configuration.description.TextElement.text;
52+
import static org.apache.flink.docs.util.Utils.getProjectRootDir;
5253
import static org.assertj.core.api.Assertions.assertThat;
5354
import static org.assertj.core.api.Assertions.assertThatException;
5455
import static org.assertj.core.api.Assertions.assertThatNoException;
@@ -768,15 +769,4 @@ void testVerifyClassAnnotation() {
768769
ConfigOptionsDocGenerator.verifyClassAnnotation(
769770
InternalOptions.class));
770771
}
771-
772-
static String getProjectRootDir() {
773-
final String dirFromProperty = System.getProperty("rootDir");
774-
if (dirFromProperty != null) {
775-
return dirFromProperty;
776-
}
777-
778-
// to make this work in the IDE use a default fallback
779-
final String currentDir = System.getProperty("user.dir");
780-
return new File(currentDir).getParent();
781-
}
782772
}

flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocsCompletenessITCase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.flink.configuration.ConfigOption;
2323
import org.apache.flink.docs.util.ConfigurationOptionLocator;
2424
import org.apache.flink.docs.util.OptionWithMetaInfo;
25+
import org.apache.flink.docs.util.Utils;
2526

2627
import org.jsoup.Jsoup;
2728
import org.jsoup.nodes.Document;
@@ -232,7 +233,7 @@ private static void compareDocumentedAndExistingOptions(
232233
}
233234

234235
private static Map<String, List<DocumentedOption>> parseDocumentedOptions() throws IOException {
235-
final String rootDir = ConfigOptionsDocGeneratorTest.getProjectRootDir();
236+
final String rootDir = Utils.getProjectRootDir();
236237

237238
Path includeFolder =
238239
Paths.get(rootDir, "docs", "layouts", "shortcodes", "generated").toAbsolutePath();
@@ -284,7 +285,7 @@ private static Collection<DocumentedOption> parseDocumentedOptionsFromFile(Path
284285

285286
private static Map<String, List<ExistingOption>> findExistingOptions(
286287
Predicate<OptionWithMetaInfo> predicate) throws Exception {
287-
final String rootDir = ConfigOptionsDocGeneratorTest.getProjectRootDir();
288+
final String rootDir = Utils.getProjectRootDir();
288289

289290
final Collection<ExistingOption> existingOptions = new ArrayList<>();
290291
new ConfigurationOptionLocator()

flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsYamlSpecTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Collectors;
3030
import java.util.stream.Stream;
3131

32+
import static org.apache.flink.docs.util.Utils.getProjectRootDir;
3233
import static org.assertj.core.api.Assertions.assertThat;
3334

3435
/**
@@ -44,7 +45,7 @@ void testNoKeyPrefixOfOtherKey() throws Exception {
4445
final Collection<String> allOptions = new ArrayList<>();
4546
new ConfigurationOptionLocator()
4647
.discoverOptionsAndApply(
47-
Paths.get(ConfigOptionsDocGeneratorTest.getProjectRootDir()),
48+
Paths.get(getProjectRootDir()),
4849
(aClass, optionWithMetaInfos) -> {
4950
optionWithMetaInfos.stream()
5051
.filter(o -> o.field.getAnnotation(Deprecated.class) == null)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.flink.docs.rest;
20+
21+
import org.apache.flink.docs.util.Utils;
22+
import org.apache.flink.runtime.rest.util.DocumentingDispatcherRestEndpoint;
23+
import org.apache.flink.runtime.rest.versioning.RuntimeRestAPIVersion;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
27+
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
30+
31+
import static org.apache.flink.docs.rest.RuntimeOpenApiSpecGenerator.RUNTIME_OPEN_API_TITLE;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Verifies that generated Runtime REST and Open API docs (HTML + OpenAPI yml) match committed
36+
* files.
37+
*
38+
* <p>This acts as a freshness check to ensure the documentation stays in sync with the code.
39+
*/
40+
class RuntimeOpenRestAPIDocsCompletenessITCase {
41+
42+
@Test
43+
void testRuntimeRestApiDocsUpToDate(@TempDir Path tempDir) throws Exception {
44+
final DocumentingDispatcherRestEndpoint endpoint = new DocumentingDispatcherRestEndpoint();
45+
for (final RuntimeRestAPIVersion apiVersion : RuntimeRestAPIVersion.values()) {
46+
if (apiVersion == RuntimeRestAPIVersion.V0) {
47+
continue;
48+
}
49+
final String version = apiVersion.getURLVersionPrefix();
50+
String targetHtmlName = String.format("rest_%s_dispatcher.html", version);
51+
final Path pathOfGeneratedHtml = tempDir.resolve(targetHtmlName);
52+
final Path pathOfCommittedHtml = getPathOfCommittedHtml(targetHtmlName);
53+
RestAPIDocGenerator.createHtmlFile(endpoint, apiVersion, pathOfGeneratedHtml);
54+
assertThat(pathOfCommittedHtml)
55+
.as("Missing committed %s file: %s", targetHtmlName, pathOfCommittedHtml)
56+
.exists()
57+
.as(
58+
"Committed `%s` file is out of date. Please regenerate docs under `flink-docs` module based on `README.md`.",
59+
targetHtmlName)
60+
.hasSameTextualContentAs(pathOfGeneratedHtml);
61+
62+
String targetYmlName = String.format("rest_%s_dispatcher.yml", version);
63+
final Path pathOfGeneratedYml = tempDir.resolve(targetYmlName);
64+
final Path pathOfCommittedYml = getPathOfCommittedYaml(targetYmlName);
65+
OpenApiSpecGenerator.createDocumentationFile(
66+
RUNTIME_OPEN_API_TITLE, endpoint, apiVersion, pathOfGeneratedYml);
67+
assertThat(pathOfCommittedYml)
68+
.as("Missing committed %s file: %s", targetYmlName, pathOfCommittedYml)
69+
.exists()
70+
.as(
71+
"Committed `%s` file is out of date. Please regenerate docs under `flink-docs` module based on `README.md`.",
72+
targetYmlName)
73+
.hasSameTextualContentAs(pathOfGeneratedYml);
74+
}
75+
}
76+
77+
static Path getPathOfCommittedHtml(String fileName) {
78+
final String rootDir = Utils.getProjectRootDir();
79+
return Paths.get(rootDir, "docs", "layouts", "shortcodes", "generated", fileName)
80+
.toAbsolutePath();
81+
}
82+
83+
static Path getPathOfCommittedYaml(String fileName) {
84+
final String rootDir = Utils.getProjectRootDir();
85+
return Paths.get(rootDir, "docs", "static", "generated", fileName).toAbsolutePath();
86+
}
87+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.flink.docs.rest;
20+
21+
import org.apache.flink.table.gateway.rest.util.DocumentingSqlGatewayRestEndpoint;
22+
import org.apache.flink.table.gateway.rest.util.SqlGatewayRestAPIVersion;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.io.TempDir;
26+
27+
import java.nio.file.Path;
28+
29+
import static org.apache.flink.docs.rest.RuntimeOpenRestAPIDocsCompletenessITCase.getPathOfCommittedHtml;
30+
import static org.apache.flink.docs.rest.RuntimeOpenRestAPIDocsCompletenessITCase.getPathOfCommittedYaml;
31+
import static org.apache.flink.docs.rest.SqlGatewayOpenApiSpecGenerator.SQL_GATEWAY_OPEN_API_TITLE;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Verifies that generated SQL Gateway REST and Open API docs (HTML + OpenAPI yml) match committed
36+
* files.
37+
*
38+
* <p>This acts as a freshness check to ensure the documentation stays in sync with the code.
39+
*/
40+
class SqlGatewayOpenRestAPIDocsCompletenessITCase {
41+
42+
@Test
43+
void testSqlGatewayRestApiDocsUpToDate(@TempDir Path tempDir) throws Exception {
44+
final DocumentingSqlGatewayRestEndpoint endpoint = new DocumentingSqlGatewayRestEndpoint();
45+
for (final SqlGatewayRestAPIVersion apiVersion : SqlGatewayRestAPIVersion.values()) {
46+
if (apiVersion == SqlGatewayRestAPIVersion.V0) {
47+
continue;
48+
}
49+
final String version = apiVersion.getURLVersionPrefix();
50+
String targetHtmlName = String.format("rest_%s_sql_gateway.html", version);
51+
final Path pathOfGeneratedHtml = tempDir.resolve(targetHtmlName);
52+
final Path pathOfCommittedHtml = getPathOfCommittedHtml(targetHtmlName);
53+
RestAPIDocGenerator.createHtmlFile(endpoint, apiVersion, pathOfGeneratedHtml);
54+
assertThat(pathOfCommittedHtml)
55+
.as("Missing committed %s file: %s", targetHtmlName, pathOfCommittedHtml)
56+
.exists()
57+
.as(
58+
"Committed `%s` file is out of date. Please regenerate docs under `flink-docs` module based on `README.md`.",
59+
targetHtmlName)
60+
.hasSameTextualContentAs(pathOfGeneratedHtml);
61+
62+
String targetYmlName = String.format("rest_%s_sql_gateway.yml", version);
63+
final Path pathOfGeneratedYml = tempDir.resolve(targetYmlName);
64+
final Path pathOfCommittedYml = getPathOfCommittedYaml(targetYmlName);
65+
OpenApiSpecGenerator.createDocumentationFile(
66+
SQL_GATEWAY_OPEN_API_TITLE, endpoint, apiVersion, pathOfGeneratedYml);
67+
assertThat(pathOfCommittedYml)
68+
.as("Missing committed %s file: %s", targetYmlName, pathOfCommittedYml)
69+
.exists()
70+
.as(
71+
"Committed `%s` file is out of date. Please regenerate docs under `flink-docs` module based on `README.md`.",
72+
targetYmlName)
73+
.hasSameTextualContentAs(pathOfGeneratedYml);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)