diff --git a/CHANGELOG.md b/CHANGELOG.md index c34af47ea..e16d10aaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Bumped Jackson version to 2.16.0 - Use String instead of UUID in `VoiceClient` call modification methods - Added public `verifyRequestSignature` method to `RequestSigning` +- Replaced custom `Locale` enum in Verify v2 with `java.util.Locale` # [8.0.0-rc2] - 2023-11-07 - Removed packages: diff --git a/src/main/java/com/vonage/client/verify2/Locale.java b/src/main/java/com/vonage/client/verify2/Locale.java deleted file mode 100644 index 939c7b305..000000000 --- a/src/main/java/com/vonage/client/verify2/Locale.java +++ /dev/null @@ -1,74 +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.verify2; - -import com.fasterxml.jackson.annotation.JsonValue; - -/** - * Represents the available locales for user verification. These are - * ISO 639-1 codes. - */ -public enum Locale { - ENGLISH_US("en-us"), - ENGLISH_UK("en-gb"), - SPANISH_SPAIN("es-es"), - SPANISH_MEXICO("es-mx"), - SPANISH_US("es-us"), - ITALIAN_ITALY("it-it"), - FRENCH_FRANCE("fr-fr"), - GERMAN_GERMANY("de-de"), - RUSSIAN_RUSSIA("ru-ru"), - HINDI_INDIA("hi-in"), - PORTUGUESE_BRAZIL("pt-br"), - PORTUGUESE_PORTUGAL("pt-pt"), - INDONESIAN_INDONESIA("id-id"), - JAPANESE_JAPAN("ja-jp"), - HEBREW_ISRAEL("he-il"), - YUE_CHINESE_CHINA("yue-cn"), - ARABIC_ARABIAN_PENINSULA("ar-xa"), - CZECH_CZECH_REPUBLIC("cs-cz"), - WELSH_UK("cy-gb"), - GREEK_GREECE("el-gr"), - ENGLISH_AUSTRALIA("en-au"), - ENGLISH_INDIA("en-in"), - FINNISH_FINLAND("fi-fi"), - FILIPINO_PHILIPPINES("fil-ph"), - FRENCH_CANADA("fr-ca"), - HUNGARIAN_HUNGARY("hu-hu"), - ICELANDIC_ICELAND("is-is"), - NORWEGIAN_BOKMAL_NORWAY("nb-no"), - DUTCH_NETHERLANDS("nl-nl"), - POLISH_POLAND("pl-pl"), - ROMANIAN_ROMANIA("ro-ro"), - SWEDISH_SWEDEN("sv-se"), - THAI_THAILAND("th-th"), - TURKISH_TURKEY("tr-tr"), - VIETNAMESE_VIETNAM("vi-vn"), - CHINESE_CHINA("zh-cn"), - CHINESE_TAIWAN("zh-tw"); - - private final String code; - - Locale(String code) { - this.code = code; - } - - @JsonValue - @Override - public String toString() { - return code; - } -} diff --git a/src/main/java/com/vonage/client/verify2/VerificationRequest.java b/src/main/java/com/vonage/client/verify2/VerificationRequest.java index 6406fc16d..cf3be8e9a 100644 --- a/src/main/java/com/vonage/client/verify2/VerificationRequest.java +++ b/src/main/java/com/vonage/client/verify2/VerificationRequest.java @@ -15,13 +15,11 @@ */ package com.vonage.client.verify2; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.*; import com.vonage.client.Jsonable; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.regex.Pattern; @@ -41,7 +39,7 @@ public class VerificationRequest implements Jsonable { static final Pattern CODE_REGEX = Pattern.compile("[a-zA-Z0-9]{4,10}"); - protected final Locale locale; + @JsonProperty("locale") protected final Locale locale; protected final Integer channelTimeout, codeLength; protected final Boolean fraudCheck; protected final String brand, code, clientRef; @@ -98,13 +96,18 @@ public String getBrand() { /** * Language for the request in ISO_639-1 format. * - * @return The language as an enum, or {@code null} if not set (the default). + * @return The locale, or {@code null} if not set (the default). */ - @JsonProperty("locale") + @JsonIgnore public Locale getLocale() { return locale; } + @JsonGetter("locale") + protected String getLocaleAsString() { + return locale == null ? null : locale.toString().replace("_", "-").toLowerCase(); + } + /** * Specifies the wait time in seconds between attempts to delivery the verification code. * @@ -292,17 +295,40 @@ public Builder channelTimeout(int timeout) { /** * (OPTIONAL) - * Languages that are available to use. + * Set the language that this request will be delivered in. Refer to + * the documentation + * for a list of supported locales. * - * @param locale The language locale as an enum. + * @param locale The language locale. * * @return This builder. + * + * @since 8.0.0 */ public Builder locale(Locale locale) { + if (locale == null || locale.toString().isEmpty()) { + throw new IllegalArgumentException("Invalid locale"); + } this.locale = locale; return this; } + /** + * (OPTIONAL) + * Set the language that this request will be delivered in. + * + * @param locale The language locale as a string. This should be a + * ISO 639-1 code. + * + * @return This builder. + * + * @since 8.0.0 + * @see #locale(Locale) + */ + public Builder locale(String locale) { + return locale(Locale.forLanguageTag(locale)); + } + /** * (OPTIONAL) * If this reference is set when the request is sent, it will be included in the callbacks. diff --git a/src/test/java/com/vonage/client/verify2/VerificationRequestTest.java b/src/test/java/com/vonage/client/verify2/VerificationRequestTest.java index 544143e4a..79bd86e27 100644 --- a/src/test/java/com/vonage/client/verify2/VerificationRequestTest.java +++ b/src/test/java/com/vonage/client/verify2/VerificationRequestTest.java @@ -22,10 +22,11 @@ import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; import java.util.Collections; +import java.util.Locale; public class VerificationRequestTest { static final boolean SANDBOX = true; - static final Locale LOCALE = Locale.PORTUGUESE_PORTUGAL; + static final Locale LOCALE = Locale.forLanguageTag("pt-pt"); static final int CODE_LENGTH = 8, CHANNEL_TIMEOUT = 120; static final String BRAND = "Vonage", @@ -309,6 +310,14 @@ public void testSilentAuthMustBeFirstWorkflow() { assertThrows(IllegalStateException.class, builder::build); } + @Test + public void testInvalidLocale() throws Exception { + VerificationRequest.Builder builder = getBuilderRequiredParamsSingleWorkflow(Channel.SMS); + assertThrows(IllegalArgumentException.class, () -> builder.locale("--++").build()); + assertThrows(IllegalArgumentException.class, () -> builder.locale("en_GB").build()); + assertNotNull(builder.locale("ab-cd").build().getLocale()); + } + @Test public void triggerJsonProcessingException() { class SelfRefrencing extends VerificationRequest { diff --git a/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java b/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java index 04cea9a5d..270f37856 100644 --- a/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java +++ b/src/test/java/com/vonage/client/verify2/Verify2ClientTest.java @@ -61,7 +61,7 @@ VerificationRequest newVerificationRequestWithAllParamsAndWorkflows() { .brand("Nexmo").fraudCheck(false) .code("ab2c3de5").codeLength(8) .channelTimeout(500) - .locale(Locale.GERMAN_GERMANY) + .locale("de-de") .clientRef("callback-ref0x1") .workflows(workflows).build(); } @@ -106,14 +106,14 @@ protected String expectedEndpointUri(VerificationRequest request) { @Override protected VerificationRequest sampleRequest() { return VerificationRequest.builder() - .clientRef("my-personal-reference").locale(Locale.ENGLISH_UK) + .clientRef("my-personal-reference").locale("ar-XA") .addWorkflow(new SmsWorkflow("447700900001", "FA+9qCX9VSu")) .brand("ACME, Inc").codeLength(6).channelTimeout(320).build(); } @Override protected String sampleRequestBodyString() { - return "{\"locale\":\"en-gb\",\"channel_timeout\":320,\"code_length\":6," + + return "{\"locale\":\"ar-xa\",\"channel_timeout\":320,\"code_length\":6," + "\"brand\":\"ACME, Inc\",\"client_ref\":\"my-personal-reference\",\"workflow\":" + "[{\"channel\":\"sms\",\"to\":\"447700900001\",\"app_hash\":\"FA+9qCX9VSu\"}]}"; }