Add context in http to ignore empty nodes 75/35375/1
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 13 Mar 2018 05:58:32 +0000 (11:28 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 13 Mar 2018 06:14:59 +0000 (11:44 +0530)
Issue-ID: CLI-101

Change-Id: Ie84158a516f66a7d957db25386d9942e112b6ebf
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java
profiles/http/src/main/java/org/onap/cli/fw/http/connect/HttpInput.java
profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java [new file with mode: 0644]
profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java
profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
profiles/http/src/main/resources/open-cli-http.properties
profiles/http/src/test/java/org/onap/cli/fw/http/HttpInputOutputTest.java
profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java
profiles/http/src/test/resources/open-cli-schema/sample-test1-schema-http.yaml
profiles/http/src/test/resources/sample-test-schema-swagger.yaml [deleted file]

index a5174a4..9663f87 100644 (file)
@@ -89,6 +89,10 @@ public class OnapCommandHttpConstants {
     public static final String SERVICE_PARAMS_MANDATORY_LIST = "cli.schema.http.service.sections.mandatory";
 
     public static final String DEFAULT_PARAMETER_NO_CATALOG = "no-catalog";
+
+    //context param
+    public static final String CONTEXT = "context";
+    public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node";
 }
 
 
index 722dd12..f592279 100644 (file)
@@ -38,6 +38,8 @@ public class HttpInput {
 
     private Map<String, String> reqCookies = new HashMap<>();
 
+    private Map<String, String> context = new HashMap<>();
+
     private boolean binaryData;
 
     public String getUri() {
@@ -116,6 +118,10 @@ public class HttpInput {
         return reqCookies;
     }
 
+    public Map<String, String> getContext() {
+        return context;
+    }
+
     public HttpInput setReqCookies(Map<String, String> reqCookies) {
         this.reqCookies = reqCookies;
         return this;
@@ -134,6 +140,6 @@ public class HttpInput {
         return "\nURL: " + this.getUri() + "\nMethod: " + this.getMethod() + "\nRequest Queries: "
                 + this.getReqQueries() + "\nRequest Body: " + this.getBody() + "\nRequest Headers: "
                 + this.getReqHeaders().toString() + "\nRequest Cookies: " + this.getReqCookies().toString()
-                + "\nbinaryData=" + this.binaryData;
+                + "\nbinaryData=" + this.binaryData + "\nContext=" + this.context;
     }
 }
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java b/profiles/http/src/main/java/org/onap/cli/fw/http/error/OnapCommandHttpInvalidRequestBody.java
new file mode 100644 (file)
index 0000000..73c721e
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2017 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.http.error;
+
+import org.onap.cli.fw.error.OnapCommandException;
+
+/**
+ * OnapCommandParameterNotFound.
+ *
+ */
+public class OnapCommandHttpInvalidRequestBody extends OnapCommandException {
+
+    private static final long serialVersionUID = 6676137916079057963L;
+
+    private static final String ERROR_CODE = "0x3008";
+    private static final String ERR_MSG = "Http request body does not have valid json ";
+
+    public OnapCommandHttpInvalidRequestBody(String name, String error) {
+        super(ERROR_CODE, ERR_MSG + name + ", " + error);
+    }
+
+    public OnapCommandHttpInvalidRequestBody(String name, Throwable throwable) {
+        super(ERROR_CODE, ERR_MSG + name, throwable);
+    }
+
+    public OnapCommandHttpInvalidRequestBody(Throwable e) {
+        this(ERROR_CODE, e.getMessage());
+    }
+}
index 8e01b58..973c4ae 100644 (file)
@@ -132,6 +132,11 @@ public class OnapCommandSchemaHttpLoader {
 
                                             cmd.getInput().setReqQueries(query);
                                             break;
+                                        case OnapCommandHttpConstants.CONTEXT:
+                                            Map<String, String> context = (Map<String, String>) map.get(key2);
+
+                                            cmd.getInput().getContext().putAll(context);
+                                            break;
                                         case OnapCommandHttpConstants.MULTIPART_ENTITY_NAME:
                                             Object multipartEntityName = map.get(key2);
                                             cmd.getInput().setMultipartEntityName(multipartEntityName.toString());
index 7b10dbc..43a8018 100644 (file)
@@ -18,6 +18,7 @@ package org.onap.cli.fw.http.utils;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -27,9 +28,11 @@ import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
 import org.onap.cli.fw.error.OnapCommandParameterNotFound;
 import org.onap.cli.fw.error.OnapCommandResultEmpty;
 import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed;
+import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
 import org.onap.cli.fw.http.connect.HttpInput;
 import org.onap.cli.fw.http.connect.HttpResult;
 import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound;
+import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody;
 import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody;
 import org.onap.cli.fw.input.OnapCommandParameter;
 import org.onap.cli.fw.input.OnapCommandParameterType;
@@ -37,6 +40,8 @@ import org.onap.cli.fw.utils.OnapCommandUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.jayway.jsonpath.JsonPath;
 import com.jayway.jsonpath.PathNotFoundException;
 
@@ -81,6 +86,12 @@ public class OnapCommandHttpUtils {
             inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params));
         }
 
+        boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false"));
+
+        if (isRemoveEmptyNodes) {
+            input.setBody(OnapCommandHttpUtils.normalizeJson(input.getBody()));
+        }
+
         return inp;
     }
 
@@ -235,5 +246,30 @@ public class OnapCommandHttpUtils {
         }
     }
 
