Skip to content

Commit cf5a640

Browse files
authored
Add service version info (#6197)
* Adding compatibility tests and version constant to each service module * Add changelog * Remove BaseVersionCompatibilityTest * Remove unused BaseVersionCompatibilityTest files * Checkstyle fix * Refactor test to fail only when old version of core is used * Checkstyle * Remove codegen test * Refactor test, edit changelog * Fix comment
1 parent 81cf9cc commit cf5a640

File tree

6 files changed

+154
-0
lines changed

6 files changed

+154
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Adding constant to each SDK module to represent its version"
6+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask;
2323
import software.amazon.awssdk.codegen.poet.client.EnvironmentTokenSystemSettingsClass;
2424
import software.amazon.awssdk.codegen.poet.client.SdkClientOptions;
25+
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
2526
import software.amazon.awssdk.codegen.poet.common.UserAgentUtilsSpec;
2627

2728
public class CommonInternalGeneratorTasks extends BaseGeneratorTasks {
@@ -40,6 +41,7 @@ protected List<GeneratorTask> createTasks() throws Exception {
4041
if (params.getModel().getCustomizationConfig().isEnableEnvironmentBearerToken()) {
4142
tasks.add(createEnvironmentTokenSystemSettingTask());
4243
}
44+
tasks.add(createServiceVersionInfoTask());
4345
return tasks;
4446
}
4547

@@ -58,6 +60,11 @@ private GeneratorTask createEnvironmentTokenSystemSettingTask() {
5860
new EnvironmentTokenSystemSettingsClass(params.getModel()));
5961
}
6062

63+
private GeneratorTask createServiceVersionInfoTask() {
64+
return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(),
65+
new ServiceVersionInfoSpec(params.getModel()));
66+
}
67+
6168
private String clientOptionsDir() {
6269
return params.getPathProvider().getClientInternalDirectory();
6370
}

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: 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.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 javax.lang.model.element.Modifier;
25+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
26+
import software.amazon.awssdk.codegen.poet.ClassSpec;
27+
import software.amazon.awssdk.codegen.poet.PoetExtension;
28+
import software.amazon.awssdk.codegen.poet.PoetUtils;
29+
30+
public class ServiceVersionInfoSpec implements ClassSpec {
31+
private final PoetExtension poetExtension;
32+
33+
public ServiceVersionInfoSpec(IntermediateModel model) {
34+
this.poetExtension = new PoetExtension(model);
35+
}
36+
37+
@Override
38+
public TypeSpec poetSpec() {
39+
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo")
40+
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
41+
.addAnnotation(PoetUtils.generatedAnnotation())
42+
.addField(FieldSpec.builder(
43+
String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
44+
.initializer("$S", SDK_VERSION)
45+
.addJavadoc("Returns the current version for the AWS SDK in which"
46+
+ " this class is running.")
47+
.build())
48+
.addMethod(privateConstructor());
49+
50+
return builder.build();
51+
}
52+
53+
protected MethodSpec privateConstructor() {
54+
return MethodSpec.constructorBuilder()
55+
.addModifiers(Modifier.PRIVATE)
56+
.build();
57+
}
58+
59+
@Override
60+
public ClassName className() {
61+
return poetExtension.getServiceVersionInfoClass();
62+
}
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
20+
21+
import com.squareup.javapoet.JavaFile;
22+
import com.squareup.javapoet.TypeSpec;
23+
import java.io.InputStream;
24+
import java.util.Scanner;
25+
import org.junit.jupiter.api.Test;
26+
import software.amazon.awssdk.codegen.poet.ClassSpec;
27+
import software.amazon.awssdk.codegen.poet.ClientTestModels;
28+
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
29+
import software.amazon.awssdk.core.util.VersionInfo;
30+
31+
public class ServiceVersionInfoSpecTest {
32+
33+
// Fixture test that compares generated ServiceVersionInfo class against expected output.
34+
// The fixture file uses {{VERSION}} as a placeholder which gets replaced with the current
35+
// SDK version at test time, since the generated code injects the actual version at build time.
36+
@Test
37+
void testServiceVersionInfoClass() {
38+
String currVersion = VersionInfo.SDK_VERSION;
39+
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels());
40+
41+
String expectedContent = loadFixtureFile("test-service-version-info-class.java");
42+
expectedContent = expectedContent.replace("{{VERSION}}", currVersion);
43+
44+
String actualContent = generateContent(serviceVersionInfoSpec);
45+
46+
assertThat(actualContent).isEqualToIgnoringWhitespace(expectedContent);
47+
}
48+
49+
private String loadFixtureFile(String filename) {
50+
InputStream is = getClass().getResourceAsStream("specs/" + filename);
51+
return new Scanner(is).useDelimiter("\\A").next();
52+
}
53+
54+
private String generateContent(ClassSpec spec) {
55+
TypeSpec typeSpec = spec.poetSpec();
56+
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build();
57+
return javaFile.toString();
58+
}
59+
}
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+
}

0 commit comments

Comments
 (0)