diff options
Diffstat (limited to 'adapters/mso-adapter-utils/src')
5 files changed, 150 insertions, 35 deletions
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 9212fbcc6c..f50a6fd1bd 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 @@ -1032,14 +1032,14 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ * Normalize an input value to an Object, based on the target parameter type. * If the type is not recognized, it will just be returned unchanged (as a string). */ - public Object convertInputValue (String inputValue, HeatTemplateParam templateParam) + public Object convertInputValue (Object inputValue, HeatTemplateParam templateParam) { String type = templateParam.getParamType(); logger.debug("Parameter: {} is of type {}", templateParam.getParamName(), type); if (type.equalsIgnoreCase("number")) { try { - return Integer.valueOf(inputValue); + return Integer.valueOf(inputValue.toString()); } catch (Exception e) { logger.debug("Unable to convert {} to an integer!", inputValue); @@ -1047,14 +1047,14 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ } } else if (type.equalsIgnoreCase("json")) { try { - return new ObjectMapper().readTree(inputValue); + return JSON_MAPPER.writeValueAsString(inputValue); } catch (Exception e) { logger.debug("Unable to convert {} to a JsonNode!", inputValue); return null; } } else if (type.equalsIgnoreCase("boolean")) { - return new Boolean(inputValue); + return new Boolean(inputValue.toString()); } // Nothing else matched. Return the original string 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 efc232e79e..ee1c79c09c 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 @@ -86,6 +86,24 @@ import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.woorea.openstack.base.client.OpenStackConnectException; +import com.woorea.openstack.base.client.OpenStackRequest; +import com.woorea.openstack.base.client.OpenStackResponseException; +import com.woorea.openstack.heat.Heat; +import com.woorea.openstack.heat.model.CreateStackParam; +import com.woorea.openstack.heat.model.Resources; +import com.woorea.openstack.heat.model.Stack; +import com.woorea.openstack.heat.model.Stack.Output; +import com.woorea.openstack.heat.model.Stacks; +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; + @Primary @Component public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ @@ -1005,7 +1023,7 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ return heatStack.getOutputs(); } - public void copyStringOutputsToInputs(Map<String, String> inputs, + public void copyStringOutputsToInputs(Map<String, Object> inputs, Map<String, Object> otherStackOutputs, boolean overWrite) { if (inputs == null || otherStackOutputs == null) return; @@ -1245,7 +1263,7 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ * (heat variable type) -> java Object type * string -> String * number -> Integer - * json -> JsonNode XXX Removed with MSO-1475 / 1802 + * json -> marshal object to json * comma_delimited_list -> ArrayList * boolean -> Boolean * if any of the conversions should fail, we will default to adding it to the inputs @@ -1256,7 +1274,7 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ * @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template * @return HashMap<String, Object> of the inputs, cleaned and converted */ - public Map<String, Object> convertInputMap(Map<String, String> inputs, HeatTemplate template) { + public Map<String, Object> convertInputMap(Map<String, Object> inputs, HeatTemplate template) { HashMap<String, Object> newInputs = new HashMap<>(); HashMap<String, HeatTemplateParam> params = new HashMap<>(); HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>(); @@ -1309,13 +1327,13 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ logger.debug("Parameter: {} is of type {}", key, type); if ("string".equalsIgnoreCase(type)) { // Easiest! - String str = inputs.get(key); + String str = inputs.get(key).toString(); if (alias) newInputs.put(realName, str); else newInputs.put(key, str); } else if ("number".equalsIgnoreCase(type)) { - String integerString = inputs.get(key); + String integerString = inputs.get(key).toString(); Integer anInteger = null; try { anInteger = Integer.parseInt(integerString); @@ -1336,16 +1354,21 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ newInputs.put(key, integerString); } } else if ("json".equalsIgnoreCase(type)) { - // MSO-1475 - Leave this as a string now - String jsonString = inputs.get(key); - logger.debug("Skipping conversion to jsonNode..."); - if (alias) + Object jsonObj = inputs.get(key); + String jsonString; + try { + jsonString = JSON_MAPPER.writeValueAsString(jsonObj); + } catch (JsonProcessingException e) { + logger.error("failed to map to json, directly converting to string instead", e); + jsonString = jsonObj.toString(); + } + if (alias) newInputs.put(realName, jsonString); else newInputs.put(key, jsonString); //} } else if ("comma_delimited_list".equalsIgnoreCase(type)) { - String commaSeparated = inputs.get(key); + String commaSeparated = inputs.get(key).toString(); try { List<String> anArrayList = this.convertCdlToArrayList(commaSeparated); if (alias) @@ -1360,7 +1383,7 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ newInputs.put(key, commaSeparated); } } else if ("boolean".equalsIgnoreCase(type)) { - String booleanString = inputs.get(key); + String booleanString = inputs.get(key).toString(); Boolean aBool = Boolean.valueOf(booleanString); if (alias) newInputs.put(realName, aBool); @@ -1368,7 +1391,7 @@ public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{ newInputs.put(key, aBool); } else { // it's null or something undefined - just add it back as a String - String str = inputs.get(key); + String str = inputs.get(key).toString(); if (alias) newInputs.put(realName, str); else 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 7c5111bd18..f63aab7ce1 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 @@ -23,7 +23,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.assertTrue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -33,7 +33,9 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -52,17 +54,23 @@ import org.onap.so.adapters.vdu.VduModelInfo; import org.onap.so.adapters.vdu.VduStateType; import org.onap.so.adapters.vdu.VduStatus; import org.onap.so.cloud.CloudConfig; -import org.onap.so.cloudify.beans.DeploymentInfoBuilder; -import org.onap.so.db.catalog.beans.CloudIdentity; -import org.onap.so.db.catalog.beans.CloudSite; import org.onap.so.cloudify.beans.DeploymentInfo; +import org.onap.so.cloudify.beans.DeploymentInfoBuilder; import org.onap.so.cloudify.beans.DeploymentStatus; import org.onap.so.cloudify.v3.client.Cloudify; import org.onap.so.cloudify.v3.model.AzureConfig; +import org.onap.so.db.catalog.beans.CloudIdentity; +import org.onap.so.db.catalog.beans.CloudSite; import org.onap.so.db.catalog.beans.CloudifyManager; import org.onap.so.db.catalog.beans.HeatTemplateParam; import org.onap.so.openstack.exceptions.MsoAdapterException; import org.onap.so.openstack.exceptions.MsoException; +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.ObjectMapper; public class MsoCloudifyUtilsTest { @@ -282,21 +290,31 @@ public class MsoCloudifyUtilsTest { } @Test - public void convertInputValue_successful() { - MsoCloudifyUtils testedObject = new MsoCloudifyUtils(); - - HeatTemplateParam heatTemplateParam = new HeatTemplateParam(); - heatTemplateParam.setParamType("number"); - Object result = testedObject.convertInputValue("5", heatTemplateParam); - assertTrue(result instanceof Integer); - - heatTemplateParam.setParamType("json"); - Object result2 = testedObject.convertInputValue("{\"key\": \"value\"}", heatTemplateParam); - assertTrue(result2 instanceof JsonNode); + public void convertInputValueTest() throws JsonParseException, JsonMappingException, IOException { + MsoCloudifyUtils utils = new MsoCloudifyUtils(); + ObjectMapper mapper = new ObjectMapper(); + HeatTemplateParam paramNum = new HeatTemplateParam(); + paramNum.setParamType("number"); + paramNum.setParamName("my-number"); + + HeatTemplateParam paramString = new HeatTemplateParam(); + paramString.setParamType("string"); + paramString.setParamName("my-string"); + + HeatTemplateParam paramJson = new HeatTemplateParam(); + paramJson.setParamType("json"); + paramJson.setParamName("my-json"); + + Map<String, Object> jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference<Map<String, Object>>(){}); + + assertEquals(3, utils.convertInputValue("3", paramNum)); + assertEquals("hello", utils.convertInputValue("hello", paramString)); + JSONAssert.assertEquals(getJson("free-form.json"), utils.convertInputValue(jsonMap, paramJson).toString(), false); + + } - heatTemplateParam.setParamType("boolean"); - Object result3 = testedObject.convertInputValue("true", heatTemplateParam); - assertTrue(result3 instanceof Boolean); + private String getJson(String filename) throws IOException { + return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/MsoHeatUtils/" + filename))); } private void mockCloudConfig(MsoCloudifyUtils testedObjectSpy) { 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 new file mode 100644 index 0000000000..40bbe90218 --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsUnitTest.java @@ -0,0 +1,67 @@ +package org.onap.so.openstack.utils; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; +import org.onap.so.db.catalog.beans.HeatTemplate; +import org.onap.so.db.catalog.beans.HeatTemplateParam; +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.ObjectMapper; + +public class MsoHeatUtilsUnitTest { + + + private ObjectMapper mapper = new ObjectMapper(); + @Test + public void convertInputMapTest() throws JsonParseException, JsonMappingException, IOException { + MsoHeatUtils utils = new MsoHeatUtils(); + + Map<String, Object> input = new HashMap<>(); + HeatTemplate template = new HeatTemplate(); + template.setArtifactUuid("my-uuid"); + Set<HeatTemplateParam> parameters = template.getParameters(); + HeatTemplateParam paramNum = new HeatTemplateParam(); + paramNum.setParamType("number"); + paramNum.setParamName("my-number"); + input.put("my-number", "3"); + + HeatTemplateParam paramString = new HeatTemplateParam(); + paramString.setParamType("string"); + paramString.setParamName("my-string"); + input.put("my-string", "hello"); + + HeatTemplateParam paramJson = new HeatTemplateParam(); + paramJson.setParamType("json"); + paramJson.setParamName("my-json"); + + Map<String, Object> jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference<Map<String, Object>>(){}); + input.put("my-json", jsonMap); + + parameters.add(paramNum); + parameters.add(paramString); + parameters.add(paramJson); + + Map<String, Object> 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); + } + + + private String getJson(String filename) throws IOException { + return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/MsoHeatUtils/" + filename))); + } + +} diff --git a/adapters/mso-adapter-utils/src/test/resources/__files/MsoHeatUtils/free-form.json b/adapters/mso-adapter-utils/src/test/resources/__files/MsoHeatUtils/free-form.json new file mode 100644 index 0000000000..3efe43682d --- /dev/null +++ b/adapters/mso-adapter-utils/src/test/resources/__files/MsoHeatUtils/free-form.json @@ -0,0 +1,7 @@ +{ + "a" : "value", + "b" : { + "my-key" : "my-value", + "array" : [1, 3, 4] + } +}
\ No newline at end of file |