Skip to content

Commit 633d6df

Browse files
committed
Adding compatibility tests and version constant to each service module
1 parent fbdc7d3 commit 633d6df

File tree

7 files changed

+255
-0
lines changed

7 files changed

+255
-0
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/CommonInternalGeneratorTasks.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import software.amazon.awssdk.codegen.emitters.GeneratorTask;
2121
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
2222
import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask;
23+
import software.amazon.awssdk.codegen.internal.Utils;
2324
import software.amazon.awssdk.codegen.poet.client.EnvironmentTokenSystemSettingsClass;
2425
import software.amazon.awssdk.codegen.poet.client.SdkClientOptions;
26+
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
27+
import software.amazon.awssdk.codegen.poet.client.specs.VersionCompatibilityTestSpec;
2528
import software.amazon.awssdk.codegen.poet.common.UserAgentUtilsSpec;
2629

2730
public class CommonInternalGeneratorTasks extends BaseGeneratorTasks {
@@ -40,6 +43,8 @@ protected List<GeneratorTask> createTasks() throws Exception {
4043
if (params.getModel().getCustomizationConfig().isEnableEnvironmentBearerToken()) {
4144
tasks.add(createEnvironmentTokenSystemSettingTask());
4245
}
46+
tasks.add(createServiceVersionInfoTask());
47+
tasks.add(createVersionCompatibilityTestTask());
4348
return tasks;
4449
}
4550

@@ -58,6 +63,21 @@ private GeneratorTask createEnvironmentTokenSystemSettingTask() {
5863
new EnvironmentTokenSystemSettingsClass(params.getModel()));
5964
}
6065

66+
private GeneratorTask createServiceVersionInfoTask() {
67+
return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(),
68+
new ServiceVersionInfoSpec(params.getModel()));
69+
}
70+
71+
private GeneratorTask createVersionCompatibilityTestTask() {
72+
return new PoetGeneratorTask(testDir(), params.getModel().getFileHeader(),
73+
new VersionCompatibilityTestSpec(params.getModel()));
74+
}
75+
76+
private String testDir() {
77+
return params.getPathProvider().getTestDirectory() + "/" +
78+
Utils.packageToDirectory(params.getModel().getMetadata().getFullClientInternalPackageName());
79+
}
80+
6181
private String clientOptionsDir() {
6282
return params.getPathProvider().getClientInternalDirectory();
6383
}

codegen/src/main/java/software/amazon/awssdk/codegen/poet/PoetExtension.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public ClassName getUserAgentClass() {
7979
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "UserAgentUtils");
8080
}
8181

