diff options
author | subhash kumar singh <subhash.kumar.singh@huawei.com> | 2018-02-23 06:05:28 +0000 |
---|---|---|
committer | subhash kumar singh <subhash.kumar.singh@huawei.com> | 2018-02-28 09:12:38 +0000 |
commit | 225400a393da49322bdf500a61ae423ec00efead (patch) | |
tree | 2d55acf0d8b7833bd8e595cb902e29a4c789d9bb /framework/src | |
parent | 3903891dfe8f9e2488fc9d1bd0858fa4670cff0e (diff) |
Make default value type object
Enhance default value type from String to Object so that it will
directly support different type (BOOL, Array, Map).
Issue-ID: CLI-74
Change-Id: Iaca438e58150c80047340acba92917ecae685242
Signed-off-by: subhash kumar singh <subhash.kumar.singh@huawei.com>
Diffstat (limited to 'framework/src')
6 files changed, 166 insertions, 64 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java index 465dffa4..22f341d5 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java +++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java @@ -16,18 +16,20 @@ package org.onap.cli.fw.input; -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.UUID; - +import com.fasterxml.jackson.databind.ObjectMapper; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandParameterMissing; import org.onap.cli.fw.utils.OnapCommandUtils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; /** * Oclip Command's input parameter. @@ -63,7 +65,7 @@ public class OnapCommandParameter { /* * Default value */ - private String defaultValue = ""; + private Object defaultValue; /* * raw default value, stored with out processing it. @@ -150,12 +152,24 @@ public class OnapCommandParameter { public void setParameterType(OnapCommandParameterType parameterType) { this.parameterType = parameterType; - if (this.defaultValue.isEmpty()) { - if (this.getParameterType().equals(OnapCommandParameterType.BOOL)) { - // For bool type always the default param is false - this.defaultValue = "false"; - } else if (this.getParameterType().equals(OnapCommandParameterType.UUID)) { - this.defaultValue = UUID.randomUUID().toString(); + if (defaultValue == null) { + + switch (getParameterType()) { + case MAP: + this.defaultValue = new HashMap<String, String>(); + break; + case ARRAY: + defaultValue = new ArrayList<String>(); + break; + case BOOL: + defaultValue = false; + break; + case UUID: + this.defaultValue = UUID.randomUUID().toString(); + break; + default: + this.defaultValue = null; + break; } } } @@ -165,7 +179,7 @@ public class OnapCommandParameter { * * @return string */ - public String getDefaultValue() { + public Object getDefaultValue() { return defaultValue; } @@ -187,9 +201,63 @@ public class OnapCommandParameter { return this.rawDefaultValue.trim().substring(7, this.rawDefaultValue.length() - 1); } - public void setDefaultValue(String defaultValue) { - this.rawDefaultValue = defaultValue; - this.defaultValue = OnapCommandUtils.replaceLineForSpecialValues(this.rawDefaultValue); + public void setRawDefaultValue(String value) throws OnapCommandInvalidParameterValue { + this.rawDefaultValue = value; + String processedValue= OnapCommandUtils.replaceLineForSpecialValues(value); + + switch (parameterType) { + case MAP: + try { + defaultValue = new ObjectMapper().readValue(processedValue, Map.class); + } catch (IOException e) { + throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e); + } + break; + + case ARRAY: + try { + defaultValue = new ObjectMapper().readValue(processedValue, List.class); + } catch (IOException e) { + throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e); + } + break; + + case BOOL: + defaultValue = processedValue.equalsIgnoreCase("true"); + break; + + default: + defaultValue = processedValue; + break; + } + } + + public void setDefaultValue(Object defaultValue) throws OnapCommandInvalidParameterValue { + //check type + Class<?> clz; + switch (parameterType) { + case BOOL: + clz = Boolean.class; + break; + + case MAP: + clz = Map.class; + break; + + case ARRAY: + clz = Collection.class; + break; + + default: + clz = String.class; + break; + } + + if (clz.isInstance(defaultValue)) { + this.defaultValue = defaultValue; + } else { + throw new OnapCommandInvalidParameterValue("Invalid default value for parameter: " + this.getName()); + } } /** @@ -212,34 +280,22 @@ public class OnapCommandParameter { if (OnapCommandParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http") && !value.toString().startsWith("/")) { - this.value = "/" + value; + value = "/" + value; } else if (OnapCommandParameterType.ARRAY.equals(parameterType)) { if (!(value instanceof List)) { throw new OnapCommandInvalidParameterValue(this.getName()); } - List<String> list = (List<String>) value; - ObjectMapper mapper = new ObjectMapper(); - try { - this.value = mapper.writeValueAsString(list); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); - } } else if (OnapCommandParameterType.MAP.equals(parameterType)) { if (!(value instanceof Map)) { throw new OnapCommandInvalidParameterValue(this.getName()); } - - Map<String, String> map = (Map<String, String>) value; - ObjectMapper mapper = new ObjectMapper(); - try { - this.value = mapper.writeValueAsString(map); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); + } else if (OnapCommandParameterType.BOOL.equals(parameterType)) { + if (!(value instanceof Boolean)) { + throw new OnapCommandInvalidParameterValue(this.getName()); } - } else { - this.value = value; } + this.value = value; } public boolean isOptional() { diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java index b47000a1..439eb970 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java @@ -318,7 +318,7 @@ public class OnapCommandSchemaLoader { case DEFAULT_VALUE: Object obj = parameter.get(key2); - param.setDefaultValue(obj.toString()); + param.setRawDefaultValue(obj.toString()); break; case TYPE: diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java index e9ea81d4..1387ea3d 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandHelperUtils.java @@ -157,7 +157,7 @@ public class OnapCommandHelperUtils { if (param.isRawDefaultValueAnEnv()) { optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromrRawDefaultValue() + "."; - } else if (param.getDefaultValue() != null && !((String)param.getDefaultValue()).isEmpty()) { + } else if (param.getDefaultValue() != null && param.getDefaultValue().toString().isEmpty()) { optSecondCol += defaultMsg + param.getDefaultValue() + "."; } diff --git a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java index 82781b43..5a6e6458 100644 --- a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java +++ b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.Test; @@ -49,8 +50,13 @@ public class OnapCommandParameterTest { assertTrue("value".equals(param.getValue())); param.setParameterType(OnapCommandParameterType.ARRAY); - param.setValue(Arrays.asList("1", "2", "3")); - assertTrue("[\"1\",\"2\",\"3\"]".equals(param.getValue())); + List<String> list = Arrays.asList("1", "2", "3"); + param.setValue(list); + assertTrue(((List)param.getValue()).containsAll(list)); + + param.setRawDefaultValue("[\"1\", \"2\", \"3\", \"4\"]"); + assertTrue(((List<String>)param.getDefaultValue()) + .containsAll(Arrays.asList("1", "2", "3", "4"))); param.setParameterType(OnapCommandParameterType.MAP); Map<String, String> map = new HashMap<>(); @@ -58,16 +64,20 @@ public class OnapCommandParameterTest { map.put("Two", "2"); map.put("Three", "3"); param.setValue(map); - assertTrue("{\"One\":\"1\",\"Two\":\"2\",\"Three\":\"3\"}".equals(param.getValue())); - - param.setDefaultValue("$s{env:defaultValue}"); - assertTrue("env:defaultValue".equals(param.getDefaultValue())); + HashMap<String, String> value = (HashMap<String, String>) param.getValue(); + assertTrue(value.keySet().containsAll(Arrays.asList("One", "Two", "Three"))); + assertTrue(value.values().containsAll(Arrays.asList("1", "2", "3"))); + + param.setRawDefaultValue("{\"key1\":\"$s{env:defaultValue}\"}"); + assertTrue(((Map<String, String>)param.getDefaultValue()).values().containsAll( + Arrays.asList("env:defaultValue") + )); } @Test - public void parameterEnvDefaultValueObjTest() { + public void parameterEnvDefaultValueObjTest() throws OnapCommandInvalidParameterValue { OnapCommandParameter param = new OnapCommandParameter(); - param.setDefaultValue("$s{env:DAFAULT_VALUE}"); + param.setRawDefaultValue("$s{env:DAFAULT_VALUE}"); boolean isDefaultValueAnEnv = param.isRawDefaultValueAnEnv(); assertTrue(isDefaultValueAnEnv); diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index 317920f8..11f97070 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -17,18 +17,9 @@ package org.onap.cli.fw.utils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import mockit.Invocation; +import mockit.Mock; +import mockit.MockUp; import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; @@ -48,9 +39,18 @@ import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; -import mockit.Invocation; -import mockit.Mock; -import mockit.MockUp; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class OnapCommandUtilsTest { @@ -119,7 +119,7 @@ public class OnapCommandUtilsTest { public void loadOnapCommandSchemaWithOutDefaultTest() throws OnapCommandException { OnapCommand cmd = new OnapCommandSample(); OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", false, false); - assertTrue("sample-test".equals(cmd.getName()) && cmd.getParameters().size() == 9); + assertTrue("sample-test".equals(cmd.getName()) && cmd.getParameters().size() == 10); } @Test(expected = OnapCommandParameterNameConflict.class) @@ -147,14 +147,44 @@ public class OnapCommandUtilsTest { assertTrue("sample-test".equals(cmd.getName()) && cmd.getParameters().size() > 9); for (OnapCommandParameter com : cmd.getParameters()) { - com.setValue("value"); + switch (com.getParameterType()) { + case STRING: + com.setValue("value"); + break; + + case ARRAY: + com.setValue(Collections.EMPTY_LIST); + break; + + case MAP: + com.setValue(new HashMap<String, String>()); + break; + + case BOOL: + com.setValue(true); + break; + + case TEXT: + com.setValue("value"); + break; + + case URL: + com.setValue("http:localhost/test"); + break; + + case JSON: + com.setValue("json"); + break; + + default: + break; + } } Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters()); - assertTrue(map.size() == 15); + assertTrue(map.size() == 16); } - @Test public void helpCommandTest() throws IOException, OnapCommandException { OnapCommand cmd = new OnapCommandSample(); diff --git a/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml b/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml index 3c4ace83..f4894b3f 100644 --- a/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml +++ b/framework/src/test/resources/open-cli-schema/sample-test-schema.yaml @@ -69,6 +69,12 @@ parameters: description: Oclip positional args, if no short option and no long option given for it is_optional: true default_value: http://localhost:8082/file.txt + - name: array-param + type: array + description: list parameter + long_option: list-param + short_option: q + is_optional: true results: direction: portrait attributes: |