|
| 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 software.amazon.awssdk.core.useragent.BusinessMetricCollection.METRIC_SEARCH_PATTERN; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Stream; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 29 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 30 | +import software.amazon.awssdk.core.checksums.RequestChecksumCalculation; |
| 31 | +import software.amazon.awssdk.core.checksums.ResponseChecksumValidation; |
| 32 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 33 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 34 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 35 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 36 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 37 | +import software.amazon.awssdk.regions.Region; |
| 38 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; |
| 39 | +import software.amazon.awssdk.services.protocolrestjson.model.ChecksumAlgorithm; |
| 40 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 41 | +import software.amazon.awssdk.utils.StringInputStream; |
| 42 | + |
| 43 | +/** |
| 44 | + * Test class to verify that flexible checksum business metrics are correctly included |
| 45 | + * in the User-Agent header when checksum algorithms are used. |
| 46 | + */ |
| 47 | +class FlexibleChecksumBusinessMetricTest { |
| 48 | + private static final String USER_AGENT_HEADER_NAME = "User-Agent"; |
| 49 | + private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = |
| 50 | + StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
| 51 | + |
| 52 | + private MockSyncHttpClient mockHttpClient; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void setup() { |
| 56 | + mockHttpClient = new MockSyncHttpClient(); |
| 57 | + mockHttpClient.stubNextResponse(mockResponse()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void when_noChecksumConfigurationIsSet_defaultConfigMetricsAreAdded() { |
| 62 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 63 | + .region(Region.US_WEST_2) |
| 64 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 65 | + .httpClient(mockHttpClient) |
| 66 | + .build(); |
| 67 | + |
| 68 | + client.allTypes(r -> {}); |
| 69 | + String userAgent = getUserAgentFromLastRequest(); |
| 70 | + |
| 71 | + assertThat(userAgent) |
| 72 | + .matches(METRIC_SEARCH_PATTERN.apply("Z")) |
| 73 | + .matches(METRIC_SEARCH_PATTERN.apply("b")) |
| 74 | + .doesNotMatch(METRIC_SEARCH_PATTERN.apply("a")) |
| 75 | + .doesNotMatch(METRIC_SEARCH_PATTERN.apply("c")); |
| 76 | + } |
| 77 | + |
| 78 | + @ParameterizedTest |
| 79 | + @MethodSource("checksumAlgorithmTestCases") |
| 80 | + void when_checksumAlgorithmIsUsed_correctMetricIsAdded(ChecksumAlgorithm algorithm, String expectedMetric) { |
| 81 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 82 | + .region(Region.US_WEST_2) |
| 83 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 84 | + .httpClient(mockHttpClient) |
| 85 | + .build(); |
| 86 | + |
| 87 | + client.putOperationWithChecksum(r -> r.checksumAlgorithm(algorithm), |
| 88 | + RequestBody.fromString("test content")); |
| 89 | + |
| 90 | + String userAgent = getUserAgentFromLastRequest(); |
| 91 | + assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply(expectedMetric)); |
| 92 | + } |
| 93 | + |
| 94 | + static Stream<Arguments> checksumAlgorithmTestCases() { |
| 95 | + return Stream.of( |
| 96 | + Arguments.of(ChecksumAlgorithm.CRC32, "U"), |
| 97 | + Arguments.of(ChecksumAlgorithm.CRC32_C, "V"), |
| 98 | + Arguments.of(ChecksumAlgorithm.CRC64_NVME, "W"), |
| 99 | + Arguments.of(ChecksumAlgorithm.SHA1, "X"), |
| 100 | + Arguments.of(ChecksumAlgorithm.SHA256, "Y") |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("checksumConfigurationTestCases") |
| 106 | + void when_checksumConfigurationIsSet_correctMetricIsAdded(RequestChecksumCalculation requestConfig, |
| 107 | + ResponseChecksumValidation responseConfig, |
| 108 | + String expectedRequestMetric, |
| 109 | + String expectedResponseMetric) { |
| 110 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 111 | + .region(Region.US_WEST_2) |
| 112 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 113 | + .httpClient(mockHttpClient) |
| 114 | + .requestChecksumCalculation(requestConfig) |
| 115 | + .responseChecksumValidation(responseConfig) |
| 116 | + .build(); |
| 117 | + |
| 118 | + client.allTypes(r -> {}); |
| 119 | + |
| 120 | + String userAgent = getUserAgentFromLastRequest(); |
| 121 | + assertThat(userAgent) |
| 122 | + .matches(METRIC_SEARCH_PATTERN.apply(expectedRequestMetric)) |
| 123 | + .matches(METRIC_SEARCH_PATTERN.apply(expectedResponseMetric)); |
| 124 | + } |
| 125 | + |
| 126 | + static Stream<Arguments> checksumConfigurationTestCases() { |
| 127 | + return Stream.of( |
| 128 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 129 | + ResponseChecksumValidation.WHEN_SUPPORTED, "Z", "b"), |
| 130 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 131 | + ResponseChecksumValidation.WHEN_REQUIRED, "a", "c"), |
| 132 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 133 | + ResponseChecksumValidation.WHEN_SUPPORTED, "a", "b"), |
| 134 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 135 | + ResponseChecksumValidation.WHEN_REQUIRED, "Z", "c") |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + @ParameterizedTest |
| 140 | + @MethodSource("checksumConfigurationWithAlgorithmTestCases") |
| 141 | + void when_checksumConfigurationAndAlgorithmAreSet_correctMetricsAreAdded( |
| 142 | + RequestChecksumCalculation requestConfig, |
| 143 | + ResponseChecksumValidation responseConfig, |
| 144 | + ChecksumAlgorithm algorithm, |
| 145 | + String expectedRequestMetric, |
| 146 | + String expectedResponseMetric, |
| 147 | + String expectedAlgorithmMetric) { |
| 148 | + |
| 149 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 150 | + .region(Region.US_WEST_2) |
| 151 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 152 | + .httpClient(mockHttpClient) |
| 153 | + .requestChecksumCalculation(requestConfig) |
| 154 | + .responseChecksumValidation(responseConfig) |
| 155 | + .build(); |
| 156 | + |
| 157 | + client.putOperationWithChecksum(r -> r.checksumAlgorithm(algorithm), |
| 158 | + RequestBody.fromString("test content")); |
| 159 | + |
| 160 | + String userAgent = getUserAgentFromLastRequest(); |
| 161 | + |
| 162 | + assertThat(userAgent) |
| 163 | + .matches(METRIC_SEARCH_PATTERN.apply(expectedRequestMetric)) |
| 164 | + .matches(METRIC_SEARCH_PATTERN.apply(expectedResponseMetric)) |
| 165 | + .matches(METRIC_SEARCH_PATTERN.apply(expectedAlgorithmMetric)); |
| 166 | + } |
| 167 | + |
| 168 | + static Stream<Arguments> checksumConfigurationWithAlgorithmTestCases() { |
| 169 | + return Stream.of( |
| 170 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 171 | + ResponseChecksumValidation.WHEN_SUPPORTED, |
| 172 | + ChecksumAlgorithm.CRC32, "Z", "b", "U"), |
| 173 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 174 | + ResponseChecksumValidation.WHEN_SUPPORTED, |
| 175 | + ChecksumAlgorithm.CRC32_C, "Z", "b", "V"), |
| 176 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 177 | + ResponseChecksumValidation.WHEN_SUPPORTED, |
| 178 | + ChecksumAlgorithm.SHA256, "Z", "b", "Y"), |
| 179 | + |
| 180 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 181 | + ResponseChecksumValidation.WHEN_REQUIRED, |
| 182 | + ChecksumAlgorithm.CRC32, "a", "c", "U"), |
| 183 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 184 | + ResponseChecksumValidation.WHEN_REQUIRED, |
| 185 | + ChecksumAlgorithm.CRC64_NVME, "a", "c", "W"), |
| 186 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 187 | + ResponseChecksumValidation.WHEN_REQUIRED, |
| 188 | + ChecksumAlgorithm.SHA1, "a", "c", "X"), |
| 189 | + |
| 190 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 191 | + ResponseChecksumValidation.WHEN_SUPPORTED, |
| 192 | + ChecksumAlgorithm.CRC32_C, "a", "b", "V"), |
| 193 | + Arguments.of(RequestChecksumCalculation.WHEN_REQUIRED, |
| 194 | + ResponseChecksumValidation.WHEN_SUPPORTED, |
| 195 | + ChecksumAlgorithm.SHA256, "a", "b", "Y"), |
| 196 | + |
| 197 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 198 | + ResponseChecksumValidation.WHEN_REQUIRED, |
| 199 | + ChecksumAlgorithm.CRC64_NVME, "Z", "c", "W"), |
| 200 | + Arguments.of(RequestChecksumCalculation.WHEN_SUPPORTED, |
| 201 | + ResponseChecksumValidation.WHEN_REQUIRED, |
| 202 | + ChecksumAlgorithm.SHA1, "Z", "c", "X") |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + private String getUserAgentFromLastRequest() { |
| 207 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 208 | + assertThat(lastRequest).isNotNull(); |
| 209 | + |
| 210 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 211 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 212 | + return userAgentHeaders.get(0); |
| 213 | + } |
| 214 | + |
| 215 | + private static HttpExecuteResponse mockResponse() { |
| 216 | + return HttpExecuteResponse.builder() |
| 217 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 218 | + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) |
| 219 | + .build(); |
| 220 | + } |
| 221 | +} |
0 commit comments