Skip to content

Generate client endpoint tests if test cases have operationInputs or otherwise are viable #4073

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

Merged
merged 4 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ protected List<GeneratorTask> createTasks() throws Exception {
tasks.add(generateDefaultProvider());
tasks.addAll(generateInterceptors());
if (shouldGenerateEndpointTests()) {
tasks.add(generateClientTests());
tasks.add(generateProviderTests());
}
if (shouldGenerateEndpointTests() && shouldGenerateClientEndpointTests()) {
tasks.add(generateClientTests());
}
if (hasClientContextParams()) {
tasks.add(generateClientContextParams());
}
Expand Down Expand Up @@ -118,6 +120,13 @@ private boolean shouldGenerateEndpointTests() {
!generatorTaskParams.getModel().getEndpointTestSuiteModel().getTestCases().isEmpty();
}

private boolean shouldGenerateClientEndpointTests() {
CustomizationConfig customizationConfig = generatorTaskParams.getModel().getCustomizationConfig();
boolean noTestCasesHaveOperationInputs = model.getEndpointTestSuiteModel().getTestCases().stream()
.noneMatch(t -> t.getOperationInputs() != null);
return noTestCasesHaveOperationInputs && Boolean.TRUE.equals(customizationConfig.isGenerateEndpointClientTests());
}

private boolean hasClientContextParams() {
Map<String, ClientContextParam> clientContextParams = model.getClientContextParams();
return clientContextParams != null && !clientContextParams.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ public class CustomizationConfig {
*/
private boolean skipEndpointTestGeneration;

/**
* Whether to generate client-level endpoint tests; overrides test case criteria such as operation inputs.
*/
private boolean generateEndpointClientTests;

/**
* A mapping from the skipped test's description to the reason why it's being skipped.
*/
Expand Down Expand Up @@ -577,6 +582,14 @@ public void setSkipEndpointTestGeneration(boolean skipEndpointTestGeneration) {
this.skipEndpointTestGeneration = skipEndpointTestGeneration;
}

public boolean isGenerateEndpointClientTests() {
return generateEndpointClientTests;
}

public void setGenerateEndpointClientTests(boolean generateEndpointClientTests) {
this.generateEndpointClientTests = generateEndpointClientTests;
}

public boolean useGlobalEndpoint() {
return useGlobalEndpoint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public TypeSpec poetSpec() {
b.addField(s3RegionEndpointSystemPropertySaveValueField());
}

if (serviceHasNoMatchingTestCases()) {
return b.build();
}

b.addMethod(methodSetupMethod());
b.addMethod(teardownMethod());

Expand Down Expand Up @@ -215,16 +219,22 @@ private MethodSpec syncTestsSourceMethod() {
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.returns(ParameterizedTypeName.get(List.class, SyncTestCase.class));




b.addCode("return $T.asList(", Arrays.class);

EndpointTestSuiteModel endpointTestSuiteModel = model.getEndpointTestSuiteModel();
Iterator<EndpointTestModel> testIter = endpointTestSuiteModel.getTestCases().iterator();

boolean isFirst = true;
while (testIter.hasNext()) {
EndpointTestModel test = testIter.next();

if (test.getOperationInputs() != null) {
if (testCaseHasOperationInputs(test)) {
Iterator<OperationInput> operationInputsIter = test.getOperationInputs().iterator();
if (!isFirst) {
b.addCode(", ");
}
isFirst = false;
while (operationInputsIter.hasNext()) {
OperationInput opInput = operationInputsIter.next();
OperationModel opModel = model.getOperation(opInput.getOperationName());
Expand All @@ -240,18 +250,18 @@ private MethodSpec syncTestsSourceMethod() {
b.addCode(",");
}
}
} else {
} else if (shouldGenerateClientTestsOverride()) {
if (!isFirst) {
b.addCode(", ");
}
isFirst = false;
b.addCode("new $T($S, $L, $L$L)",
SyncTestCase.class,
test.getDocumentation(),
syncOperationCallLambda(defaultOpModel, test.getParams(), Collections.emptyMap()),
TestGeneratorUtils.createExpect(test.getExpect(), defaultOpModel, null),
getSkipReasonBlock(test.getDocumentation()));
}

if (testIter.hasNext()) {
b.addCode(",");
}
}

b.addStatement(")");
Expand Down Expand Up @@ -365,12 +375,16 @@ private MethodSpec asyncTestsSourceMethod() {

EndpointTestSuiteModel endpointTestSuiteModel = model.getEndpointTestSuiteModel();
Iterator<EndpointTestModel> testIter = endpointTestSuiteModel.getTestCases().iterator();

boolean isFirst = true;
while (testIter.hasNext()) {
EndpointTestModel test = testIter.next();

if (test.getOperationInputs() != null) {
if (testCaseHasOperationInputs(test)) {
Iterator<OperationInput> operationInputsIter = test.getOperationInputs().iterator();
if (!isFirst) {
b.addCode(", ");
}
isFirst = false;
while (operationInputsIter.hasNext()) {
OperationInput opInput = operationInputsIter.next();
OperationModel opModel = model.getOperation(opInput.getOperationName());
Expand All @@ -386,18 +400,18 @@ private MethodSpec asyncTestsSourceMethod() {
b.addCode(",");
}
}
} else {
} else if (shouldGenerateClientTestsOverride()) {
if (!isFirst) {
b.addCode(", ");
}
isFirst = false;
b.addCode("new $T($S, $L, $L$L)",
AsyncTestCase.class,
test.getDocumentation(),
asyncOperationCallLambda(defaultOpModel, test.getParams(), Collections.emptyMap()),
TestGeneratorUtils.createExpect(test.getExpect(), defaultOpModel, null),
getSkipReasonBlock(test.getDocumentation()));
}

if (testIter.hasNext()) {
b.addCode(",");
}
}

b.addStatement(")");
Expand Down Expand Up @@ -649,6 +663,27 @@ private Map<String, String> getSkippedTests() {
return skippedTests;
}

private boolean serviceHasNoMatchingTestCases() {
boolean noTestCasesHaveOperationInputs = model.getEndpointTestSuiteModel().getTestCases().stream()
.noneMatch(EndpointRulesClientTestSpec::testCaseHasOperationInputs);
return noTestCasesHaveOperationInputs && !shouldGenerateClientTestsOverride();
}

/**
* Always generate client endpoint tests if the test case has operation inputs
*/
private static boolean testCaseHasOperationInputs(EndpointTestModel test) {
return test.getOperationInputs() != null;
}

/**
* Some services can run tests without operation inputs if there are other conditions that allow
* codegen to create a functioning test case
*/
private boolean shouldGenerateClientTestsOverride() {
return model.getCustomizationConfig().isGenerateEndpointClientTests();
}

private CodeBlock getSkipReasonBlock(String testName) {
if (getSkippedTests().containsKey(testName)) {
Validate.notNull(getSkippedTests().get(testName), "Test %s must have a reason for skipping", testName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import software.amazon.awssdk.services.query.QueryAsyncClientBuilder;
import software.amazon.awssdk.services.query.QueryClient;
import software.amazon.awssdk.services.query.QueryClientBuilder;
import software.amazon.awssdk.services.query.model.APostOperationRequest;
import software.amazon.awssdk.services.query.model.ChecksumStructure;
import software.amazon.awssdk.services.query.model.OperationWithContextParamRequest;

Expand Down Expand Up @@ -47,26 +46,6 @@ public void asyncClient_usesCorrectEndpoint(AsyncTestCase tc) {

private static List<SyncTestCase> syncTestCases() {
return Arrays.asList(
new SyncTestCase("test case 1", () -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getSyncHttpClient());
builder.region(Region.of("us-east-1"));
APostOperationRequest request = APostOperationRequest.builder().build();
builder.build().aPostOperation(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://foo-myservice.aws")).build()).build()),
new SyncTestCase("test case 2", () -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getSyncHttpClient());
builder.region(Region.of("us-east-1"));
builder.booleanContextParam(true);
builder.stringContextParam("this is a test");
APostOperationRequest request = APostOperationRequest.builder().build();
builder.build().aPostOperation(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://foo-myservice.aws")).build()).build()),
new SyncTestCase("test case 3", () -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
Expand All @@ -88,14 +67,6 @@ private static List<SyncTestCase> syncTestCases() {
builder.build().operationWithContextParam(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://myservice.aws")).build()).build(),
"Does not work"),
new SyncTestCase("For region us-iso-west-1 with FIPS enabled and DualStack enabled", () -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getSyncHttpClient());
APostOperationRequest request = APostOperationRequest.builder().build();
builder.build().aPostOperation(request);
}, Expect.builder().error("Should have been skipped!").build(), "Client builder does the validation"),
new SyncTestCase("Has complex operation input", () -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
Expand All @@ -104,39 +75,11 @@ private static List<SyncTestCase> syncTestCases() {
OperationWithContextParamRequest request = OperationWithContextParamRequest.builder()
.nestedMember(ChecksumStructure.builder().checksumMode("foo").build()).build();
builder.build().operationWithContextParam(request);
}, Expect.builder().error("Missing info").build()), new SyncTestCase("Has has undeclared input parameter",
() -> {
QueryClientBuilder builder = QueryClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getSyncHttpClient());
APostOperationRequest request = APostOperationRequest.builder().build();
builder.build().aPostOperation(request);
}, Expect.builder().error("Missing info").build()));
}, Expect.builder().error("Missing info").build()));
}

private static List<AsyncTestCase> asyncTestCases() {
return Arrays.asList(
new AsyncTestCase("test case 1", () -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getAsyncHttpClient());
builder.region(Region.of("us-east-1"));
APostOperationRequest request = APostOperationRequest.builder().build();
return builder.build().aPostOperation(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://foo-myservice.aws")).build()).build()),
new AsyncTestCase("test case 2", () -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getAsyncHttpClient());
builder.region(Region.of("us-east-1"));
builder.booleanContextParam(true);
builder.stringContextParam("this is a test");
APostOperationRequest request = APostOperationRequest.builder().build();
return builder.build().aPostOperation(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://foo-myservice.aws")).build()).build()),
new AsyncTestCase("test case 3", () -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
Expand All @@ -158,14 +101,6 @@ private static List<AsyncTestCase> asyncTestCases() {
return builder.build().operationWithContextParam(request);
}, Expect.builder().endpoint(Endpoint.builder().url(URI.create("https://myservice.aws")).build()).build(),
"Does not work"),
new AsyncTestCase("For region us-iso-west-1 with FIPS enabled and DualStack enabled", () -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getAsyncHttpClient());
APostOperationRequest request = APostOperationRequest.builder().build();
return builder.build().aPostOperation(request);
}, Expect.builder().error("Should have been skipped!").build(), "Client builder does the validation"),
new AsyncTestCase("Has complex operation input", () -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
Expand All @@ -174,14 +109,6 @@ private static List<AsyncTestCase> asyncTestCases() {
OperationWithContextParamRequest request = OperationWithContextParamRequest.builder()
.nestedMember(ChecksumStructure.builder().checksumMode("foo").build()).build();
return builder.build().operationWithContextParam(request);
}, Expect.builder().error("Missing info").build()), new AsyncTestCase("Has has undeclared input parameter",
() -> {
QueryAsyncClientBuilder builder = QueryAsyncClient.builder();
builder.credentialsProvider(BaseRuleSetClientTest.CREDENTIALS_PROVIDER);
builder.tokenProvider(BaseRuleSetClientTest.TOKEN_PROVIDER);
builder.httpClient(getAsyncHttpClient());
APostOperationRequest request = APostOperationRequest.builder().build();
return builder.build().aPostOperation(request);
}, Expect.builder().error("Missing info").build()));
}, Expect.builder().error("Missing info").build()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"verifiedSimpleMethods" : [
"listAccounts"
]
],
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"SendApiAsset": {
"exclude": true
}
}
},
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"listLocations",
"listTaskExecutions",
"listTasks"
]
],
"generateEndpointClientTests": true
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"generateEndpointClientTests": true
}
Loading