Commit 80c0872e authored by Yauheni Lesnikau's avatar Yauheni Lesnikau
Browse files

add methods to JsonUtils to override nested string properties

parent cf34f938
Pipeline #99087 passed with stages
in 4 minutes and 45 seconds
......@@ -152,6 +152,23 @@ public class JsonUtils {
.ifPresent(json -> json.addProperty(nestedNames[nestedNames.length - 1], value.get(0)));
}
public static void overrideNestedStringPropertyOfJsonObject(String propertyName, List<String> value, JsonObject jsonObject) {
String[] nestedNames = splitJsonPropertiesByDots(propertyName);
if (nestedNames[0].endsWith(PN_END) && isNestedArrayElementHomogeneous(nestedNames[0])) {
overrideNestedStringPropertyOfJsonObject(nestedNames, value, jsonObject);
return;
} else if (nestedNames[0].endsWith(PN_END)) {
overrideOneNestedStringPorpertyOfJsonObject(nestedNames, value, jsonObject);
return;
}
JsonObject targetJsonObject = buildNewJsonObject(nestedNames, jsonObject);
ofNullable(targetJsonObject)
.ifPresent(json -> json.addProperty(nestedNames[nestedNames.length - 1], value.get(0)));
}
private static void overrideOneNestedNumberPorpertyOfJsonObject(String[] nestedNames, List<Number> values, JsonObject jsonObject) {
String[] innerNestedNames = getInnerNestedPropertyNames(nestedNames);
JsonArray elementArray = jsonObject.getAsJsonArray(getNestedJsonArrayName(nestedNames[0])) ;
......@@ -169,6 +186,23 @@ public class JsonUtils {
}
}
private static void overrideOneNestedStringPorpertyOfJsonObject(String[] nestedNames, List<String> values, JsonObject jsonObject) {
String[] innerNestedNames = getInnerNestedPropertyNames(nestedNames);
JsonArray elementArray = jsonObject.getAsJsonArray(getNestedJsonArrayName(nestedNames[0])) ;
int elementIndex = getNestedArrayElementIndex(nestedNames[0]);
if (elementIndex < 0 || elementIndex >= elementArray.size()) {
return;
}
JsonObject element = elementArray.get(elementIndex).getAsJsonObject();
JsonObject targetJsonObject = buildNewJsonObject(innerNestedNames, element);
if (targetJsonObject != null) {
targetJsonObject.addProperty(innerNestedNames[innerNestedNames.length - 1], values.get(0));
}
}
private static void overrideNestedNumberPropertyOfJsonObject(String[] nestedNames, List<Number> values, JsonObject jsonObject) {
JsonArray elementArray = jsonObject.getAsJsonArray(getNestedJsonArrayName(nestedNames[0])) ;
String[] innerNestedNames = getInnerNestedPropertyNames(nestedNames);
......@@ -185,6 +219,22 @@ public class JsonUtils {
}
private static void overrideNestedStringPropertyOfJsonObject(String[] nestedNames, List<String> values, JsonObject jsonObject) {
JsonArray elementArray = jsonObject.getAsJsonArray(getNestedJsonArrayName(nestedNames[0])) ;
String[] innerNestedNames = getInnerNestedPropertyNames(nestedNames);
for (int i = 0; i < elementArray.size(); i++) {
JsonObject element = elementArray.get(i).getAsJsonObject();
JsonObject targetJsonObject = buildNewJsonObject(innerNestedNames, element);
if (targetJsonObject != null) {
targetJsonObject.addProperty(innerNestedNames[innerNestedNames.length - 1], values.get(i));
}
}
}
/**
* @param propertyName - property name with path split by dots e.g. depth.value
* @param value - the value of the property with String type
......
......@@ -23,6 +23,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
......@@ -385,6 +387,83 @@ public class JsonUtilsTest {
verify(mockJsonObject, never()).addProperty(anyString(), any(Number.class));
}
// ---- JsonUtils.overrideNestedStringPropertyOfJsonObject tests ----
@Test
public void overrideNestedStringPropertyOfJsonObject_succeed_whenJsonObjectPresentedInPath() {
String propertyName = "depth.value";
JsonObject internalJsonObject = mock(JsonObject.class);
String value = "testValue";
List<String> values = Collections.singletonList(value);
setupMocksForJsonPropertyTests(internalJsonObject);
overrideNestedStringPropertyOfJsonObject(propertyName, values, mockJsonObject);
verify(mockJsonObject, times(1)).get("depth");
verify(internalJsonObject, times(1)).addProperty("value", value);
}
@Test
public void overrideNestedStringPropertyOfJsonObject_notHappened_whenJsonObjectContainsNonJsonObjectElement() {
String propertyName = "depth.value";
JsonObject internalJsonObject = mock(JsonObject.class);
String value = "testValue";
List<String> values = Collections.singletonList(value);
when(mockJsonObject.get("depth")).thenReturn(mockJsonPrimitive);
when(mockJsonPrimitive.isJsonObject()).thenReturn(false);
overrideNestedStringPropertyOfJsonObject(propertyName, values, mockJsonObject);
verify(mockJsonObject, times(1)).get("depth");
verify(internalJsonObject, never()).addProperty(anyString(), any(Number.class));
verify(mockJsonObject, never()).addProperty(anyString(), any(Number.class));
}
@Test
public void overrideNestedStringPropertyOfJsonObject_succeed_whenJsonObjectPresentedInNestedArray() {
String propertyName = "markers[].value";
String value1 = "value1";
String value2 = "value2";
String value3 = "value3";
List<String> values = Arrays.asList(value1, value2, value3);
setupJsonArrayMock(3);
when(mockJsonObject.getAsJsonArray("markers")).thenReturn(mockJsonArray);
overrideNestedStringPropertyOfJsonObject(propertyName, values, mockJsonObject);
verify(mockJsonObject, times(1)).addProperty("value", value1);
verify(mockJsonObject, times(1)).addProperty("value", value2);
verify(mockJsonObject, times(1)).addProperty("value", value3);
}
@Test
public void overrideOneNestedStringPropertyOfJsonObject_succeed_whenJsonObjectPresentedInNestedArray() {
String propertyName = "markers[1].value";
String value = "testValue";
List<String> values = Collections.singletonList(value);
setupJsonArrayMock(3);
when(mockJsonObject.getAsJsonArray("markers")).thenReturn(mockJsonArray);
overrideNestedStringPropertyOfJsonObject(propertyName, values, mockJsonObject);
verify(mockJsonObject, times(1)).addProperty("value", value);
}
@Test
public void overrideNestedStringPropertyOfJsonObject_notHappened_whenJsonObjectIndexOutOfBoundary() {
String propertyName = "markers[1].value";
String value = "testValue";
List<String> values = Collections.singletonList(value);
setupJsonArrayMock(1);
when(mockJsonObject.getAsJsonArray("markers")).thenReturn(mockJsonArray);
overrideNestedStringPropertyOfJsonObject(propertyName, values, mockJsonObject);
verify(mockJsonObject, never()).addProperty(anyString(), any(Number.class));
}
// ---- JsonUtils.splitJsonPropertiesByDots tests ----
@Test
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment