Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report DEVEXP-587 fix #158

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,27 @@

import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.domains.sms.models.Parameters;
import com.sinch.sdk.domains.sms.models.Parameters.Entry;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjParameterKeyDto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class ParametersDtoConverter {

public static Parameters convert(ParameterObjDto dto) {
if (null == dto) {
return null;
}
return new Parameters(
dto.entrySet().stream()
.map(
entry -> {
@SuppressWarnings("unchecked")
Map<String, String> entryValue = (Map<String, String>) entry.getValue();
return new Parameters.Entry(
entry.getKey(),
entryValue.entrySet().stream()
.filter(
value ->
value
.getKey()
.compareTo(
ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT)
!= 0)
.map(e -> new Pair<>(e.getKey(), e.getValue()))
.findFirst()
.orElse(null),
entryValue.entrySet().stream()
.filter(
value ->
value
.getKey()
.compareTo(
ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT)
== 0)
.map(Map.Entry::getValue)
.findFirst()
.orElse(null));
})
.collect(Collectors.toList()));

ArrayList<Parameters.Entry> client = new ArrayList<>();
dto.forEach((key, value) -> client.addAll(convertParameters(key, value)));

return new Parameters(client);
}

public static ParameterObjDto convert(Parameters parameters) {
Expand All @@ -54,18 +31,52 @@ public static ParameterObjDto convert(Parameters parameters) {
parameters
.entrySet()
.iterator()
.forEachRemaining(
entry -> {
Map<String, String> obj = new HashMap<>();
Pair<String, String> pair = entry.getValue().getValue();
obj.put(pair.getLeft(), pair.getRight());
entry
.getValue()
.getDefaultValue()
.ifPresent(
def -> obj.put(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT, def));
dto.put(entry.getKey(), obj);
});
.forEachRemaining(entries -> dto.put(entries.getKey(), convert(entries.getValue())));
return dto;
}

public static HashMap<String, String> convert(Collection<Entry> client) {
if (null == client) {
return null;
}
HashMap<String, String> dto = new HashMap<>();

client.forEach(
entry -> {
dto.put(entry.getValue().getLeft(), entry.getValue().getRight());
entry
.getDefaultValue()
.ifPresent(def -> dto.put(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT, def));
});
return dto;
}

public static Collection<Parameters.Entry> convertParameters(String parameterName, Object _dto) {
if (!(_dto instanceof Map)) {
return null;
}
ArrayList<Parameters.Entry> client = new ArrayList<>();

@SuppressWarnings("unchecked")
Map<String, String> dto = (Map<String, String>) _dto;

AtomicReference<Optional<String>> defValue =
new AtomicReference<>(
dto.entrySet().stream()
.filter(
entry ->
entry.getKey().equals(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT))
.map(Map.Entry::getValue)
.findFirst());

dto.forEach(
(key, value) -> {
if (!key.equals(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT)) {
client.add(
new Entry(parameterName, new Pair<>(key, value), defValue.get().orElse(null)));
defValue.set(Optional.empty());
}
});
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import com.sinch.sdk.core.utils.Pair;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Parameters extends HashMap<String, Parameters.Entry> {
public class Parameters extends HashMap<String, List<Parameters.Entry>> {

private static final long serialVersionUID = 1L;

public Parameters(Collection<Parameters.Entry> list) {
super(list.stream().collect(Collectors.toMap(entry -> entry.key, entry -> entry)));
/**
* @throws NullPointerException – if the specified map is null
*/
public Parameters(Collection<Parameters.Entry> list) throws NullPointerException {
super(list.stream().collect(Collectors.groupingBy(Parameters.Entry::getKey)));
}

public static class Entry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import com.sinch.sdk.domains.sms.models.dto.v1.ApiBatchListDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ApiDeliveryFeedbackDto;
import com.sinch.sdk.domains.sms.models.dto.v1.DryRun200ResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjParameterKeyDto;
import com.sinch.sdk.domains.sms.models.dto.v1.SendSMS201ResponseDto;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchBinaryRequest;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchMediaRequest;
Expand Down Expand Up @@ -73,7 +71,7 @@ public class BatchesServiceTest extends BaseTest {
static final int fromTon = 6;
static final int fromNpi = 18;
static final String udh = "foo udh";
static final String body = "Hi ${name}! How are you?";
static final String body = "Hi ${name} ({an identifier}) ! How are you?";
public static final BatchBinary batchBinary =
BatchBinary.builder()
.setId(id)
Expand All @@ -99,16 +97,17 @@ public class BatchesServiceTest extends BaseTest {
static final Parameters parameters =
new Parameters(
Arrays.asList(
new Parameters.Entry("an identifier", new Pair<>("a key", "a value")),
new Parameters.Entry(
ParameterObjDto
.JSON_PROPERTY_LEFT_CURLY_BRACKET_PARAMETER_KEY_RIGHT_CURLY_BRACKET,
new Pair<>(
ParameterObjParameterKeyDto
.JSON_PROPERTY_LEFT_CURLY_BRACKET_MSISDN_RIGHT_CURLY_BRACKET,
"msisdn value"),
"default value")));
public final BatchMedia batchMedia =
"name", new Pair<>("15551231234", "name value for 15551231234"), "default value"),
new Parameters.Entry("name", new Pair<>("15551256344", "name value for 15551256344")),
new Parameters.Entry(
"an identifier",
new Pair<>("15551231234", "an identifier value for 15551231234")),
new Parameters.Entry(
"an identifier",
new Pair<>("15551256344", "an identifier value for 15551256344"))));

public static final BatchMedia batchMedia =
BatchMedia.builder()
.setId(id)
.setTo(to)
Expand All @@ -117,7 +116,7 @@ public class BatchesServiceTest extends BaseTest {
.setBody(
new MediaBody(
"https://en.wikipedia.org/wiki/Sinch_(company)#/media/File:Sinch_LockUp_RGB.png",
"Media message from Sinch!"))
"Hi ${name} ({an identifier}) ! How are you?"))
.setCreatedAt(Instant.parse("2019-08-24T14:14:22Z"))
.setModifiedAt(Instant.parse("2019-08-24T14:15:22Z"))
.setDeliveryReport(DeliveryReportType.SUMMARY)
Expand All @@ -128,7 +127,7 @@ public class BatchesServiceTest extends BaseTest {
.setFeedbackEnabled(feedbackEnabled)
.setParameters(parameters)
.build();
public final BatchText batchText =
public static final BatchText batchText =
BatchText.builder()
.setId(id)
.setTo(to)
Expand Down Expand Up @@ -177,7 +176,7 @@ public class BatchesServiceTest extends BaseTest {
.setBody(
new MediaBody(
"https://en.wikipedia.org/wiki/Sinch_(company)#/media/File:Sinch_LockUp_RGB.png",
"Media message from Sinch!"))
"Hi ${name} ({an identifier}) ! How are you?"))
.setDeliveryReport(DeliveryReportType.SUMMARY)
.setSendAt(Instant.parse("2019-08-24T14:16:22Z"))
.setExpireAt(Instant.parse("2019-08-24T14:17:22Z"))
Expand Down Expand Up @@ -225,7 +224,7 @@ public class BatchesServiceTest extends BaseTest {
.setBody(
new MediaBody(
"https://en.wikipedia.org/wiki/Sinch_(company)#/media/File:Sinch_LockUp_RGB.png",
"Media message from Sinch!"))
body))
.setDeliveryReport(DeliveryReportType.SUMMARY)
.setSendAt(Instant.parse("2019-08-24T14:16:22Z"))
.setExpireAt(Instant.parse("2019-08-24T14:17:22Z"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.adelean.inject.resources.junit.jupiter.GivenJsonResource;
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
import com.sinch.sdk.BaseTest;
import com.sinch.sdk.core.utils.Pair;
import com.sinch.sdk.core.TestHelpers;
import com.sinch.sdk.domains.sms.adapters.BatchesServiceTest;
import com.sinch.sdk.domains.sms.models.Batch;
import com.sinch.sdk.domains.sms.models.BatchBinary;
import com.sinch.sdk.domains.sms.models.BatchMedia;
import com.sinch.sdk.domains.sms.models.BatchText;
import com.sinch.sdk.domains.sms.models.Parameters;
import com.sinch.sdk.domains.sms.models.dto.v1.BinaryResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.MediaResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjDto;
import com.sinch.sdk.domains.sms.models.dto.v1.ParameterObjParameterKeyDto;
import com.sinch.sdk.domains.sms.models.dto.v1.SendSMS201ResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.SendSMSRequestDto;
import com.sinch.sdk.domains.sms.models.dto.v1.TextResponseDto;
import com.sinch.sdk.domains.sms.models.dto.v1.UpdateBatchMessageRequestDto;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -113,7 +106,7 @@ private static void compareMedia(BatchMedia client, MediaResponseDto dto) {
Assertions.assertEquals(dto.getDeliveryReport(), client.getDeliveryReport().value());
assertEquals(dto.getSendAt().toInstant(), client.getSendAt());
assertEquals(dto.getExpireAt().toInstant(), client.getExpireAt());
compareParameters(client.getParameters(), dto.getParameters());
TestHelpers.recursiveEquals(client.getParameters(), dto.getParameters());
assertEquals(dto.getCallbackUrl(), client.getCallbackUrl());
assertEquals(dto.getClientReference(), client.getClientReference());
assertEquals(dto.getFeedbackEnabled(), client.isFeedbackEnabled());
Expand All @@ -136,46 +129,28 @@ private static void compareText(BatchText client, TextResponseDto dto) {
assertEquals(dto.getFeedbackEnabled(), client.isFeedbackEnabled());
assertEquals(dto.getFlashMessage(), client.isFlashMessage());
assertEquals(dto.getTruncateConcat(), client.isTruncateConcat());
compareParameters(client.getParameters(), dto.getParameters());
TestHelpers.recursiveEquals(client.getParameters(), dto.getParameters());
assertEquals(dto.getMaxNumberOfMessageParts(), client.getMaxNumberOfMessageParts());
assertEquals(dto.getFromTon(), client.getFromTon());
assertEquals(dto.getFromNpi(), client.getFromNpi());
}

private static void compareParameters(Parameters client, ParameterObjDto dto) {
assertEquals(dto.size(), client.size());
Collection<Parameters.Entry> values = client.values();
values.forEach(
e -> {
Pair<String, String> clientItem = e.getValue();
assertTrue(dto.containsKey(e.getKey()));
@SuppressWarnings("unchecked")
Map<String, String> dtoItem = (Map<String, String>) dto.get(e.getKey());
assertTrue(dtoItem.containsKey(clientItem.getLeft()));
assertEquals(dtoItem.get(clientItem.getLeft()), clientItem.getRight());
Optional<String> defaultValue = e.getDefaultValue();
if (defaultValue.isPresent()) {
assertEquals(
dtoItem.get(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT), defaultValue.get());
} else {
assertNull(dtoItem.get(ParameterObjParameterKeyDto.JSON_PROPERTY_DEFAULT));
}
});
}

@Test
void convertBinaryResponse() {
compareWithDto(BatchDtoConverter.convert(binaryResponseDto), binaryResponseDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(binaryResponseDto), BatchesServiceTest.batchBinary);
}

@Test
void convertMediaResponse() {
compareWithDto(BatchDtoConverter.convert(mediaResponseDto), mediaResponseDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(mediaResponseDto), BatchesServiceTest.batchMedia);
}

@Test
void convertTextResponse() {
compareWithDto(BatchDtoConverter.convert(textResponseDto), textResponseDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(textResponseDto), BatchesServiceTest.batchText);
}

@Test
Expand All @@ -187,7 +162,7 @@ void convertSendBinaryRequest() {
}

@Test
void convertSendtediaRequest() {
void convertSendMediaRequest() {
org.assertj.core.api.Assertions.assertThat(
BatchDtoConverter.convert(BatchesServiceTest.sendSmsBatchMediaRequest))
.usingRecursiveComparison()
Expand All @@ -204,26 +179,23 @@ void convertSendTextRequest() {

@Test
void convertUpdateTextRequest() {
org.assertj.core.api.Assertions.assertThat(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchTextRequest))
.usingRecursiveComparison()
.isEqualTo(updateTextRequestDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchTextRequest),
updateTextRequestDto);
}

@Test
void convertUpdateMediaRequest() {
org.assertj.core.api.Assertions.assertThat(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchMediaRequest))
.usingRecursiveComparison()
.isEqualTo(updateMediaRequestDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchMediaRequest),
updateMediaRequestDto);
}

@Test
void convertUpdateBinaryRequest() {
org.assertj.core.api.Assertions.assertThat(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchBinaryRequest))
.usingRecursiveComparison()
.isEqualTo(updateBinaryRequestDto);
TestHelpers.recursiveEquals(
BatchDtoConverter.convert(BatchesServiceTest.updateSmsBatchBinaryRequest),
updateBinaryRequestDto);
}

@Test
Expand Down
Loading
Loading