Skip to content

Commit e262eac

Browse files
authored
Fix bug in UserCreateRequest (#40)
* Fix bug in UserCreateRequest * general cleanup
1 parent d665999 commit e262eac

File tree

9 files changed

+258
-27
lines changed

9 files changed

+258
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 1.1.1 (12/19/2019)
6+
- Fixed bug in User create endpoint where setting a new user's role could only be set via a RoleId. Now you can set the role by name or id.
7+
58
## 1.1.0 (12/17/2019)
69

710
- Removed `org.apache.logging.log4j` dependency, instead relying on the org.slf4j logging interface/facade dependency explicitly.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.darksci</groupId>
88
<artifactId>pardot-api-client</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.1.1</version>
1010
<packaging>jar</packaging>
1111

1212
<!-- Require Maven 3.5.0 -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2017, 2018, 2019 Stephen Powis https://github.com/Crim/pardot-java-client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package com.darksci.pardot.api;
19+
20+
/**
21+
* An error occurred while attempting to connect to remote server.
22+
* Typically, the connection was refused remotely.
23+
*/
24+
public class ConnectionFailedException extends InvalidRequestException {
25+
public ConnectionFailedException(final String message, final int errorCode) {
26+
super(message, errorCode);
27+
}
28+
29+
public ConnectionFailedException(final String message, final Throwable cause) {
30+
super(message, cause);
31+
}
32+
}

src/main/java/com/darksci/pardot/api/request/user/UserCreateRequest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ public UserCreateRequest withUser(final NewUser user) {
4545
.withFirstName(user.getFirstName())
4646
.withLastName(user.getLastName())
4747
.withJobTitle(user.getJobTitle())
48-
.withRoleId(user.getRoleId())
4948
.withPhone(user.getPhone())
5049
.withUrl(user.getUrl())
5150
.withPasswordExpireable(user.isPasswordExpirable());
5251

52+
// Determine if role is being passed as a name or id.
53+
if (user.getRoleId() != null) {
54+
// RoleId is not null, so let's use that.
55+
withRole(user.getRoleId());
56+
} else if (user.getRole() != null) {
57+
// Role Name is not null, so lets use that.
58+
withRole(user.getRole());
59+
}
60+
5361
// Optional Timezone
5462
if (user.getTimezone() != null) {
5563
withTimezone(user.getTimezone());
@@ -114,13 +122,44 @@ public UserCreateRequest withJobTitle(final String jobTitle) {
114122
}
115123

116124
/**
117-
* Define the RoleId field on the user.
125+
* Define the Role field on the user by the role's id.
118126
*
119127
* @param roleId roleId for new user.
120128
* @return UserCreateRequest builder.
129+
* @deprecated see withRole(Long)
121130
*/
122131
public UserCreateRequest withRoleId(final Long roleId) {
123-
return setParam("role", roleId);
132+
return withRole(roleId);
133+
}
134+
135+
/**
136+
* Define the Role field on the user by name of the role.
137+
*
138+
* @param roleName name of the role for new user.
139+
* @return UserCreateRequest builder.
140+
*/
141+
public UserCreateRequest withRole(final String roleName) {
142+
// If roleName was passed
143+
if (roleName != null ) {
144+
// Clear out role_id property.
145+
setParam("role_id", null);
146+
}
147+
return setParam("role_name", roleName);
148+
}
149+
150+
/**
151+
* Define the Role field on the user by name of the role.
152+
*
153+
* @param roleId id of the role for new user.
154+
* @return UserCreateRequest builder.
155+
*/
156+
public UserCreateRequest withRole(final Long roleId) {
157+
// If roleId was passed
158+
if (roleId != null) {
159+
// Clear out role_name property.
160+
setParam("role_name", null);
161+
}
162+
return setParam("role_id", roleId);
124163
}
125164

126165
/**

src/main/java/com/darksci/pardot/api/response/user/NewUser.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,36 @@ public String getEmail() {
4343
return email;
4444
}
4545

46-
public void setEmail(final String email) {
46+
public NewUser setEmail(final String email) {
4747
this.email = email;
48+
return this;
4849
}
4950

5051
public String getFirstName() {
5152
return firstName;
5253
}
5354

54-
public void setFirstName(final String firstName) {
55+
public NewUser setFirstName(final String firstName) {
5556
this.firstName = firstName;
57+
return this;
5658
}
5759

5860
public String getLastName() {
5961
return lastName;
6062
}
6163

62-
public void setLastName(final String lastName) {
64+
public NewUser setLastName(final String lastName) {
6365
this.lastName = lastName;
66+
return this;
6467
}
6568

6669
public String getJobTitle() {
6770
return jobTitle;
6871
}
6972

70-
public void setJobTitle(final String jobTitle) {
73+
public NewUser setJobTitle(final String jobTitle) {
7174
this.jobTitle = jobTitle;
75+
return this;
7276
}
7377

7478
public Long getRoleId() {
@@ -79,13 +83,14 @@ public Long getRoleId() {
7983
* You can set the user's role by Id OR Name.
8084
* @param roleId Id of the role to set.
8185
*/
82-
public void setRoleId(final Long roleId) {
86+
public NewUser setRoleId(final Long roleId) {
8387
this.roleId = roleId;
8488

8589
// Null out role name.
8690
if (roleId != null) {
8791
this.role = null;
8892
}
93+
return this;
8994
}
9095

9196
public String getRole() {
@@ -96,53 +101,59 @@ public String getRole() {
96101
* You can set the user's role by Name or Id.
97102
* @param role Name of the role to set.
98103
*/
99-
public void setRole(final String role) {
104+
public NewUser setRole(final String role) {
100105
this.role = role;
101106

102107
// Null out roleId
103108
if (role != null) {
104109
roleId = null;
105110
}
111+
return this;
106112
}
107113

108114
public String getPhone() {
109115
return phone;
110116
}
111117

112-
public void setPhone(final String phone) {
118+
public NewUser setPhone(final String phone) {
113119
this.phone = phone;
120+
return this;
114121
}
115122

116123
public String getUrl() {
117124
return url;
118125
}
119126

120-
public void setUrl(final String url) {
127+
public NewUser setUrl(final String url) {
121128
this.url = url;
129+
return this;
122130
}
123131

124132
public DateTimeZone getTimezone() {
125133
return timezone;
126134
}
127135

128-
public void setTimezone(final DateTimeZone timezone) {
136+
public NewUser setTimezone(final DateTimeZone timezone) {
129137
this.timezone = timezone;
138+
return this;
130139
}
131140

132141
public boolean isPasswordExpirable() {
133142
return isPasswordExpirable;
134143
}
135144

136-
public void setPasswordExpirable(final boolean passwordExpirable) {
145+
public NewUser setPasswordExpirable(final boolean passwordExpirable) {
137146
isPasswordExpirable = passwordExpirable;
147+
return this;
138148
}
139149

140150
public String getCrmUsername() {
141151
return crmUsername;
142152
}
143153

144-
public void setCrmUsername(final String crmUsername) {
154+
public NewUser setCrmUsername(final String crmUsername) {
145155
this.crmUsername = crmUsername;
156+
return this;
146157
}
147158

148159
@Override

src/main/java/com/darksci/pardot/api/response/user/UserAbilitiesResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public List<String> getCredentials() {
6969

7070
@Override
7171
public String toString() {
72-
return credentials.toString();
72+
if (credentials != null) {
73+
return credentials.toString();
74+
}
75+
return "[]";
7376
}
7477
}
7578
}

src/main/java/com/darksci/pardot/api/rest/HttpClientRestClient.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.darksci.pardot.api.rest;
1919

2020
import com.darksci.pardot.api.Configuration;
21+
import com.darksci.pardot.api.ConnectionFailedException;
2122
import com.darksci.pardot.api.request.Request;
2223
import com.darksci.pardot.api.rest.handlers.RestResponseHandler;
2324
import com.darksci.pardot.api.rest.interceptor.RequestInterceptor;
@@ -48,6 +49,7 @@
4849
import javax.net.ssl.TrustManager;
4950
import javax.net.ssl.TrustManagerFactory;
5051
import java.io.IOException;
52+
import java.net.ConnectException;
5153
import java.nio.charset.StandardCharsets;
5254
import java.security.KeyManagementException;
5355
import java.security.KeyStore;
@@ -245,6 +247,7 @@ private <T> T submitRequest(final Request request, final ResponseHandler<T> resp
245247
* @param responseHandler The response Handler to use to parse the response
246248
* @param <T> The type that ResponseHandler returns.
247249
* @return Parsed response.
250+
* @throws ConnectionFailedException if remote server does not accept connection.
248251
*/
249252
private <T> T submitRequest(final String url, final Map<String, String> postParams, final ResponseHandler<T> responseHandler) throws IOException {
250253
try {
@@ -261,7 +264,7 @@ private <T> T submitRequest(final String url, final Map<String, String> postPara
261264
}
262265

263266
// Attach submitRequest params
264-
for (Map.Entry<String, String> entry : postParams.entrySet()) {
267+
for (final Map.Entry<String, String> entry : postParams.entrySet()) {
265268
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
266269
}
267270
post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
@@ -278,6 +281,12 @@ private <T> T submitRequest(final String url, final Map<String, String> postPara
278281
return httpClient.execute(post, responseHandler);
279282
} catch (final ClientProtocolException exception) {
280283
logger.error("Caught ClientProtocolException: {}", exception.getMessage(), exception);
284+
} catch (final ConnectException connectException) {
285+
// Signals that an error occurred while attempting to connect a
286+
// socket to a remote address and port. Typically, the connection
287+
// was refused remotely (e.g., no process is listening on the
288+
// remote address/port).
289+
throw new ConnectionFailedException(connectException.getMessage(), connectException);
281290
} catch (final IOException exception) {
282291
// Typically this is a parse error.
283292
logger.error("Caught IOException: {}", exception.getMessage(), exception);

src/test/java/com/darksci/pardot/api/PardotClientTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void userCreateTest() {
272272
.withPhone("123-123-1234")
273273
.withUrl("http:/www.example.com")
274274
.withPasswordExpireable(false)
275-
.withRoleId(4L)
275+
.withRole(4L)
276276
.withTimezone(DateTimeZone.UTC);
277277

278278
final User response = client.userCreate(createRequest);
@@ -295,7 +295,7 @@ public void userDeleteByEmailTest() {
295295
.withPhone("123-123-1234")
296296
.withUrl("http:/www.example.com")
297297
.withPasswordExpireable(false)
298-
.withRoleId(4L)
298+
.withRole(4L)
299299
.withTimezone(DateTimeZone.UTC);
300300

301301
final User response = client.userCreate(createRequest);
@@ -322,7 +322,7 @@ public void userDeleteByIdTest() {
322322
.withPhone("123-123-1234")
323323
.withUrl("http:/www.example.com")
324324
.withPasswordExpireable(false)
325-
.withRoleId(4L)
325+
.withRole(4L)
326326
.withTimezone(DateTimeZone.UTC);
327327

328328
final User response = client.userCreate(createRequest);
@@ -355,7 +355,7 @@ public void userUpdateRoleByIdTest() {
355355
.withPhone("123-123-1234")
356356
.withUrl("http:/www.example.com")
357357
.withPasswordExpireable(false)
358-
.withRoleId(originalRoleId)
358+
.withRole(originalRoleId)
359359
.withTimezone(DateTimeZone.UTC);
360360

361361
final User response = client.userCreate(createRequest);
@@ -391,7 +391,7 @@ public void campaignQueryTest() {
391391
* Attempt to read campaign.
392392
*/
393393
@Test
394-
public void campaignReadTest() throws IOException {
394+
public void campaignReadTest() {
395395
CampaignReadRequest request = new CampaignReadRequest()
396396
.selectById(14885L);
397397

@@ -746,7 +746,7 @@ public void formQueryTest() {
746746
* Attempt to read campaign.
747747
*/
748748
@Test
749-
public void formReadTest() throws IOException {
749+
public void formReadTest() {
750750
final FormReadRequest request = new FormReadRequest()
751751
.selectById(1L);
752752

@@ -1111,8 +1111,6 @@ public void prospectDeleteTest() {
11111111

11121112
// Issue request
11131113
final boolean response = client.prospectDelete(request);
1114-
1115-
assertNotNull("Should not be null", response);
11161114
logger.info("Response: {}", response);
11171115
}
11181116

@@ -1247,8 +1245,8 @@ public void tagObjectReadQuery() {
12471245

12481246
final TagObject response = client.tagObjectRead(request);
12491247
assertNotNull("Should not be null", response);
1250-
response.getType();
1251-
response.getTypeName();
1248+
assertNotNull(response.getType());
1249+
assertNotNull(response.getTypeName());
12521250
logger.info("Response: {}", response);
12531251
}
12541252

0 commit comments

Comments
 (0)