-
Notifications
You must be signed in to change notification settings - Fork 916
Add version compatibility test #6197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RanVaknin
wants to merge
7
commits into
master
Choose a base branch
from
rvaknin/add-version-compatibility-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bb045f
Adding compatibility tests and version constant to each service module
RanVaknin dfc62bb
Add changelog
RanVaknin 06efd2b
Remove BaseVersionCompatibilityTest
RanVaknin 9649f53
Remove unused BaseVersionCompatibilityTest files
RanVaknin 63c51e2
Checkstyle fix
RanVaknin f90abc1
Refactor test to fail only when old version of core is used
RanVaknin d67931b
Checkstyle
RanVaknin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Adding compatibility tests between sdk-core and service packages" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...rc/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.client.specs; | ||
|
||
import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.TypeSpec; | ||
import javax.lang.model.element.Modifier; | ||
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
import software.amazon.awssdk.codegen.poet.PoetExtension; | ||
import software.amazon.awssdk.codegen.poet.PoetUtils; | ||
|
||
public class ServiceVersionInfoSpec implements ClassSpec { | ||
private final PoetExtension poetExtension; | ||
|
||
public ServiceVersionInfoSpec(IntermediateModel model) { | ||
this.poetExtension = new PoetExtension(model); | ||
} | ||
|
||
@Override | ||
public TypeSpec poetSpec() { | ||
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo") | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.addAnnotation(PoetUtils.generatedAnnotation()) | ||
.addField(FieldSpec.builder( | ||
String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.initializer("$S", SDK_VERSION) | ||
.addJavadoc("Returns the current version for the AWS SDK in which" | ||
+ " this class is running.") | ||
.build()) | ||
.addMethod(privateConstructor()); | ||
|
||
return builder.build(); | ||
} | ||
|
||
protected MethodSpec privateConstructor() { | ||
return MethodSpec.constructorBuilder() | ||
.addModifiers(Modifier.PRIVATE) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ClassName className() { | ||
return poetExtension.getServiceVersionInfoClass(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...n/java/software/amazon/awssdk/codegen/poet/client/specs/VersionCompatibilityTestSpec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.client.specs; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.TypeSpec; | ||
import javax.lang.model.element.Modifier; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
import software.amazon.awssdk.codegen.poet.PoetUtils; | ||
|
||
public class VersionCompatibilityTestSpec implements ClassSpec { | ||
private final IntermediateModel model; | ||
|
||
public VersionCompatibilityTestSpec(IntermediateModel model) { | ||
this.model = model; | ||
} | ||
|
||
@Override | ||
public TypeSpec poetSpec() { | ||
return PoetUtils.createClassBuilder(className()) | ||
.addModifiers(Modifier.PUBLIC) | ||
.addMethod(compatibilityTest()) | ||
.addMethod(isVersionCompatibleMethod()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ClassName className() { | ||
return ClassName.get(model.getMetadata().getFullClientPackageName(), "VersionCompatibilityTest"); | ||
} | ||
|
||
private MethodSpec compatibilityTest() { | ||
ClassName serviceVersionInfo = ClassName.get( | ||
model.getMetadata().getFullInternalPackageName(), | ||
"ServiceVersionInfo" | ||
); | ||
|
||
ClassName versionInfo = ClassName.get("software.amazon.awssdk.core.util", "VersionInfo"); | ||
ClassName assertions = ClassName.get("org.assertj.core.api", "Assertions"); | ||
|
||
return MethodSpec.methodBuilder("checkCompatibility") | ||
.addModifiers(Modifier.PUBLIC) | ||
.addAnnotation(Test.class) | ||
.returns(void.class) | ||
.addStatement("String coreVersion = $T.SDK_VERSION", versionInfo) | ||
.addStatement("String serviceVersion = $T.VERSION", serviceVersionInfo) | ||
.addStatement("$T.assertThat(isVersionCompatible(coreVersion, serviceVersion))" + | ||
".withFailMessage(\"Core version %s must be equal to or newer than service version %s\", " | ||
+ "coreVersion, serviceVersion).isTrue()", | ||
assertions) | ||
.build(); | ||
} | ||
|
||
private MethodSpec isVersionCompatibleMethod() { | ||
return MethodSpec.methodBuilder("isVersionCompatible") | ||
.addModifiers(Modifier.PRIVATE) | ||
.returns(boolean.class) | ||
.addParameter(String.class, "coreVersion") | ||
.addParameter(String.class, "serviceVersion") | ||
.addStatement("String normalizedCore = coreVersion.replace(\"-SNAPSHOT\", \"\")") | ||
.addStatement("String normalizedService = serviceVersion.replace(\"-SNAPSHOT\", \"\")") | ||
.addStatement("String[] coreParts = normalizedCore.split(\"\\\\.\")") | ||
.addStatement("String[] serviceParts = normalizedService.split(\"\\\\.\")") | ||
.addCode("\n") | ||
.addStatement("int coreMajor = Integer.parseInt(coreParts[0])") | ||
.addStatement("int serviceMajor = Integer.parseInt(serviceParts[0])") | ||
.beginControlFlow("if (coreMajor != serviceMajor)") | ||
.addStatement("return coreMajor >= serviceMajor") | ||
.endControlFlow() | ||
.addCode("\n") | ||
.addStatement("int coreMinor = Integer.parseInt(coreParts[1])") | ||
.addStatement("int serviceMinor = Integer.parseInt(serviceParts[1])") | ||
.beginControlFlow("if (coreMinor != serviceMinor)") | ||
.addStatement("return coreMinor >= serviceMinor") | ||
.endControlFlow() | ||
.addCode("\n") | ||
.addStatement("int corePatch = Integer.parseInt(coreParts[2])") | ||
.addStatement("int servicePatch = Integer.parseInt(serviceParts[2])") | ||
.addStatement("return corePatch >= servicePatch") | ||
.build(); | ||
} | ||
} | ||
|
63 changes: 63 additions & 0 deletions
63
.../src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.client; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import com.squareup.javapoet.JavaFile; | ||
import com.squareup.javapoet.TypeSpec; | ||
import java.io.InputStream; | ||
import java.util.Scanner; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
import software.amazon.awssdk.codegen.poet.ClientTestModels; | ||
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec; | ||
import software.amazon.awssdk.core.util.VersionInfo; | ||
|
||
public class ServiceVersionInfoSpecTest { | ||
|
||
// a fixture test that dynamically updates the generated fixture with the current version | ||
// this is needed because every time codegen runs, the version will change. | ||
// we need a way to generate the fixture, and then edit it in place with the current version and only then make the assertion. | ||
@Test | ||
void testServiceVersionInfoClass() { | ||
String currVersion = VersionInfo.SDK_VERSION; | ||
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels()); | ||
|
||
String expectedContent = loadFixtureFile("test-service-version-info-class.java"); | ||
String[] parts = expectedContent.split("public static final String VERSION = \""); | ||
if (parts.length == 2) { | ||
String privateConstructor = parts[1].substring(parts[1].indexOf("\"")); | ||
expectedContent = parts[0] + "public static final String VERSION = \"" + currVersion | ||
+ privateConstructor; | ||
} | ||
|
||
String actualContent = generateContent(serviceVersionInfoSpec); | ||
|
||
assertThat(actualContent).isEqualTo(expectedContent); | ||
} | ||
|
||
private String loadFixtureFile(String filename) { | ||
InputStream is = getClass().getResourceAsStream("specs/" + filename); | ||
return new Scanner(is).useDelimiter("\\A").next(); | ||
} | ||
|
||
private String generateContent(ClassSpec spec) { | ||
TypeSpec typeSpec = spec.poetSpec(); | ||
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build(); | ||
return javaFile.toString(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ces/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package software.amazon.awssdk.services.json.internal; | ||
|
||
import java.lang.String; | ||
import software.amazon.awssdk.annotations.Generated; | ||
|
||
@Generated("software.amazon.awssdk:codegen") | ||
public final class ServiceVersionInfo { | ||
/** | ||
* Returns the current version for the AWS SDK in which this class is running. | ||
*/ | ||
public static final String VERSION = "{{VERSION}}"; | ||
|
||
private ServiceVersionInfo() { | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think generating a fixture test for a code generated test might be overkill, but for better readability here is what this generated test would look like for SQS: