Skip to content

Commit 26e0555

Browse files
authored
make smoke tests Java 8 compatible (#3187)
* make smoke tests Java 8 compatible
1 parent 3b3459d commit 26e0555

File tree

8 files changed

+113
-102
lines changed

8 files changed

+113
-102
lines changed

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/CppBlockWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Block(int indentLevel)
9090

9191
public void addCode(String code, int indentLevel)
9292
{
93-
String linePrefix = " ".repeat(indentLevel);
93+
String linePrefix = new String(new char[indentLevel]).replace('\0', ' ');
9494
//split by line and add indent. make sure to skip escaped newlines
9595
String[] splitCode = code.split("(?<!\\\\)\\n");
9696
for (String line : splitCode) {

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/CppImportContainer.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
package com.amazonaws.util.awsclientsmithygenerator.generators;
77

8+
import com.google.common.collect.ImmutableMap;
9+
import com.google.common.collect.ImmutableSet;
810
import software.amazon.smithy.codegen.core.ImportContainer;
911
import software.amazon.smithy.codegen.core.Symbol;
12+
1013
import java.util.Collections;
1114
import java.util.HashSet;
1215
import java.util.Set;
1316
import java.util.Map;
1417

15-
public final class CppImportContainer implements ImportContainer {
18+
public final class CppImportContainer implements ImportContainer {
1619

1720
private final String c2jNamespace;
1821
private final Set<String> coreHeaders;
@@ -41,21 +44,17 @@ public CppImportContainer(String namespace) {
4144
"algorithm"
4245
);
4346

44-
this.unitTestHeaders = Set.of(
45-
"aws/testing/AwsCppSdkGTestSuite.h",
46-
"aws/testing/AwsTestHelpers.h"
47-
);
47+
this.unitTestHeaders = ImmutableSet.of("aws/testing/AwsCppSdkGTestSuite.h", "aws/testing/AwsTestHelpers.h");
4848

49-
//this will be added to based upon datastructures found
50-
containerHeaderMap = Map.ofEntries(
51-
Map.entry("Aws::String", "aws/core/utils/memory/stl/AWSString.h"),
52-
Map.entry("Aws::Map", "aws/core/utils/memory/stl/AWSMap.h"),
53-
Map.entry("Aws::Utils::DateTime", "aws/core/utils/DateTime.h"),
54-
Map.entry("Aws::Utils::Document","aws/core/utils/Document.h"),
55-
Map.entry("Aws::Utils::ByteBuffer","aws/core/utils/Array.h")
56-
);
57-
58-
dynamicHeaders.add(String.format("aws/%s/%sClient.h", c2jNamespace, clientNamespace));
49+
//this will be added to based upon datastructures found
50+
containerHeaderMap = ImmutableMap.of(
51+
"Aws::String", "aws/core/utils/memory/stl/AWSString.h",
52+
"Aws::Map", "aws/core/utils/memory/stl/AWSMap.h",
53+
"Aws::Utils::DateTime", "aws/core/utils/DateTime.h",
54+
"Aws::Utils::Document", "aws/core/utils/Document.h",
55+
"Aws::Utils::ByteBuffer", "aws/core/utils/Array.h");
56+
57+
dynamicHeaders.add(String.format("aws/%s/%sClient.h", c2jNamespace, clientNamespace));
5958

6059
}
6160

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/CppSymbolVisitor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public Symbol booleanShape(BooleanShape booleanShape) {
8484

8585
@Override
8686
public Symbol listShape(ListShape listShape) {
87-
var targetShape = model.expectShape(listShape.getMember().getTarget());
88-
var targetSymbol = targetShape.accept(this);
87+
Shape targetShape = model.expectShape(listShape.getMember().getTarget());
88+
Symbol targetSymbol = targetShape.accept(this);
8989
return createSymbolBuilder(listShape, "Aws::Vector<" + targetSymbol + ">")
9090
.addReference(targetSymbol)
9191
.build();
@@ -96,10 +96,10 @@ public Symbol listShape(ListShape listShape) {
9696
*/
9797
@Override
9898
public Symbol mapShape(MapShape mapShape) {
99-
var keyShape = model.expectShape(mapShape.getKey().getTarget());
100-
var keyType = keyShape.accept(this);
101-
var valueShape = model.expectShape(mapShape.getValue().getTarget());
102-
var valueType = valueShape.accept(this);
99+
Shape keyShape = model.expectShape(mapShape.getKey().getTarget());
100+
Symbol keyType = keyShape.accept(this);
101+
Shape valueShape = model.expectShape(mapShape.getValue().getTarget());
102+
Symbol valueType = valueShape.accept(this);
103103
return createSymbolBuilder(mapShape, "Aws::Map<"+keyType +"," + valueType + ">")
104104
.addReference(valueType)
105105
.build();

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/SmithyC2JNamespaceMap.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.lang.reflect.Type;
88
import java.util.Map;
9-
9+
import java.util.HashMap;
1010
import com.google.gson.Gson;
1111
import com.google.gson.reflect.TypeToken;
1212

@@ -17,9 +17,16 @@ public class SmithyC2JNamespaceMap {
1717

1818
private SmithyC2JNamespaceMap(String jsonString){
1919

20-
Type mapType = new TypeToken<Map<String, String>>(){}.getType();
21-
Gson gson = new Gson();
22-
this.SMITHY_C2J_SERVICE_NAME_MAP = gson.fromJson(jsonString, mapType);
20+
if(jsonString.isEmpty())
21+
{
22+
this.SMITHY_C2J_SERVICE_NAME_MAP = new HashMap<>();
23+
}
24+
else {
25+
Type mapType = new TypeToken<Map<String, String>>() {
26+
}.getType();
27+
Gson gson = new Gson();
28+
this.SMITHY_C2J_SERVICE_NAME_MAP = gson.fromJson(jsonString, mapType);
29+
}
2330
}
2431

2532
//This method must be called once to construct an internal mapping of smithy to c2j namespace

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/SmokeTestsCMakeWriter.java

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,55 @@ public String generate()
1919
String projectNameCaps = folderNamespace.toUpperCase();
2020
String awsTestSrc = "AWS_" + projectNameCaps + "_GENERATED_SMOKE_TEST_SRC" ;
2121

22-
write("""
23-
add_project($L-smoke-tests
24-
\"Tests for the AWS $L C++ SDK\"
25-
testing-resources
26-
aws-cpp-sdk-$L
27-
aws-cpp-sdk-core
28-
)
29-
""",folderNamespace, projectNameCaps,folderNamespace
22+
write(
23+
"add_project(" +
24+
folderNamespace + "-smoke-tests\n" +
25+
"\"Tests for the AWS " + projectNameCaps + " C++ SDK\"\n" +
26+
"testing-resources\n" +
27+
"aws-cpp-sdk-" + folderNamespace + "\n" +
28+
"aws-cpp-sdk-core\n" +
29+
")"
3030
);
3131

32-
write("""
33-
file(GLOB AWS_$L_GENERATED_SMOKE_TEST_SRC
34-
\"$${CMAKE_CURRENT_SOURCE_DIR}/../RunTests.cpp\"
35-
\"$${CMAKE_CURRENT_SOURCE_DIR}/*.cpp\"
36-
)
37-
if(MSVC AND BUILD_SHARED_LIBS)
38-
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
39-
endif()
40-
41-
if (CMAKE_CROSSCOMPILING)
42-
set(AUTORUN_UNIT_TESTS OFF)
43-
endif()
44-
45-
if (AUTORUN_UNIT_TESTS)
46-
enable_testing()
47-
endif()
48-
""",projectNameCaps
32+
write(
33+
"file(GLOB AWS_$L_GENERATED_SMOKE_TEST_SRC\n" +
34+
"\"$${CMAKE_CURRENT_SOURCE_DIR}/../RunTests.cpp\"\n" +
35+
"\"$${CMAKE_CURRENT_SOURCE_DIR}/*.cpp\"\n" +
36+
")\n" +
37+
"if(MSVC AND BUILD_SHARED_LIBS)\n" +
38+
" add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)\n" +
39+
"endif()\n" +
40+
"\n" +
41+
"if (CMAKE_CROSSCOMPILING)\n" +
42+
" set(AUTORUN_UNIT_TESTS OFF)\n" +
43+
"endif()\n" +
44+
"\n" +
45+
"if (AUTORUN_UNIT_TESTS)\n" +
46+
" enable_testing()\n" +
47+
"endif()\n",
48+
projectNameCaps
4949
);
5050

51+
write(
52+
"if(PLATFORM_ANDROID AND BUILD_SHARED_LIBS)\n" +
53+
" add_library($${PROJECT_NAME} \"$${$L}\")\n" +
54+
"else()\n" +
55+
" add_executable($${PROJECT_NAME} \"$${$L}\")\n" +
56+
"endif()\n\n" +
57+
"set_compiler_flags($${PROJECT_NAME})\n" +
58+
"set_compiler_warnings($${PROJECT_NAME})\n\n" +
59+
"target_include_directories($${PROJECT_NAME} PUBLIC\n" +
60+
" $${CMAKE_CURRENT_SOURCE_DIR}/../../src/aws-cpp-sdk-$L/include)\n\n" +
61+
"target_link_libraries($${PROJECT_NAME}\n" +
62+
" $${PROJECT_LIBS})\n",
63+
awsTestSrc, awsTestSrc, folderNamespace
64+
);
5165

52-
write("""
53-
if(PLATFORM_ANDROID AND BUILD_SHARED_LIBS)
54-
add_library($${PROJECT_NAME} "$${$L}")
55-
else()
56-
add_executable($${PROJECT_NAME} "$${$L}")
57-
endif()
58-
59-
set_compiler_flags($${PROJECT_NAME})
60-
set_compiler_warnings($${PROJECT_NAME})
61-
62-
target_include_directories($${PROJECT_NAME} PUBLIC
63-
$${CMAKE_CURRENT_SOURCE_DIR}/../../src/aws-cpp-sdk-$L/include)
64-
65-
target_link_libraries($${PROJECT_NAME}
66-
$${PROJECT_LIBS})
67-
""", awsTestSrc, awsTestSrc,folderNamespace);
68-
69-
write("""
70-
if(NOT CMAKE_CROSSCOMPILING)
71-
SET_TARGET_PROPERTIES($${PROJECT_NAME} PROPERTIES OUTPUT_NAME $${PROJECT_NAME})
72-
endif()
73-
""");
66+
write(
67+
"if(NOT CMAKE_CROSSCOMPILING)\n" +
68+
" SET_TARGET_PROPERTIES($${PROJECT_NAME} PROPERTIES OUTPUT_NAME $${PROJECT_NAME})\n" +
69+
"endif()\n"
70+
);
7471
return toString();
7572
}
7673

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/SmokeTestsParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static String toKebabCase(String input) {
114114

115115
private String getServiceName(ServiceShape serviceShape)
116116
{
117-
if(serviceShape.getTrait(ServiceTrait.class).isEmpty())
117+
if(!serviceShape.getTrait(ServiceTrait.class).isPresent())
118118
{
119119
throw new RuntimeException(String.format("No service trait detected in service shape with name=%s",serviceShape.getId().getName()));
120120
}

tools/code-generation/smithy/codegen/cpp-smoke-tests-codegen/src/main/java/com/amazonaws/util/awsclientsmithygenerator/generators/SmokeTestsSourceWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ protected void defineTestCase(SmokeTestData test)
4646
//declare test fixture
4747
write("TEST_F($LSmokeTestSuite, $L )",clientNamespace, test.getTestcaseName()).write("{").indent().
4848
write("Aws::$L::$LClientConfiguration clientConfiguration;", clientNamespace,clientNamespace);
49-
if(test.getConfig().getParams() instanceof AwsVendorParams configParams)
49+
if(test.getConfig().getParams() instanceof AwsVendorParams)
5050
{
51+
AwsVendorParams configParams = (AwsVendorParams) test.getConfig().getParams();
5152
if(!configParams.getRegion().isEmpty())
5253
{
5354
write("clientConfiguration.region = \"$L\";",configParams.getRegion());

tools/code-generation/smithy/codegen/cpp-smoke-tests/build.gradle.kts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import software.amazon.smithy.model.Model
22
import software.amazon.smithy.model.node.Node
33
import software.amazon.smithy.model.shapes.ServiceShape
44
import software.amazon.smithy.aws.traits.ServiceTrait
5-
import software.amazon.smithy.model.shapes.OperationShape
5+
import org.gradle.api.logging.Logging
6+
import kotlin.streams.toList
7+
8+
val logger = Logging.getLogger("MyLogger")
9+
610
plugins {
711
id("java-library")
812
id("software.amazon.smithy.gradle.smithy-base").version("1.0.0")
@@ -30,8 +34,6 @@ dependencies {
3034
implementation(codegen.waiters)
3135
implementation(codegen.aws.smoke.test.model)
3236
implementation(codegen.aws.endpoints)
33-
34-
3537
}
3638

3739
tasks.jar {
@@ -56,44 +58,49 @@ tasks.register("generate-smithy-build") {
5658
val c2jMapStr: String = project.findProperty("c2jMap")?.toString() ?: ""
5759

5860
fileTree(models).filter { it.isFile }.files.forEach eachFile@{ file ->
61+
5962
val model = Model.assembler()
6063
.addImport(file.absolutePath)
6164
// Grab the result directly rather than worrying about checking for errors via unwrap.
6265
// All we care about here is the service shape, any unchecked errors will be exposed
6366
// as part of the actual build task done by the smithy gradle plugin.
6467
.assemble().result.get();
65-
val services = model.shapes(ServiceShape::class.javaObjectType).sorted().toList();
68+
69+
val services = model.shapes(ServiceShape::class.java).sorted().toList();
70+
6671
if (services.size != 1) {
6772
throw Exception("There must be exactly one service in each aws model file, but found " +
68-
"${services.size} in ${file.name}: ${services.map { it.id }}");
73+
"${services.size} in ${file.name}");
6974
}
75+
7076
val service = services[0]
71-
72-
val serviceTrait = service.getTrait(ServiceTrait::class.javaObjectType).get();
73-
77+
val serviceTrait = service.getTrait(ServiceTrait::class.java).get()
78+
79+
// Clean up sdkId
7480
val sdkId = serviceTrait.sdkId
75-
.replace(" ", "-")
76-
.replace("_", "-")
77-
.lowercase()
78-
79-
//service names must be match
80-
if (filteredServiceList.isNotEmpty())
81-
{
82-
if(sdkId.toString() !in filteredServiceList)
83-
{
84-
return@eachFile
85-
}
81+
.replace(" ", "-")
82+
.replace("_", "-")
83+
.lowercase()
84+
85+
// Filter by service id if necessary
86+
if (filteredServiceList.isNotEmpty() && sdkId !in filteredServiceList) {
87+
return@eachFile // Skip this file if the sdkId doesn't match
8688
}
89+
println("Processing file: ${file.name}")
8790

91+
// Create projection contents
8892
val projectionContents = Node.objectNodeBuilder()
89-
.withMember("imports", Node.fromStrings("${models.absolutePath}${File.separator}${file.name}"))
90-
.withMember("plugins", Node.objectNode()
91-
.withMember("cpp-codegen-smoke-tests-plugin", Node.objectNodeBuilder()
92-
.withMember("serviceFilter", Node.arrayNode())
93-
.withMember("c2jMap", Node.from(c2jMapStr))
94-
.build()))
95-
.build()
96-
projectionsBuilder.withMember(sdkId + "." + service.version.lowercase(), projectionContents)
93+
.withMember("imports", Node.fromStrings("${models.absolutePath}${File.separator}${file.name}"))
94+
.withMember("plugins", Node.objectNode()
95+
.withMember("cpp-codegen-smoke-tests-plugin", Node.objectNodeBuilder()
96+
.withMember("serviceFilter", Node.arrayNode())
97+
.withMember("c2jMap", Node.from(c2jMapStr))
98+
.build()))
99+
.build()
100+
101+
// Add the projection contents to the projections builder
102+
projectionsBuilder.withMember("$sdkId.${service.version.lowercase()}", projectionContents)
103+
97104
}
98105
val outputDirectoryArg = project.findProperty("outputDirectory")?.toString() ?: "output"
99106

0 commit comments

Comments
 (0)