|
| 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