Skip to content

Commit

Permalink
protocol tests generation stub
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Jan 20, 2025
1 parent c21f620 commit 0bff034
Show file tree
Hide file tree
Showing 16 changed files with 666 additions and 30 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ if (LEGACY_BUILD)
option(USE_TLS_V1_2 "Set http client to enforce TLS 1.2" ON)
option(USE_TLS_V1_3 "Set http client to enforce TLS 1.3" OFF)
option(ENABLE_SMOKE_TESTS "Enable smoke tests" OFF)
option(ENABLE_PROTOCOL_TESTS "Enable protocol tests" ON)
option(DISABLE_DNS_REQUIRED_TESTS "Disable unit tests that require DNS lookup to succeed, useful when using a http client that does not perform DNS lookup" OFF)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import lombok.Data;

@Data
public class C2jGiven {
@Data
public class C2jGivenHttp {
private String method;
private String requestUri;
}

@Data
public class C2jGivenInput {
private String shape;
private String locationName;
}

@Data
public class C2jGivenEndpoint {
private String hostPrefix;
}

private String name;
private C2jGivenHttp http;
private C2jGivenInput input;
private C2jGivenEndpoint endpoint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class C2jInputSerialized {
/**
* The HTTP method.
*/
private String method;
/**
* The HTTP body as a string.
*/
private String body;
/**
* The uri path and querystring of the request.
*/
private String uri;
/**
* The URL host of the request.
*/
private String host;
/**
* Any HTTP headers for the HTTP request.
*/
private Map<String, String> headers;
/**
* A list of header names that must not exist in the HTTP Request.
*/
private List<String> forbidHeaders;
/**
* A list of header names that must exist in the HTTP Request.
*/
private List<String> requireHeaders;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;

@Data
public class C2jInputTestCase {
/**
* A test case id.
*/
private String id;
/**
* A test case description.
*/
private String description;
/**
* This corresponds to the JSON object that would define an operation in the service's JSON model.
* Valid keys include http, input, endpoint, and name.
*/
private C2jGiven given;
/**
* The input parameters a user would provide.
*/
private JsonNode params;

/**
* The expected serialized HTTP request.
*/
private C2jInputSerialized serialized;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;


import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jMetadata;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jShape;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class C2jInputTestSuite {
/**
* A description of the tests
*/
private String description;
/**
* The top level metadata that would correspond to the metadata key in the service's JSON model.
*/
private C2jMetadata metadata;
/**
* An URL string the test cases must use when constructing the request's URL endpoint.
*/
private String clientEndpoint;
/**
* A JSON object of shapes. This would correspond to the top level shapes key in the service's JSON model.
*/
private Map<String, C2jShape> shapes;
/**
* a JSON list of test cases.
*/
private List<C2jInputTestCase> cases;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import lombok.Data;

import java.util.Map;

@Data
public class C2jOutputResponse {
Integer status_code;
private Map<String, String> headers;
private String body;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;

@Data
public class C2jOutputTestCase {
/**
* A test case id.
*/
private String id;
/**
* A test case description.
*/
private String description;
/**
* This corresponds to the JSON object that would define an operation in the service's JSON model.
* Valid keys include http, input, endpoint, and name.
*/
private C2jGiven given;
/**
* A JSON hash representing the structure of the parsed response. Either this or error (not both) must appear in the test case.
*/
private JsonNode result;
/**
* A JSON hash representing the structure of the parsed error response. Either this or result (not both) must appear in the test case.
*/
private JsonNode error;
/**
* A string specifying the AWS error code extracted from the response. Corresponds to the error shape to be unmarshalled and
* should always be set, even when the shape can not be found. Must be present when the error key is present.
*/
private String errorCode;
/**
* A string specifying the error message extracted from the response.
* Should be able to be extracted even when an error shape is not unmarshalled. May only be present when the error key is present.
*/
private String errorMessage;
/**
* The HTTP response to be parsed
*/
private C2jOutputResponse response;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jMetadata;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jShape;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class C2jOutputTestSuite {
/**
* A description of the tests
*/
private String description;
/**
* The top level metadata that would correspond to the metadata key in the service's JSON model.
*/
private C2jMetadata metadata;
/**
* A URL string the test cases must use when constructing the request's URL endpoint.
*/
private String clientEndpoint;
/**
* A JSON object of C2jShapes. This would correspond to the top level C2jShapes key in the service's JSON model.
*/
private Map<String, C2jShape> shapes;
/**
* a JSON list of test cases.
*/
private List<C2jOutputTestCase> cases;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test;

import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jMetadata;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jShape;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class C2jTestSuite {
public enum TestSuiteType {
INPUT,
OUTPUT
}
TestSuiteType type; // only can be present.
private List<C2jInputTestSuite> inputTestSuites;
private List<C2jOutputTestSuite> outputTestSuites;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jServiceModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jXmlNamespace;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jXmlNamespaceDeserializer;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test.C2jInputTestSuite;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test.C2jOutputTestSuite;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test.C2jTestSuite;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.EndpointRuleSetModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.PartitionsModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.defaults.BaseOption;
Expand All @@ -22,8 +25,11 @@
import com.amazonaws.util.awsclientgenerator.domainmodels.endpoints.EndpointTests;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Type;
import java.util.List;

public class DirectFromC2jGenerator {

Expand Down Expand Up @@ -168,4 +174,35 @@ public ByteArrayOutputStream generateServiceTestSourceFromModels(String rawJson,
return mainClientGenerator.generateTestSourceFromModel(c2jServiceModel, serviceName, languageBinding, namespace,
licenseText);
}

/**
* A function to generate C++ source for service client tests
*
* @throws Exception
*/
public ByteArrayOutputStream generateProtocolTestSourceFromModels(String c2jModelJson, String protocolTestsJson,
String protocolTestsType, String testName) throws Exception {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(C2jXmlNamespace.class, new C2jXmlNamespaceDeserializer());
Gson gson = gsonBuilder.create();

C2jServiceModel c2jServiceModel = gson.fromJson(c2jModelJson, C2jServiceModel.class);
c2jServiceModel.setServiceName(testName);

C2jTestSuite c2jProtocolTestModel = new C2jTestSuite();
if (protocolTestsType.equalsIgnoreCase("input")) {
c2jProtocolTestModel.setType(C2jTestSuite.TestSuiteType.INPUT);
Type listType = new TypeToken<List<C2jInputTestSuite>>() {}.getType();
List<C2jInputTestSuite> testSuite = gson.fromJson(protocolTestsJson, listType);
c2jProtocolTestModel.setInputTestSuites(testSuite);
} else if (protocolTestsType.equalsIgnoreCase("output")) {
c2jProtocolTestModel.setType(C2jTestSuite.TestSuiteType.OUTPUT);
Type listType = new TypeToken<List<C2jOutputTestSuite>>() {}.getType();
List<C2jOutputTestSuite> testSuite = gson.fromJson(protocolTestsJson, listType);
c2jProtocolTestModel.setOutputTestSuites(testSuite);
}

c2jServiceModel.setServiceName(testName);
return mainClientGenerator.generateProtocolTestSourceFromModel(c2jServiceModel, c2jProtocolTestModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.amazonaws.util.awsclientgenerator.config.ServiceGeneratorConfig;
import com.amazonaws.util.awsclientgenerator.domainmodels.SdkFileEntry;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j.C2jServiceModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.c2j_protocol_test.C2jTestSuite;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.DefaultsModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.PartitionsModel;
import com.amazonaws.util.awsclientgenerator.domainmodels.codegeneration.ServiceModel;
Expand All @@ -17,6 +18,7 @@
import com.amazonaws.util.awsclientgenerator.domainmodels.defaults.DefaultClientConfigs;
import com.amazonaws.util.awsclientgenerator.generators.cpp.CppDefaultsGenerator;
import com.amazonaws.util.awsclientgenerator.generators.cpp.CppPartitionsGenerator;
import com.amazonaws.util.awsclientgenerator.generators.cpp.CppProtocolTestGenerator;
import com.amazonaws.util.awsclientgenerator.generators.cpp.CppServiceClientTestGenerator;
import com.amazonaws.util.awsclientgenerator.transform.C2jModelToGeneratorModelTransformer;

Expand Down Expand Up @@ -105,6 +107,26 @@ public ByteArrayOutputStream generateTestSourceFromModel(C2jServiceModel c2jMode
return compressFilesToZip(apiFiles, componentOutputName);
}

public ByteArrayOutputStream generateProtocolTestSourceFromModel(C2jServiceModel c2jModel,
C2jTestSuite testSuiteModel) throws Exception {
SdkSpec spec = new SdkSpec("cpp", c2jModel.getServiceName(), null);
// Transform to intermediate code gen models from input c2j format.
ServiceModel serviceModel = new C2jModelToGeneratorModelTransformer(c2jModel, false).convert();

serviceModel.setRuntimeMajorVersion("@RUNTIME_MAJOR_VERSION@");
serviceModel.setRuntimeMajorVersionUpperBound("@RUNTIME_MAJOR_VERSION_UPPER_BOUND@");
serviceModel.setRuntimeMinorVersion("@RUNTIME_MINOR_VERSION@");
serviceModel.setNamespace("Aws");
serviceModel.setEnableVirtualOperations(true);

spec.setVersion(serviceModel.getMetadata().getApiVersion());
CppProtocolTestGenerator cppTestGenerator = new CppProtocolTestGenerator();
SdkFileEntry[] apiFiles = cppTestGenerator.generateSourceFiles(serviceModel);
String componentOutputName = String.format("%s", serviceModel.getMetadata().getProjectName());

return compressFilesToZip(apiFiles, componentOutputName);
}

private static ByteArrayOutputStream compressFilesToZip(final SdkFileEntry[] apiFiles, final String componentOutputName) throws IOException {
//we need to add a BOM to accommodate MSFT compilers.
//as specified here https://blogs.msdn.microsoft.com/vcblog/2016/02/22/new-options-for-managing-character-sets-in-the-microsoft-cc-compiler/
Expand Down
Loading

0 comments on commit 0bff034

Please sign in to comment.