diff options
author | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-06-18 15:11:08 +0530 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-07-01 09:51:04 +0530 |
commit | 268be523fb1142138a3f710642b5773453841eeb (patch) | |
tree | 2e88b62a1e3059a050335d2f19467e3d2d451a41 /profiles | |
parent | a13ffa03bf353bc70cb20cc0481ba89e24fd2cdb (diff) |
code improvements
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Issue-ID: CLI-270
Change-Id: If9eab4bc8642c7c20a841d58c8a9e2bf5ae6c98b
Diffstat (limited to 'profiles')
9 files changed, 62 insertions, 33 deletions
diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java index 0ed930d1..eb7b808b 100644 --- a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/OpenCommandShellCmd.java @@ -83,7 +83,7 @@ public class OpenCommandShellCmd extends OnapCommand { return successStatusCodes; } - public void setSuccessStatusCodes(ArrayList<Integer> successStatusCodes) { + public void setSuccessStatusCodes(List<Integer> successStatusCodes) { this.successStatusCodes = successStatusCodes; } @@ -197,7 +197,7 @@ public class OpenCommandShellCmd extends OnapCommand { FileOutputStream stderrStream = null; String outputValue = ""; - try { + try { //NOSONAR pr.setTimeout(timeout); if (this.getExecutionContext() != null) { @@ -370,7 +370,7 @@ public class OpenCommandShellCmd extends OnapCommand { // Process jsonpath macros List<Object> values = new ArrayList<>(); - String processedPattern = ""; + StringBuilder processedPattern = new StringBuilder(); currentIdx = 0; int maxRows = 1; // in normal case, only one row will be there while (currentIdx < line.length()) { @@ -378,7 +378,7 @@ public class OpenCommandShellCmd extends OnapCommand { if (idxS == -1) { idxS = line.indexOf("$e{", currentIdx); //check for error stream if (idxS == -1) { - processedPattern += line.substring(currentIdx); + processedPattern.append(line.substring(currentIdx)); break; } } @@ -405,23 +405,23 @@ public class OpenCommandShellCmd extends OnapCommand { maxRows = arr.size(); } } - processedPattern += line.substring(currentIdx, idxS) + "%s"; + processedPattern.append(line.substring(currentIdx, idxS) + "%s"); values.add(value); currentIdx = idxE + 1; } - if (processedPattern.isEmpty()) { + if (processedPattern.toString().isEmpty()) { result.add(line); return result; } else { for (int i = 0; i < maxRows; i++) { currentIdx = 0; - String bodyProcessedLine = ""; + StringBuilder bodyProcessedLine = new StringBuilder(); int positionalIdx = 0; // %s positional idx while (currentIdx < processedPattern.length()) { int idxS = processedPattern.indexOf("%s", currentIdx); if (idxS == -1) { - bodyProcessedLine += processedPattern.substring(currentIdx); + bodyProcessedLine.append(processedPattern.substring(currentIdx)); break; } @@ -441,7 +441,7 @@ public class OpenCommandShellCmd extends OnapCommand { } } - bodyProcessedLine += processedPattern.substring(currentIdx, idxS) + valStr; + bodyProcessedLine.append(processedPattern.substring(currentIdx, idxS) + valStr); currentIdx = idxEnd; positionalIdx++; } catch (OnapCommandResultEmpty e) { @@ -450,7 +450,7 @@ public class OpenCommandShellCmd extends OnapCommand { throw new OnapCommandResultMapProcessingFailed(line, e); } } - result.add(bodyProcessedLine); + result.add(bodyProcessedLine.toString()); } return result; diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java index 965bd2b1..676aeb30 100644 --- a/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java +++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/schema/OnapCommandSchemaCmdLoader.java @@ -44,9 +44,9 @@ public class OnapCommandSchemaCmdLoader { * @throws OnapCommandException * on error */ - public static ArrayList<String> parseCmdSchema(OpenCommandShellCmd cmd, + public static List<String> parseCmdSchema(OpenCommandShellCmd cmd, final Map<String, ?> values, - boolean validate) throws OnapCommandException { + boolean validate) throws OnapCommandException { //NOSONAR ArrayList<String> errorList = new ArrayList<>(); Map<String, ?> valMap = (Map<String, ?>) values.get(OnapCommandCmdConstants.CMD); diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java index 53a2d042..387ec3be 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java @@ -253,7 +253,7 @@ public class OnapHttpCommand extends OnapCommand { this.resultMap.put(resultMapEntry.getKey(), value); } - Map<String, ArrayList<String>> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output); + Map<String, List<String>> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output); //results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap()); for (OnapCommandResultAttribute attr : this.getResult().getRecords()) { diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java index a2e025a8..9c4185b7 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java @@ -43,7 +43,7 @@ import java.io.InputStreamReader; public class MocoServer { private Runner runner; - private Map<String, Object> mocoServerConfigs = new HashMap(); + private Map<String, Object> mocoServerConfigs = new HashMap<>(); private static Gson gson = new GsonBuilder().serializeNulls().create(); public MocoServer(String mockFile) throws OnapCommandException { diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java index df9c84fc..33f58060 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java @@ -92,7 +92,7 @@ public class OnapCommandSchemaHttpLoader { * @throws OnapCommandException * on error */ - public static ArrayList<String> parseHttpSchema(OnapHttpCommand cmd, + public static List<String> parseHttpSchema(OnapHttpCommand cmd, final Map<String, ?> values, boolean validate) throws OnapCommandException { ArrayList<String> errorList = new ArrayList<>(); diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java index a592ac79..fcd25c24 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java @@ -145,9 +145,9 @@ public class OnapCommandHttpUtils { * @throws OnapCommandResultMapProcessingFailed * map processing failed exception */ - public static Map<String, ArrayList<String>> populateOutputs(Map<String, String> resultMap, HttpResult resultHttp) + public static Map<String, List<String>> populateOutputs(Map<String, String> resultMap, HttpResult resultHttp) throws OnapCommandException { - Map<String, ArrayList<String>> resultsProcessed = new HashMap<>(); + Map<String, List<String>> resultsProcessed = new HashMap<>(); for (Entry<String, String> entry : resultMap.entrySet()) { String key = entry.getKey(); @@ -161,10 +161,10 @@ public class OnapCommandHttpUtils { return resultsProcessed; } - public static ArrayList<String> replaceLineFromOutputResults(String line, HttpResult resultHttp) + public static List<String> replaceLineFromOutputResults(String line, HttpResult resultHttp) throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody, OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty { - String headerProcessedLine = ""; + StringBuilder headerProcessedLine = new StringBuilder(); ArrayList<String> result = new ArrayList<>(); if (!line.contains("$b{") && !line.contains("$h{")) { @@ -187,7 +187,7 @@ public class OnapCommandHttpUtils { while (currentIdx < line.length()) { int idxS = line.indexOf("$h{", currentIdx); if (idxS == -1) { - headerProcessedLine += line.substring(currentIdx); + headerProcessedLine.append(line.substring(currentIdx)); break; } int idxE = line.indexOf("}", idxS); @@ -198,19 +198,19 @@ public class OnapCommandHttpUtils { } String value = resultHttp.getRespHeaders().get(headerName); - headerProcessedLine += line.substring(currentIdx, idxS) + value; + headerProcessedLine.append(line.substring(currentIdx, idxS) + value); currentIdx = idxE + 1; } // Process body jsonpath macros List<Object> values = new ArrayList<>(); - String bodyProcessedPattern = ""; + StringBuilder bodyProcessedPattern = new StringBuilder(); currentIdx = 0; int maxRows = 1; // in normal case, only one row will be there while (currentIdx < headerProcessedLine.length()) { int idxS = headerProcessedLine.indexOf("$b{", currentIdx); if (idxS == -1) { - bodyProcessedPattern += headerProcessedLine.substring(currentIdx); + bodyProcessedPattern.append(headerProcessedLine.substring(currentIdx)); break; } int idxE = headerProcessedLine.indexOf("}", idxS); @@ -233,23 +233,23 @@ public class OnapCommandHttpUtils { maxRows = arr.size(); } } - bodyProcessedPattern += headerProcessedLine.substring(currentIdx, idxS) + "%s"; + bodyProcessedPattern.append(headerProcessedLine.substring(currentIdx, idxS) + "%s"); values.add(value); currentIdx = idxE + 1; } - if (bodyProcessedPattern.isEmpty()) { - result.add(headerProcessedLine); + if (bodyProcessedPattern.toString().isEmpty()) { + result.add(headerProcessedLine.toString()); return result; } else { for (int i = 0; i < maxRows; i++) { currentIdx = 0; - String bodyProcessedLine = ""; + StringBuilder bodyProcessedLine = new StringBuilder(); int positionalIdx = 0; // %s positional idx while (currentIdx < bodyProcessedPattern.length()) { int idxS = bodyProcessedPattern.indexOf("%s", currentIdx); if (idxS == -1) { - bodyProcessedLine += bodyProcessedPattern.substring(currentIdx); + bodyProcessedLine.append(bodyProcessedPattern.substring(currentIdx)); break; } int idxE = idxS + 2; // %s @@ -265,7 +265,7 @@ public class OnapCommandHttpUtils { } } - bodyProcessedLine += bodyProcessedPattern.substring(currentIdx, idxS) + valueS; + bodyProcessedLine.append(bodyProcessedPattern.substring(currentIdx, idxS) + valueS); currentIdx = idxE; positionalIdx++; } catch (OnapCommandResultEmpty e) { @@ -274,7 +274,7 @@ public class OnapCommandHttpUtils { throw new OnapCommandResultMapProcessingFailed(line, e); } } - result.add(bodyProcessedLine); + result.add(bodyProcessedLine.toString()); } return result; diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java index 6e03e74b..37fc8461 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java @@ -49,6 +49,10 @@ import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.onap.cli.fw.utils.OnapCommandUtils; +import java.util.List; +import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed; +import org.onap.cli.fw.error.OnapCommandResultEmpty; +import static org.junit.Assert.assertFalse; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class OnapCommandUtilsTest { @@ -116,7 +120,7 @@ public class OnapCommandUtilsTest { params.put("body", "$b{$.serviceName}"); params.put("key", "value"); - Map<String, ArrayList<String>> input1 = OnapCommandHttpUtils.populateOutputs(params, output); + Map<String, List<String>> input1 = OnapCommandHttpUtils.populateOutputs(params, output); assertEquals("{head=[value1], body=[test], key=[value]}", input1.toString()); params.put("body", "$b{{$.serviceName}"); @@ -135,6 +139,31 @@ public class OnapCommandUtilsTest { input1 = OnapCommandHttpUtils.populateOutputs(params, output); } + @Test + public void replaceLineFromOutputResultsTest() throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody, OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty { + HttpResult output = new HttpResult(); + + Map<String, String> mapHead = new HashMap<>(); + mapHead.put("head1", "value1"); + output.setRespHeaders(mapHead); + output.setStatus(0); + List<String> actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("", output); + assertTrue(actualResult.get(0).isEmpty()); + output.setBody(""); + actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}$b{$.serviceName}", output); + assertTrue(actualResult.isEmpty()); +// assertTrue(actualResult.size() > 0); + output.setBody( + "{\"serviceName\":\"test\",\"version\":\"v1\",\"url\":\"/api/test/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"1\",\"lb_policy\":\"hash\",\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"8012\"," + + "\"ttl\":0,\"nodeId\":\"test_127.0.0.1_8012\",\"expiration\":\"2017-02-10T05:33:25Z\"," + + "\"created_at\":\"2017-02-10T05:33:25Z\",\"updated_at\":\"2017-02-10T05:33:25Z\"}]," + + "\"status\":\"1\"}"); + actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}${$.serviceName}", output); + assertTrue(actualResult.size()>0); + actualResult = OnapCommandHttpUtils.replaceLineFromOutputResults("$h{head1}$b{$.serviceName}", output); + assertFalse(actualResult.get(0).isEmpty()); + } @OnapCommandSchema(schema = "sample-test-schema-http.yaml") class OnapHttpCommandSample extends OnapHttpCommand { diff --git a/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/cmd/OnapSnmpCommand.java b/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/cmd/OnapSnmpCommand.java index ed88b0c6..3e841e76 100644 --- a/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/cmd/OnapSnmpCommand.java +++ b/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/cmd/OnapSnmpCommand.java @@ -75,7 +75,7 @@ public class OnapSnmpCommand extends OnapCommand { return pdu; } - private Target getTarget() throws OnapCommandException { + private Target getTarget() throws OnapCommandException { //NOSONAR Address targetAddress = GenericAddress.parse(this.getAgent()); //udp:127.0.0.1/161 CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(OnapCommandSnmpConstants.SNMP_COMMNUNITY_STRING)); diff --git a/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/schema/OnapCommandSchemaSnmpLoader.java b/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/schema/OnapCommandSchemaSnmpLoader.java index c132be26..ca3f44e0 100644 --- a/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/schema/OnapCommandSchemaSnmpLoader.java +++ b/profiles/snmp/src/main/java/org/onap/cli/fw/snmp/schema/OnapCommandSchemaSnmpLoader.java @@ -40,7 +40,7 @@ public class OnapCommandSchemaSnmpLoader { public static List<String> parseSnmpSchema(OnapSnmpCommand cmd, final Map<String, ?> values, - boolean validate) throws OnapCommandException { + boolean validate) throws OnapCommandException { //NOSONAR ArrayList<String> errorList = new ArrayList<>(); Map<String, ?> valMap = (Map<String, ?>) values.get(OnapCommandSnmpConstants.SNMP); |