diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2671e620..219f097de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
+# [8.14.0] - 2024-11-??
+- Close HTTP responses to prevent resource leaks
+- Added `RedactResponseException` and deprecated `VonageBadRequestException`
+
# [8.13.0] - 2024-10-28
- Added support for Verify custom templates
diff --git a/pom.xml b/pom.xml
index a0b61c041..5b95edc29 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
com.vonage
server-sdk
- 8.13.1
+ 8.14.0
Vonage Java Server SDK
Java client for Vonage APIs
diff --git a/src/main/java/com/vonage/client/AbstractMethod.java b/src/main/java/com/vonage/client/AbstractMethod.java
index ed4279465..932493056 100644
--- a/src/main/java/com/vonage/client/AbstractMethod.java
+++ b/src/main/java/com/vonage/client/AbstractMethod.java
@@ -18,15 +18,14 @@
import com.vonage.client.auth.*;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
-import org.apache.http.impl.client.BasicResponseHandler;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.Set;
-import java.util.UUID;
import java.util.stream.Collectors;
/**
@@ -77,20 +76,20 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
.setHeader("User-Agent", httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();
- HttpResponse response = httpWrapper.getHttpClient().execute(httpRequest);
- try {
- return postProcessParsedResponse(parseResponse(response));
+ try (CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
+ try {
+ return postProcessParsedResponse(parseResponse(response));
+ }
+ catch (IOException iox) {
+ throw new VonageResponseParseException(iox);
+ }
}
- catch (IOException io) {
- throw new VonageResponseParseException("Unable to parse response.", io);
+ catch (IOException iox) {
+ throw new VonageMethodFailedException("Something went wrong while executing the HTTP request.", iox);
}
}
- catch (UnsupportedEncodingException uee) {
- throw new VonageUnexpectedException("UTF-8 encoding is not supported by this JVM.", uee);
- }
- catch (IOException io) {
- throw new VonageMethodFailedException("Something went wrong while executing the HTTP request: " +
- io.getMessage() + ".", io);
+ catch (UnsupportedEncodingException uex) {
+ throw new VonageUnexpectedException("UTF-8 encoding is not supported by this JVM.", uex);
}
}
@@ -105,7 +104,7 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
*
* @throws VonageClientException If no appropriate {@link AuthMethod} is available
*/
- protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException {
+ final RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException {
AuthMethod am = getAuthMethod();
if (am instanceof HeaderAuthMethod) {
request.setHeader("Authorization", ((HeaderAuthMethod) am).getHeaderValue());
diff --git a/src/main/java/com/vonage/client/HttpWrapper.java b/src/main/java/com/vonage/client/HttpWrapper.java
index c8a7906d3..c9c5384e7 100644
--- a/src/main/java/com/vonage/client/HttpWrapper.java
+++ b/src/main/java/com/vonage/client/HttpWrapper.java
@@ -23,6 +23,7 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.SocketConfig;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import java.nio.charset.StandardCharsets;
@@ -34,12 +35,12 @@
public class HttpWrapper {
private static final String
CLIENT_NAME = "vonage-java-sdk",
- CLIENT_VERSION = "8.13.1",
+ CLIENT_VERSION = "8.14.0",
JAVA_VERSION = System.getProperty("java.version"),
USER_AGENT = String.format("%s/%s java/%s", CLIENT_NAME, CLIENT_VERSION, JAVA_VERSION);
private AuthCollection authCollection;
- private HttpClient httpClient;
+ private CloseableHttpClient httpClient;
private HttpConfig httpConfig;
public HttpWrapper(HttpConfig httpConfig, AuthCollection authCollection) {
@@ -64,7 +65,7 @@ public HttpWrapper(HttpConfig httpConfig, AuthMethod... authMethods) {
*
* @return The Apache HTTP client instance.
*/
- public HttpClient getHttpClient() {
+ public CloseableHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = createHttpClient();
}
@@ -103,7 +104,7 @@ public String getApiKey() {
@Deprecated
public void setHttpClient(HttpClient httpClient) {
- this.httpClient = httpClient;
+ this.httpClient = (CloseableHttpClient) httpClient;
}
@Deprecated
@@ -125,7 +126,7 @@ public void setAuthCollection(AuthCollection authCollection) {
this.authCollection = authCollection;
}
- protected HttpClient createHttpClient() {
+ protected CloseableHttpClient createHttpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(200);
connectionManager.setMaxTotal(200);
diff --git a/src/main/java/com/vonage/client/VonageBadRequestException.java b/src/main/java/com/vonage/client/VonageBadRequestException.java
index 841521e8d..0e1442e45 100644
--- a/src/main/java/com/vonage/client/VonageBadRequestException.java
+++ b/src/main/java/com/vonage/client/VonageBadRequestException.java
@@ -15,6 +15,7 @@
*/
package com.vonage.client;
+@Deprecated
public class VonageBadRequestException extends VonageClientException {
public VonageBadRequestException() {
super();
diff --git a/src/main/java/com/vonage/client/VonageClient.java b/src/main/java/com/vonage/client/VonageClient.java
index d2c96c499..dcb16c46f 100644
--- a/src/main/java/com/vonage/client/VonageClient.java
+++ b/src/main/java/com/vonage/client/VonageClient.java
@@ -42,6 +42,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.Objects;
import java.util.UUID;
/**
@@ -52,7 +53,11 @@
*
.
*/
public class VonageClient {
- private final HttpWrapper httpWrapper;
+ /**
+ * The HTTP wrapper for this client and its sub-clients.
+ */
+ final HttpWrapper httpWrapper;
+
private final AccountClient account;
private final ApplicationClient application;
private final InsightClient insight;
@@ -74,9 +79,16 @@ public class VonageClient {
private final SimSwapClient simSwap;
private final NumberVerificationClient numberVerification;
+ /**
+ * Constructor which uses the builder pattern for instantiation.
+ *
+ * @param builder The builder object to use for configuration.
+ */
private VonageClient(Builder builder) {
httpWrapper = new HttpWrapper(builder.httpConfig, builder.authCollection);
- httpWrapper.setHttpClient(builder.httpClient);
+ if (builder.httpClient != null) {
+ httpWrapper.setHttpClient(builder.httpClient);
+ }
account = new AccountClient(httpWrapper);
application = new ApplicationClient(httpWrapper);
@@ -100,45 +112,91 @@ private VonageClient(Builder builder) {
numberVerification = new NumberVerificationClient(httpWrapper);
}
+ /**
+ * Returns the Account API client.
+ *
+ * @return The {@linkplain AccountClient}.
+ */
public AccountClient getAccountClient() {
return account;
}
+ /**
+ * Returns the Application API client.
+ *
+ * @return The {@linkplain ApplicationClient}.
+ */
public ApplicationClient getApplicationClient() {
return application;
}
+ /**
+ * Returns the Number Insight API client.
+ *
+ * @return The {@linkplain InsightClient}.
+ */
public InsightClient getInsightClient() {
return insight;
}
+ /**
+ * Returns the Numbers API client.
+ *
+ * @return The {@linkplain NumbersClient}.
+ */
public NumbersClient getNumbersClient() {
return numbers;
}
+ /**
+ * Returns the SMS API client.
+ *
+ * @return The {@linkplain SmsClient}.
+ */
public SmsClient getSmsClient() {
return sms;
}
+ /**
+ * Returns the Verify v1 API client.
+ *
+ * @return The {@linkplain VerifyClient}.
+ */
public VerifyClient getVerifyClient() {
return verify;
}
+ /**
+ * Returns the Voice API client.
+ *
+ * @return The {@linkplain VoiceClient}.
+ */
public VoiceClient getVoiceClient() {
return voice;
}
+ /**
+ * Returns the Conversion API client.
+ *
+ * @return The {@linkplain ConversionClient}.
+ */
public ConversionClient getConversionClient() {
return conversion;
}
+ /**
+ * Returns the Redact API client.
+ *
+ * @return The {@linkplain RedactClient}.
+ */
public RedactClient getRedactClient() {
return redact;
}
/**
+ * Returns the Messages v1 API client.
*
- * @return The Messages v1 client.
+ * @return The {@linkplain MessagesClient}.
* @since 6.5.0
*/
public MessagesClient getMessagesClient() {
@@ -146,8 +204,9 @@ public MessagesClient getMessagesClient() {
}
/**
+ * Returns the Proactive Connect API client.
*
- * @return The Proactive Connect client.
+ * @return The {@linkplain ProactiveConnectClient}.
* @since 7.6.0
* @deprecated This API is sunset and will be removed in the next major release.
*/
@@ -157,10 +216,10 @@ public ProactiveConnectClient getProactiveConnectClient() {
}
/**
+ * Returns the Meetings API client.
*
- * @return The Meetings client.
+ * @return The {@linkplain MeetingsClient}.
* @since 7.6.0
- *
* @deprecated Support for this API will be removed in the next major release.
*/
@Deprecated
@@ -169,8 +228,9 @@ public MeetingsClient getMeetingsClient() {
}
/**
+ * Returns the Verify v2 API client.
*
- * @return The Verify v2 client.
+ * @return The {@linkplain Verify2Client}.
* @since 7.4.0
*/
public Verify2Client getVerify2Client() {
@@ -178,9 +238,9 @@ public Verify2Client getVerify2Client() {
}
/**
+ * Returns the Subaccounts API client.
*
- *
- * @return The Subaccounts client.
+ * @return The {@linkplain SubaccountsClient}.
* @since 7.5.0
*/
public SubaccountsClient getSubaccountsClient() {
@@ -188,9 +248,9 @@ public SubaccountsClient getSubaccountsClient() {
}
/**
+ * Returns the Users API client.
*
- *
- * @return The Users client.
+ * @return The {@linkplain UsersClient}.
* @since 7.7.0
*/
public UsersClient getUsersClient() {
@@ -198,9 +258,9 @@ public UsersClient getUsersClient() {
}
/**
- * Returns the Video client.
+ * Returns the Video API client.
*
- * @return The Video API client.
+ * @return The {@linkplain VideoClient}.
* @since 8.0.0-beta1
*/
public VideoClient getVideoClient() {
@@ -208,9 +268,9 @@ public VideoClient getVideoClient() {
}
/**
- * Returns the Number Insight v2 client.
+ * Returns the Fraud Detection API client.
*
- * @return The NI v2 client.
+ * @return The {@linkplain NumberInsight2Client}.
* @since 8.2.0
*/
public NumberInsight2Client getNumberInsight2Client() {
@@ -228,9 +288,9 @@ public ConversationsClient getConversationsClient() {
}
/**
- * Returns the CAMARA SIM Swap client.
+ * Returns the CAMARA SIM Swap API client.
*
- * @return The SIM Swap client.
+ * @return The {@linkplain SimSwapClient}.
* @since 8.8.0
*/
public SimSwapClient getSimSwapClient() {
@@ -238,9 +298,9 @@ public SimSwapClient getSimSwapClient() {
}
/**
- * Returns the CAMARA Number Verification client.
+ * Returns the CAMARA Number Verification API client.
*
- * @return The Number Verification client.
+ * @return The {@linkplain NumberVerificationClient}.
* @since 8.9.0
*/
public NumberVerificationClient getNumberVerificationClient() {
@@ -258,13 +318,6 @@ public String generateJwt() throws VonageUnacceptableAuthException {
return httpWrapper.getAuthCollection().getAuth(JWTAuthMethod.class).generateToken();
}
- /**
- * @return The {@link HttpWrapper}
- */
- HttpWrapper getHttpWrapper() {
- return httpWrapper;
- }
-
/**
* Entry point for constructing an instance of this class.
*
@@ -274,6 +327,9 @@ public static Builder builder() {
return new Builder();
}
+ /**
+ * Builder for specifying the properties of the client.
+ */
public static class Builder {
private AuthCollection authCollection;
private HttpConfig httpConfig = HttpConfig.defaultConfig();
@@ -284,6 +340,8 @@ public static class Builder {
private HashUtil.HashType hashType = HashUtil.HashType.MD5;
/**
+ * Configure the HTTP client parameters.
+ *
* @param httpConfig Configuration options for the {@link HttpWrapper}.
*
* @return This builder.
@@ -294,6 +352,8 @@ public Builder httpConfig(HttpConfig httpConfig) {
}
/**
+ * Set the underlying HTTP client instance.
+ *
* @param httpClient Custom implementation of {@link HttpClient}.
*
* @return This builder.
@@ -370,8 +430,9 @@ public Builder signatureSecret(String signatureSecret) {
}
/**
+ * Sets the hash type to use for signing requests.
*
- * @param hashType The hashing strategy for signature keys.
+ * @param hashType The hashing strategy for signature keys as an enum.
*
* @return This builder.
*/
@@ -438,7 +499,9 @@ public Builder privateKeyPath(String privateKeyPath) throws VonageUnableToReadPr
}
/**
- * @return a new {@link VonageClient} from the stored builder options.
+ * Builds the client with this builder's parameters.
+ *
+ * @return A new {@link VonageClient} from the stored builder options.
*
* @throws VonageClientCreationException if credentials aren't provided in a valid pairing or there were issues
* generating an {@link JWTAuthMethod} with the provided credentials.
diff --git a/src/main/java/com/vonage/client/VonageResponseParseException.java b/src/main/java/com/vonage/client/VonageResponseParseException.java
index 91c443bb3..ed2443d20 100644
--- a/src/main/java/com/vonage/client/VonageResponseParseException.java
+++ b/src/main/java/com/vonage/client/VonageResponseParseException.java
@@ -15,7 +15,6 @@
*/
package com.vonage.client;
-
/**
* An exception that indicates the contents of an HttpResponse could not be parsed.
*/
@@ -24,6 +23,10 @@ public VonageResponseParseException(String message) {
this(message, null);
}
+ public VonageResponseParseException(Throwable ex) {
+ this("Failed to parse the API response.", ex);
+ }
+
public VonageResponseParseException(String message, Throwable t) {
super(message, t);
}
diff --git a/src/main/java/com/vonage/client/redact/RedactClient.java b/src/main/java/com/vonage/client/redact/RedactClient.java
index d6cb118bc..32e548833 100644
--- a/src/main/java/com/vonage/client/redact/RedactClient.java
+++ b/src/main/java/com/vonage/client/redact/RedactClient.java
@@ -32,10 +32,10 @@ final class Endpoint extends DynamicEndpoint {
Endpoint(R... type) {
super(DynamicEndpoint. builder(type)
.wrapper(wrapper).requestMethod(HttpMethod.POST)
- .responseExceptionType(VonageBadRequestException.class)
+ .responseExceptionType(RedactResponseException.class)
.authMethod(ApiKeyHeaderAuthMethod.class)
.pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig()
- .getVersionedApiBaseUri("v1") + "/redact/transaction"
+ .getApiBaseUri() + "/v1/redact/transaction"
)
);
}
diff --git a/src/main/java/com/vonage/client/redact/RedactResponseException.java b/src/main/java/com/vonage/client/redact/RedactResponseException.java
new file mode 100644
index 000000000..96efbb23a
--- /dev/null
+++ b/src/main/java/com/vonage/client/redact/RedactResponseException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2024 Vonage
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.vonage.client.redact;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.vonage.client.VonageApiResponseException;
+
+/**
+ * Response returned when an error is encountered (i.e. the API returns a non-2xx status code).
+ *
+ * @since 8.14.0
+ */
+public final class RedactResponseException extends VonageApiResponseException {
+
+ void setStatusCode(int statusCode) {
+ this.statusCode = statusCode;
+ }
+
+ /**
+ * Creates an instance of this class from a JSON payload.
+ *
+ * @param json The JSON string to parse.
+ * @return An instance of this class with all known fields populated from the JSON payload, if present.
+ */
+ @JsonCreator
+ public static RedactResponseException fromJson(String json) {
+ return fromJson(RedactResponseException.class, json);
+ }
+}
diff --git a/src/test/java/com/vonage/client/AbstractMethodTest.java b/src/test/java/com/vonage/client/AbstractMethodTest.java
index 2f10bc7d7..fc67a351f 100644
--- a/src/test/java/com/vonage/client/AbstractMethodTest.java
+++ b/src/test/java/com/vonage/client/AbstractMethodTest.java
@@ -19,15 +19,15 @@
import static com.vonage.client.TestUtils.*;
import com.vonage.client.auth.*;
import com.vonage.client.auth.hashutils.HashUtil;
-import io.jsonwebtoken.lang.Assert;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.*;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import static org.junit.jupiter.api.Assertions.*;
@@ -90,20 +90,29 @@ static String getEntityContentsAsString(HttpEntity entity) throws IOException {
}
}
+ static class CloseableBasicHttpResponse extends BasicHttpResponse implements CloseableHttpResponse {
+ CloseableBasicHttpResponse() {
+ super(new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK"));
+ }
+
+ @Override
+ public void close() {
+ System.out.println("CloseableBasicHttpResponse.close() called");
+ }
+ }
+
private HttpServer httpServer;
private HttpWrapper mockWrapper;
- private HttpClient mockHttpClient;
+ private CloseableHttpClient mockHttpClient;
private AuthMethod mockAuthMethod;
- private final HttpResponse basicResponse = new BasicHttpResponse(
- new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), 200, "OK")
- );
+ private final CloseableHttpResponse basicResponse = new CloseableBasicHttpResponse();
@BeforeEach
public void setUp() throws Exception {
mockWrapper = mock(HttpWrapper.class);
AuthCollection mockAuthMethods = mock(AuthCollection.class);
mockAuthMethod = mock(AuthMethod.class);
- mockHttpClient = mock(HttpClient.class);
+ mockHttpClient = mock(CloseableHttpClient.class);
when(mockAuthMethods.getAcceptableAuthMethod(any())).thenReturn(mockAuthMethod);
when(mockWrapper.getHttpClient()).thenReturn(mockHttpClient);
when(mockHttpClient.execute(any(HttpUriRequest.class))).thenReturn(basicResponse);
@@ -239,10 +248,10 @@ public void rse() throws Exception {
ConcreteMethod method = mockJsonResponse(json, true);
try {
method.execute("");
- Assert.isTrue(false,"Should have gotten a Parsing exception");
+ fail("Should have gotten a Parsing exception");
}
catch (VonageResponseParseException ex){
- Assert.isTrue(ex.getCause() instanceof IOException, "Unknown Exception Caused Throw");
+ assertInstanceOf(IOException.class, ex.getCause(), "Unknown Exception Caused Throw");
}
}
@@ -254,10 +263,10 @@ public void testFailedHttpExecute() throws Exception {
when(mockHttpClient.execute(any(HttpUriRequest.class))).thenThrow(ex);
try {
method.execute("");
- Assert.isTrue(false, "There should have been a Vonage Client exception thrown");
+ fail("There should have been a Vonage Client exception thrown");
}
catch (VonageMethodFailedException e) {
- Assert.isTrue(e.getCause() instanceof IOException, "The cause of the exception was not correct");
+ assertInstanceOf(IOException.class, e.getCause(), "The cause of the exception was not correct");
}
}
diff --git a/src/test/java/com/vonage/client/TestUtils.java b/src/test/java/com/vonage/client/TestUtils.java
index a32c50b26..8268f3d45 100644
--- a/src/test/java/com/vonage/client/TestUtils.java
+++ b/src/test/java/com/vonage/client/TestUtils.java
@@ -23,8 +23,10 @@
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import static org.junit.jupiter.api.Assertions.*;
@@ -159,15 +161,15 @@ public static HttpWrapper httpWrapperWithAllAuthMethods() {
// TODO make package-private after removing Meetings
- public static HttpClient stubHttpClient(int statusCode) throws Exception {
+ public static CloseableHttpClient stubHttpClient(int statusCode) throws Exception {
return stubHttpClient(statusCode, "");
}
// TODO make package-private after removing Meetings
- public static HttpClient stubHttpClient(int statusCode, String content, String... additionalReturns) throws Exception {
- HttpClient result = mock(HttpClient.class);
+ public static CloseableHttpClient stubHttpClient(int statusCode, String content, String... additionalReturns) throws Exception {
+ CloseableHttpClient result = mock(CloseableHttpClient.class);
- HttpResponse response = mock(HttpResponse.class);
+ CloseableHttpResponse response = mock(CloseableHttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);
diff --git a/src/test/java/com/vonage/client/VonageClientTest.java b/src/test/java/com/vonage/client/VonageClientTest.java
index eb9e94df6..5097b890a 100644
--- a/src/test/java/com/vonage/client/VonageClientTest.java
+++ b/src/test/java/com/vonage/client/VonageClientTest.java
@@ -21,14 +21,12 @@
import com.vonage.client.voice.Call;
import com.vonage.client.voice.CallEvent;
import com.vonage.client.voice.CallStatus;
-import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
-import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
public class VonageClientTest extends AbstractClientTest {
@@ -89,11 +87,11 @@ public void testGenerateJwt() throws Exception {
String constructedToken = client.generateJwt();
byte[] publicKeyBytes = testUtils.loadKey("test/keys/application_public_key.der");
- X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
- KeyFactory kf = KeyFactory.getInstance("RSA");
- PublicKey key = kf.generatePublic(spec);
+ var spec = new X509EncodedKeySpec(publicKeyBytes);
+ var keyFactory = KeyFactory.getInstance("RSA");
+ var key = keyFactory.generatePublic(spec);
- Claims claims = Jwts.parser().verifyWith(key).build().parseSignedClaims(constructedToken).getPayload();
+ var claims = Jwts.parser().verifyWith(key).build().parseSignedClaims(constructedToken).getPayload();
assertEquals(APPLICATION_ID_STR, claims.get("application_id"));
}
@@ -136,7 +134,7 @@ public void testSoloPrivateKeyContents() {
@Test
public void testApiKeyWithSecret() throws VonageUnacceptableAuthException {
VonageClient vonageClient = VonageClient.builder().apiKey("api-key").apiSecret("api-secret").build();
- AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();
+ AuthCollection authCollection = vonageClient.httpWrapper.getAuthCollection();
assertTrue(authCollection.hasAuthMethod(ApiKeyHeaderAuthMethod.class));
}
@@ -146,7 +144,7 @@ public void testApiKeyWithSignatureSecret() throws Exception {
var vonageClient = VonageClient.builder()
.apiKey(API_KEY).signatureSecret(SIGNATURE_SECRET).hashType(hashType).build();
- var sigAuthMethod = vonageClient.getHttpWrapper().getAuthCollection().getAuth(SignatureAuthMethod.class);
+ var sigAuthMethod = vonageClient.httpWrapper.getAuthCollection().getAuth(SignatureAuthMethod.class);
var params = sigAuthMethod.getAuthParams(new RequestQueryParams());
// This is messy but trying to generate a signature auth method and then comparing with
@@ -171,7 +169,7 @@ public void testApplicationIdWithCertContentsAsBytes() throws Exception {
.privateKeyContents(keyBytes)
.build();
- AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();
+ AuthCollection authCollection = vonageClient.httpWrapper.getAuthCollection();
assertTrue(authCollection.hasAuthMethod(JWTAuthMethod.class));
}
@@ -181,7 +179,7 @@ public void testApplicationIdWithCertContentsAsString() throws Exception {
String key = new String(testUtils.loadKey("test/keys/application_key"));
VonageClient vonageClient = VonageClient.builder().applicationId(APPLICATION_ID_STR).privateKeyContents(key).build();
- AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();
+ AuthCollection authCollection = vonageClient.httpWrapper.getAuthCollection();
assertTrue(authCollection.hasAuthMethod(JWTAuthMethod.class));
}
@@ -191,7 +189,7 @@ public void testApplicationIdWithCertPath() throws Exception {
.applicationId(APPLICATION_ID_STR)
.privateKeyPath(privateKeyPath)
.build();
- AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();
+ AuthCollection authCollection = vonageClient.httpWrapper.getAuthCollection();
assertTrue(authCollection.hasAuthMethod(JWTAuthMethod.class));
}
@@ -201,7 +199,7 @@ public void testApplicationIdWithCertPathAsString() throws Exception {
.applicationId(APPLICATION_ID)
.privateKeyPath(privateKeyPath)
.build();
- AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();
+ AuthCollection authCollection = vonageClient.httpWrapper.getAuthCollection();
assertTrue(authCollection.hasAuthMethod(JWTAuthMethod.class));
}
@@ -217,8 +215,8 @@ public void testDefaultHttpConfig() {
HttpConfig config = HttpConfig.defaultConfig();
VonageClient vonageClient = VonageClient.builder().build();
- assertEquals(config.getApiBaseUri(), vonageClient.getHttpWrapper().getHttpConfig().getApiBaseUri());
- assertEquals(config.getRestBaseUri(), vonageClient.getHttpWrapper().getHttpConfig().getRestBaseUri());
+ assertEquals(config.getApiBaseUri(), vonageClient.httpWrapper.getHttpConfig().getApiBaseUri());
+ assertEquals(config.getRestBaseUri(), vonageClient.httpWrapper.getHttpConfig().getRestBaseUri());
}
@Test
@@ -226,7 +224,7 @@ public void testHttpConfig() {
HttpConfig config = HttpConfig.builder().apiBaseUri("https://example.org").build();
VonageClient vonageClient = VonageClient.builder().httpConfig(config).build();
- assertEquals(config, vonageClient.getHttpWrapper().getHttpConfig());
+ assertEquals(config, vonageClient.httpWrapper.getHttpConfig());
}
@Test
diff --git a/src/test/java/com/vonage/client/auth/AuthCollectionTest.java b/src/test/java/com/vonage/client/auth/AuthCollectionTest.java
index 4f0f3ebec..b4220b4c6 100644
--- a/src/test/java/com/vonage/client/auth/AuthCollectionTest.java
+++ b/src/test/java/com/vonage/client/auth/AuthCollectionTest.java
@@ -21,7 +21,6 @@
import static org.junit.jupiter.api.Assertions.*;
import java.util.Collections;
import java.util.Set;
-import java.util.UUID;
public class AuthCollectionTest {
private static final Set>
diff --git a/src/test/java/com/vonage/client/redact/RedactClientTest.java b/src/test/java/com/vonage/client/redact/RedactClientTest.java
index 9c2e4f81b..1549f89a5 100644
--- a/src/test/java/com/vonage/client/redact/RedactClientTest.java
+++ b/src/test/java/com/vonage/client/redact/RedactClientTest.java
@@ -18,14 +18,13 @@
import com.vonage.client.AbstractClientTest;
import com.vonage.client.DynamicEndpointTestSpec;
import com.vonage.client.RestEndpoint;
-import com.vonage.client.VonageBadRequestException;
import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
import com.vonage.client.auth.AuthMethod;
import com.vonage.client.common.HttpMethod;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.*;
-import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
public class RedactClientTest extends AbstractClientTest {
@@ -61,11 +60,11 @@ public void testWrongCredentials() throws Exception {
RedactRequest redactRequest = new RedactRequest("test-id", RedactRequest.Product.VOICE);
stubResponseAndAssertThrows(401, () ->
client.redactTransaction(redactRequest),
- VonageBadRequestException.class
+ RedactResponseException.class
);
stubResponseAndAssertThrows(401, () ->
client.redactTransaction(redactRequest.getId(), redactRequest.getProduct()),
- VonageBadRequestException.class
+ RedactResponseException.class
);
}
@@ -73,11 +72,11 @@ public void testWrongCredentials() throws Exception {
public void testPrematureRedactionOrUnauthorized() throws Exception {
RedactRequest redactRequest = new RedactRequest("test-id", RedactRequest.Product.VOICE);
stubResponseAndAssertThrows(403, () ->
- client.redactTransaction(redactRequest), VonageBadRequestException.class
+ client.redactTransaction(redactRequest), RedactResponseException.class
);
stubResponseAndAssertThrows(403, () ->
client.redactTransaction(redactRequest.getId(), redactRequest.getProduct()),
- VonageBadRequestException.class
+ RedactResponseException.class
);
}
@@ -85,11 +84,11 @@ public void testPrematureRedactionOrUnauthorized() throws Exception {
public void testInvalidId() throws Exception {
RedactRequest redactRequest = new RedactRequest("test-id", RedactRequest.Product.VOICE);
stubResponseAndAssertThrows(404, () ->
- client.redactTransaction(redactRequest), VonageBadRequestException.class
+ client.redactTransaction(redactRequest), RedactResponseException.class
);
stubResponseAndAssertThrows(404, () ->
client.redactTransaction(redactRequest.getId(), redactRequest.getProduct()),
- VonageBadRequestException.class
+ RedactResponseException.class
);
}
@@ -109,12 +108,12 @@ protected HttpMethod expectedHttpMethod() {
@Override
protected Collection> expectedAuthMethods() {
- return Arrays.asList(ApiKeyHeaderAuthMethod.class);
+ return List.of(ApiKeyHeaderAuthMethod.class);
}
@Override
protected Class extends Exception> expectedResponseExceptionType() {
- return VonageBadRequestException.class;
+ return RedactResponseException.class;
}
@Override