diff options
Diffstat (limited to 'framework/src/main/java')
6 files changed, 204 insertions, 88 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java index 29a29ce3..2a5956cc 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java @@ -16,11 +16,13 @@ package org.onap.cli.fw.cmd; +import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; @@ -32,12 +34,16 @@ 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.OnapCommandResultType; +import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.onap.cli.fw.schema.OnapCommandSchemaMerger; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandHelperUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; /** * Oclip Command. @@ -149,7 +155,6 @@ public abstract class OnapCommand { return this.initializeSchema(schema, false); } - public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException { this.setSchemaName(schema); @@ -178,6 +183,13 @@ public abstract class OnapCommand { } } + protected void preRun() throws OnapCommandException { + LOG.debug("CMD: " + this.getName() + "pre run."); + } + + protected void postRun() throws OnapCommandException { + LOG.debug("CMD: " + this.getName() + "post run."); + } /** * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all * parameters value. @@ -238,10 +250,13 @@ public abstract class OnapCommand { } } + preRun(); + this.run(); LOG.info("OUTPUT: " + this.cmdResult.getRecords()); + postRun(); return this.cmdResult; } @@ -270,6 +285,5 @@ public abstract class OnapCommand { public String printHelp() throws OnapCommandHelpFailed { return OnapCommandHelperUtils.help(this); } - // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format } diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java index 23d42f87..c2bfc1d5 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java @@ -148,6 +148,26 @@ public class OnapCommandConstants { 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 VERIFY_SAMPLES_DIRECTORY = "open-cli-sample"; + public static final String VERIFY_SAMPLES_FILE_PATTERN = VERIFY_SAMPLES_DIRECTORY + YAML_PATTERN; + public static final String VERIFY_SAMPLES_MOCK_PATTERN = VERIFY_SAMPLES_DIRECTORY + JSON_PATTERN; + public static final String VERIFY_SAMPLES = "samples"; + public static final String VERIFY_CMD_NAME = "name"; + public static final String VERIFY_CMD_VERSION = "version"; + public static final String VERIFY_OUPUT = "output"; + public static final String VERIFY_INPUT = "input"; + public static final String VERIFY_MOCO = "moco"; + public static final String VERIFY_SAMPLE_FILE_ID = "samplefileid"; + public static final String VERIFY_SAMPLE_ID = "sampleid"; + public static final String VERIFY_RESULT_PASS = "pass"; + public static final String VERIFY_RESULT_FAIL = "fail"; + public static final String VERIFY_CONTEXT_PARAM = "context"; + + + + public static final String VERIFY_LONG_OPTION = "--verify"; + public static final String VERIFY_SHORT_OPTION = "-V"; + private OnapCommandConstants() { } diff --git a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java index 6564628d..15a086c7 100644 --- a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java @@ -16,13 +16,6 @@ package org.onap.cli.fw.registrar; -import java.io.IOException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.apache.commons.io.IOUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -48,6 +41,13 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * Oclip Command registrar provides a common place, where every command would get registered automatically when its @@ -93,11 +93,11 @@ public class OnapCommandRegistrar { public void setProfile(String profileName, List<String> includes, List<String> excludes) { this.paramCache.setProfile(profileName); - for (String profile: includes) { + for (String profile : includes) { this.paramCache.includeProfile(profile); } - for (String profile: excludes) { + for (String profile : excludes) { this.paramCache.excludeProfile(profile); } } @@ -111,12 +111,9 @@ public class OnapCommandRegistrar { /** * Register the command into registrar and throws OnapInvalidCommandRegistration for invalid command. * - * @param name - * Command Name - * @param cmd - * Command Class - * @throws OnapCommandInvalidRegistration - * Invalid registration exception + * @param name Command Name + * @param cmd Command Class + * @throws OnapCommandInvalidRegistration Invalid registration exception * @throws OnapCommandRegistrationProductInfoMissing */ private void register(String name, String version, Class<? extends OnapCommand> cmd) throws OnapCommandInvalidRegistration, OnapCommandRegistrationProductInfoMissing { @@ -135,16 +132,15 @@ public class OnapCommandRegistrar { private OnapCommandRegistrar() { this.enabledProductVersion = System.getenv(OnapCommandConstants.OPEN_CLI_PRODUCT_IN_USE_ENV_NAME); - if (this.enabledProductVersion == null) { - this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); + if (this.enabledProductVersion == null) { + this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); } } /** * Get global registrar. * - * @throws OnapCommandException - * exception + * @throws OnapCommandException exception */ public static OnapCommandRegistrar getRegistrar() throws OnapCommandException { if (registrar == null) { @@ -177,7 +173,7 @@ public class OnapCommandRegistrar { return cmds; } - for (String cmd: this.registry.keySet()) { + for (String cmd : this.registry.keySet()) { if (cmd.split(":")[1].equalsIgnoreCase(version)) { cmds.add(cmd.split(":")[0]); } @@ -213,8 +209,7 @@ public class OnapCommandRegistrar { * Returns command details. * * @return map - * @throws OnapCommandException - * exception + * @throws OnapCommandException exception */ public List<OnapCommandSchemaInfo> listCommandInfo() throws OnapCommandException { return OnapCommandDiscoveryUtils.discoverSchemas(); @@ -223,11 +218,9 @@ public class OnapCommandRegistrar { /** * Get the OnapCommand, which CLI main would use to find the command based on the command name. * - * @param cmdName - * Name of command + * @param cmdName Name of command * @return OnapCommand - * @throws OnapCommandException - * Exception + * @throws OnapCommandException Exception */ public OnapCommand get(String cmdName) throws OnapCommandException { return this.get(cmdName, this.getEnabledProductVersion()); @@ -236,13 +229,10 @@ public class OnapCommandRegistrar { /** * Get the OnapCommand, which CLI main would use to find the command based on the command name. * - * @param cmdName - * Name of command - * @param version - * product version + * @param cmdName Name of command + * @param version product version * @return OnapCommand - * @throws OnapCommandException - * Exception + * @throws OnapCommandException Exception */ public OnapCommand get(String cmdName, String version) throws OnapCommandException { Class<? extends OnapCommand> cls = registry.get(cmdName + ":" + version); @@ -293,9 +283,9 @@ public class OnapCommandRegistrar { } //First check if there is an specific plugin exist, otherwise check for profile plugin - if (plugins.containsKey(schema.getSchemaName())) { - this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); - } else if (plugins.containsKey(schema.getSchemaProfile())) { + if (plugins.containsKey(schema.getSchemaName())) { + this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); + } else if (plugins.containsKey(schema.getSchemaProfile())) { this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile())); } else { LOG.info("Ignoring schema " + schema.getSchemaURI()); @@ -315,7 +305,7 @@ public class OnapCommandRegistrar { } String buildTime = OnapCommandHelperUtils.findLastBuildTime(); - if (buildTime!= null && !buildTime.isEmpty()) { + if (buildTime != null && !buildTime.isEmpty()) { buildTime = " [" + buildTime + "]"; } else { buildTime = ""; @@ -341,8 +331,7 @@ public class OnapCommandRegistrar { * Provides the help message in tabular format for all commands registered in this registrar. * * @return string - * @throws OnapCommandHelpFailed - * Help cmd failed + * @throws OnapCommandHelpFailed Help cmd failed */ public String getHelp() throws OnapCommandHelpFailed { return this.getHelp(false); @@ -387,7 +376,7 @@ public class OnapCommandRegistrar { OnapCommand cmd; try { if (!isEnabledProductVersionOnly) { - String []cmdVer = cmdName.split(":"); + String[] cmdVer = cmdName.split(":"); cmd = this.get(cmdVer[0], cmdVer[1]); attr.getValues().add(cmdVer[0]); attrVer.getValues().add(cmdVer[1]); @@ -409,4 +398,8 @@ public class OnapCommandRegistrar { throw new OnapCommandHelpFailed(e); } } + + public List<Map<String, ?>> getTestSuite(String cmd) throws OnapCommandException { + return OnapCommandDiscoveryUtils.createTestSuite(cmd, enabledProductVersion); + } } diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java index 67675480..af444b8f 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java @@ -19,6 +19,9 @@ package org.onap.cli.fw.schema; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConstants; +import java.util.ArrayList; +import java.util.List; + /** * OnapCommandSchemaInfo is used in discovery caching. * @@ -39,6 +42,8 @@ public class OnapCommandSchemaInfo { private String product; + private List<String> sampleFiles = new ArrayList(); + /** * OCS version */ @@ -118,5 +123,7 @@ public class OnapCommandSchemaInfo { this.ignore = ignore; } - + public List<String> getSampleFiles() { + return sampleFiles; + } } 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 439eb970..628ddf3a 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 @@ -67,6 +67,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.io.FileUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -524,26 +525,8 @@ public class OnapCommandSchemaLoader { /** * Get schema map. * - * @param resource - * resource obj - * @return map - * @throws OnapCommandInvalidSchema - * exception - */ - public static Map<String, ?> loadSchema(Resource resource) throws OnapCommandInvalidSchema { - try { - return loadSchema(resource.getInputStream(), resource.getFilename()); - } catch (IOException e) { - throw new OnapCommandInvalidSchema(resource.getFilename(), e); - } - - } - - /** - * Get schema map. - * - * @param resource - * resource obj + * @param stream + * @param schemaName * @return map * @throws OnapCommandInvalidSchema * exception diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java index b878503a..2b3cf941 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java @@ -16,33 +16,13 @@ package org.onap.cli.fw.utils; -import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_DIRECTORY; -import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_JSON_PATTERN; -import static org.onap.cli.fw.conf.OnapCommandConstants.DISCOVERY_FILE; -import static org.onap.cli.fw.conf.OnapCommandConstants.NAME; -import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION; -import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_DIRECTORY; -import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_PATH_PATERN; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Map.Entry; - +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.FileUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.conf.OnapCommandConstants; -import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; -import org.onap.cli.fw.error.OnapCommandException; -import org.onap.cli.fw.error.OnapCommandInstantiationFailed; -import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.error.*; +import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.springframework.core.io.Resource; @@ -50,7 +30,13 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.yaml.snakeyaml.Yaml; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.*; + +import static org.onap.cli.fw.conf.OnapCommandConstants.*; public class OnapCommandDiscoveryUtils { @@ -248,7 +234,7 @@ public class OnapCommandDiscoveryUtils { for (Resource resource : res) { try { - resourceMap = OnapCommandSchemaLoader.loadSchema(resource); + resourceMap = loadYaml(resource); } catch (OnapCommandException e) { OnapCommandUtils.LOG.error("Ignores invalid schema " + resource.getURI().toString(), e); continue; @@ -297,9 +283,34 @@ public class OnapCommandDiscoveryUtils { throw new OnapCommandDiscoveryFailed(SCHEMA_DIRECTORY, e); } + try { + Resource[] samples = findResources(OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN); + for (Resource sample : samples) { + updateSchemaInfoWithSample(sample, extSchemas); + } + } catch (IOException e) { + throw new OnapCommandDiscoveryFailed(OnapCommandConstants.VERIFY_SAMPLES_DIRECTORY, e); + } + return extSchemas; } + private static void updateSchemaInfoWithSample(Resource sampleResourse, + List<OnapCommandSchemaInfo> schemaInfos) throws OnapCommandInvalidSchema, IOException { + Map<String, ?> infoMap = loadSchema(sampleResourse); + String cmdName = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_NAME); + String version = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_VERSION); + + Optional<OnapCommandSchemaInfo> optSchemaInfo = schemaInfos.stream() + .filter(e -> e.getCmdName().equals(cmdName) && e.getProduct().equals(version)) + .findFirst(); + + if (optSchemaInfo.isPresent()) { + OnapCommandSchemaInfo onapCommandSchemaInfo = optSchemaInfo.get(); + onapCommandSchemaInfo.getSampleFiles().add(sampleResourse.getFilename()); + } + } + /** * Discover the Oclip commands. * @@ -329,4 +340,92 @@ public class OnapCommandDiscoveryUtils { } } + + public static List<Map<String, ?>> createTestSuite(String cmd, String version) throws OnapCommandException { + + ArrayList<Map<String, ?>> testSamples = new ArrayList(); + + List<Resource> resources = new ArrayList(); + OnapCommandSchemaInfo schemaInfo = getSchemaInfo(cmd, version); + + List<String> sampleFiles = new ArrayList(); + if (schemaInfo != null && !schemaInfo.getSampleFiles().isEmpty()) { + sampleFiles.addAll(schemaInfo.getSampleFiles()); + } + + for (String sampleFile : sampleFiles) { + try { + Resource resource = OnapCommandDiscoveryUtils.findResource(sampleFile, + OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN); + resources.add(resource); + } catch (IOException e) { + throw new OnapCommandInvalidSample("Sample file does not exist : " + sampleFile , e); + } + } + + for (Resource resource : resources) { + + Map<String, ?> stringMap = OnapCommandDiscoveryUtils.loadYaml(resource); + Map<String, Map<String, String>> samples = (Map<String, Map<String, String>>) stringMap + .get(OnapCommandConstants.VERIFY_SAMPLES); + + for (String sampleId : samples.keySet()) { + + Map<String, String> sample = samples.get(sampleId); + + List<String> inputArgs = new ArrayList(); + inputArgs.add(cmd); + if (sample.get(OnapCommandConstants.VERIFY_INPUT) != null) { + inputArgs.addAll(Arrays.asList(sample.get(OnapCommandConstants.VERIFY_INPUT).trim().split(" "))); + } + inputArgs.add(OnapCommandConstants.VERIFY_LONG_OPTION); + + HashMap map = new HashMap(); + map.put(OnapCommandConstants.VERIFY_INPUT, inputArgs); + map.put(OnapCommandConstants.VERIFY_OUPUT, sample.get(OnapCommandConstants.VERIFY_OUPUT)); + map.put(OnapCommandConstants.VERIFY_MOCO, sample.get(OnapCommandConstants.VERIFY_MOCO)); + map.put(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID, resource.getFilename()); + map.put(OnapCommandConstants.VERIFY_SAMPLE_ID, sampleId); + testSamples.add(map); + } + } + return testSamples; + } + + /** + * Get schema map. + * + * @param resource + * resource obj + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map<String, ?> loadYaml(Resource resource) throws OnapCommandInvalidSchema { + Map<String, ?> values = null; + try { + values = (Map<String, ?>) new Yaml().load(resource.getInputStream()); + } catch (Exception e) { + throw new OnapCommandInvalidSchema(resource.getFilename(), e); + } + return values; + } + + /** + * Get schema map. + * + * @param filePath + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema { + Map<String, ?> values = null; + try { + values = (Map<String, Object>) new Yaml().load(FileUtils.readFileToString(new File(filePath))); + } catch (Exception e) { + throw new OnapCommandInvalidSchema(filePath, e); + } + return values; + } } |