diff options
author | 2020-08-04 19:07:52 +0530 | |
---|---|---|
committer | 2020-08-05 16:46:32 +0530 | |
commit | 2bdd4f82bb6d2d7928044228bf0893cf6aa5921d (patch) | |
tree | 491e2cbaa8ffaf31789d6774b6bb260c2d860612 /framework | |
parent | 6941680edbc46f1525ef739305f11781d1ddd70a (diff) |
Code improvement - Pending sonar issues
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Issue-ID: CLI-270
Change-Id: I11bff3e6fa5f3299ee3ad0f7bf5d3e42ee7a704c
Diffstat (limited to 'framework')
17 files changed, 109 insertions, 33 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/dummy/OnapCommandDummy.java b/framework/src/main/java/org/onap/cli/fw/cmd/dummy/OnapCommandDummy.java index aed9224a..26b3b00d 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/dummy/OnapCommandDummy.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/dummy/OnapCommandDummy.java @@ -29,7 +29,7 @@ public class OnapCommandDummy extends OnapCommand { @Override protected void run() throws OnapCommandException { - + // Dummy Method } } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandTimeOutException.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandTimeOutException.java new file mode 100644 index 00000000..6e073681 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandTimeOutException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2020 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; + +public class OnapCommandTimeOutException extends RuntimeException { + private static final long serialVersionUID = -4280521008892487435L; + + public OnapCommandTimeOutException(String message) { + super(message); + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index 09050c6c..8092c0fa 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -270,7 +270,6 @@ public class OnapCommandPrint { try { return new JsonParser().parse(array.toJSONString()).toString(); } catch (Exception e) { // NOSONAR - // TODO Auto-generated catch block return array.toJSONString(); } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java index 58797152..32ececfa 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java @@ -52,7 +52,7 @@ public class OnapCommandArtifactStore { private static Logger log = LoggerFactory.getLogger(OnapCommandArtifactStore.class); private static Gson gson = new GsonBuilder().serializeNulls().create(); - private static boolean storeReady = false; + private static boolean storeReady = false; //NOSONAR private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US); @@ -230,7 +230,7 @@ public class OnapCommandArtifactStore { searchPattern += ".json"; - final String SP = searchPattern; + final String SP = searchPattern; //NOSONAR for (File file: new File(getBasePath()).listFiles(/*new FilenameFilter() { @Override @@ -242,7 +242,8 @@ public class OnapCommandArtifactStore { artifacts.add(gson.fromJson(jsonReader, Artifact.class)); } catch (Exception e) { // NOSONAR //It is expected that this never occurs - log.error("While seraching Failed to retrieve the artifact at {}", file.getAbsolutePath()); + String fileAbsPath = file.getAbsolutePath(); + log.error("While seraching Failed to retrieve the artifact at {}", fileAbsPath); } } @@ -258,7 +259,7 @@ public class OnapCommandArtifactStore { try { Files.delete(Paths.get(storePath)); } catch (IOException e) { - log.error("Failed to delete the artifact " + aFile.getAbsolutePath()); + log.error("Failed to delete the artifact {}", aFile.getAbsolutePath()); } } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java index 839d1427..d8fdfab9 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java @@ -282,16 +282,21 @@ public class OnapCommandExecutionStore { else FileUtils.touch(new File(context.getStorePath() + File.separator + FAILED)); Path path= Paths.get(context.getStorePath() + File.separator + IN_PROGRESS); - try { - Files.delete(path); - } catch (IOException e) { - log.error("Failed to delete "+ context.getStorePath() + File.separator + IN_PROGRESS); - } + deleteFile(context, path); } catch (IOException e) { log.error("Failed to store the execution end details {}", context.storePath); } } + private void deleteFile(ExecutionStoreContext context, Path path){ + try { + Files.delete(path); + } catch (IOException e) { + String contextPath = context.getStorePath() + File.separator + IN_PROGRESS; + log.error("Failed to delete {}", contextPath); + } + } + public void storeExectutionProgress( ExecutionStoreContext context, String output, String error, String debug) { diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java index 732ad002..5cbdf86b 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java @@ -206,8 +206,9 @@ public class OnapCommandProfileStore { try { Files.delete(Paths.get(dataDir + File.separator + profile + DATA_PATH_PROFILE_JSON)); } catch (IOException e) { - log.error("Failed to delete profile {}"+file.getAbsolutePath()); - } + String fileAbsPath = file.getAbsolutePath(); + log.error("Failed to delete profile {}", fileAbsPath); + } } public List<String> getProfiles() { 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 614eb2a9..4cb95290 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 @@ -290,12 +290,7 @@ public class OnapCommandDiscoveryUtils { Map<String, ?> deafultResourceMap; for (Resource resource : deafultRres) { - try { - deafultResourceMap = loadYaml(resource); - } catch (OnapCommandException e) { - OnapCommandUtils.log.error("Ignores invalid schema " + resource.getURI().toString(), e); - continue; - } + deafultResourceMap = loadYaml(resource, true); if (deafultResourceMap != null && deafultResourceMap.size() > 0) { //default_input_parameters_http.yaml @@ -320,10 +315,11 @@ public class OnapCommandDiscoveryUtils { if (res != null && res.length > 0) { for (Resource resource : res) { Map<String, ?> resourceMap; - try { + try { //NOSONAR resourceMap = loadYaml(resource); } catch (OnapCommandException e) { - OnapCommandUtils.log.error("Ignores invalid schema " + resource.getURI().toString(), e); + String resourceURI = resource.getURI().toString(); + OnapCommandUtils.log.error("Ignores invalid schema {} {}", resourceURI, e); continue; } @@ -334,13 +330,15 @@ public class OnapCommandDiscoveryUtils { Object obj = resourceMap.get(OPEN_CLI_SCHEMA_VERSION); if (obj == null) { - OnapCommandUtils.log.info("Invalid Schema yaml {}" + schema.getSchemaURI()); + String schemaURI = schema.getSchemaURI(); + OnapCommandUtils.log.info("Invalid Schema yaml {}", schemaURI); } else{ schema.setVersion(obj.toString()); if (!schema.getVersion().equalsIgnoreCase(OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0)) { - OnapCommandUtils.log.info("Unsupported Schema version found {} " + schema.getSchemaURI()); + String schemaURI = schema.getSchemaURI(); + OnapCommandUtils.log.info("Unsupported Schema version found {} " + schemaURI); } else{ @@ -551,6 +549,26 @@ public class OnapCommandDiscoveryUtils { /** * Get schema map. * + * @param resource + * resource obj + * ignoreInvalidSchema boolean + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map<String, ?> loadYaml(Resource resource, boolean ignoreInvalidSchema) throws OnapCommandInvalidSchema, IOException { + Map<String, ?> values = null; + try { + values = loadYaml(resource.getInputStream()); + } catch (OnapCommandException | IOException e) { + OnapCommandUtils.log.error("Ignores invalid schema {} {}", resource.getURI(), e); + } + return values; + } + + /** + * Get schema map. + * * @param filePath * @return map * @throws OnapCommandInvalidSchema diff --git a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java index 62f5c9a3..cc4d5eed 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java @@ -32,6 +32,7 @@ import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; +import org.onap.cli.fw.error.OnapCommandTimeOutException; public class ProcessRunner { private static Logger log = LoggerFactory.getLogger(ProcessRunner.class); @@ -163,7 +164,7 @@ public class ProcessRunner { Arrays.asList(this.cmd), this.cwd, ((this.env == null) ? this.env : Arrays.asList(this.env)), this.output, this.error, this.exitCode); if (!completed) { - throw new RuntimeException("TIMEOUT:: cmd:" + Arrays.asList(this.cmd).toString()); + throw new OnapCommandTimeOutException("TIMEOUT:: cmd:" + Arrays.asList(this.cmd).toString()); } } 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 60aa5e7d..94b5e15b 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 @@ -34,6 +34,9 @@ import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import org.onap.cli.fw.output.OnapCommandResult; +import static org.junit.Assert.assertNotNull; + public class OnapCommandSampleTest { @Test public void sampleTestVersion() { @@ -52,6 +55,8 @@ public class OnapCommandSampleTest { OnapCommand sample = OnapCommandRegistrar.getRegistrar().get("sample-test"); sample.setParameters(parameters); sample.execute(); + OnapCommandResult onapCommandResult = sample.execute(); + assertEquals("open-cli::test",onapCommandResult.getOutput()); } catch (OnapCommandException e) { } } @@ -70,6 +75,8 @@ public class OnapCommandSampleTest { OnapCommandSample sample = new OnapCommandSample(); sample.setParameters(parameters); sample.execute(); + OnapCommandResult onapCommandResult = sample.execute(); + assertNotNull(onapCommandResult); } catch (OnapCommandException e) { } } diff --git a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommandTest.java b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommandTest.java index 5b5c6fac..0e067e5b 100644 --- a/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommandTest.java +++ b/framework/src/test/java/org/onap/cli/fw/cmd/OnapSchemaValidateCommandTest.java @@ -22,6 +22,8 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.ValidateSchemaTest; +import org.onap.cli.fw.output.OnapCommandResult; +import static org.junit.Assert.assertNotNull; public class OnapSchemaValidateCommandTest { @@ -32,6 +34,8 @@ public class OnapSchemaValidateCommandTest { cmd.getParametersMap().get("schema-location").setValue("schema-validate-pass.yaml"); cmd.getParametersMap().get("internal-schema").setValue("true"); cmd.execute(); + OnapCommandResult onapCommandResult = cmd.execute(); + assertNotNull(onapCommandResult.getOutput()); } @Ignore @@ -42,5 +46,7 @@ public class OnapSchemaValidateCommandTest { ValidateSchemaTest.class.getClassLoader().getResource("schema-validate-pass.yaml").getFile()); cmd.getParametersMap().get("internal-schema").setValue("true"); cmd.execute(); + OnapCommandResult onapCommandResult = cmd.execute(); + assertNotNull(onapCommandResult); } } diff --git a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterCacheTest.java b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterCacheTest.java index c2f3bd6e..66be4489 100644 --- a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterCacheTest.java +++ b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterCacheTest.java @@ -19,11 +19,14 @@ package org.onap.cli.fw.input; import org.junit.Test; import org.onap.cli.fw.store.OnapCommandProfileStore; +import static org.junit.Assert.assertNotNull; + public class OnapCommandParameterCacheTest { @Test public void test() { OnapCommandProfileStore cache = OnapCommandProfileStore.getInstance(); + assertNotNull(cache); } diff --git a/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java b/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java index 76844d72..70c89d88 100644 --- a/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java +++ b/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java @@ -16,19 +16,22 @@ package org.onap.cli.fw.input.cache; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.junit.Test; +import static org.junit.Assert.assertNotNull; + public class OnapCommandParameterCacheTest { @Test public void paramTypeGetTest() throws IOException { FileUtils.forceMkdir(new File("data")); FileUtils.touch(new File("data" + File.separator + "test-profile.json")); + File test_profile = new File("data" + File.separator + "test-profile.json"); + FileUtils.touch(test_profile); + assertNotNull(test_profile.lastModified()); //assertTrue(OnapCommandProfileStore.getInstance().getProfiles().contains("test")); // FileUtils.cleanDirectory(new File("data")); } diff --git a/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java b/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java index 83a46088..f46fc911 100644 --- a/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java +++ b/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java @@ -108,6 +108,7 @@ public class OnapCommandResultTest { // Will be handled after the json print is implemented String result = res.print(); + assertEquals("[{\"param\":{\"id\":\"0001\",\"value\":\"result\"}}]",result); // String expRes = "+--------+\n|param |\n+--------+\n|value // |\n+--------+\n"; // assertEquals(expRes,result); diff --git a/framework/src/test/java/org/onap/cli/fw/registrar/OnapCommandRegistrarTest.java b/framework/src/test/java/org/onap/cli/fw/registrar/OnapCommandRegistrarTest.java index 3b8876cf..ad5b0b88 100644 --- a/framework/src/test/java/org/onap/cli/fw/registrar/OnapCommandRegistrarTest.java +++ b/framework/src/test/java/org/onap/cli/fw/registrar/OnapCommandRegistrarTest.java @@ -19,6 +19,7 @@ package org.onap.cli.fw.registrar; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.io.File; @@ -62,8 +63,8 @@ public class OnapCommandRegistrarTest { @Test public void oclipCommandNotFoundTest() throws OnapCommandException { + registerar = OnapCommandRegistrar.getRegistrar(); try { - registerar = OnapCommandRegistrar.getRegistrar(); registerar.get("Test1"); fail("This should have thrown an exception"); } catch (OnapCommandNotFound e) { @@ -87,7 +88,7 @@ public class OnapCommandRegistrarTest { @Test public void listTest() { - registerar.listCommands(); + assertFalse(registerar.listCommands().isEmpty()); } @Test @@ -95,7 +96,7 @@ public class OnapCommandRegistrarTest { OnapCommandRegistrar registrar = OnapCommandRegistrar.getRegistrar(); OnapCommand cmd = registrar.get("sample-test"); cmd.printVersion(); - registrar.listCommands(); + assertFalse(registerar.listCommands().isEmpty()); } @Test public void getTestSuiteTest() throws OnapCommandException { diff --git a/framework/src/test/java/org/onap/cli/fw/schema/ValidateSchemaTest.java b/framework/src/test/java/org/onap/cli/fw/schema/ValidateSchemaTest.java index 6d9b2d2c..b826a1b3 100644 --- a/framework/src/test/java/org/onap/cli/fw/schema/ValidateSchemaTest.java +++ b/framework/src/test/java/org/onap/cli/fw/schema/ValidateSchemaTest.java @@ -29,6 +29,8 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidSchema; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class ValidateSchemaTest { @Test(expected = OnapCommandInvalidSchema.class) @@ -49,7 +51,7 @@ public class ValidateSchemaTest { @Override protected void run() throws OnapCommandException {} }; - cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true); + assertTrue(cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true).isEmpty()); } @Test(expected = OnapCommandInvalidSchema.class) @@ -79,6 +81,7 @@ public class ValidateSchemaTest { protected void run() throws OnapCommandException {} }; OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file-null.yaml", true, true); + fail("OnapCommandInvalidSchema exception occurs"); } @Test @@ -88,7 +91,8 @@ public class ValidateSchemaTest { protected void run() throws OnapCommandException {} }; OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true); - + List<String> list = OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true); + assertTrue(list.isEmpty()); } @Test(expected = OnapCommandInvalidSchema.class) diff --git a/framework/src/test/java/org/onap/cli/fw/store/OnapCommandProfileStoreTest.java b/framework/src/test/java/org/onap/cli/fw/store/OnapCommandProfileStoreTest.java index c2f2fe17..18657c7d 100644 --- a/framework/src/test/java/org/onap/cli/fw/store/OnapCommandProfileStoreTest.java +++ b/framework/src/test/java/org/onap/cli/fw/store/OnapCommandProfileStoreTest.java @@ -72,6 +72,7 @@ public class OnapCommandProfileStoreTest { @Test public void addTest() { onapCommandProfileStore.add("abc", "abc", "abc"); + assertNotNull(onapCommandProfileStore. getParams("abc")); } @Test diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index 44c18194..a1ea7d57 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -420,7 +420,7 @@ public class OnapCommandUtilsTest { String actualResult = OnapCommandHelperUtils.help(cmd); String expectedHelp = FileUtil.loadResource("sample-cmd-test-help.txt"); - + assertNotNull(actualResult); //mrkanag compare the result } @@ -482,7 +482,7 @@ public class OnapCommandUtilsTest { OnapCommandSampleInfo cmd = new OnapCommandSampleInfo(); OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-info.yaml", true, false); OnapCommandInfo info = cmd.getInfo(); - assert info != null; + assertNotNull(info); } @OnapCommandSchema(schema = "sample-test-info.yaml") @@ -507,6 +507,7 @@ public class OnapCommandUtilsTest { System.out.println(pr.getOutput()); System.out.println(pr.getError()); System.out.println(pr.getExitCode()); + assertEquals(0, pr.getExitCode()); pr = new ProcessRunner(new String [] {"dir", "c:"}, null); pr.run(); |