|
13 | 13 | import java.io.IOException; |
14 | 14 | import java.io.OutputStream; |
15 | 15 | import java.net.URI; |
| 16 | +import java.net.URL; |
16 | 17 | import java.nio.file.Files; |
17 | 18 | import java.nio.file.Path; |
18 | | -import java.nio.file.Paths; |
19 | 19 | import java.util.ArrayList; |
20 | 20 | import java.util.Arrays; |
| 21 | +import java.util.Comparator; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Set; |
23 | 24 | import java.util.stream.Collectors; |
|
33 | 34 | import javax.tools.StandardJavaFileManager; |
34 | 35 | import javax.tools.ToolProvider; |
35 | 36 | import org.junit.jupiter.api.Named; |
36 | | -import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 37 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
37 | 38 | import org.junit.jupiter.api.parallel.Execution; |
38 | 39 | import org.junit.jupiter.api.parallel.ExecutionMode; |
39 | 40 | import org.junit.jupiter.params.ParameterizedTest; |
|
42 | 43 | import software.amazon.smithy.build.PluginContext; |
43 | 44 | import software.amazon.smithy.model.Model; |
44 | 45 | import software.amazon.smithy.model.loader.ModelAssembler; |
| 46 | +import software.amazon.smithy.model.loader.ModelDiscovery; |
45 | 47 | import software.amazon.smithy.model.node.ArrayNode; |
46 | 48 | import software.amazon.smithy.model.node.ObjectNode; |
47 | 49 | import software.amazon.smithy.model.shapes.ServiceShape; |
48 | 50 |
|
49 | 51 | /** |
50 | 52 | * Tests that Java code generation produces compilable code for all AWS service models. |
51 | 53 | * |
52 | | - * <p>Set the {@code API_MODELS_AWS_DIR} environment variable to the root of a local |
53 | | - * checkout of {@code aws/api-models-aws} to enable this test. |
| 54 | + * <p>AWS service models are discovered on the classpath via |
| 55 | + * {@link ModelDiscovery#findModels()}. |
54 | 56 | */ |
55 | | -@EnabledIfEnvironmentVariable(named = "API_MODELS_AWS_DIR", matches = ".*") |
| 57 | +@EnabledIfSystemProperty(named = "awsModelsTests", matches = "true") |
56 | 58 | class AwsModelCodegenCompilationTest { |
57 | 59 |
|
58 | 60 | private static final Set<String> IGNORED_SDK_IDS = Set.of( |
59 | 61 | "timestream-write", |
60 | 62 | "timestream-query", |
61 | | - "clouddirectory" |
| 63 | + "clouddirectory"); |
62 | 64 |
|
63 | | - ); |
64 | | - |
65 | | - private static Path getModelsDir() { |
66 | | - var dir = System.getenv("API_MODELS_AWS_DIR"); |
67 | | - return Paths.get(dir, "models"); |
| 65 | + static Stream<Named<URL>> awsModels() { |
| 66 | + return ModelDiscovery.findModels() |
| 67 | + .stream() |
| 68 | + .filter(url -> url.toString().endsWith(".json")) |
| 69 | + .map(url -> Named.of(artifactName(url), url)) |
| 70 | + .sorted(Comparator.comparing(Named::getName)); |
68 | 71 | } |
69 | 72 |
|
70 | | - static Stream<Named<Path>> awsModels() throws IOException { |
71 | | - var modelsDir = getModelsDir(); |
72 | | - return Files.find(modelsDir, |
73 | | - 4, |
74 | | - (p, a) -> p.toString().endsWith(".json") |
75 | | - && p.getParent().getParent().getFileName().toString().equals("service")) |
76 | | - .map(p -> Named.of(sdkId(modelsDir, p), p)); |
| 73 | + private static String artifactName(URL modelUrl) { |
| 74 | + String urlStr = modelUrl.toString(); |
| 75 | + int bangIdx = urlStr.indexOf("!/"); |
| 76 | + String jarPath = urlStr.substring(0, bangIdx); |
| 77 | + String jarName = jarPath.substring(jarPath.lastIndexOf('/') + 1); |
| 78 | + return jarName.replaceFirst("-\\d[\\d.]*\\.jar$", ""); |
77 | 79 | } |
78 | 80 |
|
79 | 81 | @ParameterizedTest(name = "client: {0}") |
80 | 82 | @MethodSource("awsModels") |
81 | 83 | @Execution(ExecutionMode.CONCURRENT) |
82 | | - void compileGeneratedCode(Path modelPath) { |
83 | | - generateAndCompile(modelPath, "client", "server"); |
| 84 | + void compileGeneratedCode(URL modelUrl) { |
| 85 | + generateAndCompile(modelUrl, "client", "server"); |
84 | 86 | } |
85 | 87 |
|
86 | | - private void generateAndCompile(Path modelPath, String... modes) { |
| 88 | + private void generateAndCompile(URL modelUrl, String... modes) { |
87 | 89 | // 1. Load model |
88 | 90 | Model model = Model.assembler() |
89 | | - .addImport(modelPath) |
| 91 | + .addImport(modelUrl) |
90 | 92 | .putProperty(ModelAssembler.ALLOW_UNKNOWN_TRAITS, true) |
91 | 93 | .disableValidation() |
92 | 94 | .assemble() |
@@ -149,7 +151,7 @@ private void generateAndCompile(Path modelPath, String... modes) { |
149 | 151 | } |
150 | 152 | } |
151 | 153 | } catch (Throwable t) { |
152 | | - if (IGNORED_SDK_IDS.contains(sdkId(getModelsDir(), modelPath))) { |
| 154 | + if (IGNORED_SDK_IDS.contains(artifactName(modelUrl))) { |
153 | 155 | abort("Known failure for " + service.getId() + ": " + t.getMessage()); |
154 | 156 | } |
155 | 157 | sneakyThrow(t); |
@@ -177,10 +179,6 @@ private static Path dumpGeneratedSources(String serviceId, List<JavaFileObject> |
177 | 179 | } |
178 | 180 | } |
179 | 181 |
|
180 | | - private static String sdkId(Path modelsDir, Path modelPath) { |
181 | | - return modelsDir.relativize(modelPath).getName(0).toString(); |
182 | | - } |
183 | | - |
184 | 182 | private static String sanitize(String name) { |
185 | 183 | return name.toLowerCase().replaceAll("[^a-z0-9]", ""); |
186 | 184 | } |
|
0 commit comments