Skip to content

Commit 07afa55

Browse files
authored
breaking: Remove verify2.Locale enum (#499)
1 parent 1de01a9 commit 07afa55

5 files changed

Lines changed: 49 additions & 87 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
- Bumped Jackson version to 2.16.0
1010
- Use String instead of UUID in `VoiceClient` call modification methods
1111
- Added public `verifyRequestSignature` method to `RequestSigning`
12+
- Replaced custom `Locale` enum in Verify v2 with `java.util.Locale`
1213

1314
# [8.0.0-rc2] - 2023-11-07
1415
- Removed packages:

src/main/java/com/vonage/client/verify2/Locale.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/main/java/com/vonage/client/verify2/VerificationRequest.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package com.vonage.client.verify2;
1717

18-
import com.fasterxml.jackson.annotation.JsonIgnore;
19-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20-
import com.fasterxml.jackson.annotation.JsonInclude;
21-
import com.fasterxml.jackson.annotation.JsonProperty;
18+
import com.fasterxml.jackson.annotation.*;
2219
import com.vonage.client.Jsonable;
2320
import java.util.ArrayList;
2421
import java.util.List;
22+
import java.util.Locale;
2523
import java.util.Objects;
2624
import java.util.regex.Pattern;
2725

@@ -41,7 +39,7 @@
4139
public class VerificationRequest implements Jsonable {
4240
static final Pattern CODE_REGEX = Pattern.compile("[a-zA-Z0-9]{4,10}");
4341

44-
protected final Locale locale;
42+
@JsonProperty("locale") protected final Locale locale;
4543
protected final Integer channelTimeout, codeLength;
4644
protected final Boolean fraudCheck;
4745
protected final String brand, code, clientRef;
@@ -98,13 +96,18 @@ public String getBrand() {
9896
/**
9997
* Language for the request in ISO_639-1 format.
10098
*
101-
* @return The language as an enum, or {@code null} if not set (the default).
99+
* @return The locale, or {@code null} if not set (the default).
102100
*/
103-
@JsonProperty("locale")
101+
@JsonIgnore
104102
public Locale getLocale() {
105103
return locale;
106104
}
107105

106+
@JsonGetter("locale")
107+
protected String getLocaleAsString() {
108+
return locale == null ? null : locale.toString().replace("_", "-").toLowerCase();
109+
}
110+
108111
/**
109112
* Specifies the wait time in seconds between attempts to delivery the verification code.
110113
*
@@ -292,17 +295,40 @@ public Builder channelTimeout(int timeout) {
292295

293296
/**
294297
* (OPTIONAL)
295-
* Languages that are available to use.
298+
* Set the language that this request will be delivered in. Refer to
299+
* <a href=https://developer.vonage.com/en/verify/guides/verify-v2-languages>the documentation</a>
300+
* for a list of supported locales.
296301
*
297-
* @param locale The language locale as an enum.
302+
* @param locale The language locale.
298303
*
299304
* @return This builder.
305+
*
306+
* @since 8.0.0
300307
*/
301308
public Builder locale(Locale locale) {
309+
if (locale == null || locale.toString().isEmpty()) {
310+
throw new IllegalArgumentException("Invalid locale");
311+
}
302312
this.locale = locale;
303313
return this;
304314
}
305315

316+
/**
317+
* (OPTIONAL)
318+
* Set the language that this request will be delivered in.
319+
*
320+
* @param locale The language locale as a string. This should be a
321+
* <a href=https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>ISO 639-1 code</a>.
322+
*
323+
* @return This builder.
324+
*
325+
* @since 8.0.0
326+
* @see #locale(Locale)
327+
*/
328+
public Builder locale(String locale) {
329+
return locale(Locale.forLanguageTag(locale));
330+
}
331+
306332
/**
307333
* (OPTIONAL)
308334
* If this reference is set when the request is sent, it will be included in the callbacks.

src/test/java/com/vonage/client/verify2/VerificationRequestTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import static org.junit.jupiter.api.Assertions.*;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.Locale;
2526

2627
public class VerificationRequestTest {
2728
static final boolean SANDBOX = true;
28-
static final Locale LOCALE = Locale.PORTUGUESE_PORTUGAL;
29+
static final Locale LOCALE = Locale.forLanguageTag("pt-pt");
2930
static final int CODE_LENGTH = 8, CHANNEL_TIMEOUT = 120;
3031
static final String
3132
BRAND = "Vonage",
@@ -309,6 +310,14 @@ public void testSilentAuthMustBeFirstWorkflow() {
309310
assertThrows(IllegalStateException.class, builder::build);
310311
}
311312

313+
@Test
314+
public void testInvalidLocale() throws Exception {
315+
VerificationRequest.Builder builder = getBuilderRequiredParamsSingleWorkflow(Channel.SMS);
316+
assertThrows(IllegalArgumentException.class, () -> builder.locale("--++").build());
317+
assertThrows(IllegalArgumentException.class, () -> builder.locale("en_GB").build());
318+
assertNotNull(builder.locale("ab-cd").build().getLocale());
319+
}
320+
312321
@Test
313322
public void triggerJsonProcessingException() {
314323
class SelfRefrencing extends VerificationRequest {

src/test/java/com/vonage/client/verify2/Verify2ClientTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ VerificationRequest newVerificationRequestWithAllParamsAndWorkflows() {
6161
.brand("Nexmo").fraudCheck(false)
6262
.code("ab2c3de5").codeLength(8)
6363
.channelTimeout(500)
64-
.locale(Locale.GERMAN_GERMANY)
64+
.locale("de-de")
6565
.clientRef("callback-ref0x1")
6666
.workflows(workflows).build();
6767
}
@@ -106,14 +106,14 @@ protected String expectedEndpointUri(VerificationRequest request) {
106106
@Override
107107
protected VerificationRequest sampleRequest() {
108108
return VerificationRequest.builder()
109-
.clientRef("my-personal-reference").locale(Locale.ENGLISH_UK)
109+
.clientRef("my-personal-reference").locale("ar-XA")
110110
.addWorkflow(new SmsWorkflow("447700900001", "FA+9qCX9VSu"))
111111
.brand("ACME, Inc").codeLength(6).channelTimeout(320).build();
112112
}
113113

114114
@Override
115115
protected String sampleRequestBodyString() {
116-
return "{\"locale\":\"en-gb\",\"channel_timeout\":320,\"code_length\":6," +
116+
return "{\"locale\":\"ar-xa\",\"channel_timeout\":320,\"code_length\":6," +
117117
"\"brand\":\"ACME, Inc\",\"client_ref\":\"my-personal-reference\",\"workflow\":" +
118118
"[{\"channel\":\"sms\",\"to\":\"447700900001\",\"app_hash\":\"FA+9qCX9VSu\"}]}";
119119
}

0 commit comments

Comments
 (0)