Skip to content

Commit

Permalink
feat (Verification/StartSMS): Support additional properties onto Star…
Browse files Browse the repository at this point in the history
…t request
  • Loading branch information
JPPortier committed Sep 4, 2024
1 parent d93c34d commit e9caba6
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sinch.sdk.domains.verification.adapters;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Expand All @@ -9,11 +10,12 @@
import com.sinch.sdk.core.TestHelpers;
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.domains.verification.adapters.converters.VerificationsDtoConverterTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.response.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseCallout;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseFlashCall;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseSMS;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseSeamless;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequestSms;
import com.sinch.sdk.domains.verification.models.v1.start.request.internal.VerificationStartRequestInternalImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -22,16 +24,18 @@
@TestWithResources
public class VerificationsStartServiceTest extends VerificationBaseTest {

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestPhoneCallDto.json")
@GivenJsonResource(
"/domains/verification/v1/start/request/VerificationStartRequestPhoneCallDto.json")
public VerificationStartRequestInternalImpl startVerificationPhoneCallRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestFlashCallDto.json")
@GivenJsonResource(
"/domains/verification/v1/start/request/VerificationStartRequestFlashCallDto.json")
public VerificationStartRequestInternalImpl startVerificationFlashCallRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestDataDto.json")
@GivenJsonResource("/domains/verification/v1/start/request/VerificationStartRequestDataDto.json")
public VerificationStartRequestInternalImpl startVerificationDataRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestSmsDto.json")
@GivenJsonResource("/domains/verification/v1/start/request/VerificationStartRequestSmsDto.json")
public VerificationStartRequestInternalImpl startVerificationSmsRequestDto;

@Mock com.sinch.sdk.domains.verification.api.v1.VerificationStartService v1;
Expand All @@ -46,7 +50,7 @@ public void initMocks() {
@Test
void startSms() throws ApiException {

when(v1.startSms(eq(startVerificationSmsRequestDto.getVerificationStartRequestSmsImpl())))
when(v1.startSms(any(VerificationStartRequestSms.class)))
.thenReturn(
VerificationStartResponseTest.expectedStartVerificationSmsDto
.getVerificationStartResponseSmsImpl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.sinch.sdk.domains.verification.models.VerificationStatusType;
import com.sinch.sdk.domains.verification.models.dto.v1.report.VerificationReportRequestDtoTest;
import com.sinch.sdk.domains.verification.models.dto.v1.report.VerificationReportResponseDtoTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.VerificationStartRequestTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.request.VerificationStartRequestTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.response.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.dto.v1.status.VerificationStatusResponseTest;
import com.sinch.sdk.domains.verification.models.requests.SMSCodeType;
import com.sinch.sdk.domains.verification.models.requests.StartVerificationCalloutOptions;
Expand All @@ -35,6 +35,8 @@
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseFlashCall;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseSMS;
import com.sinch.sdk.domains.verification.models.response.StartVerificationResponseSeamless;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequest;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequestSms;
import java.time.Instant;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -136,9 +138,18 @@ void convertStartDataRequest() {
@Test
void convertStartSmsRequest() {

VerificationStartRequest expected =
VerificationStartRequestSms.builder()
.setCustom("a custom")
.setReference("a reference")
.setIdentity(
com.sinch.sdk.domains.verification.models.v1.NumberIdentity.valueOf("+endpoint"))
.setExpiry("01:02:03")
.setCodeType(VerificationStartRequestSms.CodeTypeEnum.ALPHANUMERIC)
.setTemplate("My template require to use '{{CODE}}' code")
.build();
TestHelpers.recursiveEquals(
VerificationsDtoConverter.convert(startVerificationSmsRequest),
VerificationStartRequestTest.startVerificationSmsDto.getVerificationStartRequestSmsImpl());
VerificationsDtoConverter.convert(startVerificationSmsRequest), expected);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sinch.sdk.domains.verification.api.v1.adapters;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
Expand All @@ -13,8 +14,8 @@
import com.sinch.sdk.core.http.HttpClient;
import com.sinch.sdk.domains.verification.adapters.VerificationBaseTest;
import com.sinch.sdk.domains.verification.api.v1.internal.VerificationsStartApi;
import com.sinch.sdk.domains.verification.models.dto.v1.start.VerificationStartRequestTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.request.VerificationStartRequestTest;
import com.sinch.sdk.domains.verification.models.dto.v1.start.response.VerificationStartResponseTest;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequest;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequestSms;
import com.sinch.sdk.domains.verification.models.v1.start.request.VerificationStartRequestSmsImpl;
Expand All @@ -33,16 +34,18 @@
@TestWithResources
public class VerificationStartServiceTest extends VerificationBaseTest {

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestPhoneCallDto.json")
@GivenJsonResource(
"/domains/verification/v1/start/request/VerificationStartRequestPhoneCallDto.json")
public VerificationStartRequestInternal startVerificationPhoneCallRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestFlashCallDto.json")
@GivenJsonResource(
"/domains/verification/v1/start/request/VerificationStartRequestFlashCallDto.json")
public VerificationStartRequestInternal startVerificationFlashCallRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestDataDto.json")
@GivenJsonResource("/domains/verification/v1/start/request/VerificationStartRequestDataDto.json")
public VerificationStartRequestInternal startVerificationDataRequestDto;

@GivenJsonResource("/domains/verification/v1/start/VerificationStartRequestSmsDto.json")
@GivenJsonResource("/domains/verification/v1/start/request/VerificationStartRequestSmsDto.json")
public VerificationStartRequestInternal startVerificationSmsRequestDto;

@Mock VerificationsStartApi api;
Expand All @@ -62,7 +65,7 @@ public void initMocks() {
@Test
void startSms() throws ApiException {

when(api.startVerification(eq(startVerificationSmsRequestDto), eq(null)))
when(api.startVerification(any(VerificationStartRequestInternal.class), eq(null)))
.thenReturn(VerificationStartResponseTest.expectedStartVerificationSmsDto);

VerificationStartResponseSms response =
Expand Down Expand Up @@ -95,7 +98,7 @@ void startSmsWithAcceptLanguage() throws ApiException {
new VerificationStartRequestInternalImpl();
internalWithAcceptLanguage.setActualInstance(withAcceptLanguage);

when(api.startVerification(eq(internalWithAcceptLanguage), eq("es-ES")))
when(api.startVerification(any(VerificationStartRequestInternal.class), eq("es-ES")))
.thenReturn(VerificationStartResponseTest.expectedStartVerificationSmsDto);

VerificationStartResponseSms response =
Expand Down
30 changes: 18 additions & 12 deletions core/src/main/com/sinch/sdk/core/utils/databind/Mapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sinch.sdk.core.utils.databind;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -47,19 +48,24 @@ public void serializeAsField(
// Do not serialize uninitialized fields to avoid sending a null value when not required
boolean serialize;

// Call getter to obtain value: if of OptionalValue type: use it to check serialization
// state
Object value = pojo.getClass().getMethod(member.getName()).invoke(pojo);
if (value instanceof OptionalValue) {
serialize = ((OptionalValue<?>) value).isPresent();
if (member.hasAnnotation(JsonAnyGetter.class)) {
serialize = true;
} else {
/* @Deprecated
* Not find the expected OptionalValue: fallback to xxxDefined function call
* The xxDefined function feature will have to be deprecated in favour of OptionalValue usage
* This part could be suppressed as soon as all domains had moved to the OptionalValue support from OAS generated files
*/
serialize =
(boolean) pojo.getClass().getMethod(member.getName() + "Defined").invoke(pojo);
// Call getter to obtain value: if of OptionalValue type: use it to check
// serialization
// state
Object value = pojo.getClass().getMethod(member.getName()).invoke(pojo);
if (value instanceof OptionalValue) {
serialize = ((OptionalValue<?>) value).isPresent();
} else {
/* @Deprecated
* Not find the expected OptionalValue: fallback to xxxDefined function call
* The xxDefined function feature will have to be deprecated in favour of OptionalValue usage
* This part could be suppressed as soon as all domains had moved to the OptionalValue support from OAS generated files
*/
serialize =
(boolean) pojo.getClass().getMethod(member.getName() + "Defined").invoke(pojo);
}
}
// not set but is field is required ?
if (!serialize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public static String valueOf(CodeTypeEnum e) {
*/
String getAcceptLanguage();

/**
* Return the extra-additional property with the specified name. Used in case of delegation
* pattern in use by class
*
* @param key the name of the property
* @return the additional property with the specified name
*/
Object getExtraOption(String key);

/**
* Getting builder
*
Expand Down Expand Up @@ -181,6 +190,14 @@ interface Builder
*/
Builder setAcceptLanguage(String acceptLanguage);

/**
* see getter
*
* @return Current builder
* @see #getExtraOption
*/
Builder putExtraOption(String key, Object value);

/**
* Create instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ public String getExpiry() {
}

public OptionalValue<String> expiry() {
return null != smsOptions
? smsOptions.map(VerificationStartSmsOptions::getExpiry)
return null != smsOptions && smsOptions.isPresent()
? smsOptions
.map(f -> ((VerificationStartSmsOptionsImpl) f).expiry())
.orElse(OptionalValue.empty())
: OptionalValue.empty();
}

Expand All @@ -140,7 +142,7 @@ public CodeTypeEnum getCodeType() {
}

public OptionalValue<CodeTypeEnum> codeType() {
return null != smsOptions
return null != smsOptions && smsOptions.isPresent()
? smsOptions.map(f -> CodeTypeEnum.from(smsOptions.get().getCodeType().value()))
: OptionalValue.empty();
}
Expand All @@ -154,8 +156,10 @@ public String getTemplate() {
}

public OptionalValue<String> template() {
return null != smsOptions
? smsOptions.map(VerificationStartSmsOptions::getTemplate)
return null != smsOptions && smsOptions.isPresent()
? smsOptions
.map(f -> ((VerificationStartSmsOptionsImpl) f).template())
.orElse(OptionalValue.empty())
: OptionalValue.empty();
}

Expand All @@ -170,11 +174,18 @@ public String getAcceptLanguage() {
}

public OptionalValue<String> acceptLanguage() {
return null != smsOptions
? smsOptions.map(VerificationStartSmsOptions::getAcceptLanguage)
return null != smsOptions && smsOptions.isPresent()
? smsOptions
.map(f -> ((VerificationStartSmsOptionsImpl) f).acceptLanguage())
.orElse(OptionalValue.empty())
: OptionalValue.empty();
}

@JsonIgnore
public Object getExtraOption(String key) {
return null != smsOptions && smsOptions.isPresent() ? smsOptions.get().get(key) : null;
}

/** Return true if this VerificationStartRequestSms object is equal to o. */
@Override
public boolean equals(Object o) {
Expand Down Expand Up @@ -283,6 +294,11 @@ public Builder setAcceptLanguage(String acceptLanguage) {
return this;
}

public Builder putExtraOption(String key, Object value) {
getDelegatedBuilder().put(key, value);
return this;
}

private VerificationStartSmsOptions.Builder getDelegatedBuilder() {
if (null == _delegatedBuilder) {
this._delegatedBuilder = VerificationStartSmsOptions.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public static String valueOf(CodeTypeEnum e) {
*/
String getAcceptLanguage();

/**
* Return the additional property with the specified name.
*
* @param key the name of the property
* @return the additional property with the specified name
*/
Object get(String key);

/**
* Getting builder
*
Expand Down Expand Up @@ -132,6 +140,14 @@ interface Builder {
*/
Builder setAcceptLanguage(String acceptLanguage);

/**
* see getter
*
* @return Current builder
* @see #get
*/
Builder put(String key, Object value);

/**
* Create instance
*
Expand Down
Loading

0 comments on commit e9caba6

Please sign in to comment.