Skip to content

Commit 4075972

Browse files
committed
Fix syntax error + add functional test
1 parent 7a7e432 commit 4075972

File tree

6 files changed

+118
-5
lines changed

6 files changed

+118
-5
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/rules/EndpointResolverInterceptorSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ private MethodSpec setMetricValuesMethod() {
918918

919919
b.beginControlFlow("if (endpoint.attribute($T.METRIC_VALUES) != null)", AwsEndpointAttribute.class);
920920
b.addStatement("executionAttributes.getOptionalAttribute($T.BUSINESS_METRICS).ifPresent("
921-
+ "metrics -> endpoint.attribute($T.METRIC_VALUES).forEach(v -> metrics.addMetric(v))))",
921+
+ "metrics -> endpoint.attribute($T.METRIC_VALUES).forEach(v -> metrics.addMetric(v)))",
922922
SdkInternalExecutionAttribute.class, AwsEndpointAttribute.class);
923923
b.endControlFlow();
924924
return b.build();

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor-preSra.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private static String recordAccountIdEndpointMode(ExecutionAttributes executionA
289289

290290
private void setMetricValues(Endpoint endpoint, ExecutionAttributes executionAttributes) {
291291
if (endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES) != null) {
292-
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v))));
292+
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v)));
293293
}
294294
}
295295
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor-with-endpointsbasedauth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private static String recordAccountIdEndpointMode(ExecutionAttributes executionA
265265

266266
private void setMetricValues(Endpoint endpoint, ExecutionAttributes executionAttributes) {
267267
if (endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES) != null) {
268-
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v))));
268+
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v)));
269269
}
270270
}
271271
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor-with-multiauthsigv4a.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private static Optional<String> hostPrefix(String operationName, SdkRequest requ
168168

169169
private void setMetricValues(Endpoint endpoint, ExecutionAttributes executionAttributes) {
170170
if (endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES) != null) {
171-
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v))));
171+
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v)));
172172
}
173173
}
174174
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private static String recordAccountIdEndpointMode(ExecutionAttributes executionA
263263

264264
private void setMetricValues(Endpoint endpoint, ExecutionAttributes executionAttributes) {
265265
if (endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES) != null) {
266-
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v))));
266+
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(metrics -> endpoint.attribute(AwsEndpointAttribute.METRIC_VALUES).forEach(v -> metrics.addMetric(v)));
267267
}
268268
}
269269
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.services;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.hamcrest.Matchers.containsInAnyOrder;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
26+
27+
import java.net.URI;
28+
import java.util.Arrays;
29+
import java.util.HashSet;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.Set;
33+
import java.util.concurrent.CompletableFuture;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
36+
import org.junit.jupiter.api.BeforeEach;
37+
import org.junit.jupiter.api.Test;
38+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
39+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
40+
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute;
41+
import software.amazon.awssdk.core.interceptor.Context;
42+
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
43+
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
44+
import software.amazon.awssdk.endpoints.Endpoint;
45+
import software.amazon.awssdk.regions.Region;
46+
import software.amazon.awssdk.services.restjsonendpointproviders.RestJsonEndpointProvidersClient;
47+
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.RestJsonEndpointProvidersEndpointParams;
48+
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.RestJsonEndpointProvidersEndpointProvider;
49+
50+
public class EndpointMetricValuesTest {
51+
private static final String USER_AGENT_HEADER_NAME = "User-Agent";
52+
private static final StaticCredentialsProvider CREDENTIALS_PROVIDER =
53+
StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
54+
55+
56+
private RestJsonEndpointProvidersEndpointProvider mockEndpointProvider;
57+
private CapturingInterceptor capturingInterceptor;
58+
59+
@BeforeEach
60+
void setup() {
61+
capturingInterceptor = new CapturingInterceptor();
62+
mockEndpointProvider = mock(RestJsonEndpointProvidersEndpointProvider.class);
63+
when(mockEndpointProvider.resolveEndpoint(any(RestJsonEndpointProvidersEndpointParams.class)))
64+
.thenReturn(CompletableFuture.completedFuture(
65+
Endpoint.builder()
66+
.url(URI.create("https://my-service.com"))
67+
.putAttribute(AwsEndpointAttribute.METRIC_VALUES, Arrays.asList("O", "K"))
68+
.build()));
69+
}
70+
71+
@Test
72+
void endpointMetricValuesAreAddedToUserAgent() {
73+
RestJsonEndpointProvidersClient client =
74+
RestJsonEndpointProvidersClient.builder()
75+
.endpointProvider(mockEndpointProvider)
76+
.region(Region.US_WEST_2)
77+
.credentialsProvider(CREDENTIALS_PROVIDER)
78+
.overrideConfiguration(c -> c.addExecutionInterceptor(capturingInterceptor))
79+
.build();
80+
81+
assertThatThrownBy(() -> client.operationWithNoInputOrOutput(r -> {
82+
})).hasMessageContaining("short-circuit");
83+
84+
String userAgent = assertAndGetUserAgentString();
85+
Matcher businessMetricMatcher = Pattern.compile("m/([^\\s]+)").matcher(userAgent);
86+
assertTrue(businessMetricMatcher.matches());
87+
assertNotNull(businessMetricMatcher.group(1));
88+
Set<String> metrics = new HashSet<>(Arrays.asList((businessMetricMatcher.group(1).split(","))));
89+
assertTrue(metrics.containsAll(Arrays.asList("O", "K")));
90+
}
91+
92+
private String assertAndGetUserAgentString() {
93+
Map<String, List<String>> headers = capturingInterceptor.context.httpRequest().headers();
94+
assertThat(headers).containsKey(USER_AGENT_HEADER_NAME);
95+
return headers.get(USER_AGENT_HEADER_NAME).get(0);
96+
}
97+
98+
private static class CapturingInterceptor implements ExecutionInterceptor {
99+
private Context.BeforeTransmission context;
100+
private ExecutionAttributes executionAttributes;
101+
102+
@Override
103+
public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttributes executionAttributes) {
104+
this.context = context;
105+
this.executionAttributes = executionAttributes;
106+
throw new RuntimeException("short-circuit");
107+
}
108+
109+
public ExecutionAttributes executionAttributes() {
110+
return executionAttributes;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)