From 10ad53c9a0b11fb1d5b3ffa8a6d040048e45e3d5 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Wed, 10 Jul 2024 09:14:55 -0400 Subject: [PATCH] MLE-14482 Better error message when username/password is empty --- gradle.properties | 2 +- .../impl/DatabaseClientPropertySource.java | 12 ++++++-- .../test/DatabaseClientBuilderTest.java | 28 +++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 0513440b7..b7edca21b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group=com.marklogic -version=6.6-SNAPSHOT +version=6.7-SNAPSHOT describedName=MarkLogic Java Client API publishUrl=file:../marklogic-java/releases diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java b/marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java index 5125ba6b5..e82d0937e 100644 --- a/marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java +++ b/marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java @@ -183,9 +183,13 @@ private DatabaseClientFactory.SecurityContext newSecurityContext(String type, SS } private String getRequiredStringValue(String propertyName) { + return getRequiredStringValue(propertyName, String.format("%s must be of type String", propertyName)); + } + + private String getRequiredStringValue(String propertyName, String errorMessage) { Object value = propertySource.apply(PREFIX + propertyName); if (value == null || !(value instanceof String)) { - throw new IllegalArgumentException(propertyName + " must be of type String"); + throw new IllegalArgumentException(errorMessage); } return (String) value; } @@ -204,13 +208,15 @@ private String getNullableStringValue(String propertyName, String defaultValue) private DatabaseClientFactory.SecurityContext newBasicAuthContext() { return new DatabaseClientFactory.BasicAuthContext( - getRequiredStringValue("username"), getRequiredStringValue("password") + getRequiredStringValue("username", "Must specify a username when using basic authentication."), + getRequiredStringValue("password", "Must specify a password when using basic authentication.") ); } private DatabaseClientFactory.SecurityContext newDigestAuthContext() { return new DatabaseClientFactory.DigestAuthContext( - getRequiredStringValue("username"), getRequiredStringValue("password") + getRequiredStringValue("username", "Must specify a username when using digest authentication."), + getRequiredStringValue("password", "Must specify a password when using digest authentication.") ); } diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java index 93af54f29..355b7b8fc 100644 --- a/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java +++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java @@ -91,6 +91,20 @@ void digest() { assertEquals("my-password", context.getPassword()); } + @Test + void digestNoUsername() { + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> + Common.newClientBuilder().withDigestAuth(null, "my-password").buildBean()); + assertEquals("Must specify a username when using digest authentication.", ex.getMessage()); + } + + @Test + void digestNoPassword() { + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> + Common.newClientBuilder().withDigestAuth("my-user", null).buildBean()); + assertEquals("Must specify a password when using digest authentication.", ex.getMessage()); + } + @Test void basic() { bean = Common.newClientBuilder() @@ -102,6 +116,20 @@ void basic() { assertEquals("my-password", context.getPassword()); } + @Test + void basicNoUsername() { + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> + Common.newClientBuilder().withBasicAuth(null, "my-password").buildBean()); + assertEquals("Must specify a username when using basic authentication.", ex.getMessage()); + } + + @Test + void basicNoPassword() { + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> + Common.newClientBuilder().withBasicAuth("my-user", null).buildBean()); + assertEquals("Must specify a password when using basic authentication.", ex.getMessage()); + } + @Test void cloudWithBasePath() { bean = Common.newClientBuilder()