From ff72718a3b13f6d5b68bcec67d39983155486498 Mon Sep 17 00:00:00 2001 From: Sina Madani Date: Tue, 29 Aug 2023 16:49:03 +0100 Subject: [PATCH] Refactor Numbers and Conversion clients to use DynamicEndpoint (#477) * Refactored Numbers to use DynamicEndpoint * Refactored ConversionClient * Refactored NumbersClient endpoint tests * Test NumbersResponseException * Improved Numbers coverage --- .../client/conversion/ConversionClient.java | 15 +- .../client/conversion/ConversionEndpoint.java | 67 ---- .../client/conversion/ConversionRequest.java | 15 +- .../client/numbers/BaseNumberRequest.java | 51 +++ .../client/numbers/BuyNumberEndpoint.java | 63 ---- .../client/numbers/BuyNumberRequest.java | 20 +- .../client/numbers/CancelNumberEndpoint.java | 63 ---- .../client/numbers/CancelNumberRequest.java | 20 +- .../client/numbers/ListNumbersEndpoint.java | 58 --- .../client/numbers/ListNumbersFilter.java | 31 +- .../client/numbers/ListNumbersResponse.java | 15 +- .../vonage/client/numbers/NumbersClient.java | 42 ++- .../numbers/NumbersResponseException.java | 62 +++ .../vonage/client/numbers/OwnedNumber.java | 3 +- .../client/numbers/SearchNumbersEndpoint.java | 58 --- .../client/numbers/SearchNumbersFilter.java | 27 +- .../client/numbers/SearchNumbersResponse.java | 15 +- .../client/numbers/UpdateNumberEndpoint.java | 65 ---- .../client/numbers/UpdateNumberRequest.java | 43 +-- .../client/DynamicEndpointTestSpec.java | 24 +- .../conversion/ConversionClientTest.java | 136 +++++-- .../conversion/ConversionEndpointTest.java | 108 ------ .../client/numbers/BuyNumberEndpointTest.java | 128 ------- .../client/numbers/BuyNumberRequestTest.java | 28 -- .../numbers/CancelNumberEndpointTest.java | 111 ------ .../numbers/CancelNumberRequestTest.java | 28 -- .../numbers/ListNumbersEndpointTest.java | 135 ------- .../client/numbers/NumbersClientTest.java | 356 ++++++++++++++---- .../numbers/NumbersEndpointTestSpec.java | 46 +++ .../numbers/SearchNumbersEndpointTest.java | 164 -------- .../numbers/UpdateNumberEndpointTest.java | 143 ------- .../subaccounts/SubaccountsClientTest.java | 5 +- 32 files changed, 683 insertions(+), 1462 deletions(-) delete mode 100644 src/main/java/com/vonage/client/conversion/ConversionEndpoint.java create mode 100644 src/main/java/com/vonage/client/numbers/BaseNumberRequest.java delete mode 100644 src/main/java/com/vonage/client/numbers/BuyNumberEndpoint.java delete mode 100644 src/main/java/com/vonage/client/numbers/CancelNumberEndpoint.java delete mode 100644 src/main/java/com/vonage/client/numbers/ListNumbersEndpoint.java create mode 100644 src/main/java/com/vonage/client/numbers/NumbersResponseException.java delete mode 100644 src/main/java/com/vonage/client/numbers/SearchNumbersEndpoint.java delete mode 100644 src/main/java/com/vonage/client/numbers/UpdateNumberEndpoint.java delete mode 100644 src/test/java/com/vonage/client/conversion/ConversionEndpointTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/BuyNumberEndpointTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/BuyNumberRequestTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/CancelNumberEndpointTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/CancelNumberRequestTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/ListNumbersEndpointTest.java create mode 100644 src/test/java/com/vonage/client/numbers/NumbersEndpointTestSpec.java delete mode 100644 src/test/java/com/vonage/client/numbers/SearchNumbersEndpointTest.java delete mode 100644 src/test/java/com/vonage/client/numbers/UpdateNumberEndpointTest.java diff --git a/src/main/java/com/vonage/client/conversion/ConversionClient.java b/src/main/java/com/vonage/client/conversion/ConversionClient.java index 3ccaee585..dcb73a88b 100644 --- a/src/main/java/com/vonage/client/conversion/ConversionClient.java +++ b/src/main/java/com/vonage/client/conversion/ConversionClient.java @@ -16,6 +16,9 @@ package com.vonage.client.conversion; import com.vonage.client.*; +import com.vonage.client.auth.SignatureAuthMethod; +import com.vonage.client.auth.TokenAuthMethod; +import com.vonage.client.common.HttpMethod; import java.util.Date; /** @@ -27,10 +30,16 @@ * See the Conversion API documentation. */ public class ConversionClient { - final ConversionEndpoint conversionEndpoint; + final RestEndpoint conversionEndpoint; - public ConversionClient(HttpWrapper httpWrapper) { - conversionEndpoint = new ConversionEndpoint(httpWrapper); + @SuppressWarnings("unchecked") + public ConversionClient(HttpWrapper wrapper) { + conversionEndpoint = DynamicEndpoint. builder(Void.class) + .pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig().getApiBaseUri() + + "/conversions/" + req.getType().name().toLowerCase() + ) + .authMethod(SignatureAuthMethod.class, TokenAuthMethod.class) + .requestMethod(HttpMethod.POST).wrapper(wrapper).build(); } /** diff --git a/src/main/java/com/vonage/client/conversion/ConversionEndpoint.java b/src/main/java/com/vonage/client/conversion/ConversionEndpoint.java deleted file mode 100644 index b0beef7c1..000000000 --- a/src/main/java/com/vonage/client/conversion/ConversionEndpoint.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2023 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.conversion; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.SignatureAuthMethod; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.SimpleDateFormat; - -class ConversionEndpoint extends AbstractMethod { - private static final Class[] ALLOWED_AUTH_METHODS = {SignatureAuthMethod.class, TokenAuthMethod.class}; - private static final String PATH = "/conversions/"; - - ConversionEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(ConversionRequest conversionRequest) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getApiBaseUri() + PATH + conversionRequest.getType().name().toLowerCase(); - return RequestBuilder.post(uri) - .addParameter("message-id", conversionRequest.getMessageId()) - .addParameter("delivered", String.valueOf(conversionRequest.isDelivered())) - .addParameter( - "timestamp", - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(conversionRequest.getTimestamp()) - ); - } - - @Override - public Void parseResponse(HttpResponse response) throws IOException, VonageClientException { - if (response.getStatusLine().getStatusCode() != 200) { - throw new VonageBadRequestException(EntityUtils.toString(response.getEntity())); - } - return null; - } - - public String getBaseUri() { - return httpWrapper.getHttpConfig().getApiBaseUri() + PATH; - } -} diff --git a/src/main/java/com/vonage/client/conversion/ConversionRequest.java b/src/main/java/com/vonage/client/conversion/ConversionRequest.java index 38bf415a0..0d10c321b 100644 --- a/src/main/java/com/vonage/client/conversion/ConversionRequest.java +++ b/src/main/java/com/vonage/client/conversion/ConversionRequest.java @@ -15,9 +15,13 @@ */ package com.vonage.client.conversion; +import com.vonage.client.QueryParamsRequest; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; -public class ConversionRequest { +public class ConversionRequest implements QueryParamsRequest { private final Type type; private final String messageId; private final boolean delivered; @@ -46,6 +50,15 @@ public Date getTimestamp() { return timestamp; } + @Override + public Map makeParams() { + LinkedHashMap params = new LinkedHashMap<>(4); + params.put("message-id", messageId); + params.put("delivered", String.valueOf(delivered)); + params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp)); + return params; + } + public enum Type { SMS, VOICE } diff --git a/src/main/java/com/vonage/client/numbers/BaseNumberRequest.java b/src/main/java/com/vonage/client/numbers/BaseNumberRequest.java new file mode 100644 index 000000000..ca3adc5ab --- /dev/null +++ b/src/main/java/com/vonage/client/numbers/BaseNumberRequest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 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.numbers; + +import com.vonage.client.QueryParamsRequest; +import org.apache.http.client.methods.RequestBuilder; +import java.util.LinkedHashMap; +import java.util.Map; + +class BaseNumberRequest implements QueryParamsRequest { + private final String country, msisdn; + + public BaseNumberRequest(String country, String msisdn) { + this.country = country; + this.msisdn = msisdn; + } + + public String getCountry() { + return country; + } + + public String getMsisdn() { + return msisdn; + } + + @Deprecated + public void addParams(RequestBuilder request) { + makeParams().forEach(request::addParameter); + } + + @Override + public Map makeParams() { + LinkedHashMap params = new LinkedHashMap<>(4); + params.put("country", country); + params.put("msisdn", msisdn); + return params; + } +} diff --git a/src/main/java/com/vonage/client/numbers/BuyNumberEndpoint.java b/src/main/java/com/vonage/client/numbers/BuyNumberEndpoint.java deleted file mode 100644 index d1f117b74..000000000 --- a/src/main/java/com/vonage/client/numbers/BuyNumberEndpoint.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -class BuyNumberEndpoint extends AbstractMethod { - private static final String PATH = "/number/buy"; - private static final Class[] ALLOWED_AUTH_METHODS = {TokenAuthMethod.class}; - - BuyNumberEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(BuyNumberRequest request) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getRestBaseUri() + PATH; - RequestBuilder requestBuilder = RequestBuilder.post(uri) - .setHeader("Content-Type", "application/x-www-form-urlencoded"); - request.addParams(requestBuilder); - return requestBuilder; - } - - @Override - public Void parseResponse(HttpResponse response) throws IOException, VonageClientException { - if (response.getStatusLine().getStatusCode() != 200) { - throw new VonageBadRequestException(EntityUtils.toString(response.getEntity())); - } - return null; - } - - @Override - protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException { - return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); - } -} diff --git a/src/main/java/com/vonage/client/numbers/BuyNumberRequest.java b/src/main/java/com/vonage/client/numbers/BuyNumberRequest.java index 2ba753cc3..6b5fe9b35 100644 --- a/src/main/java/com/vonage/client/numbers/BuyNumberRequest.java +++ b/src/main/java/com/vonage/client/numbers/BuyNumberRequest.java @@ -15,25 +15,9 @@ */ package com.vonage.client.numbers; -import org.apache.http.client.methods.RequestBuilder; - -public class BuyNumberRequest { - private final String country, msisdn; +public class BuyNumberRequest extends BaseNumberRequest { public BuyNumberRequest(String country, String msisdn) { - this.country = country; - this.msisdn = msisdn; - } - - public String getCountry() { - return country; - } - - public String getMsisdn() { - return msisdn; - } - - public void addParams(RequestBuilder request) { - request.addParameter("country", country).addParameter("msisdn", msisdn); + super(country, msisdn); } } diff --git a/src/main/java/com/vonage/client/numbers/CancelNumberEndpoint.java b/src/main/java/com/vonage/client/numbers/CancelNumberEndpoint.java deleted file mode 100644 index 488921c4a..000000000 --- a/src/main/java/com/vonage/client/numbers/CancelNumberEndpoint.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -class CancelNumberEndpoint extends AbstractMethod { - private static final String PATH = "/number/cancel"; - private static final Class[] ALLOWED_AUTH_METHODS = {TokenAuthMethod.class}; - - CancelNumberEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(CancelNumberRequest request) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getRestBaseUri() + PATH; - RequestBuilder requestBuilder = RequestBuilder.post(uri) - .setHeader("Content-Type", "application/x-www-form-urlencoded"); - request.addParams(requestBuilder); - return requestBuilder; - } - - @Override - public Void parseResponse(HttpResponse response) throws IOException { - if (response.getStatusLine().getStatusCode() != 200) { - throw new VonageBadRequestException(EntityUtils.toString(response.getEntity())); - } - return null; - } - - @Override - protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException { - return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); - } -} diff --git a/src/main/java/com/vonage/client/numbers/CancelNumberRequest.java b/src/main/java/com/vonage/client/numbers/CancelNumberRequest.java index db7272ae1..b34b5b505 100644 --- a/src/main/java/com/vonage/client/numbers/CancelNumberRequest.java +++ b/src/main/java/com/vonage/client/numbers/CancelNumberRequest.java @@ -15,25 +15,9 @@ */ package com.vonage.client.numbers; -import org.apache.http.client.methods.RequestBuilder; - -public class CancelNumberRequest { - private final String country, msisdn; +public class CancelNumberRequest extends BaseNumberRequest { public CancelNumberRequest(String country, String msisdn) { - this.country = country; - this.msisdn = msisdn; - } - - public String getCountry() { - return country; - } - - public String getMsisdn() { - return msisdn; - } - - public void addParams(RequestBuilder request) { - request.addParameter("country", country).addParameter("msisdn", msisdn); + super(country, msisdn); } } diff --git a/src/main/java/com/vonage/client/numbers/ListNumbersEndpoint.java b/src/main/java/com/vonage/client/numbers/ListNumbersEndpoint.java deleted file mode 100644 index a8d69fb86..000000000 --- a/src/main/java/com/vonage/client/numbers/ListNumbersEndpoint.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -class ListNumbersEndpoint extends AbstractMethod { - private static final String PATH = "/account/numbers"; - private static final Class[] ALLOWED_AUTH_METHODS = {TokenAuthMethod.class}; - - ListNumbersEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(ListNumbersFilter request) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getRestBaseUri() + PATH; - RequestBuilder requestBuilder = RequestBuilder.get(uri) - .setHeader("Accept", "application/json"); - request.addParams(requestBuilder); - return requestBuilder; - } - - @Override - public ListNumbersResponse parseResponse(HttpResponse response) throws IOException { - return ListNumbersResponse.fromJson(basicResponseHandler.handleResponse(response)); - } - - @Override - protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException { - return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); - } -} diff --git a/src/main/java/com/vonage/client/numbers/ListNumbersFilter.java b/src/main/java/com/vonage/client/numbers/ListNumbersFilter.java index 066262dc0..9436f42fe 100644 --- a/src/main/java/com/vonage/client/numbers/ListNumbersFilter.java +++ b/src/main/java/com/vonage/client/numbers/ListNumbersFilter.java @@ -15,10 +15,12 @@ */ package com.vonage.client.numbers; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.vonage.client.QueryParamsRequest; import org.apache.http.client.methods.RequestBuilder; +import java.util.LinkedHashMap; +import java.util.Map; -public class ListNumbersFilter { +public class ListNumbersFilter implements QueryParamsRequest { private Integer index, size; private String pattern; private SearchPattern searchPattern; @@ -28,10 +30,10 @@ public ListNumbersFilter() { } public ListNumbersFilter( - @JsonProperty Integer index, - @JsonProperty Integer size, - @JsonProperty String pattern, - @JsonProperty SearchPattern searchPattern) { + Integer index, + Integer size, + String pattern, + SearchPattern searchPattern) { this.index = index; this.size = size; this.pattern = pattern; @@ -70,19 +72,26 @@ public void setSearchPattern(SearchPattern searchPattern) { this.searchPattern = searchPattern; } + @Deprecated public void addParams(RequestBuilder request) { + makeParams().forEach(request::addParameter); + } + + @Override + public Map makeParams() { + LinkedHashMap params = new LinkedHashMap<>(4); if (index != null) { - request.addParameter("index", index.toString()); + params.put("index", index.toString()); } if (size != null) { - request.addParameter("size", size.toString()); + params.put("size", size.toString()); } if (pattern != null) { - request.addParameter("pattern", pattern); + params.put("pattern", pattern); } if (searchPattern != null) { - request.addParameter("search_pattern", Integer.toString(searchPattern.getValue())); + params.put("search_pattern", Integer.toString(searchPattern.getValue())); } + return params; } - } diff --git a/src/main/java/com/vonage/client/numbers/ListNumbersResponse.java b/src/main/java/com/vonage/client/numbers/ListNumbersResponse.java index b5ae92b8e..ee6e3b5cf 100644 --- a/src/main/java/com/vonage/client/numbers/ListNumbersResponse.java +++ b/src/main/java/com/vonage/client/numbers/ListNumbersResponse.java @@ -16,15 +16,13 @@ package com.vonage.client.numbers; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.vonage.client.VonageUnexpectedException; -import java.io.IOException; +import com.vonage.client.Jsonable; /** * Response from a request to list the numbers currently being rented buy an account. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class ListNumbersResponse { +public class ListNumbersResponse implements Jsonable { private int count = 0; private OwnedNumber[] numbers = new OwnedNumber[0]; @@ -37,11 +35,8 @@ public OwnedNumber[] getNumbers() { } public static ListNumbersResponse fromJson(String json) { - try { - ObjectMapper mapper = new ObjectMapper(); - return mapper.readValue(json, ListNumbersResponse.class); - } catch (IOException jpe) { - throw new VonageUnexpectedException("Failed to produce json from ListNumbersResponse object.", jpe); - } + ListNumbersResponse response = new ListNumbersResponse(); + response.updateFromJson(json); + return response; } } diff --git a/src/main/java/com/vonage/client/numbers/NumbersClient.java b/src/main/java/com/vonage/client/numbers/NumbersClient.java index 70a83ede3..9360568f5 100644 --- a/src/main/java/com/vonage/client/numbers/NumbersClient.java +++ b/src/main/java/com/vonage/client/numbers/NumbersClient.java @@ -15,28 +15,40 @@ */ package com.vonage.client.numbers; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageClient; -import com.vonage.client.VonageClientException; -import com.vonage.client.VonageResponseParseException; +import com.vonage.client.*; +import com.vonage.client.auth.TokenAuthMethod; +import com.vonage.client.common.HttpMethod; /** * A client for accessing the Vonage API calls that manage phone numbers. The standard way to obtain an instance of * this class is to use {@link VonageClient#getNumbersClient()}. */ public class NumbersClient { - final ListNumbersEndpoint listNumbers; - final SearchNumbersEndpoint searchNumbers; - final CancelNumberEndpoint cancelNumber; - final BuyNumberEndpoint buyNumber; - final UpdateNumberEndpoint updateNumber; + final RestEndpoint listNumbers; + final RestEndpoint searchNumbers; + final RestEndpoint cancelNumber; + final RestEndpoint buyNumber; + final RestEndpoint updateNumber; - public NumbersClient(HttpWrapper httpWrapper) { - listNumbers = new ListNumbersEndpoint(httpWrapper); - searchNumbers = new SearchNumbersEndpoint(httpWrapper); - cancelNumber = new CancelNumberEndpoint(httpWrapper); - buyNumber = new BuyNumberEndpoint(httpWrapper); - updateNumber = new UpdateNumberEndpoint(httpWrapper); + public NumbersClient(HttpWrapper wrapper) { + @SuppressWarnings("unchecked") + final class Endpoint extends DynamicEndpoint { + Endpoint(final String path, HttpMethod method, R... type) { + super(DynamicEndpoint. builder((Class) type.getClass().getComponentType()) + .responseExceptionType(NumbersResponseException.class) + .wrapper(wrapper).requestMethod(method) + .authMethod(TokenAuthMethod.class).applyAsBasicAuth() + .contentTypeHeader(method == HttpMethod.POST ? "application/x-www-form-urlencoded" : null) + .pathGetter((de, req) -> de.getHttpWrapper().getHttpConfig().getRestBaseUri()+ "/" + path) + ); + } + } + + listNumbers = new Endpoint<>("account/numbers", HttpMethod.GET); + searchNumbers = new Endpoint<>("number/search", HttpMethod.GET); + cancelNumber = new Endpoint<>("number/cancel", HttpMethod.POST); + buyNumber = new Endpoint<>("number/buy", HttpMethod.POST); + updateNumber = new Endpoint<>("number/update", HttpMethod.POST); } /** diff --git a/src/main/java/com/vonage/client/numbers/NumbersResponseException.java b/src/main/java/com/vonage/client/numbers/NumbersResponseException.java new file mode 100644 index 000000000..097a387b6 --- /dev/null +++ b/src/main/java/com/vonage/client/numbers/NumbersResponseException.java @@ -0,0 +1,62 @@ +/* + * Copyright 2023 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.numbers; + +import com.fasterxml.jackson.annotation.*; +import com.vonage.client.VonageApiResponseException; + +/** + * Response returned when an error is encountered (i.e. the API returns a non-2xx status code). + * + * @since 7.8.0 + */ +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public final class NumbersResponseException extends VonageApiResponseException { + @JsonProperty("error-code") String errorCode; + @JsonProperty("error-code-label") String errorCodeLabel; + + @JsonSetter("error-code") + private void setErrorCode(String errorCode) { + if (errorCode != null && !errorCode.trim().isEmpty()) { + setStatusCode(Integer.parseInt(errorCode)); + } + } + + /** + * The status code description. + * + * @return The error code label, or {@code null} if unknown. + */ + public String getErrorCodeLabel() { + return errorCodeLabel; + } + + 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 NumbersResponseException fromJson(String json) { + return fromJson(NumbersResponseException.class, json); + } +} diff --git a/src/main/java/com/vonage/client/numbers/OwnedNumber.java b/src/main/java/com/vonage/client/numbers/OwnedNumber.java index 53a7a4c8f..391d99d5e 100644 --- a/src/main/java/com/vonage/client/numbers/OwnedNumber.java +++ b/src/main/java/com/vonage/client/numbers/OwnedNumber.java @@ -16,9 +16,10 @@ package com.vonage.client.numbers; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.vonage.client.Jsonable; @JsonIgnoreProperties(ignoreUnknown = true) -public class OwnedNumber { +public class OwnedNumber implements Jsonable { private String country, msisdn, moHttpUrl, type, voiceCallbackType, voiceCallbackValue; private String[] features; diff --git a/src/main/java/com/vonage/client/numbers/SearchNumbersEndpoint.java b/src/main/java/com/vonage/client/numbers/SearchNumbersEndpoint.java deleted file mode 100644 index 6bd3b834c..000000000 --- a/src/main/java/com/vonage/client/numbers/SearchNumbersEndpoint.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -class SearchNumbersEndpoint extends AbstractMethod { - private static final String PATH = "/number/search"; - private static final Class[] ALLOWED_AUTH_METHODS = {TokenAuthMethod.class}; - - SearchNumbersEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(SearchNumbersFilter request) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getRestBaseUri() + PATH; - RequestBuilder requestBuilder = RequestBuilder.get(uri) - .setHeader("Accept", "application/json"); - request.addParams(requestBuilder); - return requestBuilder; - } - - @Override - public SearchNumbersResponse parseResponse(HttpResponse response) throws IOException { - return SearchNumbersResponse.fromJson(basicResponseHandler.handleResponse(response)); - } - - @Override - protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException { - return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); - } -} diff --git a/src/main/java/com/vonage/client/numbers/SearchNumbersFilter.java b/src/main/java/com/vonage/client/numbers/SearchNumbersFilter.java index 1c0e130a7..ad62cbce2 100644 --- a/src/main/java/com/vonage/client/numbers/SearchNumbersFilter.java +++ b/src/main/java/com/vonage/client/numbers/SearchNumbersFilter.java @@ -15,12 +15,15 @@ */ package com.vonage.client.numbers; +import com.vonage.client.QueryParamsRequest; import org.apache.http.client.methods.RequestBuilder; +import java.util.LinkedHashMap; +import java.util.Map; /** * This class encapsulates a request to search for available Vonage Virtual Numbers. */ -public class SearchNumbersFilter { +public class SearchNumbersFilter implements QueryParamsRequest { private final String country; private String pattern; @@ -100,25 +103,33 @@ public void setSearchPattern(SearchPattern searchPattern) { this.searchPattern = searchPattern; } + @Deprecated public void addParams(RequestBuilder request) { - request.addParameter("country", country); + makeParams().forEach(request::addParameter); + } + + @Override + public Map makeParams() { + LinkedHashMap params = new LinkedHashMap<>(8); + params.put("country", country); if (features != null && features.length > 0) { - request.addParameter("features", String.join(",", features)); + params.put("features", String.join(",", features)); } if (index != null) { - request.addParameter("index", index.toString()); + params.put("index", index.toString()); } if (size != null) { - request.addParameter("size", size.toString()); + params.put("size", size.toString()); } if (pattern != null) { - request.addParameter("pattern", pattern); + params.put("pattern", pattern); } if (searchPattern != null) { - request.addParameter("search_pattern", Integer.toString(searchPattern.getValue())); + params.put("search_pattern", Integer.toString(searchPattern.getValue())); } if (type != null) { - request.addParameter("type", type.getType()); + params.put("type", type.getType()); } + return params; } } diff --git a/src/main/java/com/vonage/client/numbers/SearchNumbersResponse.java b/src/main/java/com/vonage/client/numbers/SearchNumbersResponse.java index 231355d54..5860875f7 100644 --- a/src/main/java/com/vonage/client/numbers/SearchNumbersResponse.java +++ b/src/main/java/com/vonage/client/numbers/SearchNumbersResponse.java @@ -16,15 +16,13 @@ package com.vonage.client.numbers; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.vonage.client.VonageUnexpectedException; -import java.io.IOException; +import com.vonage.client.Jsonable; /** * Represents the response to a "searchNumbers" request from the Vonage API. */ @JsonIgnoreProperties(ignoreUnknown = true) -public class SearchNumbersResponse { +public class SearchNumbersResponse implements Jsonable { private int count = 0; private AvailableNumber[] numbers = new AvailableNumber[]{}; @@ -44,12 +42,9 @@ public AvailableNumber[] getNumbers() { } public static SearchNumbersResponse fromJson(String json) { - try { - ObjectMapper mapper = new ObjectMapper(); - return mapper.readValue(json, SearchNumbersResponse.class); - } catch (IOException jpe) { - throw new VonageUnexpectedException("Failed to produce json from SearchNumbersResponse object.", jpe); - } + SearchNumbersResponse response = new SearchNumbersResponse(); + response.updateFromJson(json); + return response; } } diff --git a/src/main/java/com/vonage/client/numbers/UpdateNumberEndpoint.java b/src/main/java/com/vonage/client/numbers/UpdateNumberEndpoint.java deleted file mode 100644 index 6f50904c9..000000000 --- a/src/main/java/com/vonage/client/numbers/UpdateNumberEndpoint.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.AbstractMethod; -import com.vonage.client.HttpWrapper; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.VonageClientException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.util.EntityUtils; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - -class UpdateNumberEndpoint extends AbstractMethod { - private static final Class[] ALLOWED_AUTH_METHODS = {TokenAuthMethod.class}; - private static final String PATH = "/number/update"; - - UpdateNumberEndpoint(HttpWrapper httpWrapper) { - super(httpWrapper); - } - - @Override - protected Class[] getAcceptableAuthMethods() { - return ALLOWED_AUTH_METHODS; - } - - @Override - public RequestBuilder makeRequest(UpdateNumberRequest request) throws UnsupportedEncodingException { - String uri = httpWrapper.getHttpConfig().getRestBaseUri() + PATH; - RequestBuilder requestBuilder = RequestBuilder.post(uri) - .setHeader("Content-Type", "application/x-www-form-urlencoded") - .setHeader("Accept", "application/json"); - - request.addParams(requestBuilder); - return requestBuilder; - } - - @Override - public Void parseResponse(HttpResponse response) throws IOException, VonageClientException { - if (response.getStatusLine().getStatusCode() != 200) { - throw new VonageBadRequestException(EntityUtils.toString(response.getEntity())); - } - return null; - } - - @Override - protected RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException { - return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); - } -} diff --git a/src/main/java/com/vonage/client/numbers/UpdateNumberRequest.java b/src/main/java/com/vonage/client/numbers/UpdateNumberRequest.java index d2c82bc06..0bcc544fa 100644 --- a/src/main/java/com/vonage/client/numbers/UpdateNumberRequest.java +++ b/src/main/java/com/vonage/client/numbers/UpdateNumberRequest.java @@ -15,29 +15,14 @@ */ package com.vonage.client.numbers; -import org.apache.http.client.methods.RequestBuilder; +import java.util.Map; -public class UpdateNumberRequest { - private final String country; - private final String msisdn; - private String moHttpUrl; - private String moSmppSysType; +public class UpdateNumberRequest extends BaseNumberRequest { private CallbackType voiceCallbackType; - private String voiceCallbackValue; - private String voiceStatusCallback; - private String messagesCallbackValue; + private String moHttpUrl, moSmppSysType, voiceCallbackValue, voiceStatusCallback, messagesCallbackValue; public UpdateNumberRequest(String msisdn, String country) { - this.country = country; - this.msisdn = msisdn; - } - - public String getCountry() { - return country; - } - - public String getMsisdn() { - return msisdn; + super(country, msisdn); } public String getMoHttpUrl() { @@ -88,27 +73,29 @@ public void setMessagesCallbackValue(String messagesCallbackValue) { this.messagesCallbackValue = messagesCallbackValue; } - public void addParams(RequestBuilder request) { - request.addParameter("country", country).addParameter("msisdn", msisdn); + @Override + public Map makeParams() { + Map params = super.makeParams(); if (moHttpUrl != null) { - request.addParameter("moHttpUrl", moHttpUrl); + params.put("moHttpUrl", moHttpUrl); } if (moSmppSysType != null) { - request.addParameter("moSmppSysType", moSmppSysType); + params.put("moSmppSysType", moSmppSysType); } if (voiceCallbackType != null) { - request.addParameter("voiceCallbackType", voiceCallbackType.paramValue()); + params.put("voiceCallbackType", voiceCallbackType.paramValue()); } if (voiceCallbackValue != null) { - request.addParameter("voiceCallbackValue", voiceCallbackValue); + params.put("voiceCallbackValue", voiceCallbackValue); } if (voiceStatusCallback != null) { - request.addParameter("voiceStatusCallback", voiceStatusCallback); + params.put("voiceStatusCallback", voiceStatusCallback); } if (messagesCallbackValue != null) { - request.addParameter("messagesCallbackValue", messagesCallbackValue); - request.addParameter("messagesCallbackType", CallbackType.APP.paramValue()); + params.put("messagesCallbackValue", messagesCallbackValue); + params.put("messagesCallbackType", CallbackType.APP.paramValue()); } + return params; } public enum CallbackType { diff --git a/src/test/java/com/vonage/client/DynamicEndpointTestSpec.java b/src/test/java/com/vonage/client/DynamicEndpointTestSpec.java index 54cbfcef6..658a4c0e4 100644 --- a/src/test/java/com/vonage/client/DynamicEndpointTestSpec.java +++ b/src/test/java/com/vonage/client/DynamicEndpointTestSpec.java @@ -131,23 +131,17 @@ protected final void assertRequestUriAndBody(T request, String expectedRequestBo protected final void assertRequestUriAndBody(T request, String expectedRequestBody, Map expectedQueryParams) throws Exception { - String queryParams; - if (expectedQueryParams == null || expectedQueryParams.isEmpty()) { - queryParams = ""; - } - else { - String paramsStr = expectedQueryParams.entrySet().stream() - .map(entry -> entry.getKey() + "=" + entry.getValue()) - .collect(Collectors.joining("&")); - queryParams = "?" + paramsStr;//URLEncoder.encode(paramsStr, Charset.defaultCharset().name()); - } - RequestBuilder builder = makeTestRequest(request); if (expectedRequestBody != null) { assertEquals(expectedRequestBody, EntityUtils.toString(builder.getEntity())); } - String expectedUri = expectedDefaultBaseUri() + expectedEndpointUri(request) + queryParams; - assertEquals(expectedUri, builder.build().getURI().toString()); + if (expectedQueryParams != null) { + Map paramsMap = builder.getParameters().stream() + .collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue)); + assertEquals(expectedQueryParams, paramsMap); + } + String expectedUri = expectedDefaultBaseUri() + expectedEndpointUri(request); + assertEquals(expectedUri, builder.build().getURI().toString().split("\\?")[0]); AbstractMethod endpoint = endpointAsAbstractMethod(); HttpConfig originalConfig = endpoint.httpWrapper.getHttpConfig(); @@ -155,8 +149,8 @@ protected final void assertRequestUriAndBody(T request, String expectedRequestBo String baseUri = customBaseUri(); endpoint.httpWrapper.setHttpConfig(HttpConfig.builder().baseUri(baseUri).build()); builder = makeTestRequest(request); - expectedUri = baseUri + expectedEndpointUri(request) + queryParams; - assertEquals(expectedUri, builder.build().getURI().toString()); + expectedUri = baseUri + expectedEndpointUri(request); + assertEquals(expectedUri, builder.build().getURI().toString().split("\\?")[0]); } finally { endpoint.httpWrapper.setHttpConfig(originalConfig); diff --git a/src/test/java/com/vonage/client/conversion/ConversionClientTest.java b/src/test/java/com/vonage/client/conversion/ConversionClientTest.java index e96a9fa91..5de9dfb35 100644 --- a/src/test/java/com/vonage/client/conversion/ConversionClientTest.java +++ b/src/test/java/com/vonage/client/conversion/ConversionClientTest.java @@ -15,67 +15,155 @@ */ package com.vonage.client.conversion; -import com.vonage.client.ClientTest; -import com.vonage.client.VonageBadRequestException; -import org.junit.Before; +import com.vonage.client.*; +import com.vonage.client.auth.AuthMethod; +import com.vonage.client.auth.SignatureAuthMethod; +import com.vonage.client.auth.TokenAuthMethod; +import com.vonage.client.common.HttpMethod; +import static org.junit.Assert.assertNotNull; import org.junit.Test; +import java.text.ParseException; import java.text.SimpleDateFormat; -import static org.junit.Assert.fail; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; public class ConversionClientTest extends ClientTest { - @Before - public void setUp() { + + public ConversionClientTest() { client = new ConversionClient(wrapper); } @Test - public void testSuccessfulResponse() { - try { - wrapper.setHttpClient(stubHttpClient(200, "")); - client.submitConversion(ConversionRequest.Type.VOICE, - "MESSAGE-ID", - true, - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); - } catch (Exception e) { - fail("No exceptions should be thrown."); - } + public void testSuccessfulResponse() throws Exception { + stubResponse(200); + client.submitConversion(ConversionRequest.Type.VOICE, + "MESSAGE-ID", + true, + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); } - @Test(expected = VonageBadRequestException.class) + @Test(expected = VonageApiResponseException.class) public void testWrongCredentials() throws Exception { - wrapper.setHttpClient(stubHttpClient(401, "")); + stubResponse(401); client.submitConversion(ConversionRequest.Type.SMS, "MESSAGE-ID", true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); } - @Test(expected = VonageBadRequestException.class) + @Test(expected = VonageApiResponseException.class) public void testConversionNotEnabled() throws Exception { - wrapper.setHttpClient(stubHttpClient(402, "")); + stubResponse(402); client.submitConversion(ConversionRequest.Type.SMS, "MESSAGE-ID", true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); } - @Test(expected = VonageBadRequestException.class) + @Test(expected = VonageApiResponseException.class) public void testInvalidParameters() throws Exception { - wrapper.setHttpClient(stubHttpClient(423, "")); + stubResponse(423); client.submitConversion(ConversionRequest.Type.SMS, "MESSAGE-ID", true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); } - @Test(expected = VonageBadRequestException.class) + @Test(expected = VonageApiResponseException.class) public void testInvalidParametersEnhanceCalm() throws Exception { - wrapper.setHttpClient(stubHttpClient(420, "")); + stubResponse(420); client.submitConversion(ConversionRequest.Type.SMS, "MESSAGE-ID", true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12")); } + @Test + public void testConversionEndpoint() throws Exception { + new DynamicEndpointTestSpec() { + + @Override + protected RestEndpoint endpoint() { + return client.conversionEndpoint; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.POST; + } + + @Override + protected Collection> expectedAuthMethods() { + return Arrays.asList(SignatureAuthMethod.class, TokenAuthMethod.class); + } + + @Override + protected Class expectedResponseExceptionType() { + return VonageApiResponseException.class; + } + + @Override + protected String expectedDefaultBaseUri() { + return "https://api.nexmo.com"; + } + @Override + protected String expectedEndpointUri(ConversionRequest request) { + return "/conversions/" + request.getType().name().toLowerCase(); + } + + @Override + protected ConversionRequest sampleRequest() { + try { + return new ConversionRequest(ConversionRequest.Type.SMS, + "MESSAGE-ID", + true, + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12") + ); + } + catch (ParseException px) { + throw new IllegalStateException(px); + } + } + + @Override + protected String sampleRequestBodyString() { + return null; + } + + @Override + protected Map sampleQueryParams() { + LinkedHashMap params = new LinkedHashMap<>(4); + params.put("message-id", "MESSAGE-ID"); + params.put("delivered", "true"); + params.put("timestamp", "2014-03-04 10:11:12"); + return params; + } + + @Override + public void runTests() throws Exception { + super.runTests(); + testVoice(); + } + + void testVoice() throws Exception { + String timestamp = "2023-08-28 10:48:12"; + ConversionRequest request = new ConversionRequest(ConversionRequest.Type.VOICE, + "messAG3-1D", + false, + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestamp) + ); + + assertNotNull(request.getTimestamp()); + LinkedHashMap expectedParams = new LinkedHashMap<>(4); + expectedParams.put("message-id", request.getMessageId()); + expectedParams.put("delivered", String.valueOf(request.isDelivered())); + expectedParams.put("timestamp", timestamp); + assertRequestUriAndBody(request, null, expectedParams); + } + } + .runTests(); + } } diff --git a/src/test/java/com/vonage/client/conversion/ConversionEndpointTest.java b/src/test/java/com/vonage/client/conversion/ConversionEndpointTest.java deleted file mode 100644 index e14ce7687..000000000 --- a/src/test/java/com/vonage/client/conversion/ConversionEndpointTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2023 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.conversion; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import org.apache.http.NameValuePair; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.message.BasicNameValuePair; -import org.junit.Before; -import org.junit.Test; -import java.text.SimpleDateFormat; -import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class ConversionEndpointTest { - private ConversionEndpoint method; - - @Before - public void setUp() throws Exception { - method = new ConversionEndpoint(new HttpWrapper()); - } - - @Test - public void testConstructParametersWithSms() throws Exception { - ConversionRequest request = new ConversionRequest(ConversionRequest.Type.SMS, - "MESSAGE-ID", - true, - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12") - ); - RequestBuilder requestBuilder = method.makeRequest(request); - List params = requestBuilder.getParameters(); - - assertContainsParam(params, "message-id", "MESSAGE-ID"); - assertContainsParam(params, "delivered", "true"); - assertContainsParam(params, "timestamp", "2014-03-04 10:11:12"); - assertEquals(method.getBaseUri() + ConversionRequest.Type.SMS.name().toLowerCase(), - requestBuilder.getUri().toString() - ); - } - - @Test - public void testConstructParametersWithVoice() throws Exception { - ConversionRequest request = new ConversionRequest(ConversionRequest.Type.VOICE, - "MESSAGE-ID", - true, - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12") - ); - RequestBuilder requestBuilder = method.makeRequest(request); - List params = requestBuilder.getParameters(); - - assertContainsParam(params, "message-id", "MESSAGE-ID"); - assertContainsParam(params, "delivered", "true"); - assertContainsParam(params, "timestamp", "2014-03-04 10:11:12"); - assertEquals(method.getBaseUri() + ConversionRequest.Type.VOICE.name().toLowerCase(), - requestBuilder.getUri().toString() - ); - } - - private void assertContainsParam(List params, String key, String value) { - NameValuePair item = new BasicNameValuePair(key, value); - assertTrue("" + params + " should contain " + item, params.contains(item)); - } - - @Test - public void testDefaultUri() throws Exception { - ConversionRequest request = new ConversionRequest(ConversionRequest.Type.VOICE, - "MESSAGE-ID", - true, - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12") - ); - - RequestBuilder builder = method.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://api.nexmo.com/conversions/voice", - builder.build().getURI().toString() - ); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - ConversionEndpoint method = new ConversionEndpoint(wrapper); - ConversionRequest request = new ConversionRequest(ConversionRequest.Type.VOICE, - "MESSAGE-ID", - true, - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-03-04 10:11:12") - ); - - RequestBuilder builder = method.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://example.com/conversions/voice", builder.build().getURI().toString()); - } -} diff --git a/src/test/java/com/vonage/client/numbers/BuyNumberEndpointTest.java b/src/test/java/com/vonage/client/numbers/BuyNumberEndpointTest.java deleted file mode 100644 index 23b688c6c..000000000 --- a/src/test/java/com/vonage/client/numbers/BuyNumberEndpointTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.ProtocolVersion; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.message.BasicHttpResponse; -import org.apache.http.message.BasicStatusLine; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import org.junit.Before; -import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; - -public class BuyNumberEndpointTest { - private BuyNumberEndpoint endpoint; - - @Before - public void setUp() throws Exception { - endpoint = new BuyNumberEndpoint(new HttpWrapper( - new TokenAuthMethod("abc123", "a2cd5f789") - )); - } - - @Test - public void makeRequest() throws Exception { - RequestBuilder request = endpoint.makeRequest(new BuyNumberRequest("AA", "447700900000")); - - assertEquals("POST", request.getMethod()); - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("AA", params.get("country")); - assertEquals("447700900000", params.get("msisdn")); - } - - @Test - public void applyAuth() throws Exception { - RequestBuilder builder = endpoint.applyAuth(endpoint.makeRequest( - new BuyNumberRequest("UK", "447700900001") - )); - List params = builder.getParameters(); - assertEquals(2, params.size()); - } - - @Test - public void parseResponse() throws Exception { - HttpResponse stubResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), - 200, - "OK" - )); - - String json = "{\n" + " \"error-code\":\"200\",\n" + " \"error-code-label\":\"success\"\n" + "}"; - InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); - BasicHttpEntity entity = new BasicHttpEntity(); - entity.setContent(jsonStream); - stubResponse.setEntity(entity); - - endpoint.parseResponse(stubResponse); - } - - @Test - public void parseBadRequestResponse() throws Exception { - HttpResponse stubResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), - 400, - "OK" - )); - - String json = "{\n" + " \"error_title\": \"Bad Request\",\n" + " \"invalid_parameters\": {\n" - + " \"country\": \"Is required.\"\n" + " },\n" + " \"type\": \"BAD_REQUEST\"\n" + "}"; - InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); - BasicHttpEntity entity = new BasicHttpEntity(); - entity.setContent(jsonStream); - stubResponse.setEntity(entity); - - try { - endpoint.parseResponse(stubResponse); - fail("A 400 response should raise a VonageBadRequestException"); - } catch (VonageBadRequestException e) { - // This is expected - } - } - - @Test - public void testDefaultUri() throws Exception { - BuyNumberRequest request = new BuyNumberRequest("AA", "447700900000"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://rest.nexmo.com/number/buy", - builder.build().getURI().toString() - ); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - BuyNumberEndpoint endpoint = new BuyNumberEndpoint(wrapper); - BuyNumberRequest request = new BuyNumberRequest("AA", "447700900000"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://example.com/number/buy", builder.build().getURI().toString()); - } -} diff --git a/src/test/java/com/vonage/client/numbers/BuyNumberRequestTest.java b/src/test/java/com/vonage/client/numbers/BuyNumberRequestTest.java deleted file mode 100644 index de62724a4..000000000 --- a/src/test/java/com/vonage/client/numbers/BuyNumberRequestTest.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class BuyNumberRequestTest { - @Test - public void testFilterValues() throws Exception { - BuyNumberRequest request = new BuyNumberRequest("YY", "447700900000"); - assertEquals("YY", request.getCountry()); - assertEquals("447700900000", request.getMsisdn()); - } -} diff --git a/src/test/java/com/vonage/client/numbers/CancelNumberEndpointTest.java b/src/test/java/com/vonage/client/numbers/CancelNumberEndpointTest.java deleted file mode 100644 index 0d0a62864..000000000 --- a/src/test/java/com/vonage/client/numbers/CancelNumberEndpointTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import com.vonage.client.VonageBadRequestException; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.NameValuePair; -import org.apache.http.client.methods.RequestBuilder; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import org.junit.Before; -import org.junit.Test; -import java.util.List; -import java.util.Map; - -public class CancelNumberEndpointTest { - CancelNumberEndpoint endpoint; - - @Before - public void setUp() throws Exception { - endpoint = new CancelNumberEndpoint(new HttpWrapper( - new TokenAuthMethod("abc123", "a2cd5f789") - )); - } - - @Test - public void makeRequest() throws Exception { - RequestBuilder request = endpoint.makeRequest(new CancelNumberRequest("AA", "447700900000")); - - assertEquals("POST", request.getMethod()); - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("AA", params.get("country")); - assertEquals("447700900000", params.get("msisdn")); - } - - @Test - public void applyAuth() throws Exception { - RequestBuilder builder = endpoint.applyAuth(endpoint.makeRequest( - new CancelNumberRequest("GB", "447700900001") - )); - List params = builder.getParameters(); - assertEquals(2, params.size()); - } - - @Test - public void parseResponse() throws Exception { - String json = "{\n" + " \"error-code\":\"200\",\n" + " \"error-code-label\":\"success\"\n" + "}"; - - endpoint.parseResponse(TestUtils.makeJsonHttpResponse(200, json)); - } - - @Test - public void parseBadRequestResponse() throws Exception { - String json = "{\n" + " \"error_title\": \"Bad Request\",\n" + " \"invalid_parameters\": {\n" - + " \"country\": \"Is required.\"\n" + " },\n" + " \"type\": \"BAD_REQUEST\"\n" + "}"; - try { - endpoint.parseResponse(TestUtils.makeJsonHttpResponse(400, json)); - fail("A 400 response should raise a VonageBadRequestException"); - } catch (VonageBadRequestException e) { - // This is expected - } - } - - @Test - public void parseMethodFailedResponse() throws Exception { - String json = "{\n" + " \"error_title\": \"Bad Request\",\n" + " \"invalid_parameters\": {\n" - + " \"country\": \"Is required.\"\n" + " },\n" + " \"type\": \"BAD_REQUEST\"\n" + "}"; - try { - endpoint.parseResponse(TestUtils.makeJsonHttpResponse(500, json)); - fail("A 500 response should raise a VonageBadRequestException"); - } catch (VonageBadRequestException e) { - // This is expected - } - } - - @Test - public void testDefaultUri() throws Exception { - CancelNumberRequest request = new CancelNumberRequest("AA", "447700900000"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://rest.nexmo.com/number/cancel", builder.build().getURI().toString()); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - CancelNumberEndpoint endpoint = new CancelNumberEndpoint(wrapper); - CancelNumberRequest request = new CancelNumberRequest("AA", "447700900000"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://example.com/number/cancel", builder.build().getURI().toString()); - } -} diff --git a/src/test/java/com/vonage/client/numbers/CancelNumberRequestTest.java b/src/test/java/com/vonage/client/numbers/CancelNumberRequestTest.java deleted file mode 100644 index d596fb956..000000000 --- a/src/test/java/com/vonage/client/numbers/CancelNumberRequestTest.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class CancelNumberRequestTest { - @Test - public void testFilterValues() throws Exception { - CancelNumberRequest request = new CancelNumberRequest("YY", "447700900000"); - assertEquals("YY", request.getCountry()); - assertEquals("447700900000", request.getMsisdn()); - } -} diff --git a/src/test/java/com/vonage/client/numbers/ListNumbersEndpointTest.java b/src/test/java/com/vonage/client/numbers/ListNumbersEndpointTest.java deleted file mode 100644 index 0ef55354d..000000000 --- a/src/test/java/com/vonage/client/numbers/ListNumbersEndpointTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import static com.vonage.client.TestUtils.test429; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.ProtocolVersion; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.entity.ContentType; -import org.apache.http.message.BasicHttpResponse; -import org.apache.http.message.BasicStatusLine; -import static org.junit.Assert.assertEquals; -import org.junit.Before; -import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; - - -public class ListNumbersEndpointTest { - private ListNumbersEndpoint endpoint; - - @Before - public void setUp() throws Exception { - endpoint = new ListNumbersEndpoint(new HttpWrapper( - new TokenAuthMethod("abc123", "a2cd5f789") - )); - } - - @Test - public void makeRequest() throws Exception { - ListNumbersFilter filter = new ListNumbersFilter(); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - RequestBuilder request = endpoint.makeRequest(filter); - - assertEquals("GET", request.getMethod()); - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("234", params.get("pattern")); - assertEquals("0", params.get("search_pattern")); - assertEquals("10", params.get("index")); - assertEquals("20", params.get("size")); - assertEquals(ContentType.APPLICATION_JSON.getMimeType(), request.getFirstHeader("Accept").getValue()); - } - - @Test - public void applyAuth() throws Exception { - RequestBuilder builder = endpoint.applyAuth(endpoint.makeRequest(new ListNumbersFilter())); - List params = builder.getParameters(); - assertEquals(0, params.size()); - } - - @Test - public void parseResponse() throws Exception { - HttpResponse stubResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), - 200, - "OK" - )); - - String json = "{\n" + " \"count\": 1,\n" + " \"numbers\": [\n" + " {\n" + " \"country\": \"GB\",\n" - + " \"msisdn\": \"447700900000\",\n" + " \"moHttpUrl\": \"https://example.com/mo\",\n" - + " \"type\": \"mobile-lvn\",\n" + " \"features\": [\n" + " \"VOICE\",\n" - + " \"SMS\"\n" + " ],\n" + " \"voiceCallbackType\": \"app\",\n" - + " \"voiceCallbackValue\": \"aaaaaaaa-bbbb-cccc-dddd-0123456789ab\"\n" + " }\n" + " ]\n" - + "}"; - InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); - BasicHttpEntity entity = new BasicHttpEntity(); - entity.setContent(jsonStream); - stubResponse.setEntity(entity); - - ListNumbersResponse response = endpoint.parseResponse(stubResponse); - assertEquals(1, response.getCount()); - } - - @Test - public void testRequestThrottleResponse() throws Exception { - test429(new ListNumbersEndpoint(null)); - } - - @Test - public void testDefaultUri() throws Exception { - ListNumbersFilter filter = new ListNumbersFilter(); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - - RequestBuilder builder = endpoint.makeRequest(filter); - assertEquals("GET", builder.getMethod()); - assertEquals("https://rest.nexmo.com/account/numbers?index=10&size=20&pattern=234&search_pattern=0", - builder.build().getURI().toString() - ); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - ListNumbersEndpoint endpoint = new ListNumbersEndpoint(wrapper); - ListNumbersFilter filter = new ListNumbersFilter(); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - - RequestBuilder builder = endpoint.makeRequest(filter); - assertEquals("GET", builder.getMethod()); - assertEquals( - "https://example.com/account/numbers?index=10&size=20&pattern=234&search_pattern=0", - builder.build().getURI().toString() - ); - } -} diff --git a/src/test/java/com/vonage/client/numbers/NumbersClientTest.java b/src/test/java/com/vonage/client/numbers/NumbersClientTest.java index 20352a115..841bbd589 100644 --- a/src/test/java/com/vonage/client/numbers/NumbersClientTest.java +++ b/src/test/java/com/vonage/client/numbers/NumbersClientTest.java @@ -15,58 +15,36 @@ */ package com.vonage.client.numbers; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import com.vonage.client.auth.AuthCollection; -import com.vonage.client.auth.TokenAuthMethod; -import com.vonage.client.logging.LoggingUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpUriRequest; -import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.nio.charset.StandardCharsets; +import com.vonage.client.ClientTest; +import com.vonage.client.RestEndpoint; +import com.vonage.client.common.HttpMethod; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class NumbersClientTest { - private final TestUtils testUtils = new TestUtils(); - - private HttpWrapper stubHttpWrapper(int statusCode, String content) throws Exception { - HttpClient client = mock(HttpClient.class); - - HttpResponse response = mock(HttpResponse.class); - StatusLine sl = mock(StatusLine.class); - HttpEntity entity = mock(HttpEntity.class); - - when(client.execute(any(HttpUriRequest.class))).thenReturn(response); - when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged"); - when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))); - when(sl.getStatusCode()).thenReturn(statusCode); - when(response.getStatusLine()).thenReturn(sl); - when(response.getEntity()).thenReturn(entity); - - byte[] keyBytes = testUtils.loadKey("test/keys/application_key"); - AuthCollection authCollection = new AuthCollection(); - authCollection.add(new TokenAuthMethod( - "dummyKey", - "dummySecret" - )); - - HttpWrapper wrapper = new HttpWrapper(authCollection); - wrapper.setHttpClient(client); - - return wrapper; +import org.junit.Test; +import org.junit.function.ThrowingRunnable; +import java.util.LinkedHashMap; +import java.util.Map; + +public class NumbersClientTest extends ClientTest { + + public NumbersClientTest() { + client = new NumbersClient(wrapper); + } + + void assert401ResponseException(ThrowingRunnable invocation) throws Exception { + String response = "{\n" + + " \"title\": \"Test reason\",\n" + + " \"error-code\": \"401\",\n" + + " \"error-code-label\": \"authentication failed\"\n" + + "}"; + NumbersResponseException ex = assertApiResponseException( + 401, response, NumbersResponseException.class, invocation + ); + assertEquals("authentication failed", ex.getErrorCodeLabel()); } @Test public void testListNumbers() throws Exception { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + + stubResponse(200, "{\n" + " \"count\": 1,\n" + " \"numbers\": [\n" + " {\n" + @@ -82,14 +60,15 @@ public void testListNumbers() throws Exception { " \"voiceCallbackValue\": \"aaaaaaaa-bbbb-cccc-dddd-0123456789ab\"\n" + " }\n" + " ]\n" + - "}")); + "}"); ListNumbersResponse response = client.listNumbers(); assertEquals(1, response.getCount()); + assert401ResponseException(client::listNumbers); } @Test public void testListNumberWithParams() throws Exception { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + + stubResponse(200, "{\n" + " \"count\": 1,\n" + " \"numbers\": [\n" + " {\n" + @@ -105,7 +84,7 @@ public void testListNumberWithParams() throws Exception { " \"voiceCallbackValue\": \"aaaaaaaa-bbbb-cccc-dddd-0123456789ab\"\n" + " }\n" + " ]\n" + - "}")); + "}"); ListNumbersFilter filter = new ListNumbersFilter(); filter.setIndex(10); @@ -114,11 +93,12 @@ public void testListNumberWithParams() throws Exception { filter.setSearchPattern(SearchPattern.ENDS_WITH); ListNumbersResponse response = client.listNumbers(filter); assertEquals(1, response.getCount()); + assert401ResponseException(() -> client.listNumbers(filter)); } @Test public void testSearchNumbers() throws Exception { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + + stubResponse(200, "{\n" + " \"count\": 4,\n" + " \"numbers\": [\n" + " {\n" + @@ -132,58 +112,278 @@ public void testSearchNumbers() throws Exception { " ]\n" + " }\n" + " ]\n" + - "}")); + "}"); SearchNumbersResponse response = client.searchNumbers("YY"); assertEquals(4, response.getCount()); - + assert401ResponseException(() -> client.searchNumbers(new SearchNumbersFilter("FR"))); } @Test public void testCancelNumber() throws Exception { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + + stubResponse(200, "{\n" + " \"error-code\":\"200\",\n" + " \"error-code-label\":\"success\"\n" + - "}")); - + "}"); client.cancelNumber("AA", "447700900000"); + assert401ResponseException(() -> client.cancelNumber("UK", "447700900000")); } @Test public void testBuyNumber() throws Exception { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + + stubResponse(200, "{\n" + " \"error-code\":\"200\",\n" + " \"error-code-label\":\"success\"\n" + - "}")); - + "}"); client.buyNumber("AA", "447700900000"); + assert401ResponseException(() -> client.buyNumber("UK", "447700900000")); } @Test public void testUpdateNumber() throws Exception { - try { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + - " \"error-code\":\"200\",\n" + - " \"error-code-label\":\"success\"\n" + - "}")); - client.updateNumber(new UpdateNumberRequest( - "447700900328", "UK" - )); - } catch (Exception e) { - fail("Parsing a valid response should not raise an exception"); - } + stubResponse(200, "{\n" + + " \"error-code\":\"200\",\n" + + " \"error-code-label\":\"success\"\n" + + "}"); + UpdateNumberRequest request = new UpdateNumberRequest("447700900328", "UK"); + client.updateNumber(request); + assert401ResponseException(() -> client.updateNumber(request)); } @Test public void testLinkNumber() throws Exception { - try { - NumbersClient client = new NumbersClient(stubHttpWrapper(200, "{\n" + - " \"error-code\":\"200\",\n" + - " \"error-code-label\":\"success\"\n" + - "}")); - client.linkNumber("447700900328", "UK", "my-app-id"); - } catch (Exception e) { - fail("Parsing a valid response should not raise an exception"); + stubResponse(200, "{\n" + + " \"error-code\":\"200\",\n" + + " \"error-code-label\":\"success\"\n" + + "}"); + client.linkNumber("447700900328", "UK", "my-app-id"); + } + + // ENDPOINTS + + @Test + public void testListNumbersEndpoint() throws Exception { + new NumbersEndpointTestSpec() { + + @Override + protected RestEndpoint endpoint() { + return client.listNumbers; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.GET; + } + + @Override + protected String expectedEndpointUri(ListNumbersFilter request) { + return "/account/numbers"; + } + + @Override + protected ListNumbersFilter sampleRequest() { + ListNumbersFilter filter = new ListNumbersFilter(); + filter.setIndex(10); + filter.setSize(20); + filter.setPattern("234"); + filter.setSearchPattern(SearchPattern.STARTS_WITH); + return filter; + } + + @Override + protected Map sampleQueryParams() { + ListNumbersFilter request = sampleRequest(); + Map params = new LinkedHashMap<>(8); + params.put("pattern", request.getPattern()); + params.put("search_pattern", String.valueOf(request.getSearchPattern().getValue())); + params.put("index", String.valueOf(request.getIndex())); + params.put("size", String.valueOf(request.getSize())); + return params; + } } + .runTests(); + } + @Test + public void testSearchNumbersEndpoint() throws Exception { + new NumbersEndpointTestSpec() { + + @Override + protected RestEndpoint endpoint() { + return client.searchNumbers; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.GET; + } + + @Override + protected String expectedEndpointUri(SearchNumbersFilter request) { + return "/number/search"; + } + + @Override + protected SearchNumbersFilter sampleRequest() { + SearchNumbersFilter filter = new SearchNumbersFilter("BB"); + filter.setIndex(11); + filter.setSize(25); + filter.setPattern("234"); + filter.setFeatures(new String[]{"SMS", "VOICE"}); + filter.setSearchPattern(SearchPattern.STARTS_WITH); + filter.setType(Type.LANDLINE_TOLL_FREE); + return filter; + } + + @Override + protected Map sampleQueryParams() { + SearchNumbersFilter filter = sampleRequest(); + Map params = new LinkedHashMap<>(8); + params.put("country", filter.getCountry()); + params.put("features", String.join(",", filter.getFeatures())); + params.put("pattern", filter.getPattern()); + params.put("search_pattern", String.valueOf(filter.getSearchPattern().getValue())); + params.put("index", String.valueOf(filter.getIndex())); + params.put("size", String.valueOf(filter.getSize())); + params.put("type", filter.getType().getType()); + return params; + } + } + .runTests(); + } + + @Test + public void testBuyNumberEndpoint() throws Exception { + new NumbersEndpointTestSpec() { + + @Override + protected String expectedContentTypeHeader(BuyNumberRequest request) { + return "application/x-www-form-urlencoded"; + } + + @Override + protected RestEndpoint endpoint() { + return client.buyNumber; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.POST; + } + + @Override + protected String expectedEndpointUri(BuyNumberRequest request) { + return "/number/buy"; + } + + @Override + protected BuyNumberRequest sampleRequest() { + return new BuyNumberRequest("DE", "4930901820"); + } + + @Override + protected Map sampleQueryParams() { + BuyNumberRequest request = sampleRequest(); + Map params = new LinkedHashMap<>(4); + params.put("country", request.getCountry()); + params.put("msisdn", request.getMsisdn()); + return params; + } + } + .runTests(); + } + + @Test + public void testCancelNumberEndpoint() throws Exception { + new NumbersEndpointTestSpec() { + + @Override + protected String expectedContentTypeHeader(CancelNumberRequest request) { + return "application/x-www-form-urlencoded"; + } + + @Override + protected RestEndpoint endpoint() { + return client.cancelNumber; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.POST; + } + + @Override + protected String expectedEndpointUri(CancelNumberRequest request) { + return "/number/cancel"; + } + + @Override + protected CancelNumberRequest sampleRequest() { + return new CancelNumberRequest("DE", "4930901820"); + } + + @Override + protected Map sampleQueryParams() { + Map params = new LinkedHashMap<>(4); + params.put("country", "DE"); + params.put("msisdn", "4930901820"); + return params; + } + } + .runTests(); + } + + @Test + public void testUpdateNumberEndpoint() throws Exception { + new NumbersEndpointTestSpec() { + + @Override + protected String expectedContentTypeHeader(UpdateNumberRequest request) { + return "application/x-www-form-urlencoded"; + } + + @Override + protected RestEndpoint endpoint() { + return client.updateNumber; + } + + @Override + protected HttpMethod expectedHttpMethod() { + return HttpMethod.POST; + } + + @Override + protected String expectedEndpointUri(UpdateNumberRequest request) { + return "/number/update"; + } + + @Override + protected UpdateNumberRequest sampleRequest() { + UpdateNumberRequest request = new UpdateNumberRequest("447700900013", "UK"); + request.setMoHttpUrl("https://api.example.com/mo"); + request.setMoSmppSysType("inbound"); + request.setVoiceCallbackValue("1234-5678-9123-4567"); + request.setVoiceCallbackType(UpdateNumberRequest.CallbackType.APP); + request.setVoiceStatusCallback("https://api.example.com/callback"); + request.setMessagesCallbackValue("MESSAGES-APPLICATION-ID"); + return request; + } + + @Override + protected Map sampleQueryParams() { + UpdateNumberRequest request = sampleRequest(); + Map params = new LinkedHashMap<>(); + params.put("country", request.getCountry()); + params.put("msisdn", request.getMsisdn()); + params.put("moHttpUrl", request.getMoHttpUrl()); + params.put("moSmppSysType", request.getMoSmppSysType()); + params.put("voiceCallbackValue", request.getVoiceCallbackValue()); + params.put("voiceCallbackType", request.getVoiceCallbackType().paramValue()); + params.put("voiceStatusCallback", request.getVoiceStatusCallback()); + params.put("messagesCallbackValue", request.getMessagesCallbackValue()); + params.put("messagesCallbackType", "app"); + return params; + } + } + .runTests(); } } diff --git a/src/test/java/com/vonage/client/numbers/NumbersEndpointTestSpec.java b/src/test/java/com/vonage/client/numbers/NumbersEndpointTestSpec.java new file mode 100644 index 000000000..3194ef1af --- /dev/null +++ b/src/test/java/com/vonage/client/numbers/NumbersEndpointTestSpec.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 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.numbers; + +import com.vonage.client.DynamicEndpointTestSpec; +import com.vonage.client.VonageApiResponseException; +import com.vonage.client.auth.AuthMethod; +import com.vonage.client.auth.TokenAuthMethod; +import java.util.Arrays; +import java.util.Collection; + +abstract class NumbersEndpointTestSpec extends DynamicEndpointTestSpec { + + @Override + protected Collection> expectedAuthMethods() { + return Arrays.asList(TokenAuthMethod.class); + } + + @Override + protected Class expectedResponseExceptionType() { + return NumbersResponseException.class; + } + + @Override + protected String expectedDefaultBaseUri() { + return "https://rest.nexmo.com"; + } + + @Override + protected String sampleRequestBodyString() { + return null; + } +} diff --git a/src/test/java/com/vonage/client/numbers/SearchNumbersEndpointTest.java b/src/test/java/com/vonage/client/numbers/SearchNumbersEndpointTest.java deleted file mode 100644 index 3258955db..000000000 --- a/src/test/java/com/vonage/client/numbers/SearchNumbersEndpointTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import static com.vonage.client.TestUtils.test429; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.ProtocolVersion; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.entity.ContentType; -import org.apache.http.message.BasicHttpResponse; -import org.apache.http.message.BasicStatusLine; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import org.junit.Before; -import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; - - -public class SearchNumbersEndpointTest { - private SearchNumbersEndpoint endpoint; - - @Before - public void setUp() throws Exception { - endpoint = new SearchNumbersEndpoint(new HttpWrapper( - new TokenAuthMethod("abc123", "a2cd5f789") - )); - } - - @Test - public void makeRequest() throws Exception { - SearchNumbersFilter filter = new SearchNumbersFilter("BB"); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setFeatures(new String[]{"SMS", "VOICE"}); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - filter.setType(Type.LANDLINE_TOLL_FREE); - RequestBuilder request = endpoint.makeRequest(filter); - - assertEquals("GET", request.getMethod()); - assertEquals(ContentType.APPLICATION_JSON.getMimeType(), request.getFirstHeader("Accept").getValue()); - - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("BB", params.get("country")); - assertEquals("SMS,VOICE", params.get("features")); - assertEquals("234", params.get("pattern")); - assertEquals("0", params.get("search_pattern")); - assertEquals("10", params.get("index")); - assertEquals("20", params.get("size")); - assertEquals("landline-toll-free", params.get("type")); - } - - @Test - public void applyAuth() throws Exception { - RequestBuilder builder = endpoint.applyAuth(endpoint.makeRequest(new SearchNumbersFilter("GB"))); - List params = builder.getParameters(); - assertEquals(1, params.size()); - } - - @Test - public void testNullFeatureParam() throws Exception { - SearchNumbersFilter filter = new SearchNumbersFilter("BB"); - RequestBuilder request = endpoint.makeRequest(filter); - - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("BB", params.get("country")); - assertNull(params.get("features")); - } - - @Test - public void testEmptyFeature() throws Exception { - SearchNumbersFilter filter = new SearchNumbersFilter("BB"); - filter.setFeatures(new String[]{}); - RequestBuilder request = endpoint.makeRequest(filter); - - Map params = TestUtils.makeParameterMap(request.getParameters()); - assertEquals("BB", params.get("country")); - assertNull(params.get("features")); - } - - @Test - public void testParseResponse() throws Exception { - HttpResponse stubResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("1.1", 1, 1), - 200, - "OK" - )); - - String json = "{\n" + " \"count\": 4,\n" + " \"numbers\": [\n" + " {\n" + " \"country\": \"GB\",\n" - + " \"msisdn\": \"447700900000\",\n" + " \"cost\": \"0.50\",\n" - + " \"type\": \"mobile\",\n" + " \"features\": [\n" + " \"VOICE\",\n" - + " \"SMS\"\n" + " ]\n" + " }\n" + " ]\n" + "}"; - InputStream jsonStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); - BasicHttpEntity entity = new BasicHttpEntity(); - entity.setContent(jsonStream); - stubResponse.setEntity(entity); - - SearchNumbersResponse response = endpoint.parseResponse(stubResponse); - assertEquals(4, response.getCount()); - } - - @Test - public void testRequestThrottleResponse() throws Exception { - test429(new SearchNumbersEndpoint(null)); - } - - @Test - public void testDefaultUri() throws Exception { - SearchNumbersFilter filter = new SearchNumbersFilter("BB"); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setFeatures(new String[]{"SMS", "VOICE"}); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - - RequestBuilder builder = endpoint.makeRequest(filter); - assertEquals("GET", builder.getMethod()); - assertEquals( - "https://rest.nexmo.com/number/search?country=BB&features=SMS%2CVOICE&index=10&size=20&pattern=234&search_pattern=0", - builder.build().getURI().toString() - ); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - SearchNumbersEndpoint endpoint = new SearchNumbersEndpoint(wrapper); - SearchNumbersFilter filter = new SearchNumbersFilter("BB"); - filter.setIndex(10); - filter.setSize(20); - filter.setPattern("234"); - filter.setFeatures(new String[]{"SMS", "VOICE"}); - filter.setSearchPattern(SearchPattern.STARTS_WITH); - - RequestBuilder builder = endpoint.makeRequest(filter); - assertEquals("GET", builder.getMethod()); - assertEquals( - "https://example.com/number/search?country=BB&features=SMS%2CVOICE&index=10&size=20&pattern=234&search_pattern=0", - builder.build().getURI().toString() - ); - } -} diff --git a/src/test/java/com/vonage/client/numbers/UpdateNumberEndpointTest.java b/src/test/java/com/vonage/client/numbers/UpdateNumberEndpointTest.java deleted file mode 100644 index b987dbfd6..000000000 --- a/src/test/java/com/vonage/client/numbers/UpdateNumberEndpointTest.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright 2023 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.numbers; - -import com.vonage.client.HttpConfig; -import com.vonage.client.HttpWrapper; -import com.vonage.client.TestUtils; -import com.vonage.client.auth.TokenAuthMethod; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.entity.ContentType; -import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; -import java.util.List; -import java.util.Map; - -public class UpdateNumberEndpointTest { - private UpdateNumberEndpoint endpoint; - - @Before - public void setUp() throws Exception { - endpoint = new UpdateNumberEndpoint(new HttpWrapper( - new TokenAuthMethod("abc123", "a2cd5f789") - )); - } - - @Test - public void testGetAcceptableAuthMethods() throws Exception { - Class[] auths = endpoint.getAcceptableAuthMethods(); - assertArrayEquals(new Class[]{TokenAuthMethod.class}, auths); - } - - @Test - public void testMakeRequest() throws Exception { - RequestBuilder builder = endpoint.makeRequest(new UpdateNumberRequest("447700900013", "UK")); - assertEquals("POST", builder.getMethod()); - assertEquals("https://rest.nexmo.com/number/update", builder.build().getURI().toString()); - - assertEquals(ContentType.APPLICATION_JSON.getMimeType(), builder.getFirstHeader("Accept").getValue()); - Map params = TestUtils.makeParameterMap(builder.getParameters()); - assertEquals(2, params.size()); - assertEquals("447700900013", params.get("msisdn")); - assertEquals("UK", params.get("country")); - } - - @Test - public void testMakeRequestWithOptionalParams() throws Exception { - UpdateNumberRequest request = new UpdateNumberRequest("447700900013", "UK"); - request.setMoHttpUrl("https://api.example.com/mo"); - request.setMoSmppSysType("inbound"); - request.setVoiceCallbackValue("1234-5678-9123-4567"); - request.setVoiceCallbackType(UpdateNumberRequest.CallbackType.APP); - request.setVoiceStatusCallback("https://api.example.com/callback"); - request.setMessagesCallbackValue("MESSAGES-APPLICATION-ID"); - - RequestBuilder builder = endpoint.makeRequest(request); - - assertEquals("POST", builder.getMethod()); - assertEquals("https://rest.nexmo.com/number/update", builder.build().getURI().toString()); - - Map params = TestUtils.makeParameterMap(builder.getParameters()); - assertEquals(9, params.size()); - assertEquals("447700900013", params.get("msisdn")); - assertEquals("UK", params.get("country")); - assertEquals("https://api.example.com/mo", params.get("moHttpUrl")); - assertEquals("inbound", params.get("moSmppSysType")); - assertEquals("1234-5678-9123-4567", params.get("voiceCallbackValue")); - assertEquals("app", params.get("voiceCallbackType")); - assertEquals("https://api.example.com/callback", params.get("voiceStatusCallback")); - assertEquals("MESSAGES-APPLICATION-ID", params.get("messagesCallbackValue")); - assertEquals(UpdateNumberRequest.CallbackType.APP.paramValue(), params.get("messagesCallbackType")); - } - - @Test - public void testApplyAuth() throws Exception { - RequestBuilder builder = endpoint.applyAuth(endpoint.makeRequest( - new UpdateNumberRequest("447700900015", "UK") - )); - List params = builder.getParameters(); - assertEquals(2, params.size()); - } - - @Test - public void testParseResponse() throws Exception { - try { - HttpResponse stub = TestUtils.makeJsonHttpResponse(200, - "{\n" + " \"error-code\":\"200\",\n" + " \"error-code-label\":\"success\"\n" + "}" - ); - endpoint.parseResponse(stub); - } catch (Exception e) { - fail("Parsing a 200 response should not raise an error."); - } - } - - @Test - public void testParseErrorResponse() throws Exception { - try { - HttpResponse stub = TestUtils.makeJsonHttpResponse( - 500, - "{\n" + " \"error-code\":\"500\",\n" + " \"error-code-label\":\"There was an error\"\n" + "}" - ); - endpoint.parseResponse(stub); - fail("An exception should have been thrown here."); - } catch (Exception e) { - // This is expected. - } - } - - @Test - public void testDefaultUri() throws Exception { - UpdateNumberRequest request = new UpdateNumberRequest("447700900013", "UK"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://rest.nexmo.com/number/update", builder.build().getURI().toString()); - } - - @Test - public void testCustomUri() throws Exception { - HttpWrapper wrapper = new HttpWrapper(HttpConfig.builder().baseUri("https://example.com").build()); - UpdateNumberEndpoint endpoint = new UpdateNumberEndpoint(wrapper); - UpdateNumberRequest request = new UpdateNumberRequest("447700900013", "UK"); - - RequestBuilder builder = endpoint.makeRequest(request); - assertEquals("POST", builder.getMethod()); - assertEquals("https://example.com/number/update", builder.build().getURI().toString()); - } -} diff --git a/src/test/java/com/vonage/client/subaccounts/SubaccountsClientTest.java b/src/test/java/com/vonage/client/subaccounts/SubaccountsClientTest.java index b8a8918c2..4b6086a61 100644 --- a/src/test/java/com/vonage/client/subaccounts/SubaccountsClientTest.java +++ b/src/test/java/com/vonage/client/subaccounts/SubaccountsClientTest.java @@ -338,8 +338,7 @@ protected HttpMethod expectedHttpMethod() { protected String expectedEndpointUri(ListTransfersFilter request) { assertNotNull(request.getStartDate()); assertNotNull(request.getEndDate()); - return "/accounts/"+apiKey+"/credit-transfers?start_date=2022-06-01T08%3A00%3A00Z" + - "&end_date=2023-06-08T09%3A01%3A40Z&subaccount=" + request.getSubaccount(); + return "/accounts/"+apiKey+"/credit-transfers"; } @Override @@ -388,7 +387,7 @@ protected String expectedEndpointUri(ListTransfersFilter request) { assertNotNull(request.getStartDate()); assertNull(request.getEndDate()); assertNull(request.getSubaccount()); - return "/accounts/"+apiKey+"/balance-transfers?start_date=1970-01-01T00%3A00%3A00Z"; + return "/accounts/"+apiKey+"/balance-transfers"; } @Override