Merge "Reduced code complexity"
authorKanagaraj Manickam <kanagaraj.manickam@huawei.com>
Thu, 31 Mar 2022 04:09:27 +0000 (04:09 +0000)
committerGerrit Code Review <gerrit@onap.org>
Thu, 31 Mar 2022 04:09:27 +0000 (04:09 +0000)
framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java
framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java
framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java
framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java
framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
main/src/main/java/org/onap/cli/main/OnapCli.java
main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java

index b88477c..fa9bcf0 100644 (file)
@@ -186,20 +186,8 @@ public class OnapCommandResult {
 
         return noOfRecords;
     }
-    /**
-     * Helps to print the result based on the type.
-     *
-     * @return string
-     * @throws OnapCommandOutputFormatNotsupported
-     *             excpetion
-     * @throws OnapCommandOutputPrintingFailed
-     *             exception
-     */
-    public String print() throws OnapCommandException {
-        if (this.getType().equals(OnapCommandResultType.TEXT)) {
-             return this.getOutput().toString();
-        }
 
+    public OnapCommandPrint createAndLoadPrint() {
         OnapCommandPrint print = new OnapCommandPrint();
         print.setPrintTitle(this.isIncludeTitle());
         print.setDirection(this.printDirection);
@@ -232,6 +220,24 @@ public class OnapCommandResult {
                 print.addColumn(val.getName(), val.getValues());
             }
         }
+        return print;
+    }
+
+    /**
+     * Helps to print the result based on the type.
+     *
+     * @return string
+     * @throws OnapCommandOutputFormatNotsupported
+     *             excpetion
+     * @throws OnapCommandOutputPrintingFailed
+     *             exception
+     */
+    public String print() throws OnapCommandException {
+        if (this.getType().equals(OnapCommandResultType.TEXT)) {
+             return this.getOutput().toString();
+        }
+
+        OnapCommandPrint print = createAndLoadPrint();
 
         if (this.getType().equals(OnapCommandResultType.JSON)) {
             return print.printJson();
index d2bc98b..14e37c3 100644 (file)
@@ -98,6 +98,21 @@ public class OnapCommandPrint {
         return max;
     }
 
+    public List<List<Object>>  addTitle(List<List<Object>> rows, boolean isNormalize){
+        if (this.isPrintTitle()) {
+            List<Object> list = new ArrayList<>();
+            for (String key : this.data.keySet()) {
+                if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) {
+                    list.add(splitIntoList(key, MAX_COLUMN_LENGTH));
+                } else {
+                    list.add(key);
+                }
+            }
+            rows.add(list);
+        }
+        return  rows;
+    }
+
     /**
      * Helps to form the rows from columns.
      *
@@ -112,17 +127,7 @@ public class OnapCommandPrint {
         List<List<Object>> rows = new ArrayList<>();
 
         // add title
-        if (this.isPrintTitle()) {
-            List<Object> list = new ArrayList<>();
-            for (String key : this.data.keySet()) {
-                if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) {
-                    list.add(splitIntoList(key, MAX_COLUMN_LENGTH));
-                } else {
-                    list.add(key);
-                }
-            }
-            rows.add(list);
-        }
+        rows = addTitle(rows, isNormalize);
 
         // form row
         for (int i = 0; i < this.findMaxRows(); i++) {
@@ -166,7 +171,7 @@ public class OnapCommandPrint {
         }
         // new line is converted to space char
         if (inp.contains("\n")) {
-            inp = inp.replaceAll("\n", "");
+            inp = inp.replace("\n", "");
         }
 
         StringTokenizer tok = new StringTokenizer(inp, " ");
@@ -243,15 +248,20 @@ public class OnapCommandPrint {
         }
     }
 
+    public JSONObject printPortrait(List<List<Object>> rows){
+        JSONObject result = new JSONObject();
+        for (int i=1; i<rows.size(); i++) {
+            if (rows.get(i).get(1) != null)
+                result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString()));
+        }
+        return result;
+    }
+
     public String printJson() {
         List<List<Object>> rows = this.formRows(false);
 
         if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) {
-            JSONObject result = new JSONObject();
-            for (int i=1; i<rows.size(); i++) {
-                if (rows.get(i).get(1) != null)
-                    result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString()));
-            }
+            JSONObject result = printPortrait(rows);
             return result.toJSONString();
         } else {
             JSONArray array = new JSONArray();
index 28344de..912ea23 100644 (file)
@@ -17,6 +17,7 @@
 package org.onap.cli.fw.registrar;
 
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -332,7 +333,7 @@ public class OnapCommandRegistrar {
 
         String versionInfo = "";
         try {
-            versionInfo = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO));
+            versionInfo = IOUtils.toString((this.getClass().getClassLoader().getResourceAsStream(OnapCommandConstants.VERSION_INFO)), (Charset) null);
         } catch (IOException e) { // NOSONAR
             //Never occurs
         }
index e051d5d..71a189e 100644 (file)
@@ -183,7 +183,7 @@ public class OnapCommandSchemaLoader {
         List<String> longOptions = new ArrayList<>();
 
         if (validate) {
-            OnapCommandUtils.validateTags(exceptionList, (Map<String, Object>) values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST),
+            OnapCommandUtils.validateTags(exceptionList, values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST),
                     OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_MANDATORY_LIST), "root level");
         }
 
index 32ececf..0d68db1 100644 (file)
@@ -263,6 +263,37 @@ public class OnapCommandArtifactStore {
         }
     }
 
+    public Artifact setArtifact(Artifact artifact, Artifact existing) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist, IOException, NoSuchAlgorithmException {
+        if (artifact.getName() == null) {
+            artifact.setName(existing.getName());
+        }
+
+        if (artifact.getDescription() == null) {
+            artifact.setDescription(existing.getDescription());
+        }
+
+        if (artifact.getCategoty() == null) {
+            artifact.setCategoty(existing.getCategoty());
+        }
+
+        if (artifact.getPath()!= null) {
+            if (!new File(artifact.getPath()).exists()) {
+                throw new OnapCommandArtifactContentNotExist(artifact.getPath());
+            }
+            String actual = this.getChecksum(artifact.getPath());
+            if (!existing.getChecksum().equals(actual)) {
+                artifact.setChecksum(actual);
+                artifact.setSize(new File(artifact.getPath()).length() / 1024);
+            }
+        } else {
+            artifact.setPath(existing.getPath());
+        }
+
+        artifact.setCreateAt(existing.getCreateAt());
+        artifact.setLastUpdatedAt(dateFormatter.format(new Date()));
+        return artifact;
+    }
+
     public Artifact updateArtifact(String name, String category, Artifact artifact) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist {
         Artifact existing = this.getArtifact(name, category);
         String existingStorePath = getArtifactPath(name, category);
@@ -273,33 +304,7 @@ public class OnapCommandArtifactStore {
         }
 
         try {
-            if (artifact.getName() == null) {
-                artifact.setName(existing.getName());
-            }
-
-            if (artifact.getDescription() == null) {
-                artifact.setDescription(existing.getDescription());
-            }
-
-            if (artifact.getCategoty() == null) {
-                artifact.setCategoty(existing.getCategoty());
-            }
-
-            if (artifact.getPath()!= null) {
-                if (!new File(artifact.getPath()).exists()) {
-                    throw new OnapCommandArtifactContentNotExist(artifact.getPath());
-                }
-                String actual = this.getChecksum(artifact.getPath());
-                if (!existing.getChecksum().equals(actual)) {
-                    artifact.setChecksum(actual);
-                    artifact.setSize(new File(artifact.getPath()).length() / 1024);
-                }
-            } else {
-                artifact.setPath(existing.getPath());
-            }
-
-            artifact.setCreateAt(existing.getCreateAt());
-            artifact.setLastUpdatedAt(dateFormatter.format(new Date()));
+            artifact = setArtifact(artifact, existing);
             if (artifact.getMetadata().size() > 0) {
                 //update to existing one
                 for (Map.Entry<String, String> entry: artifact.getMetadata().entrySet()) {
index dab6669..acf44a9 100644 (file)
@@ -22,6 +22,7 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -270,7 +271,7 @@ public class OnapCommandUtils {
                         //start to read after file:filepath
                         String fileName = splEntry.substring(5);
                         try {
-                            value = FileUtils.readFileToString(new File(fileName));
+                            value = FileUtils.readFileToString((new File(fileName)), (Charset) null);
                         } catch (IOException e) {
                             //when file is not found, assign the same file:FILE_PATH
                             //so that it will given hit to user that FILE_PATH to be
index ee9c7b3..08ef640 100644 (file)
@@ -17,6 +17,7 @@
 package org.onap.cli.main;
 
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -189,7 +190,7 @@ public class OnapCli {
     public void handleHelp() {
         try {
             if (this.printHelp) {
-                this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt")));
+                this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt") , (Charset) null));
                 String help = OnapCommandRegistrar.getRegistrar().getHelp();
                 this.print(help);
                 this.exitSuccessfully();
index d0885de..53e9066 100644 (file)
@@ -19,6 +19,7 @@ package org.onap.cli.main.utils;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -224,7 +225,7 @@ public class OnapCliArgsParser {
         try {
             File file = new File(input);
             if (file.isFile()) {
-                return FileUtils.readFileToString(file);
+                return FileUtils.readFileToString(file, (Charset) null);
             } else {
                 return input;
             }
@@ -238,7 +239,7 @@ public class OnapCliArgsParser {
         try {
             File file = new File(input);
             if (file.isFile()) {
-                String value = FileUtils.readFileToString(file);
+                String value = FileUtils.readFileToString(file, (Charset) null);
                 YamlReader reader = new YamlReader(value);
                 value = (String) reader.read();
                 return value;