diff --git a/client/src/main/com/sinch/sdk/domains/numbers/adapters/NumbersService.java b/client/src/main/com/sinch/sdk/domains/numbers/adapters/NumbersService.java index 41ba55b1..ce2f1788 100644 --- a/client/src/main/com/sinch/sdk/domains/numbers/adapters/NumbersService.java +++ b/client/src/main/com/sinch/sdk/domains/numbers/adapters/NumbersService.java @@ -3,10 +3,10 @@ import com.sinch.sdk.auth.adapters.BasicAuthManager; import com.sinch.sdk.core.http.AuthManager; import com.sinch.sdk.core.http.HttpClient; +import com.sinch.sdk.core.utils.StringUtil; import com.sinch.sdk.models.Configuration; import java.util.AbstractMap; import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -25,9 +25,9 @@ public class NumbersService implements com.sinch.sdk.domains.numbers.NumbersServ public NumbersService(Configuration configuration, HttpClient httpClient) { - Objects.requireNonNull(configuration.getKeyId(), "'keyId' cannot be null"); - Objects.requireNonNull(configuration.getKeySecret(), "'keySecret' cannot be null"); - Objects.requireNonNull(configuration.getProjectId(), "'projectId' cannot be null"); + StringUtil.requireNonEmpty(configuration.getKeyId(), "'keyId' must be defined"); + StringUtil.requireNonEmpty(configuration.getKeySecret(), "'keySecret' must be defined"); + StringUtil.requireNonEmpty(configuration.getProjectId(), "'projectId' must be defined"); this.configuration = configuration; this.httpClient = httpClient; diff --git a/client/src/main/com/sinch/sdk/domains/sms/adapters/SMSService.java b/client/src/main/com/sinch/sdk/domains/sms/adapters/SMSService.java index ef3a39f5..813010ea 100644 --- a/client/src/main/com/sinch/sdk/domains/sms/adapters/SMSService.java +++ b/client/src/main/com/sinch/sdk/domains/sms/adapters/SMSService.java @@ -4,6 +4,7 @@ import com.sinch.sdk.core.http.AuthManager; import com.sinch.sdk.core.http.HttpClient; import com.sinch.sdk.core.http.HttpMapper; +import com.sinch.sdk.core.utils.StringUtil; import com.sinch.sdk.domains.sms.BatchesService; import com.sinch.sdk.domains.sms.DeliveryReportsService; import com.sinch.sdk.domains.sms.InboundsService; @@ -11,7 +12,6 @@ import com.sinch.sdk.models.Configuration; import java.util.AbstractMap; import java.util.Map; -import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -31,9 +31,9 @@ public SMSService(Configuration configuration, HttpClient httpClient) { // Currently, we are only supporting unified credentials: ensure credentials are // defined - Objects.requireNonNull(configuration.getKeyId(), "'keyId' cannot be null"); - Objects.requireNonNull(configuration.getKeySecret(), "'keySecret' cannot be null"); - Objects.requireNonNull(configuration.getProjectId(), "'projectId' cannot be null"); + StringUtil.requireNonEmpty(configuration.getKeyId(), "'keyId' must be defined"); + StringUtil.requireNonEmpty(configuration.getKeySecret(), "'keySecret' must be defined"); + StringUtil.requireNonEmpty(configuration.getProjectId(), "'projectId' must be defined"); this.configuration = configuration; this.httpClient = httpClient; diff --git a/client/src/main/com/sinch/sdk/domains/verification/adapters/VerificationService.java b/client/src/main/com/sinch/sdk/domains/verification/adapters/VerificationService.java index e8c48736..a310c251 100644 --- a/client/src/main/com/sinch/sdk/domains/verification/adapters/VerificationService.java +++ b/client/src/main/com/sinch/sdk/domains/verification/adapters/VerificationService.java @@ -5,12 +5,12 @@ import com.sinch.sdk.core.exceptions.ApiAuthException; import com.sinch.sdk.core.http.AuthManager; import com.sinch.sdk.core.http.HttpClient; +import com.sinch.sdk.core.utils.StringUtil; import com.sinch.sdk.domains.verification.StatusService; import com.sinch.sdk.domains.verification.VerificationsService; import com.sinch.sdk.domains.verification.WebHooksService; import com.sinch.sdk.models.Configuration; import java.util.Map; -import java.util.Objects; import java.util.TreeMap; public class VerificationService implements com.sinch.sdk.domains.verification.VerificationService { @@ -33,9 +33,10 @@ public VerificationService(Configuration configuration, HttpClient httpClient) { // Currently, we are not supporting unified credentials: ensure application credentials are // defined - Objects.requireNonNull(configuration.getApplicationKey(), "'applicationKey' cannot be null"); - Objects.requireNonNull( - configuration.getApplicationSecret(), "'applicationSecret' cannot be null"); + StringUtil.requireNonEmpty( + configuration.getApplicationKey(), "'applicationKey' must be defined"); + StringUtil.requireNonEmpty( + configuration.getApplicationSecret(), "'applicationSecret' must be defined"); this.configuration = configuration; this.httpClient = httpClient; diff --git a/client/src/test/java/com/sinch/sdk/domains/numbers/adapters/NumbersServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/numbers/adapters/NumbersServiceTest.java index 7330b847..e8c2ea0b 100644 --- a/client/src/test/java/com/sinch/sdk/domains/numbers/adapters/NumbersServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/numbers/adapters/NumbersServiceTest.java @@ -18,7 +18,7 @@ void doNotAcceptNullKey() { Configuration.builder().setKeyId(null).setKeySecret("foo").setProjectId("foo").build(); Exception exception = assertThrows( - NullPointerException.class, () -> new NumbersService(configuration, httpClient)); + IllegalArgumentException.class, () -> new NumbersService(configuration, httpClient)); assertTrue(exception.getMessage().contains("keyId")); } @@ -28,7 +28,7 @@ void doNotAcceptNullKeySecret() { Configuration.builder().setKeyId("foo").setKeySecret(null).setProjectId("foo").build(); Exception exception = assertThrows( - NullPointerException.class, () -> new NumbersService(configuration, httpClient)); + IllegalArgumentException.class, () -> new NumbersService(configuration, httpClient)); assertTrue(exception.getMessage().contains("keySecret")); } @@ -38,7 +38,7 @@ void doNotAcceptNullProject() { Configuration.builder().setKeyId("foo").setKeySecret("foo").setProjectId(null).build(); Exception exception = assertThrows( - NullPointerException.class, () -> new NumbersService(configuration, httpClient)); + IllegalArgumentException.class, () -> new NumbersService(configuration, httpClient)); assertTrue(exception.getMessage().contains("projectId")); } } diff --git a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/SMSServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/SMSServiceTest.java index 51de3bf4..8caa8614 100644 --- a/client/src/test/java/com/sinch/sdk/domains/sms/adapters/SMSServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/sms/adapters/SMSServiceTest.java @@ -17,7 +17,8 @@ void doNotAcceptNullKey() { Configuration configuration = Configuration.builder().setKeyId(null).setKeySecret("foo").setProjectId("foo").build(); Exception exception = - assertThrows(NullPointerException.class, () -> new SMSService(configuration, httpClient)); + assertThrows( + IllegalArgumentException.class, () -> new SMSService(configuration, httpClient)); assertTrue(exception.getMessage().contains("keyId")); } @@ -26,7 +27,8 @@ void doNotAcceptNullKeySecret() { Configuration configuration = Configuration.builder().setKeyId("foo").setKeySecret(null).setProjectId("foo").build(); Exception exception = - assertThrows(NullPointerException.class, () -> new SMSService(configuration, httpClient)); + assertThrows( + IllegalArgumentException.class, () -> new SMSService(configuration, httpClient)); assertTrue(exception.getMessage().contains("keySecret")); } @@ -35,7 +37,8 @@ void doNotAcceptNullProject() { Configuration configuration = Configuration.builder().setKeyId("foo").setKeySecret("foo").setProjectId(null).build(); Exception exception = - assertThrows(NullPointerException.class, () -> new SMSService(configuration, httpClient)); + assertThrows( + IllegalArgumentException.class, () -> new SMSService(configuration, httpClient)); assertTrue(exception.getMessage().contains("projectId")); } } diff --git a/client/src/test/java/com/sinch/sdk/domains/verification/adapters/VerificationServiceTest.java b/client/src/test/java/com/sinch/sdk/domains/verification/adapters/VerificationServiceTest.java index bb021686..0547370c 100644 --- a/client/src/test/java/com/sinch/sdk/domains/verification/adapters/VerificationServiceTest.java +++ b/client/src/test/java/com/sinch/sdk/domains/verification/adapters/VerificationServiceTest.java @@ -18,7 +18,8 @@ void doNotAcceptNullApplicationKey() { Configuration.builder().setApplicationKey(null).setApplicationSecret("foo secret").build(); Exception exception = assertThrows( - NullPointerException.class, () -> new VerificationService(configuration, httpClient)); + IllegalArgumentException.class, + () -> new VerificationService(configuration, httpClient)); assertTrue(exception.getMessage().contains("applicationKey")); } @@ -28,7 +29,8 @@ void doNotAcceptNullApplicationSecret() { Configuration.builder().setApplicationKey("foo key").setApplicationSecret(null).build(); Exception exception = assertThrows( - NullPointerException.class, () -> new VerificationService(configuration, httpClient)); + IllegalArgumentException.class, + () -> new VerificationService(configuration, httpClient)); assertTrue(exception.getMessage().contains("applicationSecret")); } } diff --git a/core/src/main/com/sinch/sdk/core/utils/StringUtil.java b/core/src/main/com/sinch/sdk/core/utils/StringUtil.java index ae515990..cb9c2b88 100644 --- a/core/src/main/com/sinch/sdk/core/utils/StringUtil.java +++ b/core/src/main/com/sinch/sdk/core/utils/StringUtil.java @@ -13,4 +13,21 @@ public class StringUtil { public static boolean isEmpty(String value) { return (null == value || value.trim().isEmpty()); } + + /** + * Checks that the specified object reference is not {@code null} not an empty string. This method + * is designed primarily for doing parameter validation in methods and constructors + * + * @param obj the object reference to check for nullity + * @param message detail message to be used in the event that a {@code IllegalArgumentException} + * is thrown + * @return {@code obj} if not {@code null} nor empty + * @throws IllegalArgumentException if {@code obj} is {@code null} or {@code empty} + */ + public static String requireNonEmpty(String obj, String message) { + if (isEmpty(obj)) { + throw new IllegalArgumentException(message); + } + return obj; + } }