diff --git a/app/src/main/java/ch/sbb/polarion/extension/generic/settings/SettingsModel.java b/app/src/main/java/ch/sbb/polarion/extension/generic/settings/SettingsModel.java index 137dc15..1c47d9b 100644 --- a/app/src/main/java/ch/sbb/polarion/extension/generic/settings/SettingsModel.java +++ b/app/src/main/java/ch/sbb/polarion/extension/generic/settings/SettingsModel.java @@ -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 { @@ -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; @@ -83,6 +92,11 @@ protected String deserializeEntry(String entryName, String serializedString) { return null; } + protected T deserializeEntry(String entryName, String serializedString, Class aClass, T defaultValue) { + T value = deserializeEntry(entryName, serializedString, aClass); + return Optional.ofNullable(value).orElse(defaultValue); + } + @SuppressWarnings("unchecked") protected T deserializeEntry(String entryName, String serializedString, Class aClass) { String deserializedEntry = deserializeEntry(entryName, serializedString); @@ -101,6 +115,13 @@ protected T deserializeEntry(String entryName, String serializedString, Clas } } + @SuppressWarnings("unchecked") + protected List deserializeListEntry(String entryName, String serializedString, Class aClass) { + Class arrayClass = (Class) 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); diff --git a/app/src/test/java/ch/sbb/polarion/extension/generic/settings/SettingsModelDeserializationTest.java b/app/src/test/java/ch/sbb/polarion/extension/generic/settings/SettingsModelDeserializationTest.java index 53fbf2b..93aa835 100644 --- a/app/src/test/java/ch/sbb/polarion/extension/generic/settings/SettingsModelDeserializationTest.java +++ b/app/src/test/java/ch/sbb/polarion/extension/generic/settings/SettingsModelDeserializationTest.java @@ -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"; @@ -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 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 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 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() +