From 32604f701dc859e9147dc10bb0eae2c622f5aee9 Mon Sep 17 00:00:00 2001 From: Jaykumar Gosar Date: Tue, 23 May 2023 10:33:09 -0700 Subject: [PATCH] Change ResolveIdentityRequest to an interface --- .../identity/spi/ResolveIdentityRequest.java | 74 +++++----------- .../DefaultResolveIdentityRequest.java | 85 +++++++++++++++++++ .../spi/ResolveIdentityRequestTest.java | 46 ++++++++++ 3 files changed, 151 insertions(+), 54 deletions(-) create mode 100644 core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/internal/DefaultResolveIdentityRequest.java create mode 100644 core/identity-spi/src/test/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequestTest.java diff --git a/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequest.java b/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequest.java index c15d31ef1073..31d6cf504330 100644 --- a/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequest.java +++ b/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequest.java @@ -15,18 +15,15 @@ package software.amazon.awssdk.identity.spi; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkPublicApi; import software.amazon.awssdk.annotations.ThreadSafe; -import software.amazon.awssdk.utils.ToString; +import software.amazon.awssdk.identity.spi.internal.DefaultResolveIdentityRequest; import software.amazon.awssdk.utils.builder.SdkBuilder; /** * A request to resolve an {@link Identity}. - * + *

* The Identity may be determined for each request based on properties of the request (e.g. different credentials per bucket * for S3). * @@ -35,59 +32,28 @@ @SdkPublicApi @Immutable @ThreadSafe -public final class ResolveIdentityRequest { - - private final Map, Object> properties; - - private ResolveIdentityRequest(Map, Object> properties) { - this.properties = new HashMap<>(properties); - } +public interface ResolveIdentityRequest { - public static Builder builder() { - return new Builder(); + /** + * Get a new builder for creating a {@link ResolveIdentityRequest}. + */ + static Builder builder() { + return DefaultResolveIdentityRequest.builder(); } - public T property(IdentityProperty property) { - return (T) properties.get(property); - } - - @Override - public String toString() { - return ToString.builder("ResolveIdentityRequest") - .add("properties", properties) - .build(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ResolveIdentityRequest that = (ResolveIdentityRequest) o; - return properties.equals(that.properties); - } - - @Override - public int hashCode() { - return Objects.hashCode(properties); - } - - public static final class Builder implements SdkBuilder { - private final Map, Object> properties = new HashMap<>(); - - private Builder() { - } + /** + * Returns the value of a property that the {@link IdentityProvider} can use while resolving the identity. + */ + T property(IdentityProperty property); - public Builder putProperty(IdentityProperty key, T value) { - this.properties.put(key, value); - return this; - } + /** + * A builder for a {@link ResolveIdentityRequest}. + */ + interface Builder extends SdkBuilder { - public ResolveIdentityRequest build() { - return new ResolveIdentityRequest(properties); - } + /** + * Set a property that the {@link IdentityProvider} can use while resolving the identity. + */ + Builder putProperty(IdentityProperty key, T value); } } diff --git a/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/internal/DefaultResolveIdentityRequest.java b/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/internal/DefaultResolveIdentityRequest.java new file mode 100644 index 000000000000..5c7f5c13301f --- /dev/null +++ b/core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/internal/DefaultResolveIdentityRequest.java @@ -0,0 +1,85 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.identity.spi.internal; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import software.amazon.awssdk.annotations.Immutable; +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.annotations.ThreadSafe; +import software.amazon.awssdk.identity.spi.IdentityProperty; +import software.amazon.awssdk.identity.spi.ResolveIdentityRequest; +import software.amazon.awssdk.utils.ToString; + +@SdkInternalApi +@Immutable +@ThreadSafe +public final class DefaultResolveIdentityRequest implements ResolveIdentityRequest { + + private final Map, Object> properties; + + private DefaultResolveIdentityRequest(Map, Object> properties) { + this.properties = new HashMap<>(properties); + } + + public static Builder builder() { + return new BuilderImpl(); + } + + @Override + public T property(IdentityProperty property) { + return (T) properties.get(property); + } + + @Override + public String toString() { + return ToString.builder("ResolveIdentityRequest") + .add("properties", properties) + .build(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DefaultResolveIdentityRequest that = (DefaultResolveIdentityRequest) o; + return properties.equals(that.properties); + } + + @Override + public int hashCode() { + return Objects.hashCode(properties); + } + + @SdkInternalApi + public static final class BuilderImpl implements Builder { + private final Map, Object> properties = new HashMap<>(); + + public Builder putProperty(IdentityProperty key, T value) { + this.properties.put(key, value); + return this; + } + + public ResolveIdentityRequest build() { + return new DefaultResolveIdentityRequest(properties); + } + } +} diff --git a/core/identity-spi/src/test/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequestTest.java b/core/identity-spi/src/test/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequestTest.java new file mode 100644 index 000000000000..d25530e96e60 --- /dev/null +++ b/core/identity-spi/src/test/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequestTest.java @@ -0,0 +1,46 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.identity.spi; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.identity.spi.internal.DefaultResolveIdentityRequest; + +public class ResolveIdentityRequestTest { + @Test + public void equalsHashcode() { + EqualsVerifier.forClass(DefaultResolveIdentityRequest.class) + .withNonnullFields("properties") + .verify(); + } + + @Test + public void emptyBuilder_isSuccessful() { + assertNotNull(ResolveIdentityRequest.builder().build()); + } + + @Test + public void build_withProperty_isSuccessful() { + IdentityProperty property = IdentityProperty.create(String.class, "key"); + ResolveIdentityRequest request = ResolveIdentityRequest.builder() + .putProperty(property, "value") + .build(); + assertEquals("value", request.property(property)); + } +}