82+
public ClassName getServiceVersionInfoClass() {
83+
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "ServiceVersionInfo");
84+
}
85+
8286
public ClassName getEnvironmentTokenSystemSettingsClass() {
8387
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "EnvironmentTokenSystemSettings");
8488
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.poet.client.specs;
17+
18+
import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION;
19+
20+
import com.squareup.javapoet.ClassName;
21+
import com.squareup.javapoet.FieldSpec;
22+
import com.squareup.javapoet.MethodSpec;
23+
import com.squareup.javapoet.TypeSpec;
24+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
25+
import software.amazon.awssdk.codegen.poet.ClassSpec;
26+
import software.amazon.awssdk.codegen.poet.PoetExtension;
27+
import software.amazon.awssdk.codegen.poet.PoetUtils;
28+
29+
import javax.lang.model.element.Modifier;
30+
31+
public class ServiceVersionInfoSpec implements ClassSpec {
32+
private final PoetExtension poetExtension;
33+
34+
public ServiceVersionInfoSpec(IntermediateModel model) {
35+
this.poetExtension = new PoetExtension(model);
36+
}
37+
38+
@Override
39+
public TypeSpec poetSpec() {
40+
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo")
41+
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
42+
.addAnnotation(PoetUtils.generatedAnnotation())
43+
.addField(FieldSpec.builder(
44+
String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
45+
.initializer("$S", SDK_VERSION)
46+
.addJavadoc("Returns the current version for the AWS SDK in which"
47+
+ " this class is running.")
48+
.build())
49+
.addMethod(privateConstructor());
50+
51+
return builder.build();
52+
}
53+
54+
protected MethodSpec privateConstructor() {
55+
return MethodSpec.constructorBuilder()
56+
.addModifiers(Modifier.PRIVATE)
57+
.build();
58+
}
59+
60+
@Override
61+
public ClassName className() {
62+
return poetExtension.getServiceVersionInfoClass();
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.poet.client.specs;
17+
18+
import com.squareup.javapoet.ClassName;
19+
import com.squareup.javapoet.MethodSpec;
20+
import com.squareup.javapoet.TypeSpec;
21+
import org.junit.jupiter.api.Test;
22+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
23+
import software.amazon.awssdk.codegen.poet.ClassSpec;
24+
import software.amazon.awssdk.codegen.poet.PoetUtils;
25+
26+
import javax.lang.model.element.Modifier;
27+
import software.amazon.awssdk.core.rules.testing.BaseVersionCompatibilityTest;
28+
29+
public class VersionCompatibilityTestSpec implements ClassSpec {
30+
private final IntermediateModel model;
31+
32+
public VersionCompatibilityTestSpec(IntermediateModel model) {
33+
this.model = model;
34+
}
35+
36+
@Override
37+
public TypeSpec poetSpec() {
38+
return PoetUtils.createClassBuilder(className())
39+
.superclass(BaseVersionCompatibilityTest.class)
40+
.addModifiers(Modifier.PUBLIC)
41+
.addMethod(compatibilityTest())
42+
.build();
43+
}
44+
45+
@Override
46+
public ClassName className() {
47+
return ClassName.get(model.getMetadata().getFullClientPackageName(), "VersionCompatibilityTest");
48+
}
49+
50+
private MethodSpec compatibilityTest() {
51+
ClassName serviceVersionInfo = ClassName.get(
52+
model.getMetadata().getFullInternalPackageName(),
53+
"ServiceVersionInfo"
54+
);
55+
56+
return MethodSpec.methodBuilder("checkCompatibility")
57+
.addModifiers(Modifier.PUBLIC)
58+
.addAnnotation(Test.class)
59+
.returns(void.class)
60+
.addStatement("verifyVersionCompatibility($T.VERSION)", serviceVersionInfo)
61+
.build();
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.poet.client;
17+
18+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
19+
20+
import com.squareup.javapoet.JavaFile;
21+
import com.squareup.javapoet.TypeSpec;
22+
import java.io.InputStream;
23+
import java.util.Scanner;
24+
import org.junit.jupiter.api.Test;
25+
import software.amazon.awssdk.codegen.poet.ClassSpec;
26+
import software.amazon.awssdk.codegen.poet.ClientTestModels;
27+
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
28+
import software.amazon.awssdk.core.util.VersionInfo;
29+
30+
public class ServiceVersionInfoSpecTest {
31+
32+
// a fixture test that dynamically updates the generated fixture with the current version
33+
// this is needed because every time codegen runs, the version will change.
34+
// we need a way to generate the fixture, and then edit it in place with the current version and only then make the assertion.
35+
@Test
36+
void testServiceVersionInfoClass() {
37+
String currVersion = VersionInfo.SDK_VERSION;
38+
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels());
39+
40+
String expectedContent = loadFixtureFile("test-service-version-info-class.java");
41+
String[] parts = expectedContent.split("public static final String VERSION = \"");
42+
if (parts.length == 2) {
43+
String privateConstructor = parts[1].substring(parts[1].indexOf("\""));
44+
expectedContent = parts[0] + "public static final String VERSION = \"" + currVersion
45+
+ privateConstructor;
46+
}
47+
48+
String actualContent = generateContent(serviceVersionInfoSpec);
49+
50+
assertThat(actualContent).isEqualTo(expectedContent);
51+
}
52+
53+
private String loadFixtureFile(String filename) {
54+
InputStream is = getClass().getResourceAsStream("specs/" + filename);
55+
return new Scanner(is).useDelimiter("\\A").next();
56+
}
57+
58+
private String generateContent(ClassSpec spec) {
59+
TypeSpec typeSpec = spec.poetSpec();
60+
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build();
61+
return javaFile.toString();
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package software.amazon.awssdk.services.json.internal;
2+
3+
import java.lang.String;
4+
import software.amazon.awssdk.annotations.Generated;
5+
6+
@Generated("software.amazon.awssdk:codegen")
7+
public final class ServiceVersionInfo {
8+
/**
9+
* Returns the current version for the AWS SDK in which this class is running.
10+
*/
11+
public static final String VERSION = "{{VERSION}}";
12+
13+
private ServiceVersionInfo() {
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.core.rules.testing;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import software.amazon.awssdk.core.util.VersionInfo;
21+
22+
public class BaseVersionCompatibilityTest {
23+
protected final void verifyVersionCompatibility(String serviceVersion) {
24+
assertThat(VersionInfo.SDK_VERSION).isEqualTo(serviceVersion);
25+
}
26+
}

0 commit comments

Comments
 (0)