+    public static void normalizeJson(JsonNode node) {
+        Iterator<JsonNode> it = node.iterator();
+        while (it.hasNext()) {
+            JsonNode child = it.next();
+            if (child.isTextual() && child.asText().equals(""))
+                it.remove();
+            else  if (child.isNull())
+                it.remove();
+            else
+                normalizeJson(child);
+        }
+    }
+
+    public static String normalizeJson(String json) throws OnapCommandHttpInvalidRequestBody {
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode node;
+        try {
+            node = mapper.readTree(json);
+            normalizeJson(node);
+            return mapper.writeValueAsString(node);
+        } catch (Exception e) {  //NOSONAR
+            throw new OnapCommandHttpInvalidRequestBody(e);
+        }
+
+    }
 }
 
index db19d08..bc7d33c 100644 (file)
@@ -6,7 +6,7 @@ cli.http.api_key_use_cookies=true
 cli.schema.http.sections=request,service,success_codes,result_map,sample_response
 cli.schema.http.sections.mandatory=request, success_codes
 
-cli.schema.http.request.sections=uri,method,body,headers,queries,multipart_entity_name
+cli.schema.http.request.sections=uri,method,body,headers,queries,multipart_entity_name,context
 cli.schema.http.request.sections.mandatory=uri,method
 
 cli.schema.http.service.sections=name,version,auth,mode
index 99ee3d3..7a7a31c 100644 (file)
@@ -44,7 +44,7 @@ public class HttpInputOutputTest {
         inp.setReqQueries(new HashMap<String, String>());
 
         assertTrue(
-                "\nURL: uri\nMethod: method\nRequest Queries: {}\nRequest Body: body\nRequest Headers: {}\nRequest Cookies: {}\nbinaryData=false"
+                "\nURL: uri\nMethod: method\nRequest Queries: {}\nRequest Body: body\nRequest Headers: {}\nRequest Cookies: {}\nbinaryData=false\nContext={}"
                         .equals(inp.toString()));
     }
 
index 67956a8..d98f9bf 100644 (file)
@@ -42,6 +42,7 @@ import org.onap.cli.fw.error.OnapCommandSchemaNotFound;
 import org.onap.cli.fw.http.cmd.OnapHttpCommand;
 import org.onap.cli.fw.http.connect.HttpResult;
 import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound;
+import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody;
 import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody;
 import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
 import org.onap.cli.fw.input.OnapCommandParameter;
@@ -136,4 +137,11 @@ public class OnapCommandUtilsTest {
         protected void run() throws OnapCommandException {
         }
     }
+
+    @Test
+    public void testJsonEmptyCheck() throws OnapCommandHttpInvalidRequestBody {
+        String sample = "{\"request\":{\"method\":\"\",\"uri\":\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\",\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\",\"Content-Type\":\"application/json\"},\"json\":null},\"response\":{\"status\":200,\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":null},\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\",\"other\":\"\"},\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
+        String result = "{\"request\":{\"uri\":\"/onboarding-api/v1.0/vendor-license-models/cf2d907d998e44698ce3b4cded5f66a7/versions/2.0/license-agreements\",\"headers\":{\"Authorization\":\"Basic Y3MwMDA4OmRlbW8xMjM0NTYh\",\"X-FromAppId\":\"onap-cli\",\"Accept\":\"application/json\",\"USER_ID\":\"cs0008\",\"X-TransactionId\":\"req-66a37478-d840-44f8-b436-56f4a3b6f640\",\"Content-Type\":\"application/json\"}},\"response\":{\"status\":200,\"json\":{\"listCount\":2,\"results\":[{\"name\":\"sf\",\"description\":\"sdfgdf\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},\"id\":\"1e2edfccaca847f896070d0fac26667a\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]},{\"name\":\"kanag-cli-la\",\"description\":\"kanag cli la\",\"licenseTerm\":{\"choice\":\"Fixed_Term\"},\"id\":\"77e151d0503b45ecb7e40f5f5f1a887e\",\"featureGroupsIds\":[\"3a2fb75b52a54e9c8093e7c154210f9e\"]}]}}}";
+        assertEquals(result, OnapCommandHttpUtils.normalizeJson(sample));
+    }
 }
index f162231..3120f93 100644 (file)
@@ -75,6 +75,8 @@ http:
     method: POST
     body: '{"name":"${name}","vendor":"${vendor}","version":"${vim-version}","description":"${description}","type":"${type}","url":"${url}","userName":"${username}","password":"${password}","domain":"${domain}","tenant":"${tenant}"}'
     headers:
+    context:
+      - remove_empty_node: true
   success_codes:
     - 201
     - 200
diff --git a/profiles/http/src/test/resources/sample-test-schema-swagger.yaml b/profiles/http/src/test/resources/sample-test-schema-swagger.yaml
deleted file mode 100644 (file)
index 4108d4e..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-open_cli_schema_version: 1.0
-name: sample-test-swagger
-description: Sample swagger command test.
-info:
-  product: open-cli
-  service: test
-  type: cmd
-  author: Kanagaraj Manickam kanagaraj.manickam@huawei.com
-parameters:
-  - name: user
-    type: string
-    description: Oclip user
-    short_option: n
-    long_option: username
-    is_optional: false
-results:
-  direction: portrait
-  attributes:
-    - name: name
-      description: Oclip user
-      scope: short
-      type: string
-exec:
-  api: org.onap.common_services.auth.auth_service.client.api.DefaultApi
-  client: org.onap.common_services.auth.auth_service.client.invoker.ApiClient
-  entity: org.onap.common_services.auth.auth_service.client.model.User, username(userName), password, description
-  method: create
-  exception: org.onap.common_services.auth.auth_service.client.invoker.ApiException
\ No newline at end of file