From 268be523fb1142138a3f710642b5773453841eeb Mon Sep 17 00:00:00 2001 From: "priyanka.akhade" Date: Thu, 18 Jun 2020 15:11:08 +0530 Subject: code improvements Signed-off-by: priyanka.akhade Issue-ID: CLI-270 Change-Id: If9eab4bc8642c7c20a841d58c8a9e2bf5ae6c98b --- .../org/onap/cli/fw/http/cmd/OnapHttpCommand.java | 2 +- .../java/org/onap/cli/fw/http/mock/MocoServer.java | 2 +- .../http/schema/OnapCommandSchemaHttpLoader.java | 2 +- .../cli/fw/http/utils/OnapCommandHttpUtils.java | 30 ++++++++++----------- .../cli/fw/http/utils/OnapCommandUtilsTest.java | 31 +++++++++++++++++++++- 5 files changed, 48 insertions(+), 19 deletions(-) (limited to 'profiles/http/src') 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> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output); + Map> 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 mocoServerConfigs = new HashMap(); + private Map 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 parseHttpSchema(OnapHttpCommand cmd, + public static List parseHttpSchema(OnapHttpCommand cmd, final Map values, boolean validate) throws OnapCommandException { ArrayList 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> populateOutputs(Map resultMap, HttpResult resultHttp) + public static Map> populateOutputs(Map resultMap, HttpResult resultHttp) throws OnapCommandException { - Map> resultsProcessed = new HashMap<>(); + Map> resultsProcessed = new HashMap<>(); for (Entry entry : resultMap.entrySet()) { String key = entry.getKey(); @@ -161,10 +161,10 @@ public class OnapCommandHttpUtils { return resultsProcessed; } - public static ArrayList replaceLineFromOutputResults(String line, HttpResult resultHttp) + public static List replaceLineFromOutputResults(String line, HttpResult resultHttp) throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody, OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty { - String headerProcessedLine = ""; + StringBuilder headerProcessedLine = new StringBuilder(); ArrayList 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 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> input1 = OnapCommandHttpUtils.populateOutputs(params, output); + Map> 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 mapHead = new HashMap<>(); + mapHead.put("head1", "value1"); + output.setRespHeaders(mapHead); + output.setStatus(0); + List 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 { -- cgit 1.2.3-korg