Skip to content

Commit 800b41a

Browse files
authored
Change ResolveIdentityRequest to an interface (#4030)
1 parent 1f2ac6f commit 800b41a

File tree

3 files changed

+151
-54
lines changed

3 files changed

+151
-54
lines changed

core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/ResolveIdentityRequest.java

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515

1616
package software.amazon.awssdk.identity.spi;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
20-
import java.util.Objects;
2118
import software.amazon.awssdk.annotations.Immutable;
2219
import software.amazon.awssdk.annotations.SdkPublicApi;
2320
import software.amazon.awssdk.annotations.ThreadSafe;
24-
import software.amazon.awssdk.utils.ToString;
21+
import software.amazon.awssdk.identity.spi.internal.DefaultResolveIdentityRequest;
2522
import software.amazon.awssdk.utils.builder.SdkBuilder;
2623

2724
/**
2825
* A request to resolve an {@link Identity}.
29-
*
26+
* <p>
3027
* The Identity may be determined for each request based on properties of the request (e.g. different credentials per bucket
3128
* for S3).
3229
*
@@ -35,59 +32,28 @@
3532
@SdkPublicApi
3633
@Immutable
3734
@ThreadSafe
38-
public final class ResolveIdentityRequest {
39-
40-
private final Map<IdentityProperty<?>, Object> properties;
41-
42-
private ResolveIdentityRequest(Map<IdentityProperty<?>, Object> properties) {
43-
this.properties = new HashMap<>(properties);
44-
}
35+
public interface ResolveIdentityRequest {
4536

46-
public static Builder builder() {
47-
return new Builder();
37+
/**
38+
* Get a new builder for creating a {@link ResolveIdentityRequest}.
39+
*/
40+
static Builder builder() {
41+
return DefaultResolveIdentityRequest.builder();
4842
}
4943

50-
public <T> T property(IdentityProperty<T> property) {
51-
return (T) properties.get(property);
52-
}
53-
54-
@Override
55-
public String toString() {
56-
return ToString.builder("ResolveIdentityRequest")
57-
.add("properties", properties)
58-
.build();
59-
}
60-
61-
@Override
62-
public boolean equals(Object o) {
63-
if (this == o) {
64-
return true;
65-
}
66-
if (o == null || getClass() != o.getClass()) {
67-
return false;
68-
}
69-
ResolveIdentityRequest that = (ResolveIdentityRequest) o;
70-
return properties.equals(that.properties);
71-
}
72-
73-
@Override
74-
public int hashCode() {
75-
return Objects.hashCode(properties);
76-
}
77-
78-
public static final class Builder implements SdkBuilder<Builder, ResolveIdentityRequest> {
79-
private final Map<IdentityProperty<?>, Object> properties = new HashMap<>();
80-
81-
private Builder() {
82-
}
44+
/**
45+
* Returns the value of a property that the {@link IdentityProvider} can use while resolving the identity.
46+
*/
47+
<T> T property(IdentityProperty<T> property);
8348

84-
public <T> Builder putProperty(IdentityProperty<T> key, T value) {
85-
this.properties.put(key, value);
86-
return this;
87-
}
49+
/**
50+
* A builder for a {@link ResolveIdentityRequest}.
51+
*/
52+
interface Builder extends SdkBuilder<Builder, ResolveIdentityRequest> {
8853

89-
public ResolveIdentityRequest build() {
90-
return new ResolveIdentityRequest(properties);
91-
}
54+
/**
55+
* Set a property that the {@link IdentityProvider} can use while resolving the identity.
56+
*/
57+
<T> Builder putProperty(IdentityProperty<T> key, T value);
9258
}
9359
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.identity.spi.internal;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
import software.amazon.awssdk.annotations.Immutable;
22+
import software.amazon.awssdk.annotations.SdkInternalApi;
23+
import software.amazon.awssdk.annotations.ThreadSafe;
24+
import software.amazon.awssdk.identity.spi.IdentityProperty;
25+
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
26+
import software.amazon.awssdk.utils.ToString;
27+
28+
@SdkInternalApi
29+
@Immutable
30+
@ThreadSafe
31+
public final class DefaultResolveIdentityRequest implements ResolveIdentityRequest {
32+
33+
private final Map<IdentityProperty<?>, Object> properties;
34+
35+
private DefaultResolveIdentityRequest(Map<IdentityProperty<?>, Object> properties) {
36+
this.properties = new HashMap<>(properties);
37+
}
38+
39+
public static Builder builder() {
40+
return new BuilderImpl();
41+
}
42+
43+
@Override
44+
public <T> T property(IdentityProperty<T> property) {
45+
return (T) properties.get(property);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return ToString.builder("ResolveIdentityRequest")
51+
.add("properties", properties)
52+
.build();
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) {
58+
return true;
59+
}
60+
if (o == null || getClass() != o.getClass()) {
61+
return false;
62+
}
63+
DefaultResolveIdentityRequest that = (DefaultResolveIdentityRequest) o;
64+
return properties.equals(that.properties);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hashCode(properties);
70+
}
71+
72+
@SdkInternalApi
73+
public static final class BuilderImpl implements Builder {
74+
private final Map<IdentityProperty<?>, Object> properties = new HashMap<>();
75+
76+
public <T> Builder putProperty(IdentityProperty<T> key, T value) {
77+
this.properties.put(key, value);
78+
return this;
79+
}
80+
81+
public ResolveIdentityRequest build() {
82+
return new DefaultResolveIdentityRequest(properties);
83+
}
84+
}
85+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.identity.spi;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
21+
import nl.jqno.equalsverifier.EqualsVerifier;
22+
import org.junit.jupiter.api.Test;
23+
import software.amazon.awssdk.identity.spi.internal.DefaultResolveIdentityRequest;
24+
25+
public class ResolveIdentityRequestTest {
26+
@Test
27+
public void equalsHashcode() {
28+
EqualsVerifier.forClass(DefaultResolveIdentityRequest.class)
29+
.withNonnullFields("properties")
30+
.verify();
31+
}
32+
33+
@Test
34+
public void emptyBuilder_isSuccessful() {
35+
assertNotNull(ResolveIdentityRequest.builder().build());
36+
}
37+
38+
@Test
39+
public void build_withProperty_isSuccessful() {
40+
IdentityProperty<String> property = IdentityProperty.create(String.class, "key");
41+
ResolveIdentityRequest request = ResolveIdentityRequest.builder()
42+
.putProperty(property, "value")
43+
.build();
44+
assertEquals("value", request.property(property));
45+
}
46+
}

0 commit comments

Comments
 (0)