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;
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());
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() {
}
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;
*/
private String defaultValue = "";
+ /*
+ * raw default value, stored with out processing it.
+ */
+ private String rawDefaultValue = "";
+
/*
* Is optional
*/
/*
* 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;
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();
+ }
+ }
}
/**
* @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;
}
*
* @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);
}
/**
* @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() {
import org.onap.cli.fw.input.ParameterType;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
/**
*/
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
*/
}
public List<String> getValues() {
+ if (this.values.isEmpty() && !this.defaultValue.isEmpty()) {
+ return Arrays.asList(new String [] {this.defaultValue});
+ }
return values;
}
this.isSecured = isSecured;
}
+ public String getDefaultValue() {
+ return defaultValue;
+ }
+
+ public void setDefaultValue(String defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
}
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;
}
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)))) {
}
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() + ".";
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 = "";
inp.getReqQueries().put(h, replaceLineFromInputParameters(value, params));
}
+ //mrkanag replaceLineFromInputParameters for result_map, to support input param in result output
return inp;
}
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
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
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
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
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);
@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);
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) {
}
}
@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();
}
}
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 {
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);
}
}
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("");
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