diff options
12 files changed, 209 insertions, 80 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java index 9ca43fdf..4f125aea 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -42,6 +42,7 @@ import org.onap.cli.fw.error.OnapCommandRegistrationFailed; import org.onap.cli.fw.error.OnapCommandSchemaNotFound; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResult; +import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.ResultType; import org.onap.cli.fw.utils.OnapCommandUtils; @@ -267,6 +268,15 @@ public abstract class OnapCommand { this.cmdResult.setDebug(true); } + //pre-process result attributes for spl entries and input parameters + for (OnapCommandResultAttribute attr: this.cmdResult.getRecords()) { + if (!attr.getDefaultValue().isEmpty()) { + attr.setDefaultValue(OnapCommandUtils.replaceLineForSpecialValues(attr.getDefaultValue())); + attr.setDefaultValue(OnapCommandUtils.replaceLineFromInputParameters( + attr.getDefaultValue(), this.getParametersMap())); + } + } + try { OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters()); diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java index 9fccc668..29cdbce9 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java @@ -202,6 +202,9 @@ public class Constants { public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable"; public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target"; + public static final String SPL_ENTRY_UUID = "uuid"; + public static final String SPL_ENTRY_ENV = "env:"; + private Constants() { } 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 31b83abb..3ba15ff4 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 @@ -22,6 +22,7 @@ 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 java.io.File; import java.util.List; @@ -65,6 +66,11 @@ public class OnapCommandParameter { private String defaultValue = ""; /* + * raw default value, stored with out processing it. + */ + private String rawDefaultValue = ""; + + /* * Is optional */ private boolean isOptional = false; @@ -77,7 +83,12 @@ public class OnapCommandParameter { /* * Parameter Value */ - private Object value; + private Object value = null; + + /* + * raw value, get stored as its without processing it. + */ + private Object rawValue = null; public String getName() { return cmdName; @@ -117,6 +128,15 @@ public class OnapCommandParameter { public void setParameterType(ParameterType parameterType) { this.parameterType = parameterType; + + if (this.defaultValue.isEmpty()) { + if (this.getParameterType().equals(ParameterType.BOOL)) { + // For bool type always the default param is false + this.defaultValue = "false"; + } else if (this.getParameterType().equals(ParameterType.UUID)) { + this.defaultValue = UUID.randomUUID().toString(); + } + } } /** @@ -125,16 +145,6 @@ public class OnapCommandParameter { * @return string */ public String getDefaultValue() { - if (this.isDefaultValueAnEnv()) { - String envVar = this.getEnvVarNameFromDefaultValue(); - this.defaultValue = System.getenv(envVar); - } else if (this.getParameterType().equals(ParameterType.BOOL)) { - // For bool type always the default param is false - this.defaultValue = "false"; - } else if (this.defaultValue.isEmpty() && this.getParameterType().equals(ParameterType.UUID)) { - this.defaultValue = UUID.randomUUID().toString(); - } - return defaultValue; } @@ -143,21 +153,22 @@ public class OnapCommandParameter { * * @return boolean */ - public boolean isDefaultValueAnEnv() { - return this.defaultValue.trim().startsWith("${") && this.defaultValue.trim().endsWith("}"); + public boolean isRawDefaultValueAnEnv() { + return this.rawDefaultValue.trim().startsWith("$s{env:") && this.rawDefaultValue.trim().endsWith("}"); } /** - * check if the default value is ${ENV_VAR_NAME} and return the ENV_VAR_NAME. + * check if the default value is $s{env:ENV_VAR_NAME} and return the ENV_VAR_NAME. * * @return ENV_VAR_NAME */ - public String getEnvVarNameFromDefaultValue() { - return this.defaultValue.trim().substring(2, this.defaultValue.length() - 1); + public String getEnvVarNameFromrRawDefaultValue() { + return this.rawDefaultValue.trim().substring(7, this.rawDefaultValue.length() - 1); } public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; + this.rawDefaultValue = defaultValue; + this.defaultValue = OnapCommandUtils.replaceLineForSpecialValues(this.rawDefaultValue); } /** @@ -167,44 +178,46 @@ public class OnapCommandParameter { * @throws OnapCommandInvalidParameterValue * exception */ - public Object getValue() throws OnapCommandInvalidParameterValue { + public Object getValue() { if (value != null) { - if (ParameterType.URL.equals(parameterType) && !value.toString().startsWith("http") - && !value.toString().startsWith("/")) { - value = "/" + value; - } else if (ParameterType.ARRAY.equals(parameterType)) { - if (!(value instanceof List)) { - throw new OnapCommandInvalidParameterValue(this.getName()); - } - - List<String> list = (List<String>) value; - ObjectMapper mapper = new ObjectMapper(); - try { - return mapper.writeValueAsString(list); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); - } - } else if (ParameterType.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 { - return mapper.writeValueAsString(map); - } catch (JsonProcessingException e) { - throw new OnapCommandInvalidParameterValue(this.getName(), e); - } - } - return value; } return getDefaultValue(); } - public void setValue(Object value) { - this.value = value; + public void setValue(Object value) throws OnapCommandInvalidParameterValue { + this.rawValue = value; + + if (ParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http") + && !value.toString().startsWith("/")) { + this.value = "/" + value; + } else if (ParameterType.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 (ParameterType.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 { + this.value = value; + } } public boolean isOptional() { diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java index 73617049..fcd6f1a2 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResultAttribute.java @@ -19,6 +19,7 @@ package org.onap.cli.fw.output; import org.onap.cli.fw.input.ParameterType; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -43,6 +44,11 @@ public class OnapCommandResultAttribute { private List<String> values = new ArrayList<>(); /* + * default value, useful to set when a command want to set the default value for a output attributes. + */ + private String defaultValue = ""; + + /* * Output scope */ private OnapCommandResultAttributeScope outScope = OnapCommandResultAttributeScope.SHORT; @@ -72,6 +78,9 @@ public class OnapCommandResultAttribute { } public List<String> getValues() { + if (this.values.isEmpty() && !this.defaultValue.isEmpty()) { + return Arrays.asList(new String [] {this.defaultValue}); + } return values; } @@ -99,4 +108,12 @@ public class OnapCommandResultAttribute { this.isSecured = isSecured; } + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index f891fd09..9c4306c1 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -102,6 +102,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.ServiceLoader; import java.util.Set; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -682,6 +683,11 @@ public class OnapCommandUtils { } break; + case DEFAULT_VALUE: + Object obj = map.get(key4); + attr.setDefaultValue(obj.toString()); + break; + case IS_SECURED: if (validate) { if (!validateBoolean(String.valueOf(map.get(key4)))) { @@ -1136,8 +1142,8 @@ public class OnapCommandUtils { } String defaultMsg = " By default, it is "; - if (param.isDefaultValueAnEnv()) { - optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromDefaultValue() + if (param.isRawDefaultValueAnEnv()) { + optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromrRawDefaultValue() + "."; } else if (param.getDefaultValue() != null && !((String)param.getDefaultValue()).isEmpty()) { optSecondCol += defaultMsg + param.getDefaultValue() + "."; @@ -1296,7 +1302,63 @@ public class OnapCommandUtils { return methodName; } - private static String replaceLineFromInputParameters(String line, Map<String, OnapCommandParameter> params) + /** + * There are unique values like uuid is supported, so when input, output (default) values has + * these special entries, then it will get replaced with it's value + * + * @param line + * @return + */ + public static String replaceLineForSpecialValues(String line) { + String result = ""; + + if (!line.contains("$s{")) { + return line; + } + + int currentIdx = 0; + while (currentIdx < line.length()) { + int idxS = line.indexOf("$s{", currentIdx); + if (idxS == -1) { + result += line.substring(currentIdx); + break; + } + int idxE = line.indexOf("}", idxS); + String splEntry = line.substring(idxS + 3, idxE); + splEntry = splEntry.trim(); + + String value = ""; + + switch (splEntry) { + case Constants.SPL_ENTRY_UUID: + value = UUID.randomUUID().toString(); + break; + + default: + + if (splEntry.startsWith(Constants.SPL_ENTRY_ENV)) { + //start to read after env:ENV_VAR_NAME + String envVarName = splEntry.substring(4); + value = System.getenv(envVarName); + if (value == null) { + //when env is not defined, assign the same env:ENV_VAR_NAME + //so that it will given hit to user that ENV_VAR_NAME to be + //defined. + value = splEntry; + } + } else { + value = splEntry; + } + } + + result += line.substring(currentIdx, idxS) + value; + currentIdx = idxE + 1; + } + + return result; + } + + public static String replaceLineFromInputParameters(String line, Map<String, OnapCommandParameter> params) throws OnapCommandException { String result = ""; @@ -1487,6 +1549,7 @@ public class OnapCommandUtils { inp.getReqQueries().put(h, replaceLineFromInputParameters(value, params)); } + //mrkanag replaceLineFromInputParameters for result_map, to support input param in result output return inp; } diff --git a/framework/src/main/resources/default_input_parameters.yaml b/framework/src/main/resources/default_input_parameters.yaml index cc56ee2c..c65f4d8d 100644 --- a/framework/src/main/resources/default_input_parameters.yaml +++ b/framework/src/main/resources/default_input_parameters.yaml @@ -5,14 +5,14 @@ parameters: description: Onap user name short_option: u long_option: onap-username - default_value: ${ONAP_USERNAME} + default_value: $s{env:ONAP_USERNAME} is_optional: false - name: onap-password type: string description: Onap user password short_option: p long_option: onap-password - default_value: ${ONAP_PASSWORD} + default_value: $s{env:ONAP_PASSWORD} is_secured: true is_optional: false - name: host-url @@ -21,7 +21,7 @@ parameters: short_option: m long_option: host-url is_optional: false - default_value: ${HOST_URL} + default_value: $s{env:HOST_URL} - name: help type: string description: print help message @@ -57,7 +57,7 @@ parameters: description: whether to print title or not short_option: t long_option: no-title - default_value: true + default_value: false - name: no-auth type: bool description: whether to authenticate user or not diff --git a/framework/src/main/resources/onap.properties b/framework/src/main/resources/onap.properties index 786f2d29..a963b386 100644 --- a/framework/src/main/resources/onap.properties +++ b/framework/src/main/resources/onap.properties @@ -38,11 +38,11 @@ cli.schema.service_params_mandatory_list=name,version cli.schema.input_params_list=name,description,type,short_option,long_option, is_optional,default_value,is_secured cli.schema.input_params_mandatory_list=name,description,type -cli.schema.result_params_list=name,description,scope,type,is_secured -cli.schema.result_params_mandatory_list=name, description, type +cli.schema.result_params_list=name,description,scope,type,is_secured, default_value +cli.schema.result_params_mandatory_list=name, description, type, scope cli.schema.http_sections=request,success_codes,result_map,sample_response -cli.schema.http_mandatory_sections=equest, success_codes +cli.schema.http_mandatory_sections=request, success_codes cli.schema.http_request_params=uri,method,body,headers,queries,multipart_entity_name cli.schema.http_request_mandatory_params=uri,method diff --git a/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSampleTest.java b/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSampleTest.java index e50490f4..fc3e772d 100644 --- a/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSampleTest.java +++ b/framework/src/test/java/org/onap/cli/cmd/sample/OnapCommandSampleTest.java @@ -16,23 +16,32 @@ package org.onap.cli.cmd.sample; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + import org.junit.Test; +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandRegistrar; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cli.fw.error.OnapCommandNotInitialized; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.ParameterType; - -import java.util.ArrayList; -import java.util.List; +import org.onap.cli.fw.output.OnapCommandResultAttribute; public class OnapCommandSampleTest { @Test public void sampleTestVersion() { - OnapCommandSample sample = new OnapCommandSample(); - + try { + + OnapCommand sample = OnapCommandRegistrar.getRegistrar().get("sample-test"); + List<OnapCommandParameter> parameters = new ArrayList(); OnapCommandParameter v = new OnapCommandParameter(); v.setName(Constants.DEFAULT_PARAMETER_VERSION); @@ -66,8 +75,9 @@ public class OnapCommandSampleTest { @Test public void sampleTest() { - OnapCommandSample sample = new OnapCommandSample(); + try { + OnapCommand sample = OnapCommandRegistrar.getRegistrar().get("sample-test"); List<OnapCommandParameter> parameters = new ArrayList(); OnapCommandParameter v = new OnapCommandParameter(); v.setName(Constants.DEFAULT_PARAMETER_VERSION); @@ -103,7 +113,17 @@ public class OnapCommandSampleTest { parameters.add(m); sample.setParameters(parameters); sample.execute(); - } catch (OnapCommandException e) { + + //validate whether output attributes default value got initialized as part of execute() + OnapCommandResultAttribute attr = sample.getResult().getRecordsMap().get("output-1"); + String attrValue = attr.getValues().get(0); + UUID.fromString(attrValue.substring(4)); + attr = sample.getResult().getRecordsMap().get("output-2"); + attrValue = attr.getValues().get(0); + assertEquals(attrValue, "test"); + } catch (IllegalArgumentException e){ + fail("Failed to replace the output default value on output-1"); + } catch (OnapCommandException e) { } } diff --git a/framework/src/test/java/org/onap/cli/fw/TestCommandValidate.java b/framework/src/test/java/org/onap/cli/fw/TestCommandValidate.java index 12ff2085..1f830464 100644 --- a/framework/src/test/java/org/onap/cli/fw/TestCommandValidate.java +++ b/framework/src/test/java/org/onap/cli/fw/TestCommandValidate.java @@ -50,6 +50,8 @@ public class TestCommandValidate { @Test(expected = OnapCommandParameterMissing.class) public void testNoAuthArgFalse() throws OnapCommandException { OnapCommandUtils.loadSchema(cmd, "sample-test-include-param.yaml", true, false); + OnapCommandParameter msbParam = cmd.getParameters().stream().filter(p -> p.getName().equalsIgnoreCase("host-url")).findFirst().get(); + msbParam.setValue(""); cmd.validate(); } } diff --git a/framework/src/test/java/org/onap/cli/fw/cmd/OnapCreateSwaggerBasedCommand.java b/framework/src/test/java/org/onap/cli/fw/cmd/OnapCreateSwaggerBasedCommand.java index 069ae35f..89367f1d 100644 --- a/framework/src/test/java/org/onap/cli/fw/cmd/OnapCreateSwaggerBasedCommand.java +++ b/framework/src/test/java/org/onap/cli/fw/cmd/OnapCreateSwaggerBasedCommand.java @@ -16,18 +16,17 @@ package org.onap.cli.fw.cmd; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; + import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandExecutionFailed; import org.onap.cli.fw.error.OnapCommandExecutorInfoMissing; -import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandResultInitialzationFailed; import org.onap.cli.fw.utils.OnapCommandUtils; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; - public class OnapCreateSwaggerBasedCommand extends OnapSwaggerCommand { private <T> T initializeEntity(T obj, List<String> prps) throws OnapCommandResultInitialzationFailed { @@ -45,7 +44,7 @@ public class OnapCreateSwaggerBasedCommand extends OnapSwaggerCommand { Method set = obj.getClass().getMethod(methodName, String.class); set.invoke(obj, this.getParametersMap().get(paramName).getValue()); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | OnapCommandInvalidParameterValue e) { + | InvocationTargetException e) { throw new OnapCommandResultInitialzationFailed(this.getName(), e); } } 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 e11bfed8..6137b9dc 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 @@ -60,24 +60,24 @@ public class OnapCommandParameterTest { param.setValue(map); assertTrue("{\"One\":\"1\",\"Two\":\"2\",\"Three\":\"3\"}".equals(param.getValue())); - param.setDefaultValue("${defaultValue}"); - assertTrue(null == param.getDefaultValue()); + param.setDefaultValue("$s{env:defaultValue}"); + assertTrue("env:defaultValue".equals(param.getDefaultValue())); } @Test public void parameterEnvDefaultValueObjTest() { OnapCommandParameter param = new OnapCommandParameter(); - param.setDefaultValue("${DAFAULT_VALUE}"); - boolean isDefaultValueAnEnv = param.isDefaultValueAnEnv(); + param.setDefaultValue("$s{env:DAFAULT_VALUE}"); + boolean isDefaultValueAnEnv = param.isRawDefaultValueAnEnv(); assertTrue(isDefaultValueAnEnv); - String envValue = param.getEnvVarNameFromDefaultValue(); + String envValue = param.getEnvVarNameFromrRawDefaultValue(); assertTrue("DAFAULT_VALUE".equals(envValue)); } @Test - public void parameterValidateTest() { + public void parameterValidateTest() throws OnapCommandInvalidParameterValue { OnapCommandParameter param = new OnapCommandParameter(); param.setOptional(false); param.setValue(""); diff --git a/framework/src/test/resources/sample-test-schema.yaml b/framework/src/test/resources/sample-test-schema.yaml index 0387e48c..98964a60 100644 --- a/framework/src/test/resources/sample-test-schema.yaml +++ b/framework/src/test/resources/sample-test-schema.yaml @@ -76,8 +76,10 @@ results: scope: short type: string is_secured: true + default_value: req-$s{uuid} - name: output-2 description: Onap output attribute marked in long scope: short is_secured: false - type: string
\ No newline at end of file + type: string + default_value: Hello ${string-param} !
\ No newline at end of file |