From 50368411fba4fca7efdc5341f9d485ff821eee2c Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Fri, 8 Mar 2019 00:23:11 -0500 Subject: only convert object type json when String only convert object type json when input is a String Change-Id: I465dfe905f5d85de583b6232640dc4dc13a09c7a Issue-ID: SO-1610 Signed-off-by: Benjamin, Max (mb388a) --- .../org/onap/so/cloudify/utils/MsoCloudifyUtils.java | 6 +++++- .../org/onap/so/openstack/utils/MsoHeatUtils.java | 20 +++++++++++++------- .../onap/so/cloudify/utils/MsoCloudifyUtilsTest.java | 13 +++++++++++-- .../so/openstack/utils/MsoHeatUtilsUnitTest.java | 16 ++++++++++++++-- 4 files changed, 43 insertions(+), 12 deletions(-) (limited to 'adapters/mso-adapter-utils/src') diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java index f50a6fd1bd..97e1627fd3 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java @@ -1047,7 +1047,11 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ } } else if (type.equalsIgnoreCase("json")) { try { - return JSON_MAPPER.writeValueAsString(inputValue); + if (inputValue instanceof String) { + return JSON_MAPPER.readTree(inputValue.toString()); + } + //will already marshal to json without intervention + return inputValue; } catch (Exception e) { logger.debug("Unable to convert {} to a JsonNode!", inputValue); diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java index a0fbd485d1..012c560689 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoHeatUtils.java @@ -38,6 +38,8 @@ import com.woorea.openstack.keystone.Keystone; import com.woorea.openstack.keystone.model.Access; import com.woorea.openstack.keystone.model.Authentication; import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -1356,18 +1358,22 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ } } else if ("json".equalsIgnoreCase(type)) { Object jsonObj = inputs.get(key); - String jsonString; + Object json; try { - jsonString = JSON_MAPPER.writeValueAsString(jsonObj); - } catch (JsonProcessingException e) { + if (jsonObj instanceof String) { + json = JSON_MAPPER.readTree(jsonObj.toString()); + } else { + //will already marshal to json without intervention + json = jsonObj; + } + } catch (IOException e) { logger.error("failed to map to json, directly converting to string instead", e); - jsonString = jsonObj.toString(); + json = jsonObj.toString(); } if (alias) - newInputs.put(realName, jsonString); + newInputs.put(realName, json); else - newInputs.put(key, jsonString); - //} + newInputs.put(key, json); } else if ("comma_delimited_list".equalsIgnoreCase(type)) { String commaSeparated = inputs.get(key).toString(); try { diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest.java index f63aab7ce1..833b4ab760 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/cloudify/utils/MsoCloudifyUtilsTest.java @@ -24,6 +24,7 @@ package org.onap.so.cloudify.utils; import static com.shazam.shazamcrest.MatcherAssert.assertThat; import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -70,6 +71,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class MsoCloudifyUtilsTest { @@ -305,11 +307,18 @@ public class MsoCloudifyUtilsTest { paramJson.setParamType("json"); paramJson.setParamName("my-json"); - Map jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference>(){}); + HeatTemplateParam paramJsonEscaped = new HeatTemplateParam(); + paramJsonEscaped.setParamType("json"); + paramJsonEscaped.setParamName("my-json-escaped"); + Map jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference>(){}); + assertEquals(3, utils.convertInputValue("3", paramNum)); assertEquals("hello", utils.convertInputValue("hello", paramString)); - JSONAssert.assertEquals(getJson("free-form.json"), utils.convertInputValue(jsonMap, paramJson).toString(), false); + assertTrue("expect no change in type", utils.convertInputValue(jsonMap, paramJson) instanceof Map); + assertTrue("expect string to become jsonNode", utils.convertInputValue(getJson("free-form.json"), paramJsonEscaped) instanceof JsonNode); + + JSONAssert.assertEquals(getJson("free-form.json"), mapper.writeValueAsString(utils.convertInputValue(getJson("free-form.json"), paramJsonEscaped)), false); } diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsUnitTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsUnitTest.java index 40bbe90218..66d6f59e79 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsUnitTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsUnitTest.java @@ -1,6 +1,7 @@ package org.onap.so.openstack.utils; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.nio.file.Files; @@ -17,6 +18,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class MsoHeatUtilsUnitTest { @@ -45,18 +47,28 @@ public class MsoHeatUtilsUnitTest { paramJson.setParamType("json"); paramJson.setParamName("my-json"); + HeatTemplateParam paramJsonEscaped = new HeatTemplateParam(); + paramJsonEscaped.setParamType("json"); + paramJsonEscaped.setParamName("my-json-escaped"); + Map jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference>(){}); input.put("my-json", jsonMap); + input.put("my-json-escaped", getJson("free-form.json")); + parameters.add(paramNum); parameters.add(paramString); parameters.add(paramJson); - + parameters.add(paramJsonEscaped); + Map output = utils.convertInputMap(input, template); assertEquals(3, output.get("my-number")); assertEquals("hello", output.get("my-string")); - JSONAssert.assertEquals(getJson("free-form.json"), (String)output.get("my-json"), false); + assertTrue("expect no change in type", output.get("my-json") instanceof Map); + assertTrue("expect string to become jsonNode", output.get("my-json-escaped") instanceof JsonNode); + + JSONAssert.assertEquals(getJson("free-form.json"), mapper.writeValueAsString(output.get("my-json-escaped")), false); } -- cgit 1.2.3-korg