Skip to content

Commit 10ad53c

Browse files
committed
MLE-14482 Better error message when username/password is empty
1 parent 92c78a7 commit 10ad53c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=com.marklogic
2-
version=6.6-SNAPSHOT
2+
version=6.7-SNAPSHOT
33
describedName=MarkLogic Java Client API
44
publishUrl=file:../marklogic-java/releases
55

marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ private DatabaseClientFactory.SecurityContext newSecurityContext(String type, SS
183183
}
184184

185185
private String getRequiredStringValue(String propertyName) {
186+
return getRequiredStringValue(propertyName, String.format("%s must be of type String", propertyName));
187+
}
188+
189+
private String getRequiredStringValue(String propertyName, String errorMessage) {
186190
Object value = propertySource.apply(PREFIX + propertyName);
187191
if (value == null || !(value instanceof String)) {
188-
throw new IllegalArgumentException(propertyName + " must be of type String");
192+
throw new IllegalArgumentException(errorMessage);
189193
}
190194
return (String) value;
191195
}
@@ -204,13 +208,15 @@ private String getNullableStringValue(String propertyName, String defaultValue)
204208

205209
private DatabaseClientFactory.SecurityContext newBasicAuthContext() {
206210
return new DatabaseClientFactory.BasicAuthContext(
207-
getRequiredStringValue("username"), getRequiredStringValue("password")
211+
getRequiredStringValue("username", "Must specify a username when using basic authentication."),
212+
getRequiredStringValue("password", "Must specify a password when using basic authentication.")
208213
);
209214
}
210215

211216
private DatabaseClientFactory.SecurityContext newDigestAuthContext() {
212217
return new DatabaseClientFactory.DigestAuthContext(
213-
getRequiredStringValue("username"), getRequiredStringValue("password")
218+
getRequiredStringValue("username", "Must specify a username when using digest authentication."),
219+
getRequiredStringValue("password", "Must specify a password when using digest authentication.")
214220
);
215221
}
216222

marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ void digest() {
9191
assertEquals("my-password", context.getPassword());
9292
}
9393

94+
@Test
95+
void digestNoUsername() {
96+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
97+
Common.newClientBuilder().withDigestAuth(null, "my-password").buildBean());
98+
assertEquals("Must specify a username when using digest authentication.", ex.getMessage());
99+
}
100+
101+
@Test
102+
void digestNoPassword() {
103+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
104+
Common.newClientBuilder().withDigestAuth("my-user", null).buildBean());
105+
assertEquals("Must specify a password when using digest authentication.", ex.getMessage());
106+
}
107+
94108
@Test
95109
void basic() {
96110
bean = Common.newClientBuilder()
@@ -102,6 +116,20 @@ void basic() {
102116
assertEquals("my-password", context.getPassword());
103117
}
104118

119+
@Test
120+
void basicNoUsername() {
121+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
122+
Common.newClientBuilder().withBasicAuth(null, "my-password").buildBean());
123+
assertEquals("Must specify a username when using basic authentication.", ex.getMessage());
124+
}
125+
126+
@Test
127+
void basicNoPassword() {
128+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
129+
Common.newClientBuilder().withBasicAuth("my-user", null).buildBean());
130+
assertEquals("Must specify a password when using basic authentication.", ex.getMessage());
131+
}
132+
105133
@Test
106134
void cloudWithBasePath() {
107135
bean = Common.newClientBuilder()

0 commit comments

Comments
 (0)