diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2021-01-12 11:57:59 +0100 |
---|---|---|
committer | Zebek Bogumil <bogumil.zebek@nokia.com> | 2021-01-12 11:59:15 +0100 |
commit | 81a0c6fd575fcb32be259383766ab53ce947d702 (patch) | |
tree | a2d028c7e511653fc1d2d06afd4e3ded0aac866c /framework | |
parent | 1ade33bf7ca87020a6bc7a734d8d86961e332536 (diff) |
Fix OnapCommand class model
- No Metadata in OnapCommand Info section
- Optional parameters with default values are lost after overriding selected parameter
Issue-ID: CLI-349 CLI-348
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: I893bb01809737b4262a99f355d6c7681c8c275bb
Diffstat (limited to 'framework')
4 files changed, 117 insertions, 14 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 74e488b4..167379d7 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 @@ -1,5 +1,6 @@ /* * Copyright 2017 Huawei Technologies Co., Ltd. + * Copyright 2020 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -125,7 +126,7 @@ public abstract class OnapCommand { } public void setParameters(Set<OnapCommandParameter> parameters) { - this.cmdParameters = parameters; + this.cmdParameters.addAll(parameters); } public Set<OnapCommandParameter> getParameters() { @@ -200,6 +201,7 @@ public abstract class OnapCommand { } public List<String> initializeSchema(OnapCommandSchemaInfo schema) throws OnapCommandException { + this.getInfo().setMetadata(schema.getMetadata()); return this.initializeSchema(schema.getSchemaName(), false); } 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 aa45f58e..e051d5dd 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 @@ -1,5 +1,6 @@ /* * Copyright 2017 Huawei Technologies Co., Ltd. + * Copyright 2020 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +27,7 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.DIRECTION; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_AUTHOR; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_IGNORE; +import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_METADATA; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_PARAMS_LIST; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_PARAMS_MANDATORY_LIST; import static org.onap.cli.fw.conf.OnapCommandConstants.INFO_PRODUCT; @@ -262,6 +264,11 @@ public class OnapCommandSchemaLoader { info.setAuthor(mode.toString()); break; + case INFO_METADATA: + Object metadata = infoMap.get(key1); + info.setMetadata((Map<String,String>)metadata); + break; + case INFO_IGNORE: Object ignore = infoMap.get(key1); info.setIgnore(ignore.toString().equalsIgnoreCase(OnapCommandConstants.BOOLEAN_TRUE)); 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 94b5e15b..b0ea4eb1 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 @@ -1,5 +1,6 @@ /* * Copyright 2017 Huawei Technologies Co., Ltd. + * Copyright 2020 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,18 +18,26 @@ package org.onap.cli.cmd.sample; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import org.junit.Test; import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandExecutionFailed; +import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandNotInitialized; +import org.onap.cli.fw.info.OnapCommandInfo; +import org.onap.cli.fw.info.OnapCommandState; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.fw.output.OnapCommandResultAttribute; @@ -38,11 +47,16 @@ import org.onap.cli.fw.output.OnapCommandResult; import static org.junit.Assert.assertNotNull; public class OnapCommandSampleTest { + + private static final String SAMPLE_TEST_COMMAND_NAME = "sample-test"; + private static final int EXPECTED_NUMBER_OF_COMMAND_PARAMETERS = 18; + private static final int EXPECTED_SIZE_OF_METADATA = 1; + @Test public void sampleTestVersion() { try { - Set<OnapCommandParameter> parameters = new HashSet(); + Set<OnapCommandParameter> parameters = new HashSet<>(); OnapCommandParameter version = new OnapCommandParameter(); version.setName(OnapCommandConstants.DEFAULT_PARAMETER_VERSION); version.setValue(true); @@ -52,12 +66,13 @@ public class OnapCommandSampleTest { hlp.setValue(false); parameters.add(hlp); - OnapCommand sample = OnapCommandRegistrar.getRegistrar().get("sample-test"); + OnapCommand sample = givenOnapCommand("sample-test"); sample.setParameters(parameters); sample.execute(); OnapCommandResult onapCommandResult = sample.execute(); assertEquals("open-cli::test",onapCommandResult.getOutput()); } catch (OnapCommandException e) { + fail(); } } @@ -69,7 +84,7 @@ public class OnapCommandSampleTest { ver.setValue(true); ver.setParameterType(OnapCommandParameterType.BOOL); - Set<OnapCommandParameter> parameters = new HashSet(); + Set<OnapCommandParameter> parameters = new HashSet<>(); parameters.add(ver); OnapCommandSample sample = new OnapCommandSample(); @@ -78,14 +93,67 @@ public class OnapCommandSampleTest { OnapCommandResult onapCommandResult = sample.execute(); assertNotNull(onapCommandResult); } catch (OnapCommandException e) { + fail(); } } @Test + public void shouldContainValidParametersAfterChangingDefaultValueOfSelectedParameter() throws OnapCommandException { + // given + Set<OnapCommandParameter> parameters = new HashSet<>(); + OnapCommandParameter booleanParameter = givenBooleanParameter(true); + parameters.add(booleanParameter); + + // when + OnapCommand onapCommand = givenOnapCommand(SAMPLE_TEST_COMMAND_NAME); + onapCommand.setParameters(parameters); + + // then + final Set<OnapCommandParameter> params = onapCommand.getParameters(); + assertEquals(EXPECTED_NUMBER_OF_COMMAND_PARAMETERS, params.size()); + assertTrue(params.contains(booleanParameter)); + final List<OnapCommandParameter> stringParams = findParameter(params, "string-param"); + verifyThatParameterHasValue(stringParams, "test"); + } + + @Test + public void shouldContainParametersWithDefaultValuesWhenUserDoesNotChangeAnyParameter() throws OnapCommandException { + // given + OnapCommand onapCommand = givenOnapCommand(SAMPLE_TEST_COMMAND_NAME); + + // when + final Set<OnapCommandParameter> params = onapCommand.getParameters(); + + // then + assertEquals(EXPECTED_NUMBER_OF_COMMAND_PARAMETERS, params.size()); + final List<OnapCommandParameter> stringParams = findParameter(params, "string-param"); + verifyThatParameterHasValue(stringParams, "test"); + } + + @Test + public void shouldContainValidInfoState() throws OnapCommandException { + // given + OnapCommand onapCommand = givenOnapCommand(SAMPLE_TEST_COMMAND_NAME); + + // when + final OnapCommandInfo info = onapCommand.getInfo(); + + // then + assertEquals("open-cli", info.getProduct()); + assertEquals("test", info.getService()); + assertEquals(OnapCommandType.CMD, info.getCommandType()); + assertEquals("Kanagaraj Manickam kanagaraj.manickam@huawei.com", info.getAuthor()); + assertEquals(OnapCommandState.EXPERIMENTAL, info.getState()); + final Map<String, String> metadata = info.getMetadata(); + assertEquals(EXPECTED_SIZE_OF_METADATA, metadata.size()); + assertEquals("honolulu", metadata.get("release")); + } + + @Test public void sampleTest() { try { - Set<OnapCommandParameter> parameters = new HashSet(); + Set<OnapCommandParameter> parameters = new HashSet<>(); OnapCommandParameter ver = new OnapCommandParameter(); ver.setName(OnapCommandConstants.DEFAULT_PARAMETER_VERSION); ver.setValue(false); @@ -106,12 +174,12 @@ public class OnapCommandSampleTest { title.setName(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE); title.setValue(true); parameters.add(title); - OnapCommandParameter denug = new OnapCommandParameter(); - denug.setName(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG); - denug.setValue(true); - parameters.add(denug); + OnapCommandParameter debug = new OnapCommandParameter(); + debug.setName(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG); + debug.setValue(true); + parameters.add(debug); - OnapCommand sample = OnapCommandRegistrar.getRegistrar().get("sample-test"); + OnapCommand sample = givenOnapCommand("sample-test"); sample.setParameters(parameters); sample.execute(); @@ -121,10 +189,11 @@ public class OnapCommandSampleTest { UUID.fromString(attrValue.substring(4)); attr = sample.getResult().getRecordsMap().get("output-2"); attrValue = attr.getValues().get(0); - assertEquals("test", attrValue); + assertEquals("Hello test !", attrValue); } catch (IllegalArgumentException e) { fail("Failed to replace the output default value on output-1"); } catch (OnapCommandException e) { + fail(); } } @@ -167,4 +236,26 @@ public class OnapCommandSampleTest { OnapCommandSample sample = new OnapCommandSample(false); sample.execute(); } + + private OnapCommand givenOnapCommand(String cmdName) throws OnapCommandException { + return OnapCommandRegistrar.getRegistrar().get(cmdName); + } + + private OnapCommandParameter givenBooleanParameter(boolean value) throws OnapCommandInvalidParameterValue { + OnapCommandParameter booleanParameter = new OnapCommandParameter(); + booleanParameter.setName("bool-param"); + booleanParameter.setLongOption("bool"); + booleanParameter.setShortOption("b"); + booleanParameter.setValue(value); + return booleanParameter; + } + + private List<OnapCommandParameter> findParameter(Set<OnapCommandParameter> params, String parameterName) { + return params.stream().filter(it -> it.getName().equals(parameterName)).collect(Collectors.toList()); + } + + private void verifyThatParameterHasValue(List<OnapCommandParameter> stringParams, String expectedValue) { + assertEquals(1, stringParams.size()); + assertEquals(expectedValue, stringParams.get(0).getValue()); + } } 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 3b0b905f..adaf3502 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 @@ -1,4 +1,5 @@ # Copyright 2018 Huawei Technologies Co., Ltd. +# Copyright 2020 Nokia # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,6 +22,8 @@ info: type: cmd author: Kanagaraj Manickam kanagaraj.manickam@huawei.com state: experimental + metadata: + release: honolulu parameters: - name: bool-param type: bool @@ -37,14 +40,14 @@ parameters: long_option: secure is_secured: true is_optional: true - default_Value: pass123# + default_value: pass123# - name: string-param type: string description: Oclip string param long_option: string-param short_option: c is_optional: true - default_Value: test + default_value: test - name: yaml-param type: json description: Oclip yaml file location param @@ -104,4 +107,4 @@ results: scope: short is_secured: false type: string - default_value: Hello ${string-param} !
\ No newline at end of file + default_value: Hello ${string-param} ! |