Skip to content

Commit b4938ad

Browse files
committed
make builder constructor private. extract constants
1 parent 7c0737b commit b4938ad

2 files changed

Lines changed: 37 additions & 28 deletions

File tree

lib/src/main/java/com/auth0/authentication/ParameterBuilder.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,110 +34,123 @@
3434
*/
3535
public class ParameterBuilder {
3636

37+
public static final String GRANT_TYPE_PASSWORD = "password";
38+
public static final String GRANT_TYPE_JWT = "urn:ietf:params:oauth:grant-type:jwt-bearer";
3739
public static final String SCOPE_OPENID = "openid";
3840
public static final String SCOPE_OFFLINE_ACCESS = "openid offline_access";
39-
public static final String ACCESS_TOKEN = "access_token";
40-
public static final String CONNECTION = "connection";
41-
public static final String SEND = "send";
4241

43-
public static final String GRANT_TYPE_PASSWORD = "password";
44-
public static final String GRANT_TYPE_JWT = "urn:ietf:params:oauth:grant-type:jwt-bearer";
42+
private static final String ACCESS_TOKEN_KEY = "access_token";
43+
private static final String CONNECTION_KEY = "connection";
44+
private static final String SEND_KEY = "send";
45+
private static final String CLIENT_ID_KEY = "client_id";
46+
private static final String GRANT_TYPE_KEY = "grant_type";
47+
private static final String SCOPE_KEY = "scope";
48+
private static final String DEVICE_KEY = "device";
4549

4650
private Map<String, Object> parameters;
4751

4852
/**
4953
* Creates a new builder
5054
*/
51-
public ParameterBuilder() {
55+
private ParameterBuilder() {
5256
this.parameters = new HashMap<>();
5357
setScope(SCOPE_OFFLINE_ACCESS);
5458
}
5559

5660
/**
5761
* Creates a new builder with default parameters
62+
*
5863
* @param parameters default parameters
5964
*/
60-
public ParameterBuilder(Map<String, Object> parameters) {
65+
private ParameterBuilder(Map<String, Object> parameters) {
6166
CheckHelper.checkArgument(parameters != null, "Must provide non-null parameters");
6267
this.parameters = new HashMap<>(parameters);
6368
}
6469

6570
/**
6671
* Sets the 'client_id' parameter
72+
*
6773
* @param clientId clientID
6874
* @return itself
6975
*/
7076
public ParameterBuilder setClientId(String clientId) {
71-
return set("client_id", clientId);
77+
return set(CLIENT_ID_KEY, clientId);
7278
}
7379

7480
/**
7581
* Sets the 'grant_type' parameter
82+
*
7683
* @param grantType grant type
7784
* @return itself
7885
*/
7986
public ParameterBuilder setGrantType(String grantType) {
80-
return set("grant_type", grantType);
87+
return set(GRANT_TYPE_KEY, grantType);
8188
}
8289

8390
/**
8491
* Sets the 'connection' parameter
92+
*
8593
* @param connection name of the connection
8694
* @return itself
8795
*/
8896
public ParameterBuilder setConnection(String connection) {
89-
return set(CONNECTION, connection);
97+
return set(CONNECTION_KEY, connection);
9098
}
9199

92100
/**
93101
* Sets the 'scope' parameter.
102+
*
94103
* @param scope a scope value
95104
* @return itself
96105
*/
97106
public ParameterBuilder setScope(String scope) {
98-
return set("scope", scope);
107+
return set(SCOPE_KEY, scope);
99108
}
100109

101110
/**
102111
* Sets the 'device' parameter
112+
*
103113
* @param device a device name
104114
* @return itself
105115
*/
106116
public ParameterBuilder setDevice(String device) {
107-
return set("device", device);
117+
return set(DEVICE_KEY, device);
108118
}
109119

110120
/**
111121
* Sets the 'access_token' parameter
122+
*
112123
* @param accessToken a access token
113124
* @return itself
114125
*/
115126
public ParameterBuilder setAccessToken(String accessToken) {
116-
return set(ACCESS_TOKEN, accessToken);
127+
return set(ACCESS_TOKEN_KEY, accessToken);
117128
}
118129

119130
/**
120131
* Sets the 'send' parameter
132+
*
121133
* @param passwordlessType the type of passwordless login
122134
* @return itself
123135
*/
124136
public ParameterBuilder setSend(PasswordlessType passwordlessType) {
125137
switch (passwordlessType) {
126138
default:
127139
case CODE:
128-
return set(SEND, "code");
140+
return set(SEND_KEY, "code");
129141
case LINK:
130-
return set(SEND, "link");
142+
return set(SEND_KEY, "link");
131143
case LINK_ANDROID:
132-
return set(SEND, "link_android");
144+
return set(SEND_KEY, "link_android");
133145
case LINK_IOS:
134-
return set(SEND, "link_ios");
146+
return set(SEND_KEY, "link_ios");
135147
}
136148
}
137149

138150
/**
139151
* Sets a parameter
140-
* @param key parameter name
152+
*
153+
* @param key parameter name
141154
* @param value parameter value
142155
* @return itself
143156
*/
@@ -148,6 +161,7 @@ public ParameterBuilder set(String key, Object value) {
148161

149162
/**
150163
* Adds all parameter from a map
164+
*
151165
* @param parameters map with parameters to add
152166
* @return itself
153167
*/
@@ -160,6 +174,7 @@ public ParameterBuilder addAll(Map<String, Object> parameters) {
160174

161175
/**
162176
* Clears all existing parameters
177+
*
163178
* @return itself
164179
*/
165180
public ParameterBuilder clearAll() {
@@ -169,6 +184,7 @@ public ParameterBuilder clearAll() {
169184

170185
/**
171186
* Create a {@link Map} with all the parameters
187+
*
172188
* @return a new map with the parameters
173189
*/
174190
public Map<String, Object> asDictionary() {
@@ -177,6 +193,7 @@ public Map<String, Object> asDictionary() {
177193

178194
/**
179195
* Creates a new instance of the builder with default values
196+
*
180197
* @return a new builder
181198
*/
182199
public static ParameterBuilder newBuilder() {
@@ -185,6 +202,7 @@ public static ParameterBuilder newBuilder() {
185202

186203
/**
187204
* Creates a new instance of the builder without any default values
205+
*
188206
* @return a new builder
189207
*/
190208
public static ParameterBuilder newEmptyBuilder() {
@@ -193,6 +211,7 @@ public static ParameterBuilder newEmptyBuilder() {
193211

194212
/**
195213
* Creates a new instance of the builder with parameters.
214+
*
196215
* @param parameters default parameters
197216
* @return a new builder
198217
*/

lib/src/test/java/com/auth0/authentication/ParameterBuilderTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,19 @@ public void setUp() throws Exception {
5959

6060
@Test
6161
public void shouldInstantiateWithNoArguments() throws Exception {
62-
assertThat(new ParameterBuilder(), is(notNullValue()));
6362
assertThat(ParameterBuilder.newBuilder(), is(notNullValue()));
6463
}
6564

6665
@Test
6766
public void shouldInstantiateWithDefaultScope() throws Exception {
68-
assertThat(new ParameterBuilder().asDictionary(), hasEntry("scope", ParameterBuilder.SCOPE_OFFLINE_ACCESS));
6967
assertThat(ParameterBuilder.newBuilder().asDictionary(), hasEntry("scope", ParameterBuilder.SCOPE_OFFLINE_ACCESS));
7068
}
7169

7270
@Test
7371
public void shouldInstantiateWithArguments() throws Exception {
74-
assertThat(new ParameterBuilder(new HashMap<String, Object>()), is(notNullValue()));
7572
assertThat(ParameterBuilder.newBuilder(new HashMap<String, Object>()), is(notNullValue()));
7673
}
7774

78-
@Test
79-
public void shouldFailToInstantiateWithNullParameters() throws Exception {
80-
expectedException.expect(IllegalArgumentException.class);
81-
expectedException.expectMessage(equalToIgnoringCase("Must provide non-null parameters"));
82-
new ParameterBuilder(null);
83-
}
84-
8575
@Test
8676
public void shouldFailToInstantiateWithNullParametersInFactoryMethod() throws Exception {
8777
expectedException.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)