From e45e668a4f6bdeca4ddc4071c4f37822792d65ed Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Mon, 31 Jul 2017 00:45:06 +0530 Subject: Fix invalid help parameters Introduce new section "default_parameter" to add "include" and "exclude" parameter from defalut parameter list. Following is a example for the parameter section to use it: xyz.yaml: ... default_parameters: include: - parameter1 - parameter2 ... exclude: - parameter3 - parameter4 ... parameters: - parameter5 .... .... Issue-Id: CLI-20 Change-Id: I99fd91a130739f2007fdd85a23c76d4e1b30c542 Signed-off-by: subhash kumar singh --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 24 ++- .../java/org/onap/cli/fw/ad/OnapAuthClient.java | 2 +- .../main/java/org/onap/cli/fw/conf/Constants.java | 10 +- .../org/onap/cli/fw/conf/OnapCommandConfg.java | 12 ++ .../error/OnapCommandInvalidDefaultParameter.java | 35 ++++ .../cli/fw/error/OnapCommandInvalidSchema.java | 4 + .../org/onap/cli/fw/utils/OnapCommandUtils.java | 183 ++++++++++++++------- framework/src/main/resources/onap.properties | 6 +- framework/src/main/resources/schema-refresh.yaml | 6 + framework/src/main/resources/schema-validate.yaml | 6 + .../cli/fw/cmd/OnapSchemaRefreshCommandTest.java | 43 ++--- .../TestDefaultParameterSection.java | 121 ++++++++++++++ framework/src/test/resources/onap.properties | 6 +- .../test/resources/sample-test-exclude-param.yaml | 5 + .../sample-test-import-def-param-false.yaml | 2 + .../resources/sample-test-include-exclude.yaml | 6 + .../test/resources/sample-test-include-param.yaml | 6 + .../sample-test-invalid-default-parameter.yaml | 12 ++ ...mple-test-invalid-default-params-not-exist.yaml | 4 + .../java/org/onap/cli/main/OnapCliMainTest.java | 3 +- 20 files changed, 394 insertions(+), 102 deletions(-) create mode 100644 framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java create mode 100644 framework/src/test/java/org/onap/cli/fw/defaultParameter/TestDefaultParameterSection.java create mode 100644 framework/src/test/resources/sample-test-exclude-param.yaml create mode 100644 framework/src/test/resources/sample-test-import-def-param-false.yaml create mode 100644 framework/src/test/resources/sample-test-include-exclude.yaml create mode 100644 framework/src/test/resources/sample-test-include-param.yaml create mode 100644 framework/src/test/resources/sample-test-invalid-default-parameter.yaml create mode 100644 framework/src/test/resources/sample-test-invalid-default-params-not-exist.yaml 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 d67538cc..6cee09de 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -20,6 +20,7 @@ import org.onap.cli.fw.ad.OnapAuthClient; import org.onap.cli.fw.ad.OnapCredentials; import org.onap.cli.fw.ad.OnapService; import org.onap.cli.fw.conf.Constants; +import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandInvalidParameterType; @@ -90,6 +91,11 @@ public abstract class OnapCommand { this.cmdName = name; } + public boolean isCommandInternal() { + return onapService.getName() != null + && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd()); + } + /* * Onap service, this command uses to execute it. , defined by derived command */ @@ -171,7 +177,6 @@ public abstract class OnapCommand { * Any additional profile based such as http/swagger schema could be initialized. */ protected void initializeProfileSchema() throws OnapCommandException { - } /* @@ -235,10 +240,14 @@ public abstract class OnapCommand { try { // login OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters()); - this.authClient = new OnapAuthClient(creds, this.getResult().isDebug()); + boolean isAuthRequired = !this.onapService.isNoAuth() + && "true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue()); + + if (!isCommandInternal()) { + this.authClient = new OnapAuthClient(creds, this.getResult().isDebug()); + } - if (!this.onapService.isNoAuth() - && !"true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue())) { + if (isAuthRequired) { this.authClient.login(); } @@ -246,16 +255,15 @@ public abstract class OnapCommand { this.run(); // logout - if (!this.onapService.isNoAuth() - && !"true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue())) { + if (isAuthRequired) { this.authClient.logout(); } - if (this.cmdResult.isDebug()) { + if (this.cmdResult.isDebug() && authClient != null) { this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); } } catch (OnapCommandException e) { - if (this.cmdResult.isDebug()) { + if (this.cmdResult.isDebug() && authClient != null) { this.cmdResult.setDebugInfo(this.authClient.getDebugInfo()); } throw e; diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java index 0150ee98..9c7e4a66 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java @@ -135,7 +135,7 @@ public class OnapAuthClient { * http request failed */ public String getServiceBasePath(OnapService srv) throws OnapCommandException { - if (srv.getName().equals(Constants.MSB)) { + if (srv.getName().equals(OnapCommandConfg.getApiGateway())) { return this.getMsbUrl(); } 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 928cbc79..8886de55 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 @@ -26,13 +26,14 @@ public class Constants { public static final String APPLICATION_JSON = "application/json"; public static final String X_AUTH_TOKEN = "X-Auth-Token"; - public static final String AUTH_SERVICE = "auth"; + public static final String AUTH_SERVICE = "cli.auth_service"; public static final String AUTH_SERVICE_VERSION = "v1"; public static final String TOKEN = "{\"userName\": \"%s\",\"password\": \"%s\"}"; public static final String MSB_URI = "/api/microservices/v1"; public static final String MSB_SERVICE_URI = MSB_URI + "/services/%s/version/%s"; - public static final String MSB = "msb"; + public static final String API_GATEWAY = "cli.api_gateway"; + public static final String SERVICE_NAME = "cli.service_name"; //http public static final String URI = "uri"; @@ -63,6 +64,10 @@ public class Constants { public static final String DESCRIPTION = "description"; public static final String SERVICE = "service"; public static final String PARAMETERS = "parameters"; + public static final String DEFAULT_PARAMETERS = "default_parameters"; + public static final String DEFAULT_PARAMETERS_INCLUDE = "include"; + public static final String DEFAULT_PARAMETERS_EXCLUDE = "exclude"; + public static final String RESULTS = "results"; public static final String ONAP_CMD_SCHEMA_VERSION = "onap_cmd_schema_version"; @@ -143,6 +148,7 @@ public class Constants { public static final String DEFAULT_SCHEMA_FILE_NAME = "default_input_parameters.yaml"; // Error message + public static final String SCHEMA_INVALID_DEFAULT_PARAMS_SECTION = "Invalid default parameter section"; public static final String SCHEMA_FILE_EMPTY = "The schema file cann't be null or empty"; public static final String SCHEMA_FILE_WRONG_EXTN = "Schema file should be '.yaml' extension"; public static final String SCHEMA_FILE_NOT_EXIST = "Schema file doesn't exist"; diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java index d5025e1b..bcfd6dfb 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java @@ -76,4 +76,16 @@ public final class OnapCommandConfg { return prps.getProperty(Constants.HTTP_X_AUTH_TOKEN, "X-Auth-Token"); } + public static String getInternalCmd() { + return prps.getProperty(Constants.SERVICE_NAME); + } + + public static String getApiGateway() { + return prps.getProperty(Constants.API_GATEWAY); + } + + public static String getAuthService() { + return prps.getProperty(Constants.AUTH_SERVICE); + } + } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java new file mode 100644 index 00000000..4ebf04d3 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.error; + +import java.util.List; + +/** + * Invalid default parameter exception. + */ +public class OnapCommandInvalidDefaultParameter extends OnapCommandException { + private static final long serialVersionUID = -1833571383961748514L; + + /** + * Invalid default argument exception. + * + * @param invalidParamsList message + */ + public OnapCommandInvalidDefaultParameter(List invalidParamsList) { + super("0x0024", "Invalid default parameter: " + invalidParamsList.toString()); + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java index a32a8f2a..4c05d741 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidSchema.java @@ -24,6 +24,10 @@ public class OnapCommandInvalidSchema extends OnapCommandException { private static final long serialVersionUID = -3387652326582792833L; + public OnapCommandInvalidSchema(String error) { + this("", error); + } + public OnapCommandInvalidSchema(String schema, String error) { super("0x0007", "Command schema " + schema + " is invalid, " + error); } 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 79b9596c..1ad588de 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 @@ -19,7 +19,6 @@ package org.onap.cli.fw.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; import net.minidev.json.JSONArray; - import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.ad.OnapCredentials; import org.onap.cli.fw.ad.OnapService; @@ -31,6 +30,7 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandHttpHeaderNotFound; import org.onap.cli.fw.error.OnapCommandHttpInvalidResponseBody; +import org.onap.cli.fw.error.OnapCommandInvalidDefaultParameter; import org.onap.cli.fw.error.OnapCommandInvalidParameterType; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection; @@ -65,11 +65,14 @@ import java.util.ArrayList; 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.Map.Entry; import java.util.ServiceLoader; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Provides helper method to parse Yaml files and produce required objects. @@ -87,17 +90,12 @@ public class OnapCommandUtils { /** * Validates schema version. * - * @param schemaName - * schema name - * @param version - * schema version + * @param schemaName schema name + * @param version schema version * @return map - * @throws OnapCommandInvalidSchemaVersion - * invalid schema version exception - * @throws OnapCommandInvalidSchema - * invalid schema - * @throws OnapCommandSchemaNotFound - * schema not found + * @throws OnapCommandInvalidSchemaVersion invalid schema version exception + * @throws OnapCommandInvalidSchema invalid schema + * @throws OnapCommandSchemaNotFound schema not found */ public static Map validateSchemaVersion(String schemaName, String version) throws OnapCommandException { InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName); @@ -138,59 +136,72 @@ public class OnapCommandUtils { /** * Retrieve OnapCommand from schema. * - * @param cmd - * OnapCommand - * @param schemaName - * schema name - * @param includeDefault - * include if default - * @throws OnapCommandParameterNameConflict - * param name conflict exception - * @throws OnapCommandParameterOptionConflict - * param option conflict exception - * @throws OnapCommandInvalidParameterType - * invalid param type exception - * @throws OnapCommandInvalidPrintDirection - * invalid print direction exception - * @throws OnapCommandInvalidResultAttributeScope - * invalid scope exception - * @throws OnapCommandSchemaNotFound - * schema not found - * @throws OnapCommandInvalidSchema - * invalid schema - * @throws OnapCommandInvalidSchemaVersion - * invalid schema version + * @param cmd OnapCommand + * @param schemaName schema name + * @param includeDefault include if default + * @throws OnapCommandParameterNameConflict param name conflict exception + * @throws OnapCommandParameterOptionConflict param option conflict exception + * @throws OnapCommandInvalidParameterType invalid param type exception + * @throws OnapCommandInvalidPrintDirection invalid print direction exception + * @throws OnapCommandInvalidResultAttributeScope invalid scope exception + * @throws OnapCommandSchemaNotFound schema not found + * @throws OnapCommandInvalidSchema invalid schema + * @throws OnapCommandInvalidSchemaVersion invalid schema version */ public static void loadSchema(OnapCommand cmd, String schemaName, boolean includeDefault) throws OnapCommandException { - List shortOptions = new ArrayList<>(); - List longOptions = new ArrayList<>(); - List names = new ArrayList<>(); + try { + Map defaultParameterMap = includeDefault ? + validateSchemaVersion(Constants.DEFAULT_PARAMETER_FILE_NAME, cmd.getSchemaVersion()) : new HashMap<>(); + Map>> commandYamlMap = (Map>>)validateSchemaVersion(schemaName, cmd.getSchemaVersion()); - if (includeDefault) { - loadSchema(cmd, Constants.DEFAULT_PARAMETER_FILE_NAME, shortOptions, longOptions, names); - } + List defParams = new ArrayList<>(); - loadSchema(cmd, schemaName, shortOptions, longOptions, names); + if (includeDefault) { + if (commandYamlMap.get(Constants.PARAMETERS) == null) { + commandYamlMap.put(Constants.PARAMETERS, (List>) defaultParameterMap.get(Constants.PARAMETERS)); + } else { + commandYamlMap.get(Constants.PARAMETERS).addAll((List>) defaultParameterMap.get(Constants.PARAMETERS)); + } + defParams = ((List>) defaultParameterMap.get(Constants.PARAMETERS)).stream() + .map(p -> p.get(Constants.NAME)).collect(Collectors.toList()); + } + parseSchema(cmd, commandYamlMap, defParams); + } catch (OnapCommandException e) { + throw e; + } catch (Exception e) { + throw new OnapCommandInvalidSchema(schemaName, e); + } } - private static void loadSchema(OnapCommand cmd, String schemaName, List shortOptions, - List longOptions, List names) throws OnapCommandException { - try { - Map values = validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + private static void parseSchema(OnapCommand cmd, + final Map values, + final List defaultParamNames) throws OnapCommandException { + + List shortOptions = new ArrayList<>(); + List longOptions = new ArrayList<>(); + List names = new ArrayList<>(); + Set filteredDefaultParams = new HashSet<>(); + + List sections = Arrays.asList(Constants.NAME, Constants.DESCRIPTION, Constants.SERVICE, + Constants.DEFAULT_PARAMETERS, Constants.PARAMETERS, Constants.RESULTS); - for (Map.Entry entry : values.entrySet()) { - String key = entry.getKey(); + for (String key : sections) { - if (Constants.NAME.equals(key)) { - Object val = values.get(key); + if (Constants.NAME.equals(key)) { + Object val = values.get(key); + if (val != null) { cmd.setName(val.toString()); - } else if (Constants.DESCRIPTION.equals(key)) { - Object val = values.get(key); + } + } else if (Constants.DESCRIPTION.equals(key)) { + Object val = values.get(key); + if (val != null) { cmd.setDescription(val.toString()); - } else if (Constants.SERVICE.equals(key)) { - Map map = (Map) values.get(key); + } + } else if (Constants.SERVICE.equals(key)) { + Map map = (Map) values.get(key); + if (map != null) { OnapService srv = new OnapService(); for (Map.Entry entry1 : map.entrySet()) { @@ -207,10 +218,55 @@ public class OnapCommandUtils { } cmd.setService(srv); - } else if (Constants.PARAMETERS.equals(key)) { - List> list = (ArrayList) values.get(key); + } + } else if (Constants.DEFAULT_PARAMETERS.equals(key)) { + + Map> defParameters = (Map) values.get(Constants.DEFAULT_PARAMETERS); + + if (values.containsKey(Constants.DEFAULT_PARAMETERS) && defParameters == null) { + // if default parameter section is available then it must have either include + // or exclude sub-section. + throw new OnapCommandInvalidSchema(Constants.SCHEMA_INVALID_DEFAULT_PARAMS_SECTION); + } + + if (defParameters != null) { + // validate default parameters + List includeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_INCLUDE) ? + defParameters.get(Constants.DEFAULT_PARAMETERS_INCLUDE) : new ArrayList<>(); - for (Map map : list) { + List invInclude = includeParams.stream() + .filter(p -> !defaultParamNames.contains(p)) + .collect(Collectors.toList()); + + List excludeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_EXCLUDE) ? + defParameters.get(Constants.DEFAULT_PARAMETERS_EXCLUDE) : new ArrayList<>(); + + List invExclude = excludeParams.stream() + .filter(p -> !defaultParamNames.contains(p)) + .collect(Collectors.toList()); + + if (!invExclude.isEmpty() || !invInclude.isEmpty()) { + throw new OnapCommandInvalidDefaultParameter(Stream.concat(invInclude.stream(), invExclude.stream()) + .collect(Collectors.toList())); + } + + if (!includeParams.isEmpty()) { + filteredDefaultParams.addAll(includeParams); + } else if (!excludeParams.isEmpty()) { + defaultParamNames.stream().filter(p -> !excludeParams.contains(p)) + .forEach(filteredDefaultParams::add); + } + } else { + filteredDefaultParams.addAll(defaultParamNames); + + } + + } else if (Constants.PARAMETERS.equals(key)) { + + List> parameters = (List) values.get(key); + + if (parameters != null) { + for (Map map : parameters) { OnapCommandParameter param = new OnapCommandParameter(); for (Map.Entry entry1 : map.entrySet()) { @@ -255,11 +311,18 @@ public class OnapCommandUtils { } } } - cmd.getParameters().add(param); + // Add the element to command : + // 1. if parameter is available in filtered parameter list. + // 2. otherwise, parameter p is available in command yaml file. + if (filteredDefaultParams.contains(param.getName()) || !defaultParamNames.contains(param.getName())) { + cmd.getParameters().add(param); + } } - } else if (Constants.RESULTS.equals(key)) { - Map valueMap = (Map) values.get(key); + } + } else if (Constants.RESULTS.equals(key)) { + Map valueMap = (Map) values.get(key); + if (valueMap != null) { OnapCommandResult result = new OnapCommandResult(); for (Map.Entry entry1 : valueMap.entrySet()) { String key3 = entry1.getKey(); @@ -298,10 +361,6 @@ public class OnapCommandUtils { cmd.setResult(result); } } - } catch (OnapCommandException e) { - throw e; - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); } } diff --git a/framework/src/main/resources/onap.properties b/framework/src/main/resources/onap.properties index 61049cd4..bf1cdae5 100644 --- a/framework/src/main/resources/onap.properties +++ b/framework/src/main/resources/onap.properties @@ -1,4 +1,8 @@ cli.ignore_auth=false cli.version=1.0 http.api_key=X-Auth-Token -http.api_key_use_cookies=true \ No newline at end of file +http.api_key_use_cookies=true + +cli.service_name=onap-cli +cli.api_gateway=msb +cli.auth_service=auth \ No newline at end of file diff --git a/framework/src/main/resources/schema-refresh.yaml b/framework/src/main/resources/schema-refresh.yaml index f64b0545..ffa32424 100644 --- a/framework/src/main/resources/schema-refresh.yaml +++ b/framework/src/main/resources/schema-refresh.yaml @@ -5,6 +5,12 @@ service: no-auth: true name: onap-cli version: 1.0.0 +default_parameters: + exclude: + - onap-username + - onap-password + - msb-url + - no-auth results: direction: landscape attributes: diff --git a/framework/src/main/resources/schema-validate.yaml b/framework/src/main/resources/schema-validate.yaml index 1571fb3d..06bb6563 100644 --- a/framework/src/main/resources/schema-validate.yaml +++ b/framework/src/main/resources/schema-validate.yaml @@ -5,6 +5,12 @@ service: no-auth: true name: onap-cli version: 1.0.0 +default_parameters: + exclude: + - onap-username + - onap-password + - msb-url + - no-auth parameters: - name: schema-location type: url diff --git a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java index 1c09c0d3..eda6a591 100644 --- a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java +++ b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaRefreshCommandTest.java @@ -18,7 +18,11 @@ package org.onap.cli.fw.cmd; import org.junit.Test; import org.onap.cli.fw.error.OnapCommandException; -import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.output.OnapCommandResultAttribute; + +import java.util.List; + +import static org.junit.Assert.assertTrue; public class OnapSchemaRefreshCommandTest { @@ -26,31 +30,20 @@ public class OnapSchemaRefreshCommandTest { public void validateSchemaCommandTest1() throws OnapCommandException { OnapSchemaRefreshCommand cmd = new OnapSchemaRefreshCommand(); cmd.initializeSchema("schema-refresh.yaml"); - for (OnapCommandParameter param : cmd.getParameters()) { - if ("onap-username".equals(param.getName())) { - param.setValue("test"); - } else if ("onap-password".equals(param.getName())) { - param.setValue("test"); - } else if ("msb-url".equals(param.getName())) { - param.setValue("test-url"); - } - } cmd.execute(); - } - @Test - public void validateSchemaCommandTest2() throws OnapCommandException { - OnapSchemaRefreshCommand cmd = new OnapSchemaRefreshCommand(); - cmd.initializeSchema("schema-refresh.yaml"); - for (OnapCommandParameter param : cmd.getParameters()) { - if ("onap-username".equals(param.getName())) { - param.setValue("test"); - } else if ("onap-password".equals(param.getName())) { - param.setValue("test"); - } else if ("msb-url".equals(param.getName())) { - param.setValue("test-url"); - } - } - cmd.execute(); + List onapCommandResultAttribute = cmd.getResult() + .getRecords(); + + String s1Number = onapCommandResultAttribute.get(0).getValues().get(0); + String cmdName = onapCommandResultAttribute.get(1).getValues().get(0); + String cmdFile = onapCommandResultAttribute.get(2).getValues().get(0); + String version = onapCommandResultAttribute.get(3).getValues().get(0); + + assertTrue(s1Number.equalsIgnoreCase("1")); + assertTrue(cmdName.equalsIgnoreCase("sample-test1")); + assertTrue(cmdFile.equalsIgnoreCase("sample-test1-schema-http.yaml")); + assertTrue(version.equalsIgnoreCase("1.0")); + } } diff --git a/framework/src/test/java/org/onap/cli/fw/defaultParameter/TestDefaultParameterSection.java b/framework/src/test/java/org/onap/cli/fw/defaultParameter/TestDefaultParameterSection.java new file mode 100644 index 00000000..bd2ad0d1 --- /dev/null +++ b/framework/src/test/java/org/onap/cli/fw/defaultParameter/TestDefaultParameterSection.java @@ -0,0 +1,121 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.cli.fw.defaultParameter; + +import org.junit.Test; +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidDefaultParameter; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.utils.OnapCommandUtils; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +public class TestDefaultParameterSection { + @Test + public void checkOnlyInclude() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-include-param.yaml", true); + List parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList()); + assertTrue(parameters.contains("onap-username")); + assertTrue(parameters.contains("onap-password")); + assertTrue(parameters.contains("msb-url")); + } + + @Test + public void checkOnlyExclude() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-exclude-param.yaml", true); + List parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList()); + assertFalse(parameters.contains("onap-username")); + assertTrue(parameters.contains("onap-password")); + assertFalse(parameters.contains("msb-url")); + } + + @Test + public void checkBothIncludeAndExclude() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-include-exclude.yaml", true); + List parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList()); + + assertTrue(parameters.contains("onap-username")); + assertFalse(parameters.contains("onap-password")); + assertFalse(parameters.contains("msb-url")); + } + + @Test + public void checkDefaultSectionAbsent() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "onap-test-schema.yaml", true); + List parameters = cmd.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList()); + + assertTrue(parameters.contains("onap-username")); + assertTrue(parameters.contains("onap-password")); + assertTrue(parameters.contains("msb-url")); + } + + @Test(expected = OnapCommandInvalidDefaultParameter.class) + public void checkInvalidDefaultArgument() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-default-parameter.yaml", true); + } + + @Test(expected = OnapCommandInvalidDefaultParameter.class) + public void checkInvalidDefaultArgumentNotExist() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-invalid-default-params-not-exist.yaml", true); + } + + @Test(expected = OnapCommandInvalidSchema.class) + public void checkDefaltwithNoExcludeAndInclude() throws OnapCommandException { + OnapCommand cmd = new OnapCommand() { + @Override + protected void run() throws OnapCommandException {} + }; + + OnapCommandUtils.loadSchema(cmd, "sample-test-import-def-param-false.yaml", true); + } +} diff --git a/framework/src/test/resources/onap.properties b/framework/src/test/resources/onap.properties index 3b185174..beb24a3f 100644 --- a/framework/src/test/resources/onap.properties +++ b/framework/src/test/resources/onap.properties @@ -1,4 +1,8 @@ cli.ignore_auth=true cli.version=1.0 http.api_key=X-Auth-Token -http.api_key_use_cookies=true \ No newline at end of file +http.api_key_use_cookies=true + +cli.service_name=onap-cli +cli.api_gateway=msb +cli.auth_service=auth \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-exclude-param.yaml b/framework/src/test/resources/sample-test-exclude-param.yaml new file mode 100644 index 00000000..3cfb62ac --- /dev/null +++ b/framework/src/test/resources/sample-test-exclude-param.yaml @@ -0,0 +1,5 @@ +onap_cmd_schema_version: 1.0 +default_parameters: + exclude: + - onap-username + - msb-url \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-import-def-param-false.yaml b/framework/src/test/resources/sample-test-import-def-param-false.yaml new file mode 100644 index 00000000..438ad64d --- /dev/null +++ b/framework/src/test/resources/sample-test-import-def-param-false.yaml @@ -0,0 +1,2 @@ +onap_cmd_schema_version: 1.0 +default_parameters: diff --git a/framework/src/test/resources/sample-test-include-exclude.yaml b/framework/src/test/resources/sample-test-include-exclude.yaml new file mode 100644 index 00000000..22d05c47 --- /dev/null +++ b/framework/src/test/resources/sample-test-include-exclude.yaml @@ -0,0 +1,6 @@ +onap_cmd_schema_version: 1.0 +default_parameters: + include: + - onap-username + exclude: + - msb-url \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-include-param.yaml b/framework/src/test/resources/sample-test-include-param.yaml new file mode 100644 index 00000000..0dc76bbc --- /dev/null +++ b/framework/src/test/resources/sample-test-include-param.yaml @@ -0,0 +1,6 @@ +onap_cmd_schema_version: 1.0 +default_parameters: + include: + - onap-username + - onap-password + - msb-url \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-invalid-default-parameter.yaml b/framework/src/test/resources/sample-test-invalid-default-parameter.yaml new file mode 100644 index 00000000..ae20145a --- /dev/null +++ b/framework/src/test/resources/sample-test-invalid-default-parameter.yaml @@ -0,0 +1,12 @@ +onap_cmd_schema_version: 1.0 +default_parameters: + exclude: + - invalid-param +parameters: + - name: invalid-param + type: bool + description: Onap boolean param, by default its always false. + short_option: b + long_option: bool + is_optional: true + default_value: false \ No newline at end of file diff --git a/framework/src/test/resources/sample-test-invalid-default-params-not-exist.yaml b/framework/src/test/resources/sample-test-invalid-default-params-not-exist.yaml new file mode 100644 index 00000000..7e1f39e9 --- /dev/null +++ b/framework/src/test/resources/sample-test-invalid-default-params-not-exist.yaml @@ -0,0 +1,4 @@ +onap_cmd_schema_version: 1.0 +default_parameters: + exclude: + - invalid-param-1 \ No newline at end of file diff --git a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java index ca955cc9..e1ab4914 100644 --- a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java +++ b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java @@ -268,8 +268,7 @@ public class OnapCliMainTest { for (String cmdName : cmdSchemaMap.keySet()) { System.out.println( "************************* '" + cmdSchemaMap.get(cmdName) + "' *******************************"); - this.handle(new String[] { "schema-validate", "-l", cmdSchemaMap.get(cmdName), "-i", "true", "-m", - "http://192.168.4.47:80", "-u", "root1", "-p", "root123" }); + this.handle(new String[] { "schema-validate", "-l", cmdSchemaMap.get(cmdName), "-i"}); } } -- cgit 1.2.3-korg