From 2f468e4472e56ec56cb9823d188a564749b93457 Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 15 Feb 2019 15:29:54 +0100 Subject: [PATCH] Remove remaining Jackson Remove jackson import as jackson shoul dbe removed Issue-ID: CLAMP-292 Change-Id: I43b3bd2e2f217293ae8ac262b013b2287d1e2b16 Signed-off-by: sebdet --- .../java/org/onap/clamp/clds/util/JsonUtils.java | 44 +++++++++++----------- .../org/onap/clamp/clds/util/JsonUtilsTest.java | 33 ++++++++-------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java index 96ddc292..f59864ac 100644 --- a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java @@ -31,6 +31,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; + import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -38,12 +39,13 @@ import java.util.Spliterator; import java.util.Spliterators; import java.util.stream.Collectors; import java.util.stream.StreamSupport; + import org.onap.clamp.clds.model.properties.AbstractModelElement; import org.onap.clamp.clds.service.SecureServicePermission; import org.onap.clamp.clds.service.SecureServicePermissionDeserializer; /** - * This class is used to access the jackson with restricted type access. + * This class is used to access the GSON with restricted type access. */ public class JsonUtils { @@ -52,19 +54,17 @@ public class JsonUtils { private static final String LOG_ELEMENT_NOT_FOUND_IN_JSON = "Value '{}' for key 'name' not found in JSON {}"; public static final Gson GSON = new GsonBuilder() - .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()) - .create(); + .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()).create(); private JsonUtils() { } - /** - * Return the value field of the json node element that has a name field equals to the given name. + * Return the value field of the json node element that has a name field equals + * to the given name. */ public static String getStringValueByName(JsonElement jsonElement, String name) { - String value = extractJsonValueFromElement(jsonElement, name) - .map(JsonUtils::extractStringValueFromElement) + String value = extractJsonValueFromElement(jsonElement, name).map(JsonUtils::extractStringValueFromElement) .orElse(null); if (value == null) { if (logger.isDebugEnabled()) { @@ -77,7 +77,8 @@ public class JsonUtils { } /** - * Return an array of values for the field of the json node element that has a name field equals to the given name. + * Return an array of values for the field of the json node element that has a + * name field equals to the given name. */ public static List getStringValuesByName(JsonElement jsonElement, String name) { List values = extractJsonValueFromElement(jsonElement, name) @@ -93,21 +94,22 @@ public class JsonUtils { } /** - * Return the int value field of the json node element that has a name field equals to the given name. + * Return the int value field of the json node element that has a name field + * equals to the given name. */ public static Integer getIntValueByName(JsonElement element, String name) { String value = getStringValueByName(element, name); return Integer.valueOf(value); } - /** - * Return the Json value field of the json node element that has a name field equals to the given name. + * Return the Json value field of the json node element that has a name field + * equals to the given name. */ public static JsonObject getJsonObjectByName(JsonElement jsonElement, String name) { JsonObject jsonObject = extractJsonValueFromElement(jsonElement, name).map(JsonElement::getAsJsonObject) .orElse(null); - if (jsonObject == null) { + if (jsonObject == null) { logger.warn(LOG_ELEMENT_NOT_FOUND, name); } else { logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, jsonElement.toString()); @@ -116,7 +118,7 @@ public class JsonUtils { } private static Optional extractJsonValueFromElement(JsonElement jsonElement, String name) { - if(jsonElement != null ){ + if (jsonElement != null) { if (jsonElement.isJsonArray()) { return extractValueJsonFromArray(jsonElement, name); } else if (hasMatchingParameterName(name, jsonElement)) { @@ -136,8 +138,7 @@ public class JsonUtils { } private static boolean hasMatchingParameterName(String name, JsonElement element) { - return element.isJsonObject() - && element.getAsJsonObject().has("name") + return element.isJsonObject() && element.getAsJsonObject().has("name") && name.equals(element.getAsJsonObject().get("name").getAsString()); } @@ -153,14 +154,11 @@ public class JsonUtils { private static List extractStringValuesFromElement(JsonElement element) { if (element.isJsonArray()) { - return StreamSupport.stream( - Spliterators.spliteratorUnknownSize(element.getAsJsonArray().iterator(), Spliterator.ORDERED), - false) - .filter(JsonElement::isJsonPrimitive) - .map(JsonElement::getAsJsonPrimitive) - .filter(JsonPrimitive::isString) - .map(JsonPrimitive::getAsString) - .collect(Collectors.toList()); + return StreamSupport + .stream(Spliterators.spliteratorUnknownSize(element.getAsJsonArray().iterator(), Spliterator.ORDERED), + false) + .filter(JsonElement::isJsonPrimitive).map(JsonElement::getAsJsonPrimitive) + .filter(JsonPrimitive::isString).map(JsonPrimitive::getAsString).collect(Collectors.toList()); } else { String value = extractStringValueFromElement(element); return Lists.newArrayList(value); diff --git a/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java b/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java index 3e11b8a2..82c2162a 100644 --- a/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java +++ b/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java @@ -24,15 +24,17 @@ package org.onap.clamp.clds.util; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import java.io.IOException; +import java.io.IOException; import java.util.List; + import org.junit.Test; public class JsonUtilsTest { @@ -78,7 +80,8 @@ public class JsonUtilsTest { } /** - * This method test that the security hole in Jackson is not enabled in the default ObjectMapper. + * This method test that the security hole in GSON is not enabled in the default + * ObjectMapper. */ @Test public void testCreateBeanDeserializer() { @@ -91,48 +94,47 @@ public class JsonUtilsTest { assertFalse(testObject instanceof TestObject); } - @Test public void shouldReturnJsonValueByName() throws IOException { - //given + // given String modelProperties = ResourceFileUtil .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); - //when + // when String locationName = JsonUtils.getStringValueByName(globalElement, "location"); String timeoutValue = JsonUtils.getStringValueByName(globalElement, "timeout"); - //then + // then assertThat(locationName).isEqualTo("SNDGCA64"); assertThat(timeoutValue).isEqualTo("500"); } @Test public void shouldReturnJsonObjectByPropertyName() throws IOException { - //given + // given String modelProperties = ResourceFileUtil .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); - //when + // when JsonObject deployParameters = JsonUtils.getJsonObjectByName(globalElement, "deployParameters"); - //then + // then assertThat(deployParameters).isEqualToComparingFieldByField(DEPLOY_PARAMETERS); } @Test public void shouldReturnJsonValuesByPropertyName() throws IOException { - //given + // given String modelProperties = ResourceFileUtil .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); - //when + // when List vfs = JsonUtils.getStringValuesByName(globalElement, "vf"); - //then + // then assertThat(vfs).containsExactly( "6c7aaec2-59eb-41d9-8681-b7f976ab668d", "8sadsad0-a98s-6a7s-fd12-sadji9sa8d12", @@ -140,18 +142,17 @@ public class JsonUtilsTest { ); } - @Test public void shouldReturnJsonValueAsInteger() throws IOException { - //given + // given String modelProperties = ResourceFileUtil .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); - //when + // when Integer timeoutValue = JsonUtils.getIntValueByName(globalElement, "timeout"); - //then + // then assertThat(timeoutValue).isEqualTo(500); } } -- 2.16.6