Skip to content

Commit

Permalink
feat: deserialize methods with defaultValue parameter (#159)
Browse files Browse the repository at this point in the history
Refs: #158
  • Loading branch information
grigoriev authored Sep 9, 2024
1 parent e893be8 commit 91e4c61
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import lombok.Data;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Optional;

@Data
@Schema(description = "Settings model")
public abstract class SettingsModel {
Expand Down Expand Up @@ -66,6 +70,11 @@ public void deserialize(String serializedString) {
deserializeModelData(serializedString);
}

protected String deserializeEntry(String entryName, String serializedString, String defaultValue) {
String value = deserializeEntry(entryName, serializedString);
return Optional.ofNullable(value).orElse(defaultValue);
}

protected String deserializeEntry(String entryName, String serializedString) {
if (serializedString == null) {
return null;
Expand All @@ -83,6 +92,11 @@ protected String deserializeEntry(String entryName, String serializedString) {
return null;
}

protected <T> T deserializeEntry(String entryName, String serializedString, Class<T> aClass, T defaultValue) {
T value = deserializeEntry(entryName, serializedString, aClass);
return Optional.ofNullable(value).orElse(defaultValue);
}

@SuppressWarnings("unchecked")
protected <T> T deserializeEntry(String entryName, String serializedString, Class<T> aClass) {
String deserializedEntry = deserializeEntry(entryName, serializedString);
Expand All @@ -101,6 +115,13 @@ protected <T> T deserializeEntry(String entryName, String serializedString, Clas
}
}

@SuppressWarnings("unchecked")
protected <T> List<T> deserializeListEntry(String entryName, String serializedString, Class<T> aClass) {
Class<T[]> arrayClass = (Class<T[]>) Array.newInstance(aClass, 0).getClass();
T[] array = deserializeEntry(entryName, serializedString, arrayClass);
return array == null ? List.of() : List.of(array);
}

protected abstract String serializeModelData();

protected abstract void deserializeModelData(String serializedString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

public class SettingsModelDeserializationTest {
public static final String TEST_ARRAY = "TEST_ARRAY";
public static final String TEST_LIST = "TEST_LIST";
public static final String TEST_MAP = "TEST_MAP";
public static final String TEST_OBJECT = "TEST_OBJECT";
public static final String TEST_STRING = "TEST_STRING";
Expand All @@ -26,6 +29,96 @@ void testDeserializeMap() {
Assertions.assertEquals("value2", deserializedMap.get("key2"));
}

@Test
void testDeserializeArrayOfStrings() {
String serializedArray = "-----BEGIN TEST_ARRAY-----" + System.lineSeparator() +
"[ \"value1\", \"value2\" ]" + System.lineSeparator() +
"-----END TEST_ARRAY-----" + System.lineSeparator();
String[] deserializedArray = model.deserializeEntry(TEST_ARRAY, serializedArray, String[].class);
Assertions.assertEquals(2, deserializedArray.length);
Assertions.assertEquals("value1", deserializedArray[0]);
Assertions.assertEquals("value2", deserializedArray[1]);
}

@Test
void testDeserializeArrayOfInteger() {
String serializedArray = "-----BEGIN TEST_ARRAY-----" + System.lineSeparator() +
"[ 1, 2 ]" + System.lineSeparator() +
"-----END TEST_ARRAY-----" + System.lineSeparator();
Integer[] deserializedArray = model.deserializeEntry(TEST_ARRAY, serializedArray, Integer[].class);
Assertions.assertEquals(2, deserializedArray.length);
Assertions.assertEquals(1, deserializedArray[0]);
Assertions.assertEquals(2, deserializedArray[1]);
}

@Test
void testDeserializeArrayOfObjects() {
String serializedList = "-----BEGIN TEST_LIST-----" + System.lineSeparator() +
"[ " + System.lineSeparator() +
"{\"testKey\":\"testKey1\",\"testValue\":1}," + System.lineSeparator() +
"{\"testKey\":\"testKey2\",\"testValue\":2}," + System.lineSeparator() +
"{\"testKey\":\"testKey3\",\"testValue\":3}" + System.lineSeparator() +
"]" + System.lineSeparator() +
"-----END TEST_LIST-----" + System.lineSeparator();
TestObject[] deserializedArray = model.deserializeEntry(TEST_LIST, serializedList, TestObject[].class);
Assertions.assertEquals(3, deserializedArray.length);
Assertions.assertEquals("testKey1", deserializedArray[0].getTestKey());
Assertions.assertEquals(1, deserializedArray[0].getTestValue());
Assertions.assertEquals("testKey2", deserializedArray[1].getTestKey());
Assertions.assertEquals(2, deserializedArray[1].getTestValue());
Assertions.assertEquals("testKey3", deserializedArray[2].getTestKey());
Assertions.assertEquals(3, deserializedArray[2].getTestValue());
}


@Test
void testDeserializeListOfStrings() {
String serializedList = "-----BEGIN TEST_LIST-----" + System.lineSeparator() +
"[ \"value1\", \"value2\" ]" + System.lineSeparator() +
"-----END TEST_LIST-----" + System.lineSeparator();
List<String> deserializedList = model.deserializeListEntry(TEST_LIST, serializedList, String.class);
Assertions.assertInstanceOf(List.class, deserializedList);
Assertions.assertEquals(2, deserializedList.size());
Assertions.assertEquals("value1", deserializedList.get(0));
Assertions.assertEquals("value2", deserializedList.get(1));
}

@Test
void testDeserializeListOfIntegers() {
String serializedList = "-----BEGIN TEST_LIST-----" + System.lineSeparator() +
"[ 1, 2, 3 ]" + System.lineSeparator() +
"-----END TEST_LIST-----" + System.lineSeparator();
List<Integer> deserializedList = model.deserializeListEntry(TEST_LIST, serializedList, Integer.class);
Assertions.assertInstanceOf(List.class, deserializedList);
Assertions.assertEquals(3, deserializedList.size());
Assertions.assertEquals(1, deserializedList.get(0));
Assertions.assertEquals(2, deserializedList.get(1));
Assertions.assertEquals(3, deserializedList.get(2));
}

@Test
void testDeserializeListOfObjects() {
String serializedList = "-----BEGIN TEST_LIST-----" + System.lineSeparator() +
"[ " + System.lineSeparator() +
"{\"testKey\":\"testKey1\",\"testValue\":1}," + System.lineSeparator() +
"{\"testKey\":\"testKey2\",\"testValue\":2}," + System.lineSeparator() +
"{\"testKey\":\"testKey3\",\"testValue\":3}" + System.lineSeparator() +
"]" + System.lineSeparator() +
"-----END TEST_LIST-----" + System.lineSeparator();
List<TestObject> deserializedList = model.deserializeListEntry(TEST_LIST, serializedList, TestObject.class);
Assertions.assertInstanceOf(List.class, deserializedList);
for (TestObject testObject : deserializedList) {
Assertions.assertInstanceOf(TestObject.class, testObject);
}
Assertions.assertEquals(3, deserializedList.size());
Assertions.assertEquals("testKey1", deserializedList.get(0).getTestKey());
Assertions.assertEquals(1, deserializedList.get(0).getTestValue());
Assertions.assertEquals("testKey2", deserializedList.get(1).getTestKey());
Assertions.assertEquals(2, deserializedList.get(1).getTestValue());
Assertions.assertEquals("testKey3", deserializedList.get(2).getTestKey());
Assertions.assertEquals(3, deserializedList.get(2).getTestValue());
}

@Test
void testDeserializeObject() {
String serializedObject = "-----BEGIN TEST_OBJECT-----" + System.lineSeparator() +
Expand Down

0 comments on commit 91e4c61

Please sign in to comment.