Skip to content

Commit

Permalink
feature: Move credential validation from SinchClient to 'domain' level
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Jan 25, 2024
1 parent b431a76 commit 909d369
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
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;
import com.sinch.sdk.domains.sms.WebHooksService;
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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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"));
}

Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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"));
}

Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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"));
}
}
17 changes: 17 additions & 0 deletions core/src/main/com/sinch/sdk/core/utils/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 909d369

Please sign in to comment.