Skip to content

Change ResolveIdentityRequest to an interface #4030

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
* <p>
* The Identity may be determined for each request based on properties of the request (e.g. different credentials per bucket
* for S3).
*
Expand All @@ -35,59 +32,28 @@
@SdkPublicApi
@Immutable
@ThreadSafe
public final class ResolveIdentityRequest {

private final Map<IdentityProperty<?>, Object> properties;

private ResolveIdentityRequest(Map<IdentityProperty<?>, 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> T property(IdentityProperty<T> 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<Builder, ResolveIdentityRequest> {
private final Map<IdentityProperty<?>, Object> properties = new HashMap<>();

private Builder() {
}
/**
* Returns the value of a property that the {@link IdentityProvider} can use while resolving the identity.
*/
<T> T property(IdentityProperty<T> property);

public <T> Builder putProperty(IdentityProperty<T> key, T value) {
this.properties.put(key, value);
return this;
}
/**
* A builder for a {@link ResolveIdentityRequest}.
*/
interface Builder extends SdkBuilder<Builder, ResolveIdentityRequest> {

public ResolveIdentityRequest build() {
return new ResolveIdentityRequest(properties);
}
/**
* Set a property that the {@link IdentityProvider} can use while resolving the identity.
*/
<T> Builder putProperty(IdentityProperty<T> key, T value);
}
}
Original file line number Diff line number Diff line change
@@ -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<IdentityProperty<?>, Object> properties;

private DefaultResolveIdentityRequest(Map<IdentityProperty<?>, Object> properties) {
this.properties = new HashMap<>(properties);
}

public static Builder builder() {
return new BuilderImpl();
}

@Override
public <T> T property(IdentityProperty<T> 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<IdentityProperty<?>, Object> properties = new HashMap<>();

public <T> Builder putProperty(IdentityProperty<T> key, T value) {
this.properties.put(key, value);
return this;
}

public ResolveIdentityRequest build() {
return new DefaultResolveIdentityRequest(properties);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> property = IdentityProperty.create(String.class, "key");
ResolveIdentityRequest request = ResolveIdentityRequest.builder()
.putProperty(property, "value")
.build();
assertEquals("value", request.property(property));
}
}