-
Notifications
You must be signed in to change notification settings - Fork 965
Add tests to override configuration with plugins #4572
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.core.client.config.ClientOption; | ||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; | ||
| import software.amazon.awssdk.core.client.config.SdkClientConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkClientOption; | ||
| import software.amazon.awssdk.core.internal.SdkInternalTestAdvancedClientOption; | ||
| import software.amazon.awssdk.core.signer.Signer; | ||
| import software.amazon.awssdk.profiles.ProfileFile; | ||
| import software.amazon.awssdk.profiles.ProfileFileSupplier; | ||
|
|
||
| @SdkInternalApi | ||
| public final class SdkClientConfigurationUtil { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed from |
||
| private SdkClientConfigurationUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Copies the {@link ClientOverrideConfiguration} values to the configuration builder. | ||
| */ | ||
| public static SdkClientConfiguration.Builder copyOverridesToConfiguration(ClientOverrideConfiguration overrides, | ||
| SdkClientConfiguration.Builder builder) { | ||
|
|
||
| // misc | ||
| setClientOption(builder, SdkClientOption.ADDITIONAL_HTTP_HEADERS, overrides.headers()); | ||
| setClientOption(builder, SdkClientOption.RETRY_POLICY, overrides.retryPolicy()); | ||
| setClientOption(builder, SdkClientOption.API_CALL_TIMEOUT, overrides.apiCallTimeout()); | ||
| setClientOption(builder, SdkClientOption.API_CALL_ATTEMPT_TIMEOUT, overrides.apiCallAttemptTimeout()); | ||
| setClientOption(builder, SdkClientOption.SCHEDULED_EXECUTOR_SERVICE, overrides.scheduledExecutorService()); | ||
| setClientListOption(builder, SdkClientOption.EXECUTION_INTERCEPTORS, overrides.executionInterceptors()); | ||
| setClientOption(builder, SdkClientOption.EXECUTION_ATTRIBUTES, overrides.executionAttributes()); | ||
|
|
||
| // advanced option | ||
| Signer signer = overrides.advancedOption(SdkAdvancedClientOption.SIGNER).orElse(null); | ||
| if (signer != null) { | ||
| builder.option(SdkAdvancedClientOption.SIGNER, signer); | ||
| builder.option(SdkClientOption.SIGNER_OVERRIDDEN, true); | ||
| } | ||
| setClientOption(builder, SdkAdvancedClientOption.USER_AGENT_SUFFIX, | ||
| overrides.advancedOption(SdkAdvancedClientOption.USER_AGENT_SUFFIX)); | ||
| setClientOption(builder, SdkAdvancedClientOption.USER_AGENT_PREFIX, | ||
| overrides.advancedOption(SdkAdvancedClientOption.USER_AGENT_PREFIX)); | ||
| setClientOption(builder, SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION, | ||
| overrides.advancedOption(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION)); | ||
| overrides.advancedOption(SdkInternalTestAdvancedClientOption.ENDPOINT_OVERRIDDEN_OVERRIDE).ifPresent(value -> { | ||
| builder.option(SdkClientOption.ENDPOINT_OVERRIDDEN, value); | ||
| }); | ||
|
|
||
| // profile | ||
| ProfileFile profileFile = overrides.defaultProfileFile().orElse(null); | ||
| if (profileFile != null) { | ||
| builder.option(SdkClientOption.PROFILE_FILE_SUPPLIER, ProfileFileSupplier.fixedProfileFile(profileFile)); | ||
| } | ||
| setClientOption(builder, SdkClientOption.PROFILE_NAME, overrides.defaultProfileName()); | ||
|
|
||
| // misc | ||
| setClientListOption(builder, SdkClientOption.METRIC_PUBLISHERS, overrides.metricPublishers()); | ||
| setClientOption(builder, SdkAdvancedClientOption.TOKEN_SIGNER, | ||
| overrides.advancedOption(SdkAdvancedClientOption.TOKEN_SIGNER)); | ||
| setClientOption(builder, SdkClientOption.COMPRESSION_CONFIGURATION, overrides.compressionConfiguration()); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| static <T> void setClientOption(SdkClientConfiguration.Builder builder, ClientOption<T> option, T newValue) { | ||
| if (newValue != null) { | ||
| T oldValue = builder.option(option); | ||
| if (oldValue == null || !oldValue.equals(newValue)) { | ||
| builder.option(option, newValue); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static <T> void setClientOption(SdkClientConfiguration.Builder builder, ClientOption<T> option, Optional<T> newValueOpt) { | ||
| setClientOption(builder, option, newValueOpt.orElse(null)); | ||
| } | ||
|
|
||
| static <T> void setClientListOption(SdkClientConfiguration.Builder builder, ClientOption<List<T>> option, List<T> newValue) { | ||
| if (newValue == null || newValue.isEmpty()) { | ||
| return; | ||
| } | ||
| List<T> oldValue = builder.option(option); | ||
| if (oldValue == null || oldValue.isEmpty()) { | ||
| builder.option(option, newValue); | ||
| return; | ||
| } | ||
| // Make sure that we don't override derived values or duplicate existing ones. | ||
| List<T> result = new ArrayList<>(oldValue); | ||
| Set<T> dedup = new HashSet<>(); | ||
| dedup.addAll(oldValue); | ||
| for (T value : newValue) { | ||
| if (!dedup.contains(value)) { | ||
| result.add(value); | ||
| } | ||
| } | ||
| builder.option(option, result); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SpotBugs gets confused about the nullability of the
overrideConfiguration, probably because is looking at the default method that throws anUnsupportedOperationException. Adding this since I'm certain that this value can be null.